mirror of https://github.com/apache/kafka.git
MINOR: Refactor write timeout in CoordinatorRuntime (#15976)
Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
This commit is contained in:
parent
7fea279ff9
commit
5b34574e86
|
@ -541,6 +541,28 @@ public class CoordinatorRuntime<S extends CoordinatorShard<U>, U> implements Aut
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class OperationTimeout extends TimerTask {
|
||||||
|
private final TopicPartition tp;
|
||||||
|
private final DeferredEvent event;
|
||||||
|
|
||||||
|
public OperationTimeout(
|
||||||
|
TopicPartition tp,
|
||||||
|
DeferredEvent event,
|
||||||
|
long delayMs
|
||||||
|
) {
|
||||||
|
super(delayMs);
|
||||||
|
this.event = event;
|
||||||
|
this.tp = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
String name = event.toString();
|
||||||
|
scheduleInternalOperation("OperationTimeout(name=" + name + ", tp=" + tp + ")", tp,
|
||||||
|
() -> event.complete(new TimeoutException(name + " timed out after " + delayMs + "ms")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A coordinator write operation.
|
* A coordinator write operation.
|
||||||
*
|
*
|
||||||
|
@ -614,7 +636,10 @@ public class CoordinatorRuntime<S extends CoordinatorShard<U>, U> implements Aut
|
||||||
*/
|
*/
|
||||||
final Duration writeTimeout;
|
final Duration writeTimeout;
|
||||||
|
|
||||||
private TimerTask writeTimeoutTask = null;
|
/**
|
||||||
|
* The operation timeout.
|
||||||
|
*/
|
||||||
|
private OperationTimeout operationTimeout = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The result of the write operation. It could be null
|
* The result of the write operation. It could be null
|
||||||
|
@ -753,19 +778,8 @@ public class CoordinatorRuntime<S extends CoordinatorShard<U>, U> implements Aut
|
||||||
// Add the response to the deferred queue.
|
// Add the response to the deferred queue.
|
||||||
if (!future.isDone()) {
|
if (!future.isDone()) {
|
||||||
context.deferredEventQueue.add(offset, this);
|
context.deferredEventQueue.add(offset, this);
|
||||||
writeTimeoutTask = new TimerTask(writeTimeout.toMillis()) {
|
operationTimeout = new OperationTimeout(tp, this, writeTimeout.toMillis());
|
||||||
@Override
|
timer.add(operationTimeout);
|
||||||
public void run() {
|
|
||||||
if (!future.isDone()) {
|
|
||||||
scheduleInternalOperation(
|
|
||||||
"WriteTimeout(name=" + name + ", tp=" + tp + ")",
|
|
||||||
tp,
|
|
||||||
() -> complete(new TimeoutException("CoordinatorWriteEvent " + name + " timed out after " + writeTimeout.toMillis() + "ms"))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
timer.add(writeTimeoutTask);
|
|
||||||
} else {
|
} else {
|
||||||
complete(null);
|
complete(null);
|
||||||
}
|
}
|
||||||
|
@ -798,9 +812,9 @@ public class CoordinatorRuntime<S extends CoordinatorShard<U>, U> implements Aut
|
||||||
future.completeExceptionally(exception);
|
future.completeExceptionally(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (writeTimeoutTask != null) {
|
if (operationTimeout != null) {
|
||||||
writeTimeoutTask.cancel();
|
operationTimeout.cancel();
|
||||||
writeTimeoutTask = null;
|
operationTimeout = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -988,7 +1002,10 @@ public class CoordinatorRuntime<S extends CoordinatorShard<U>, U> implements Aut
|
||||||
*/
|
*/
|
||||||
final Duration writeTimeout;
|
final Duration writeTimeout;
|
||||||
|
|
||||||
private TimerTask writeTimeoutTask = null;
|
/**
|
||||||
|
* The operation timeout.
|
||||||
|
*/
|
||||||
|
private OperationTimeout operationTimeout = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The future that will be completed with the response
|
* The future that will be completed with the response
|
||||||
|
@ -1057,20 +1074,8 @@ public class CoordinatorRuntime<S extends CoordinatorShard<U>, U> implements Aut
|
||||||
|
|
||||||
if (!future.isDone()) {
|
if (!future.isDone()) {
|
||||||
context.deferredEventQueue.add(offset, this);
|
context.deferredEventQueue.add(offset, this);
|
||||||
writeTimeoutTask = new TimerTask(writeTimeout.toMillis()) {
|
operationTimeout = new OperationTimeout(tp, this, writeTimeout.toMillis());
|
||||||
@Override
|
timer.add(operationTimeout);
|
||||||
public void run() {
|
|
||||||
if (!future.isDone()) {
|
|
||||||
scheduleInternalOperation(
|
|
||||||
"WriteTimeout(name=" + name + ", tp=" + tp + ")",
|
|
||||||
tp,
|
|
||||||
() -> complete(new TimeoutException("CoordinatorCompleteTransactionEvent " + name +
|
|
||||||
" timed out after " + writeTimeout.toMillis() + "ms"))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
timer.add(writeTimeoutTask);
|
|
||||||
} else {
|
} else {
|
||||||
complete(null);
|
complete(null);
|
||||||
}
|
}
|
||||||
|
@ -1098,9 +1103,9 @@ public class CoordinatorRuntime<S extends CoordinatorShard<U>, U> implements Aut
|
||||||
future.completeExceptionally(exception);
|
future.completeExceptionally(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (writeTimeoutTask != null) {
|
if (operationTimeout != null) {
|
||||||
writeTimeoutTask.cancel();
|
operationTimeout.cancel();
|
||||||
writeTimeoutTask = null;
|
operationTimeout = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue