denis-chudov commented on code in PR #6641:
URL: https://github.com/apache/ignite-3/pull/6641#discussion_r2387857857


##########
modules/placement-driver/src/main/java/org/apache/ignite/internal/placementdriver/AssignmentsTracker.java:
##########
@@ -156,6 +184,124 @@ public CompletableFuture<List<TokenizedAssignments>> 
getAssignments(
                 }));
     }
 
+    @Override
+    public CompletableFuture<List<TokenizedAssignments>> 
awaitNonEmptyAssignments(
+            List<? extends ReplicationGroupId> replicationGroupIds,
+            HybridTimestamp clusterTimeToAwait,
+            long timeoutMillis
+    ) {
+        return msManager
+                .clusterTime()
+                .waitFor(clusterTimeToAwait)
+                .thenCompose(ignored -> inBusyLock(busyLock, () -> {
+                    long now = coarseCurrentTimeMillis();
+                    return 
awaitNonEmptyAssignmentsWithCheckMostRecent(replicationGroupIds, now, 
timeoutMillis);
+                }))
+                .thenApply(identity());
+    }
+
+    private CompletableFuture<List<TokenizedAssignments>> 
awaitNonEmptyAssignmentsWithCheckMostRecent(
+            List<? extends ReplicationGroupId> replicationGroupIds,
+            long startTime,
+            long timeoutMillis
+    ) {
+        Map<ReplicationGroupId, TokenizedAssignments> assignmentsMap = 
stableAssignments();
+
+        Map<Integer, CompletableFuture<TokenizedAssignments>> futures = new 
HashMap<>();
+        List<TokenizedAssignments> result = new 
ArrayList<>(replicationGroupIds.size());
+
+        for (int i = 0; i < replicationGroupIds.size(); i++) {
+            ReplicationGroupId groupId = replicationGroupIds.get(i);
+
+            TokenizedAssignments a = assignmentsMap.get(groupId);
+            result.add(a);
+
+            if (a.nodes().isEmpty()) {
+                if (timeoutMillis > 0) {
+                    futures.put(i, nonEmptyAssignmentFuture(groupId, 
timeoutMillis));
+                } else {
+                    // If timeout is zero or less, then this group is failed, 
the correct exception will be thrown
+                    // in #checkEmptyAssignmentsReasons().
+                    futures.put(i, failedFuture(new TimeoutException()));
+                }
+            }
+        }
+
+        if (futures.isEmpty()) {
+            return completedFuture(result);
+        } else {
+            return allOf(futures.values())
+                    .handle((unused, ex) -> {
+                        if (ex == null) {
+                            // Get the most recent assignments after the 
waiting.
+                            long now = System.currentTimeMillis();
+                            long newTimeoutMillis = timeoutMillis - (now - 
startTime);
+                            return 
awaitNonEmptyAssignmentsWithCheckMostRecent(replicationGroupIds, startTime, 
newTimeoutMillis);

Review Comment:
   We discussed that after waiting for the futures, we should check again other 
groups to make sure we get the most recent assignments of all groups. Imagine 
the case:
   - group 0: got assignments [A,B], group 1: need to wait;
   - assignments for group 1 appeared after 10 seconds, they are [D,E], but 
assignments of group 0 already changed to [B,C].
   If we don't call we will return:
   group 0: [A,B], group 1: [D,E]
   
   With current approach `awaitNonEmptyAssignmentsWithCheckMostRecent` will be 
called once again and this is just one more `for` loop making sure that all 
assignments are most recent.



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