String instanceName = "localhost_" + (11420 + i);
clusterSetup.addInstanceToCluster(clusterName, instanceName);
}
// Create a known problematic scenario
HelixAdmin admin = clusterSetup.getClusterManagementTool();
String resourceName = "MailboxDB";
IdealState idealState = new IdealState(resourceName + "DR");
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setStateModelDefRef("LeaderStandby");
idealState.setReplicas(String.valueOf(NUM_REPLICAS));
idealState.setNumPartitions(NUM_PARTITIONS);
for (int i = 0; i < NUM_PARTITIONS; i++) {
String partitionName = resourceName + '_' + i;
List<String> assignmentList = Lists.newArrayList();
if (i < NUM_PARTITIONS / 2) {
assignmentList.add("localhost_11420");
} else {
assignmentList.add("localhost_11421");
}
Map<String, String> emptyMap = Maps.newHashMap();
idealState.getRecord().setListField(partitionName, assignmentList);
idealState.getRecord().setMapField(partitionName, emptyMap);
}
admin.addResource(clusterName, idealState.getResourceName(), idealState);
// Start everything
MockParticipantManager[] participants = new MockParticipantManager[NUM_PARTICIPANTS];
for (int i = 0; i < NUM_PARTICIPANTS; i++) {
String instanceName = "localhost_" + (11420 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
}
ClusterControllerManager controller =
new ClusterControllerManager(ZK_ADDR, clusterName, "controller_1");
controller.syncStart();
Thread.sleep(1000);
// Switch to full auto
idealState.setRebalanceMode(RebalanceMode.FULL_AUTO);
for (int i = 0; i < NUM_PARTITIONS; i++) {
List<String> emptyList = Collections.emptyList();
idealState.getRecord().setListField(resourceName + '_' + i, emptyList);
}
admin.setResourceIdealState(clusterName, idealState.getResourceName(), idealState);
Thread.sleep(1000);
// Get the external view
HelixDataAccessor accessor = controller.getHelixDataAccessor();
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
ExternalView externalView =
accessor.getProperty(keyBuilder.externalView(idealState.getResourceName()));
// Disable the partitions in an order known to cause problems
int[] pid = {
0, 7
};
for (int i = 0; i < pid.length; i++) {
String partitionName = resourceName + '_' + pid[i];
Map<String, String> stateMap = externalView.getStateMap(partitionName);
String leader = null;
for (String participantName : stateMap.keySet()) {
String state = stateMap.get(participantName);
if (state.equals("LEADER")) {
leader = participantName;
}
}
List<String> partitionNames = Lists.newArrayList(partitionName);
admin.enablePartition(false, clusterName, leader, idealState.getResourceName(),
partitionNames);
Thread.sleep(1000);
}