HdfsManager implements ControllerChangeListener { static double calculateDeviationForResource(String resource, IdealState idealState, RoutingTableProvider routingTableProvider) { Set<String> partitions = idealState.getPartitionSet(); int totalAssignments = 0, totalDeviations = 0; for (String partition : partitions) { Set<String> idealInstances = new HashSet(idealState.getInstanceSet(partition)); totalAssignments += idealInstances.size(); List<InstanceConfig> currentInstanceConfigs = routingTableProvider.getInstances( resource, partition, "ONLINE"); Set<String> currentInstances = Sets.newHashSetWithExpectedSize( currentInstanceConfigs.size()); if (currentInstanceConfigs != null) { for (InstanceConfig instanceConfig : currentInstanceConfigs) { currentInstances.add(instanceConfig.getHostName()); } } idealInstances.removeAll(currentInstances); totalDeviations += idealInstances.size(); } return (double)totalDeviations / totalAssignments; } HdfsManager(PropertiesConfiguration configuration,
                     ZooKeeperManager zkManager,
                     String clusterName,
                     HelixAdmin helixAdmin,
                     DFSClient hdfsClient,
                     RoutingTableProvider routingTableProvider); @Override synchronized void onControllerChange(NotificationContext notificationContext); void createDataPaths(); void shutdown();  }
@Test public void testCalculateDeviationForResource() { String resource = "resource"; CustomModeISBuilder isBuilder = new CustomModeISBuilder(resource); isBuilder.assignInstanceAndState("0", "host1", "ONLINE"); isBuilder.assignInstanceAndState("0", "host2", "ONLINE"); isBuilder.assignInstanceAndState("1", "host2", "ONLINE"); isBuilder.assignInstanceAndState("1", "host3", "ONLINE"); isBuilder.setNumPartitions(2); isBuilder.setNumReplica(3); isBuilder.setRebalancerMode(IdealState.RebalanceMode.CUSTOMIZED); isBuilder.setStateModel("OnlineOffline"); IdealState idealState = isBuilder.build(); Map<String, List<InstanceConfig>> onlinePartitionMap1 = (Map)ImmutableMap.of( "0", ImmutableList.of(hostConfig1, hostConfig2), "1", ImmutableList.of(hostConfig3, hostConfig4)); TestRoutingTableProvider routingTableProvider = new TestRoutingTableProvider(ImmutableMap.of(resource, onlinePartitionMap1)); assertEquals(0.25, HdfsManager.calculateDeviationForResource( resource, idealState, routingTableProvider), 0.01); Map<String, List<InstanceConfig>> onlinePartitionMap2 = (Map)ImmutableMap.of( "0", ImmutableList.of(hostConfig1, hostConfig2), "1", ImmutableList.of(hostConfig2, hostConfig3)); routingTableProvider.setOnlinePartitionMap(ImmutableMap.of(resource, onlinePartitionMap2)); assertEquals(0.0, HdfsManager.calculateDeviationForResource( resource, idealState, routingTableProvider), 0.01); }