@Test public void testOneMinAvailableReplicas() { Map<String, String> currentInstanceStateMap = SegmentAssignmentUtils.getInstanceStateMap(Arrays.asList("host1", "host2", "host3"), ONLINE); Map<String, String> targetInstanceStateMap = SegmentAssignmentUtils.getInstanceStateMap(Arrays.asList("host1", "host2", "host4"), ONLINE); Map<String, String> nextInstanceStateMap = TableRebalancer.getNextInstanceStateMap(currentInstanceStateMap, targetInstanceStateMap, 1); assertEquals(nextInstanceStateMap, targetInstanceStateMap); targetInstanceStateMap = SegmentAssignmentUtils.getInstanceStateMap(Arrays.asList("host1", "host4", "host5"), ONLINE); nextInstanceStateMap = TableRebalancer.getNextInstanceStateMap(currentInstanceStateMap, targetInstanceStateMap, 1); assertEquals(nextInstanceStateMap, targetInstanceStateMap); targetInstanceStateMap = SegmentAssignmentUtils.getInstanceStateMap(Arrays.asList("host4", "host5", "host6"), ONLINE); nextInstanceStateMap = TableRebalancer.getNextInstanceStateMap(currentInstanceStateMap, targetInstanceStateMap, 1); assertEquals(getNumCommonInstances(currentInstanceStateMap, nextInstanceStateMap), 1); assertEquals(TableRebalancer.getNextInstanceStateMap(nextInstanceStateMap, targetInstanceStateMap, 1), targetInstanceStateMap); targetInstanceStateMap = SegmentAssignmentUtils.getInstanceStateMap(Arrays.asList("host4", "host5", "host6", "host7"), ONLINE); nextInstanceStateMap = TableRebalancer.getNextInstanceStateMap(currentInstanceStateMap, targetInstanceStateMap, 1); assertEquals(getNumCommonInstances(currentInstanceStateMap, nextInstanceStateMap), 1); assertEquals(TableRebalancer.getNextInstanceStateMap(nextInstanceStateMap, targetInstanceStateMap, 1), targetInstanceStateMap); currentInstanceStateMap = SegmentAssignmentUtils.getInstanceStateMap(Arrays.asList("host1", "host2", "host3", "host4"), ONLINE); targetInstanceStateMap = SegmentAssignmentUtils.getInstanceStateMap(Arrays.asList("host5", "host6", "host7"), ONLINE); nextInstanceStateMap = TableRebalancer.getNextInstanceStateMap(currentInstanceStateMap, targetInstanceStateMap, 1); assertEquals(getNumCommonInstances(currentInstanceStateMap, nextInstanceStateMap), 1); assertEquals(TableRebalancer.getNextInstanceStateMap(nextInstanceStateMap, targetInstanceStateMap, 1), targetInstanceStateMap); }
@VisibleForTesting @SuppressWarnings("Duplicates") static Map<String, String> getNextInstanceStateMap(Map<String, String> currentInstanceStateMap, Map<String, String> targetInstanceStateMap, int minAvailableReplicas) { Map<String, String> nextInstanceStateMap = new TreeMap<>(); for (Map.Entry<String, String> entry : targetInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (currentInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); } } int instancesToKeep = minAvailableReplicas - nextInstanceStateMap.size(); if (instancesToKeep > 0) { for (Map.Entry<String, String> entry : currentInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (!nextInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); if (--instancesToKeep == 0) { break; } } } } int instancesToAdd = targetInstanceStateMap.size() - nextInstanceStateMap.size(); if (instancesToAdd > 0) { for (Map.Entry<String, String> entry : targetInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (!nextInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); if (--instancesToAdd == 0) { break; } } } } return nextInstanceStateMap; }
TableRebalancer { @VisibleForTesting @SuppressWarnings("Duplicates") static Map<String, String> getNextInstanceStateMap(Map<String, String> currentInstanceStateMap, Map<String, String> targetInstanceStateMap, int minAvailableReplicas) { Map<String, String> nextInstanceStateMap = new TreeMap<>(); for (Map.Entry<String, String> entry : targetInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (currentInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); } } int instancesToKeep = minAvailableReplicas - nextInstanceStateMap.size(); if (instancesToKeep > 0) { for (Map.Entry<String, String> entry : currentInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (!nextInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); if (--instancesToKeep == 0) { break; } } } } int instancesToAdd = targetInstanceStateMap.size() - nextInstanceStateMap.size(); if (instancesToAdd > 0) { for (Map.Entry<String, String> entry : targetInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (!nextInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); if (--instancesToAdd == 0) { break; } } } } return nextInstanceStateMap; } }
TableRebalancer { @VisibleForTesting @SuppressWarnings("Duplicates") static Map<String, String> getNextInstanceStateMap(Map<String, String> currentInstanceStateMap, Map<String, String> targetInstanceStateMap, int minAvailableReplicas) { Map<String, String> nextInstanceStateMap = new TreeMap<>(); for (Map.Entry<String, String> entry : targetInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (currentInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); } } int instancesToKeep = minAvailableReplicas - nextInstanceStateMap.size(); if (instancesToKeep > 0) { for (Map.Entry<String, String> entry : currentInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (!nextInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); if (--instancesToKeep == 0) { break; } } } } int instancesToAdd = targetInstanceStateMap.size() - nextInstanceStateMap.size(); if (instancesToAdd > 0) { for (Map.Entry<String, String> entry : targetInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (!nextInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); if (--instancesToAdd == 0) { break; } } } } return nextInstanceStateMap; } TableRebalancer(HelixManager helixManager); }
TableRebalancer { @VisibleForTesting @SuppressWarnings("Duplicates") static Map<String, String> getNextInstanceStateMap(Map<String, String> currentInstanceStateMap, Map<String, String> targetInstanceStateMap, int minAvailableReplicas) { Map<String, String> nextInstanceStateMap = new TreeMap<>(); for (Map.Entry<String, String> entry : targetInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (currentInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); } } int instancesToKeep = minAvailableReplicas - nextInstanceStateMap.size(); if (instancesToKeep > 0) { for (Map.Entry<String, String> entry : currentInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (!nextInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); if (--instancesToKeep == 0) { break; } } } } int instancesToAdd = targetInstanceStateMap.size() - nextInstanceStateMap.size(); if (instancesToAdd > 0) { for (Map.Entry<String, String> entry : targetInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (!nextInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); if (--instancesToAdd == 0) { break; } } } } return nextInstanceStateMap; } TableRebalancer(HelixManager helixManager); RebalanceResult rebalance(TableConfig tableConfig, Configuration rebalanceConfig); }
TableRebalancer { @VisibleForTesting @SuppressWarnings("Duplicates") static Map<String, String> getNextInstanceStateMap(Map<String, String> currentInstanceStateMap, Map<String, String> targetInstanceStateMap, int minAvailableReplicas) { Map<String, String> nextInstanceStateMap = new TreeMap<>(); for (Map.Entry<String, String> entry : targetInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (currentInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); } } int instancesToKeep = minAvailableReplicas - nextInstanceStateMap.size(); if (instancesToKeep > 0) { for (Map.Entry<String, String> entry : currentInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (!nextInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); if (--instancesToKeep == 0) { break; } } } } int instancesToAdd = targetInstanceStateMap.size() - nextInstanceStateMap.size(); if (instancesToAdd > 0) { for (Map.Entry<String, String> entry : targetInstanceStateMap.entrySet()) { String instanceName = entry.getKey(); if (!nextInstanceStateMap.containsKey(instanceName)) { nextInstanceStateMap.put(instanceName, entry.getValue()); if (--instancesToAdd == 0) { break; } } } } return nextInstanceStateMap; } TableRebalancer(HelixManager helixManager); RebalanceResult rebalance(TableConfig tableConfig, Configuration rebalanceConfig);  }