abhishekrb19 commented on code in PR #16719: URL: https://github.com/apache/druid/pull/16719#discussion_r1692682396
########## server/src/main/java/org/apache/druid/server/coordinator/duty/RoundRobinIterator.java: ########## @@ -0,0 +1,135 @@ +/* + * 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 org.apache.druid.error.InvalidInput; + +import javax.annotation.concurrent.NotThreadSafe; +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 is backed by an ordered list of candidates containing no duplicates. The iterator + * iterates endlessly over all the candidates. The caller must explicitly terminate it. + * The iterator has the following properties: + * <ul> + * <li> Starts with an initial random cursor position in an ordered list of candidates. </li> + * <li> Invoking {@code next()} on {@link #getIterator()} is guaranteed to result in a deterministic order + * unless the set of candidates change when {@link #updateCandidates(Set)} is called. When the candidates change, + * the cursor is reset to a random position in the new list of ordered candidates. </li> + * <li> Guarantees that no duplicate candidates are returned in two consecutive {@code next()} iterations. </li> + * </ul> + * + * <p> + * This class is not thread-safe and must be used from a single thread. + */ +@NotThreadSafe +public class RoundRobinIterator Review Comment: Okay, thanks for the suggestions. I've added `CircularList` which is currently used by the kill duty. I haven't refactored `RoundRobinServerSelector` in this PR, so a few differences: 1. `advanceCursor()` is not public, as it's not required by the kill duty 2. I didn't add `resetCursor()` to keep things simple. Since the kill duty also tracks `prevDatasourceKilled`, this seems sufficient `CircularList` is a sorted list. I've noted this in the Javadocs, but we can perhaps consider renaming the class to `CircularSortedList`. What do you think? -- 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]
