You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "Zakelly (via GitHub)" <gi...@apache.org> on 2024/08/23 09:37:01 UTC

[PR] [FLINK-36119] Conditionally interfaces for StateFuture [flink]

Zakelly opened a new pull request, #25244:
URL: https://github.com/apache/flink/pull/25244

   ## What is the purpose of the change
   
   As described in FLIP-455, this PR provides some interface for conditional branchings under `StateFuture`
   
   ## Brief change log
   
    - Add interface in `StateFuture`
   
   ## Verifying this change
   
     - Added UT under `StateFutureTest`
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): no
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: no
     - The serializers: no
     - The runtime per-record code paths (performance sensitive): no
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
     - The S3 file system connector: no
   
   ## Documentation
   
     - Does this pull request introduce a new feature? no
     - If yes, how is the feature documented? not applicable
   


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36119][state] Conditional interfaces for StateFuture [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia commented on code in PR #25244:
URL: https://github.com/apache/flink/pull/25244#discussion_r1741373553


##########
flink-core/src/main/java/org/apache/flink/core/state/StateFutureImpl.java:
##########
@@ -223,6 +224,292 @@ public <U, V> StateFuture<V> thenCombine(
         }
     }
 
+    @Override
+    public <U, V> StateFuture<Tuple2<Boolean, Object>> thenConditionallyApply(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            FunctionWithException<? super T, ? extends U, ? extends Exception> actionIfTrue,
+            FunctionWithException<? super T, ? extends V, ? extends Exception> actionIfFalse) {
+        callbackRegistered();
+        if (completableFuture.isDone()) {
+            // this branch must be invoked in task thread when expected
+            T t;
+            try {
+                t = completableFuture.get();
+            } catch (Exception e) {
+                exceptionHandler.handleException(
+                        "Caught exception when processing completed StateFuture's callback.", e);
+                return null;
+            }
+            boolean test = FunctionWithException.unchecked(condition).apply(t);
+            Object r =
+                    test
+                            ? FunctionWithException.unchecked(actionIfTrue).apply(t)
+                            : FunctionWithException.unchecked(actionIfFalse).apply(t);
+
+            callbackFinished();
+            return StateFutureUtils.completedFuture(Tuple2.of(test, r));
+        } else {
+            StateFutureImpl<Tuple2<Boolean, Object>> ret = makeNewStateFuture();
+            completableFuture
+                    .thenAccept(
+                            (t) -> {
+                                callbackRunner.submit(
+                                        () -> {
+                                            boolean test = condition.apply(t);
+                                            Object r =
+                                                    test
+                                                            ? actionIfTrue.apply(t)
+                                                            : actionIfFalse.apply(t);
+                                            ret.completeInCallbackRunner(Tuple2.of(test, r));
+                                            callbackFinished();
+                                        });
+                            })
+                    .exceptionally(
+                            (e) -> {
+                                exceptionHandler.handleException(
+                                        "Caught exception when submitting StateFuture's callback.",
+                                        e);
+                                return null;
+                            });
+            return ret;
+        }
+    }
+
+    @Override
+    public <U> StateFuture<Tuple2<Boolean, U>> thenConditionallyApply(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            FunctionWithException<? super T, ? extends U, ? extends Exception> actionIfTrue) {
+        callbackRegistered();
+        if (completableFuture.isDone()) {
+            // this branch must be invoked in task thread when expected
+            T t;
+            try {
+                t = completableFuture.get();
+            } catch (Exception e) {
+                exceptionHandler.handleException(
+                        "Caught exception when processing completed StateFuture's callback.", e);
+                return null;
+            }
+            boolean test = FunctionWithException.unchecked(condition).apply(t);
+            U r = test ? FunctionWithException.unchecked(actionIfTrue).apply(t) : null;
+
+            callbackFinished();
+            return StateFutureUtils.completedFuture(Tuple2.of(test, r));
+        } else {
+            StateFutureImpl<Tuple2<Boolean, U>> ret = makeNewStateFuture();
+            completableFuture
+                    .thenAccept(
+                            (t) -> {
+                                callbackRunner.submit(
+                                        () -> {
+                                            boolean test = condition.apply(t);
+                                            U r = test ? actionIfTrue.apply(t) : null;
+                                            ret.completeInCallbackRunner(Tuple2.of(test, r));
+                                            callbackFinished();
+                                        });
+                            })
+                    .exceptionally(
+                            (e) -> {
+                                exceptionHandler.handleException(
+                                        "Caught exception when submitting StateFuture's callback.",
+                                        e);
+                                return null;
+                            });
+            return ret;
+        }
+    }
+
+    @Override
+    public StateFuture<Boolean> thenConditionallyAccept(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            ThrowingConsumer<? super T, ? extends Exception> actionIfTrue,
+            ThrowingConsumer<? super T, ? extends Exception> actionIfFalse) {
+        callbackRegistered();
+        if (completableFuture.isDone()) {
+            // this branch must be invoked in task thread when expected
+            T t;
+            try {
+                t = completableFuture.get();
+            } catch (Exception e) {
+                exceptionHandler.handleException(
+                        "Caught exception when processing completed StateFuture's callback.", e);
+                return null;
+            }
+            boolean test = FunctionWithException.unchecked(condition).apply(t);
+            if (test) {
+                ThrowingConsumer.unchecked(actionIfTrue).accept(t);
+            } else {
+                ThrowingConsumer.unchecked(actionIfFalse).accept(t);
+            }
+            callbackFinished();
+            return StateFutureUtils.completedFuture(test);
+        } else {
+            StateFutureImpl<Boolean> ret = makeNewStateFuture();
+            completableFuture
+                    .thenAccept(
+                            (t) -> {
+                                callbackRunner.submit(
+                                        () -> {
+                                            boolean test = condition.apply(t);
+                                            if (test) {
+                                                actionIfTrue.accept(t);
+                                            } else {
+                                                actionIfFalse.accept(t);
+                                            }
+                                            ret.completeInCallbackRunner(test);
+                                            callbackFinished();
+                                        });
+                            })
+                    .exceptionally(
+                            (e) -> {
+                                exceptionHandler.handleException(
+                                        "Caught exception when submitting StateFuture's callback.",
+                                        e);
+                                return null;
+                            });
+            return ret;
+        }
+    }
+
+    @Override
+    public StateFuture<Boolean> thenConditionallyAccept(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            ThrowingConsumer<? super T, ? extends Exception> actionIfTrue) {
+        return thenConditionallyAccept(condition, actionIfTrue, (b) -> {});
+    }
+
+    @Override
+    public <U, V> StateFuture<Tuple2<Boolean, Object>> thenConditionallyCompose(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            FunctionWithException<? super T, ? extends StateFuture<U>, ? extends Exception>
+                    actionIfTrue,
+            FunctionWithException<? super T, ? extends StateFuture<V>, ? extends Exception>
+                    actionIfFalse) {
+        callbackRegistered();
+        if (completableFuture.isDone()) {
+            // this branch must be invoked in task thread when expected
+            T t;
+            try {
+                t = completableFuture.get();
+            } catch (Throwable e) {
+                exceptionHandler.handleException(
+                        "Caught exception when processing completed StateFuture's callback.", e);
+                return null;
+            }
+            callbackFinished();
+            boolean test = FunctionWithException.unchecked(condition).apply(t);
+            StateFuture<?> actionResult;
+            if (test) {
+                actionResult = FunctionWithException.unchecked(actionIfTrue).apply(t);
+            } else {
+                actionResult = FunctionWithException.unchecked(actionIfFalse).apply(t);
+            }
+            StateFutureImpl<Tuple2<Boolean, Object>> ret = makeNewStateFuture();
+            actionResult.thenAccept(
+                    (e) -> {
+                        ret.completeInCallbackRunner(Tuple2.of(test, e));
+                    });
+            callbackFinished();
+            return ret;
+        } else {
+            StateFutureImpl<Tuple2<Boolean, Object>> ret = makeNewStateFuture();
+            completableFuture
+                    .thenAccept(
+                            (t) -> {
+                                callbackRunner.submit(
+                                        () -> {
+                                            boolean test = condition.apply(t);
+                                            StateFuture<?> actionResult;
+                                            if (test) {
+                                                actionResult = actionIfTrue.apply(t);
+                                            } else {
+                                                actionResult = actionIfFalse.apply(t);
+                                            }
+                                            actionResult.thenAccept(
+                                                    (e) -> {
+                                                        ret.completeInCallbackRunner(
+                                                                Tuple2.of(test, e));
+                                                    });
+                                            callbackFinished();
+                                        });
+                            })
+                    .exceptionally(
+                            (e) -> {
+                                exceptionHandler.handleException(
+                                        "Caught exception when submitting StateFuture's callback.",
+                                        e);
+                                return null;
+                            });
+            return ret;
+        }
+    }
+
+    @Override
+    public <U> StateFuture<Tuple2<Boolean, U>> thenConditionallyCompose(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            FunctionWithException<? super T, ? extends StateFuture<U>, ? extends Exception>
+                    actionIfTrue) {
+        callbackRegistered();
+        if (completableFuture.isDone()) {
+            // this branch must be invoked in task thread when expected
+            T t;
+            try {
+                t = completableFuture.get();
+            } catch (Throwable e) {
+                exceptionHandler.handleException(
+                        "Caught exception when processing completed StateFuture's callback.", e);
+                return null;
+            }
+            callbackFinished();

Review Comment:
   IIUC, is it enough to execute `callbackFinished()` only once? 



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36119][state] Conditional interfaces for StateFuture [flink]

Posted by "Zakelly (via GitHub)" <gi...@apache.org>.
Zakelly merged PR #25244:
URL: https://github.com/apache/flink/pull/25244


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36119][state] Conditional interfaces for StateFuture [flink]

Posted by "Zakelly (via GitHub)" <gi...@apache.org>.
Zakelly commented on code in PR #25244:
URL: https://github.com/apache/flink/pull/25244#discussion_r1741599538


##########
flink-core/src/main/java/org/apache/flink/core/state/StateFutureImpl.java:
##########
@@ -223,6 +224,292 @@ public <U, V> StateFuture<V> thenCombine(
         }
     }
 
+    @Override
+    public <U, V> StateFuture<Tuple2<Boolean, Object>> thenConditionallyApply(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            FunctionWithException<? super T, ? extends U, ? extends Exception> actionIfTrue,
+            FunctionWithException<? super T, ? extends V, ? extends Exception> actionIfFalse) {
+        callbackRegistered();
+        if (completableFuture.isDone()) {
+            // this branch must be invoked in task thread when expected
+            T t;
+            try {
+                t = completableFuture.get();
+            } catch (Exception e) {
+                exceptionHandler.handleException(
+                        "Caught exception when processing completed StateFuture's callback.", e);
+                return null;
+            }
+            boolean test = FunctionWithException.unchecked(condition).apply(t);
+            Object r =
+                    test
+                            ? FunctionWithException.unchecked(actionIfTrue).apply(t)
+                            : FunctionWithException.unchecked(actionIfFalse).apply(t);
+
+            callbackFinished();
+            return StateFutureUtils.completedFuture(Tuple2.of(test, r));
+        } else {
+            StateFutureImpl<Tuple2<Boolean, Object>> ret = makeNewStateFuture();
+            completableFuture
+                    .thenAccept(
+                            (t) -> {
+                                callbackRunner.submit(
+                                        () -> {
+                                            boolean test = condition.apply(t);
+                                            Object r =
+                                                    test
+                                                            ? actionIfTrue.apply(t)
+                                                            : actionIfFalse.apply(t);
+                                            ret.completeInCallbackRunner(Tuple2.of(test, r));
+                                            callbackFinished();
+                                        });
+                            })
+                    .exceptionally(
+                            (e) -> {
+                                exceptionHandler.handleException(
+                                        "Caught exception when submitting StateFuture's callback.",
+                                        e);
+                                return null;
+                            });
+            return ret;
+        }
+    }
+
+    @Override
+    public <U> StateFuture<Tuple2<Boolean, U>> thenConditionallyApply(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            FunctionWithException<? super T, ? extends U, ? extends Exception> actionIfTrue) {
+        callbackRegistered();
+        if (completableFuture.isDone()) {
+            // this branch must be invoked in task thread when expected
+            T t;
+            try {
+                t = completableFuture.get();
+            } catch (Exception e) {
+                exceptionHandler.handleException(
+                        "Caught exception when processing completed StateFuture's callback.", e);
+                return null;
+            }
+            boolean test = FunctionWithException.unchecked(condition).apply(t);
+            U r = test ? FunctionWithException.unchecked(actionIfTrue).apply(t) : null;
+
+            callbackFinished();
+            return StateFutureUtils.completedFuture(Tuple2.of(test, r));
+        } else {
+            StateFutureImpl<Tuple2<Boolean, U>> ret = makeNewStateFuture();
+            completableFuture
+                    .thenAccept(
+                            (t) -> {
+                                callbackRunner.submit(
+                                        () -> {
+                                            boolean test = condition.apply(t);
+                                            U r = test ? actionIfTrue.apply(t) : null;
+                                            ret.completeInCallbackRunner(Tuple2.of(test, r));
+                                            callbackFinished();
+                                        });
+                            })
+                    .exceptionally(
+                            (e) -> {
+                                exceptionHandler.handleException(
+                                        "Caught exception when submitting StateFuture's callback.",
+                                        e);
+                                return null;
+                            });
+            return ret;
+        }
+    }
+
+    @Override
+    public StateFuture<Boolean> thenConditionallyAccept(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            ThrowingConsumer<? super T, ? extends Exception> actionIfTrue,
+            ThrowingConsumer<? super T, ? extends Exception> actionIfFalse) {
+        callbackRegistered();
+        if (completableFuture.isDone()) {
+            // this branch must be invoked in task thread when expected
+            T t;
+            try {
+                t = completableFuture.get();
+            } catch (Exception e) {
+                exceptionHandler.handleException(
+                        "Caught exception when processing completed StateFuture's callback.", e);
+                return null;
+            }
+            boolean test = FunctionWithException.unchecked(condition).apply(t);
+            if (test) {
+                ThrowingConsumer.unchecked(actionIfTrue).accept(t);
+            } else {
+                ThrowingConsumer.unchecked(actionIfFalse).accept(t);
+            }
+            callbackFinished();
+            return StateFutureUtils.completedFuture(test);
+        } else {
+            StateFutureImpl<Boolean> ret = makeNewStateFuture();
+            completableFuture
+                    .thenAccept(
+                            (t) -> {
+                                callbackRunner.submit(
+                                        () -> {
+                                            boolean test = condition.apply(t);
+                                            if (test) {
+                                                actionIfTrue.accept(t);
+                                            } else {
+                                                actionIfFalse.accept(t);
+                                            }
+                                            ret.completeInCallbackRunner(test);
+                                            callbackFinished();
+                                        });
+                            })
+                    .exceptionally(
+                            (e) -> {
+                                exceptionHandler.handleException(
+                                        "Caught exception when submitting StateFuture's callback.",
+                                        e);
+                                return null;
+                            });
+            return ret;
+        }
+    }
+
+    @Override
+    public StateFuture<Boolean> thenConditionallyAccept(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            ThrowingConsumer<? super T, ? extends Exception> actionIfTrue) {
+        return thenConditionallyAccept(condition, actionIfTrue, (b) -> {});
+    }
+
+    @Override
+    public <U, V> StateFuture<Tuple2<Boolean, Object>> thenConditionallyCompose(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            FunctionWithException<? super T, ? extends StateFuture<U>, ? extends Exception>
+                    actionIfTrue,
+            FunctionWithException<? super T, ? extends StateFuture<V>, ? extends Exception>
+                    actionIfFalse) {
+        callbackRegistered();
+        if (completableFuture.isDone()) {
+            // this branch must be invoked in task thread when expected
+            T t;
+            try {
+                t = completableFuture.get();
+            } catch (Throwable e) {
+                exceptionHandler.handleException(
+                        "Caught exception when processing completed StateFuture's callback.", e);
+                return null;
+            }
+            callbackFinished();
+            boolean test = FunctionWithException.unchecked(condition).apply(t);
+            StateFuture<?> actionResult;
+            if (test) {
+                actionResult = FunctionWithException.unchecked(actionIfTrue).apply(t);
+            } else {
+                actionResult = FunctionWithException.unchecked(actionIfFalse).apply(t);
+            }
+            StateFutureImpl<Tuple2<Boolean, Object>> ret = makeNewStateFuture();
+            actionResult.thenAccept(
+                    (e) -> {
+                        ret.completeInCallbackRunner(Tuple2.of(test, e));
+                    });
+            callbackFinished();
+            return ret;
+        } else {
+            StateFutureImpl<Tuple2<Boolean, Object>> ret = makeNewStateFuture();
+            completableFuture
+                    .thenAccept(
+                            (t) -> {
+                                callbackRunner.submit(
+                                        () -> {
+                                            boolean test = condition.apply(t);
+                                            StateFuture<?> actionResult;
+                                            if (test) {
+                                                actionResult = actionIfTrue.apply(t);
+                                            } else {
+                                                actionResult = actionIfFalse.apply(t);
+                                            }
+                                            actionResult.thenAccept(
+                                                    (e) -> {
+                                                        ret.completeInCallbackRunner(
+                                                                Tuple2.of(test, e));
+                                                    });
+                                            callbackFinished();
+                                        });
+                            })
+                    .exceptionally(
+                            (e) -> {
+                                exceptionHandler.handleException(
+                                        "Caught exception when submitting StateFuture's callback.",
+                                        e);
+                                return null;
+                            });
+            return ret;
+        }
+    }
+
+    @Override
+    public <U> StateFuture<Tuple2<Boolean, U>> thenConditionallyCompose(
+            FunctionWithException<? super T, Boolean, ? extends Exception> condition,
+            FunctionWithException<? super T, ? extends StateFuture<U>, ? extends Exception>
+                    actionIfTrue) {
+        callbackRegistered();
+        if (completableFuture.isDone()) {
+            // this branch must be invoked in task thread when expected
+            T t;
+            try {
+                t = completableFuture.get();
+            } catch (Throwable e) {
+                exceptionHandler.handleException(
+                        "Caught exception when processing completed StateFuture's callback.", e);
+                return null;
+            }
+            callbackFinished();

Review Comment:
   Yes, thank for pointing this out



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36119] Conditionally interfaces for StateFuture [flink]

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #25244:
URL: https://github.com/apache/flink/pull/25244#issuecomment-2306709535

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "cbf01a2d20076de2728315e127116f3d89073b85",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "cbf01a2d20076de2728315e127116f3d89073b85",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * cbf01a2d20076de2728315e127116f3d89073b85 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org