zachjsh commented on code in PR #16719:
URL: https://github.com/apache/druid/pull/16719#discussion_r1672837201


##########
server/src/main/java/org/apache/druid/server/coordinator/duty/RoundRobinIterator.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.server.coordinator.duty;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ * A round-robin iterator that has the following properties:
+ * <ul>
+ *   <li> Starts with an initial random cursor position in an ordered list of 
candidates. </li>
+ *   <li> Consecutive {@code next()} iterations from {@link #getIterator()} 
are guaranteed to be deterministic
+ *   unless the set of candidates change when {@link #updateCandidates(Set)} 
is called. </li>
+ *   <li> Guarantees that no duplicate candidates are returned in two 
consecutive {@code next()} iterations. </li>
+ * </ul>
+ */
+public class RoundRobinIterator
+{
+  private final List<String> candidates = new ArrayList<>();
+  private int currentPosition;
+  private String previousCandidate;
+
+  /**
+   * Update the candidates with the supplied input if the input set is 
different from the current candidates.
+   * If the input set is the same as the existing candidates, the cursor 
position remains unchanged so the iteration order
+   * is determistic and resumed. If the input set is different, the cursor 
position is reset to a random location in
+   * the new list of sorted candidates.
+   */
+  public void updateCandidates(final Set<String> input)
+  {
+    if (new HashSet<>(candidates).equals(input)) {
+      return;
+    }
+    this.candidates.clear();
+    this.candidates.addAll(input);
+    Collections.sort(this.candidates);
+    this.currentPosition = getInitialCursorPosition(input.size());
+  }
+
+  public Iterator<String> getIterator()
+  {
+    return new Iterator<String>()
+    {
+      @Override
+      public boolean hasNext()
+      {
+        return candidates.size() > 0;
+      }
+
+      @Override
+      public String next()
+      {
+        if (!hasNext()) {
+          throw new NoSuchElementException();
+        }
+
+        // If the next candidate to be served is the same as the previous 
candidate served (duplicate),

Review Comment:
   Does this only happen if the input list contains duplicates? If so, should 
we just remove duplicates during construction? Or do we want to allow for 
duplicates, and have the amount of duplication increase the probability of a 
candidate being constructed?



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to