// parse the YAML
Yaml yaml = new Yaml();
YAMLClusterConfig cfg = yaml.loadAs(input, YAMLClusterConfig.class);
// create the cluster
HelixAdmin helixAdmin = new ZKHelixAdmin(_zkAddress);
if (cfg.clusterName == null) {
throw new HelixException("Cluster name is required!");
}
helixAdmin.addCluster(cfg.clusterName);
// add each participant
if (cfg.participants != null) {
for (ParticipantConfig participant : cfg.participants) {
helixAdmin.addInstance(cfg.clusterName, getInstanceCfg(participant));
}
}
// add each resource
if (cfg.resources != null) {
for (ResourceConfig resource : cfg.resources) {
if (resource.name == null) {
throw new HelixException("Resources must be named!");
}
if (resource.stateModel == null || resource.stateModel.name == null) {
throw new HelixException("Resource must specify a named state model!");
}
// if states is null, assume using a built-in or already-added state model
if (resource.stateModel.states != null) {
StateModelDefinition stateModelDef =
getStateModelDef(resource.stateModel, resource.constraints);
helixAdmin.addStateModelDef(cfg.clusterName, resource.stateModel.name, stateModelDef);
} else {
StateModelDefinition stateModelDef = null;
if (resource.stateModel.name.equals("MasterSlave")) {
stateModelDef =
new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave());
} else if (resource.stateModel.name.equals("OnlineOffline")) {
stateModelDef =
new StateModelDefinition(StateModelConfigGenerator.generateConfigForOnlineOffline());
} else if (resource.stateModel.name.equals("LeaderStandby")) {
stateModelDef =
new StateModelDefinition(StateModelConfigGenerator.generateConfigForLeaderStandby());
}
if (stateModelDef != null) {
try {
helixAdmin.addStateModelDef(cfg.clusterName, resource.stateModel.name, stateModelDef);
} catch (HelixException e) {
LOG.warn("State model definition " + resource.stateModel.name
+ " could not be added.");
}
}
}
int partitions = 1;
int replicas = 1;
if (resource.partitions != null) {
if (resource.partitions.containsKey("count")) {
partitions = resource.partitions.get("count");
}
if (resource.partitions.containsKey("replicas")) {
replicas = resource.partitions.get("replicas");
}
}
if (resource.rebalancer == null || !resource.rebalancer.containsKey("mode")) {
throw new HelixException("Rebalance mode is required!");
}
helixAdmin.addResource(cfg.clusterName, resource.name, partitions,
resource.stateModel.name, resource.rebalancer.get("mode"));
// user-defined rebalancer
if (resource.rebalancer.containsKey("class")
&& resource.rebalancer.get("mode").equals(RebalanceMode.USER_DEFINED.toString())) {
IdealState idealState = helixAdmin.getResourceIdealState(cfg.clusterName, resource.name);
idealState.setRebalancerClassName(resource.rebalancer.get("class"));
helixAdmin.setResourceIdealState(cfg.clusterName, resource.name, idealState);
}
helixAdmin.rebalance(cfg.clusterName, resource.name, replicas);
}
}
// enable auto join if this option is set
if (cfg.autoJoinAllowed != null && cfg.autoJoinAllowed) {
HelixConfigScope scope =
new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(cfg.clusterName)
.build();
Map<String, String> properties = new HashMap<String, String>();
properties.put(ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN, cfg.autoJoinAllowed.toString());
helixAdmin.setConfig(scope, properties);
}
return cfg;
}