availableTargets);
+ /**
+ * Given initial key-score pair, it will output a preferred key with the highest current weight.
+ * The current weight of the chosen key will decrease the sum of effective weight. And all current
+ * weight will increment by its effective weight. It may result in "higher score with higher
+ * chosen rate". For example:
+ *
+ * ||========================================||============================================||
+ * ||------------ Broker in cluster ------------||------------- Effective weight -------------||
+ * ||------------------Broker1------------------||-------------------- 5 ---------------------||
+ * ||------------------Broker2------------------||-------------------- 1 ---------------------||
+ * ||------------------Broker3------------------||-------------------- 1 ---------------------||
+ * ||===========================================||============================================||
+ *
+ *
||===================||=======================||===============||======================||
+ * ||--- Request Number ---|| Before current weight || Target Broker || After current weight ||
+ * ||----------1-----------||------ {5, 1, 1} ------||----Broker1----||----- {-2, 1, 1} -----||
+ * ||----------2-----------||------ {3, 2, 2} ------||----Broker1----||----- {-4, 2, 2} -----||
+ * ||----------3-----------||------ {1, 3, 3} ------||----Broker2----||----- { 1,-4, 3} -----||
+ * ||----------4-----------||------ {6,-3, 4} ------||----Broker1----||----- {-1,-3, 4} -----||
+ * ||----------5-----------||------ {4,-2, 5} ------||----Broker3----||----- { 4,-2,-2} -----||
+ * ||----------6-----------||------ {9,-1,-1} ------||----Broker1----||----- { 2,-1,-1} -----||
+ * ||----------7-----------||------ {7, 0, 0} ------||----Broker1----||----- { 0, 0, 0} -----||
+ * ||======================||=======================||===============||======================||
+ */
class SmoothRoundRobin implements RoundRobin {
private final Map effectiveScores;
diff --git a/common/src/main/java/org/astraea/common/partitioner/RoundRobinKeeper.java b/common/src/main/java/org/astraea/common/partitioner/RoundRobinKeeper.java
new file mode 100644
index 0000000000..7a4e68ee79
--- /dev/null
+++ b/common/src/main/java/org/astraea/common/partitioner/RoundRobinKeeper.java
@@ -0,0 +1,59 @@
+/*
+ * 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.astraea.common.partitioner;
+
+import java.time.Duration;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.astraea.common.Lazy;
+import org.astraea.common.admin.ClusterInfo;
+import org.astraea.common.admin.NodeInfo;
+
+public class RoundRobinKeeper {
+ private final AtomicInteger next = new AtomicInteger(0);
+ final int[] roundRobin;
+ final Duration roundRobinLease;
+ volatile long timeToUpdateRoundRobin = -1;
+
+ private RoundRobinKeeper(int preLength, Duration roundRobinLease) {
+ this.roundRobin = new int[preLength];
+ this.roundRobinLease = roundRobinLease;
+ }
+
+ static RoundRobinKeeper of(int preLength, Duration roundRobinLease) {
+ return new RoundRobinKeeper(preLength, roundRobinLease);
+ }
+
+ synchronized void tryToUpdate(ClusterInfo clusterInfo, Lazy