1996fanrui commented on code in PR #2243:
URL:
https://github.com/apache/incubator-streampark/pull/2243#discussion_r1065348284
##########
streampark-common/src/main/scala/org/apache/streampark/common/util/CompletableFutureUtils.scala:
##########
@@ -56,7 +56,12 @@ object CompletableFutureUtils {
unit: TimeUnit,
handle: JavaFunc[T, T],
exceptionally: JavaFunc[Throwable, T]): CompletableFuture[T] = {
- future.applyToEither(setTimeout(timeout, unit),
handle).exceptionally(exceptionally)
+ future
+ .applyToEither(setTimeout(timeout, unit), handle)
+ .exceptionally(exceptionally)
+ .whenComplete(new BiConsumer[T, Throwable]() {
+ override def accept(t: T, u: Throwable): Unit = future.cancel(true)
Review Comment:
Could you please add some unit tests to check the future is canceled after
timeout?
##########
streampark-console/streampark-console-service/src/test/java/org/apache/org/apache/streampark/common/util/CompletableFutureUtilsTest.java:
##########
@@ -143,4 +147,77 @@ private String runStart(int sec) {
}
return "start successful";
}
+
+ @Test
+ public void thenSupplyNormally() throws Exception {
+ String successResult = "success";
+ String exceptionResult = "error";
+
+ String resp =
+ CompletableFutureUtils.supplyTimeout(
+ CompletableFuture.supplyAsync(() -> successResult),
+ 3,
+ TimeUnit.SECONDS,
+ success -> success,
+ e -> exceptionResult)
+ .thenApply(r -> r)
+ .get();
+
+ Assertions.assertEquals(resp, successResult);
+ }
+
+ @Test
+ public void thenSupplyTimeout() throws Exception {
+ String successResult = "success";
+ String exceptionResult = "error";
+ CompletableFuture<String> future =
+ CompletableFuture.supplyAsync(
+ () -> {
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ return successResult;
+ });
+ String resp =
+ CompletableFutureUtils.supplyTimeout(
+ future, 1, TimeUnit.SECONDS, success -> success, e ->
exceptionResult)
+ .thenApply(r -> r)
+ .get();
+ Assertions.assertEquals(resp, exceptionResult);
+ }
+
+ @Test
+ public void thenSupplyException() {
+ String resp;
+ String exResult = "exception";
+ try {
+ resp = exceptionally();
Review Comment:
```suggestion
resp = exceptionally();
fail("It should be called.");
```
##########
streampark-console/streampark-console-service/src/test/java/org/apache/org/apache/streampark/common/util/CompletableFutureUtilsTest.java:
##########
@@ -143,4 +147,77 @@ private String runStart(int sec) {
}
return "start successful";
}
+
+ @Test
+ public void thenSupplyNormally() throws Exception {
+ String successResult = "success";
+ String exceptionResult = "error";
+
+ String resp =
+ CompletableFutureUtils.supplyTimeout(
+ CompletableFuture.supplyAsync(() -> successResult),
+ 3,
+ TimeUnit.SECONDS,
+ success -> success,
+ e -> exceptionResult)
+ .thenApply(r -> r)
+ .get();
+
+ Assertions.assertEquals(resp, successResult);
+ }
+
+ @Test
+ public void thenSupplyTimeout() throws Exception {
+ String successResult = "success";
+ String exceptionResult = "error";
+ CompletableFuture<String> future =
+ CompletableFuture.supplyAsync(
+ () -> {
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ return successResult;
+ });
+ String resp =
+ CompletableFutureUtils.supplyTimeout(
+ future, 1, TimeUnit.SECONDS, success -> success, e ->
exceptionResult)
+ .thenApply(r -> r)
+ .get();
+ Assertions.assertEquals(resp, exceptionResult);
+ }
+
+ @Test
+ public void thenSupplyException() {
+ String resp;
+ String exResult = "exception";
+ try {
+ resp = exceptionally();
+ } catch (Exception e) {
+ resp = exResult;
Review Comment:
We should check the exception type here instead of check the `exResult`.
##########
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)
Review Comment:
The unitFuture is canceled three times:
<img width="881" alt="image"
src="https://user-images.githubusercontent.com/38427477/211469510-d7627789-c893-4067-bccb-6d199667db24.png">
##########
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:
Check `!future.isDone` first.
--
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]