fredia commented on code in PR #25308:
URL: https://github.com/apache/flink/pull/25308#discussion_r1758159745
##########
flink-runtime/src/main/java/org/apache/flink/runtime/state/v2/InternalReducingState.java:
##########
@@ -58,6 +72,108 @@ public V get() {
@Override
public void add(V value) {
- handleRequestSync(StateRequestType.REDUCING_ADD, value);
+ V oldValue = handleRequestSync(StateRequestType.REDUCING_GET, null);
+ try {
+ V newValue = oldValue == null ? value :
reduceFunction.reduce(oldValue, value);
+ handleRequestSync(StateRequestType.REDUCING_ADD, newValue);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public StateFuture<Void> asyncMergeNamespaces(N target, Collection<N>
sources)
+ throws Exception {
+ if (sources == null || sources.isEmpty()) {
+ return StateFutureUtils.completedVoidFuture();
+ }
+ List<StateFuture<V>> futures = new ArrayList<>();
+ for (N source : sources) {
+ if (source != null) {
+ setCurrentNamespace(source);
+ futures.add(handleRequest(StateRequestType.REDUCING_GET,
null));
+ }
+ }
+ return StateFutureUtils.combineAll(futures)
+ .thenCompose(
+ values -> {
+ List<StateFuture<V>> removeFutures = new
ArrayList<>();
+ V current = null;
+ Iterator<N> sourceIter = sources.iterator();
+ for (V value : values) {
+ N source = sourceIter.next();
+ if (value != null) {
+ setCurrentNamespace(source);
+ removeFutures.add(
+
handleRequest(StateRequestType.REDUCING_REMOVE, null));
+ if (current != null) {
+ current =
reduceFunction.reduce(current, value);
+ } else {
+ current = value;
+ }
+ }
+ }
+ V finalCurrent = current;
+ return StateFutureUtils.combineAll(removeFutures)
+ .thenApply(ignore -> finalCurrent);
+ })
+ .thenAccept(
+ currentValue -> {
+ if (currentValue == null) {
+ return;
+ }
+ setCurrentNamespace(target);
+ handleRequest(StateRequestType.REDUCING_GET, null)
+ .thenAccept(
+ targetValue -> {
+ if (targetValue == null) {
+ handleRequest(
+
StateRequestType.REDUCING_ADD,
+ currentValue);
+ } else {
+ handleRequest(
+
StateRequestType.REDUCING_ADD,
+
reduceFunction.reduce(
+
currentValue, (V) targetValue));
+ }
+ });
+ });
Review Comment:
👍 done.
--
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]