helix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject [06/50] [abbrv] helix git commit: Fix bug in AutoRebalanceStrategy to try to assign orphan replicas to its preferred nodes instead of random nodes.`
Date Thu, 22 Jun 2017 22:57:25 GMT
Fix bug in AutoRebalanceStrategy to try to assign orphan replicas to its preferred nodes instead
of random nodes.`


Project: http://git-wip-us.apache.org/repos/asf/helix/repo
Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/45d76a2a
Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/45d76a2a
Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/45d76a2a

Branch: refs/heads/master
Commit: 45d76a2ab443de770c0975baaf596ae569b74454
Parents: df215ed
Author: Lei Xia <lxia@linkedin.com>
Authored: Wed Aug 10 08:40:15 2016 -0700
Committer: Lei Xia <lxia@linkedin.com>
Committed: Wed Feb 8 09:58:04 2017 -0800

----------------------------------------------------------------------
 .../strategy/AutoRebalanceStrategy.java         | 20 +++++++++-----
 .../rebalancer/TestAutoRebalanceStrategy.java   | 28 ++++++++++++++++++++
 2 files changed, 41 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/helix/blob/45d76a2a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/strategy/AutoRebalanceStrategy.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/strategy/AutoRebalanceStrategy.java
b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/strategy/AutoRebalanceStrategy.java
index 65149ca..8b6a234 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/strategy/AutoRebalanceStrategy.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/strategy/AutoRebalanceStrategy.java
@@ -188,23 +188,29 @@ public class AutoRebalanceStrategy implements RebalanceStrategy {
     Iterator<Replica> it = _orphaned.iterator();
     while (it.hasNext()) {
       Replica replica = it.next();
+      boolean added = false;
 
       // first find if it preferred node still has capacity
       Node preferred = _preferredAssignment.get(replica);
-      boolean added = tryAddReplica(preferred, replica, true);
-
-      if (!added) {
+      if (preferred.capacity > preferred.currentlyAssigned && preferred.canAdd(replica))
{
+        preferred.currentlyAssigned++;
+        preferred.preferred.add(replica);
+        preferred.newReplicas.add(replica);
+        added = true;
+      } else {
         // if preferred node has no capacity, search all nodes and find one that has capacity.
         int startIndex = computeRandomStartIndex(replica);
         for (int index = startIndex; index < startIndex + _liveNodesList.size(); index++)
{
           Node receiver = _liveNodesList.get(index % _liveNodesList.size());
-          added = tryAddReplica(receiver, replica, false);
-          if (added) {
+          if (receiver.capacity > receiver.currentlyAssigned && receiver.canAdd(replica))
{
+            receiver.currentlyAssigned = receiver.currentlyAssigned + 1;
+            receiver.nonPreferred.add(replica);
+            receiver.newReplicas.add(replica);
+            added = true;
             break;
           }
         }
       }
-
       if (!added) {
         // try adding the replica by making room for it
         added = assignOrphanByMakingRoom(replica);
@@ -214,7 +220,7 @@ public class AutoRebalanceStrategy implements RebalanceStrategy {
       }
     }
     if (_orphaned.size() > 0 && logger.isInfoEnabled()) {
-      logger.info("could not assign nodes to partitions: " + _orphaned);
+      logger.warn("could not assign nodes to partitions: " + _orphaned);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/helix/blob/45d76a2a/helix-core/src/test/java/org/apache/helix/controller/rebalancer/TestAutoRebalanceStrategy.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/controller/rebalancer/TestAutoRebalanceStrategy.java
b/helix-core/src/test/java/org/apache/helix/controller/rebalancer/TestAutoRebalanceStrategy.java
index ff20fa5..b73a992 100644
--- a/helix-core/src/test/java/org/apache/helix/controller/rebalancer/TestAutoRebalanceStrategy.java
+++ b/helix-core/src/test/java/org/apache/helix/controller/rebalancer/TestAutoRebalanceStrategy.java
@@ -20,6 +20,7 @@ package org.apache.helix.controller.rebalancer;
  */
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -762,4 +763,31 @@ public class TestAutoRebalanceStrategy {
     }
     Assert.assertEquals(firstNodes.size(), 2, "masters not evenly distributed");
   }
+
+
+  @Test public void test() {
+    int nPartitions = 16;
+    final String resourceName = "something";
+    final List<String> instanceNames =
+        Arrays.asList("node-1", "node-2", "node-3", "node-4"); // Initialize to 4 unique
strings
+
+    final int nReplicas = 3;
+
+    List<String> partitions = new ArrayList<String>(nPartitions);
+    for (int i = 0; i < nPartitions; i++) {
+      partitions.add(Integer.toString(i));
+    }
+
+    LinkedHashMap<String, Integer> states = new LinkedHashMap<String, Integer>(2);
+    states.put("OFFLINE", 0);
+    states.put("ONLINE", nReplicas);
+
+    AutoRebalanceStrategy strategy = new AutoRebalanceStrategy(resourceName, partitions,
states);
+    ZNRecord znRecord = strategy.computePartitionAssignment(instanceNames, instanceNames,
+        new HashMap<String, Map<String, String>>(0), null);
+
+    for (List p : znRecord.getListFields().values()) {
+      Assert.assertEquals(p.size(), nReplicas);
+    }
+  }
 }


Mime
View raw message