Zakelly commented on code in PR #25308:
URL: https://github.com/apache/flink/pull/25308#discussion_r1758275098


##########
flink-runtime/src/main/java/org/apache/flink/runtime/state/v2/InternalReducingState.java:
##########
@@ -58,6 +72,107 @@ 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();
+        }
+        // phase 1: read from the sources and target
+        List<StateFuture<V>> futures = new ArrayList<>(sources.size() + 1);
+        for (N source : sources) {
+            if (source != null) {
+                setCurrentNamespace(source);
+                futures.add(handleRequest(StateRequestType.REDUCING_GET, 
null));
+            }
+        }
+        setCurrentNamespace(target);
+        futures.add(handleRequest(StateRequestType.REDUCING_GET, null));
+        // phase 2: merge the sources to the target
+        return StateFutureUtils.combineAll(futures)
+                .thenCompose(
+                        values -> {
+                            List<StateFuture<V>> removeFutures = new 
ArrayList<>(sources.size());
+                            V current = null;
+                            Iterator<V> valueIterator = values.iterator();
+                            for (N source : sources) {
+                                V value = valueIterator.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 targetValue = valueIterator.next();
+                            if (current != null && targetValue != null) {
+                                current = reduceFunction.reduce(current, 
targetValue);
+                            }
+                            V finalCurrent = current;
+                            return StateFutureUtils.combineAll(removeFutures)
+                                    .thenApply(ignore -> finalCurrent);
+                        })
+                .thenAccept(
+                        currentValue -> {
+                            if (currentValue == null) {
+                                return;
+                            }
+                            setCurrentNamespace(target);
+                            handleRequest(StateRequestType.REDUCING_ADD, 
currentValue);
+                        });

Review Comment:
   how about combineAll REDUCING_REMOVE and REDUCING_ADD?



-- 
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]

Reply via email to