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


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

Review Comment:
   I considered making it generic but punted on it until we have another real 
use case (perhaps the compaction duty?). We could have the caller pass a 
comparable as well, but I didn't want to make any premature changes.



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