1996fanrui commented on code in PR #2243:
URL:
https://github.com/apache/incubator-streampark/pull/2243#discussion_r1065400229
##########
streampark-common/src/main/scala/org/apache/streampark/common/util/CompletableFutureUtils.scala:
##########
@@ -75,36 +80,47 @@ object CompletableFutureUtils {
unit: TimeUnit,
handle: Consumer[T],
exceptionally: Consumer[Throwable]): CompletableFuture[Unit] = {
- future.applyToEither(
+
+ val unitFuture = future.applyToEither(
setTimeout(timeout, unit),
- new JavaFunc[T, Unit] {
+ new JavaFunc[T, Unit]() {
override def apply(t: T): Unit = {
if (handle != null) {
handle.accept(t)
}
}
- }).exceptionally(new JavaFunc[Throwable, Unit] {
+ }).exceptionally(new JavaFunc[Throwable, Unit]() {
override def apply(t: Throwable): Unit = {
if (exceptionally != null) {
exceptionally.accept(t)
}
}
})
+ cancelUnitFuture(unitFuture)
}
def runTimeout[T](future: CompletableFuture[T], timeout: Long, unit:
TimeUnit): CompletableFuture[Unit] = {
- runTimeout(
+ val unitFuture = runTimeout(
future,
timeout,
unit,
null,
- new Consumer[Throwable] {
+ new Consumer[Throwable]() {
override def accept(t: Throwable): Unit = {
if (!future.isDone) {
future.cancel(true)
}
}
})
+ cancelUnitFuture(unitFuture)
+ }
+
+ private[this] def cancelUnitFuture(future: CompletableFuture[Unit]):
CompletableFuture[Unit] = {
+ future.whenComplete(new BiConsumer[Unit, Throwable]() {
+ override def accept(t: Unit, u: Throwable): Unit = {
+ future.cancel(true)
Review Comment:
I know it can be executed multiple times, I mean we should not execute some
useless code, and we just cancel the future when it needs to be canceled.
I see you updated the `!future.isCanceled`, it doesn't make sense. When
future is finished before timeout, we don't need cancel the future, however
`future.isCanceled == false`.
That's why I said, we should check `!future.isDone` instead of
`!future.isCanceled`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]