int replicas = 1;
String resourceName = resources[i];
ZNRecord record = DefaultIdealStateCalculator
.calculateIdealState(instances, partitions, replicas,
resourceName, "MASTER", "SLAVE");
IdealState idealState = new IdealState(record);
idealState.setStateModelDefRef("MasterSlave");
HelixDataAccessor accessor = manager.getHelixDataAccessor();
Builder keyBuilder = accessor.keyBuilder();
accessor.setProperty(keyBuilder.idealStates(resourceName),
idealState);
idealStates.add(idealState);
}
// ADD A LIVE INSTANCE WITH A CURRENT STATE THAT CONTAINS RESOURCE WHICH NO
// LONGER EXISTS IN IDEALSTATE
String instanceName = "localhost_" + 3;
LiveInstance liveInstance = new LiveInstance(instanceName);
String sessionId = UUID.randomUUID().toString();
liveInstance.setSessionId(sessionId);
HelixDataAccessor accessor = manager.getHelixDataAccessor();
Builder keyBuilder = accessor.keyBuilder();
accessor.setProperty(keyBuilder.liveInstance(instanceName),
liveInstance);
String oldResource = "testResourceOld";
CurrentState currentState = new CurrentState(oldResource);
currentState.setState("testResourceOld_0", "OFFLINE");
currentState.setState("testResourceOld_1", "SLAVE");
currentState.setState("testResourceOld_2", "MASTER");
currentState.setStateModelDefRef("MasterSlave");
accessor.setProperty(keyBuilder.currentState(instanceName, sessionId, oldResource),
currentState);
ResourceComputationStage stage = new ResourceComputationStage();
runStage(event, new ReadClusterDataStage());
runStage(event, stage);
Map<String, Resource> resourceMap = event
.getAttribute(AttributeName.RESOURCES.toString());
// +1 because it will have one for current state
AssertJUnit.assertEquals(resources.length + 1, resourceMap.size());
for (int i = 0; i < resources.length; i++)
{
String resourceName = resources[i];
IdealState idealState = idealStates.get(i);
AssertJUnit.assertTrue(resourceMap.containsKey(resourceName));
AssertJUnit.assertEquals(resourceMap.get(resourceName)
.getResourceName(), resourceName);
AssertJUnit.assertEquals(resourceMap.get(resourceName)
.getStateModelDefRef(), idealState.getStateModelDefRef());
AssertJUnit.assertEquals(resourceMap.get(resourceName)
.getPartitions().size(), idealState.getNumPartitions());
}
// Test the data derived from CurrentState
AssertJUnit.assertTrue(resourceMap.containsKey(oldResource));
AssertJUnit.assertEquals(resourceMap.get(oldResource)
.getResourceName(), oldResource);