RebalanceMode idealStateMode = RebalanceMode.valueOf(args[2].toUpperCase());
ZkClient zkclient =
new ZkClient(zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT,
new ZNRecordSerializer());
ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
// add cluster
admin.addCluster(clusterName, true);
// add MasterSlave state model definition
admin.addStateModelDef(clusterName, "MasterSlave", new StateModelDefinition(
StateModelConfigGenerator.generateConfigForMasterSlave()));
// add 2 participants: "localhost:{12918, 12919}"
int n = 2;
for (int i = 0; i < n; i++) {
int port = 12918 + i;
InstanceConfig config = new InstanceConfig("localhost_" + port);
config.setHostName("localhost");
config.setPort(Integer.toString(port));
config.setInstanceEnabled(true);
admin.addInstance(clusterName, config);
}
// add ideal-state according to ideal-state-mode
String resourceName = "TestDB";
IdealState idealState = null;
switch (idealStateMode) {
case SEMI_AUTO: {
AutoModeISBuilder builder = new AutoModeISBuilder(resourceName);
builder.setStateModel("MasterSlave").setNumPartitions(2).setNumReplica(2);
builder.assignPreferenceList(buildPartitionName(resourceName, 0), "localhost_12918",
"localhost_12919").assignPreferenceList(buildPartitionName(resourceName, 1),
"localhost_12919", "localhost_12918");
idealState = builder.build();
break;
}
case FULL_AUTO: {
AutoRebalanceModeISBuilder builder = new AutoRebalanceModeISBuilder(resourceName);
builder.setStateModel("MasterSlave").setNumPartitions(2).setNumReplica(2)
.setMaxPartitionsPerNode(2);
builder.add(buildPartitionName(resourceName, 0)).add(buildPartitionName(resourceName, 1));
idealState = builder.build();
break;
}
case CUSTOMIZED: {
CustomModeISBuilder builder = new CustomModeISBuilder(resourceName);
builder.setStateModel("MasterSlave").setNumPartitions(2).setNumReplica(2);
builder
.assignInstanceAndState(buildPartitionName(resourceName, 0), "localhost_12918", "MASTER")
.assignInstanceAndState(buildPartitionName(resourceName, 0), "localhost_12919", "SLAVE")
.assignInstanceAndState(buildPartitionName(resourceName, 1), "localhost_12918", "SLAVE")
.assignInstanceAndState(buildPartitionName(resourceName, 1), "localhost_12919", "MASTER");
idealState = builder.build();
break;
}
default:
break;
}
admin.addResource(clusterName, resourceName, idealState);
// start helix controller
new Thread(new Runnable() {
@Override
public void run() {