Search in sources :

Example 1 with ParticipantConfig

use of org.apache.helix.tools.YAMLClusterSetup.YAMLClusterConfig.ParticipantConfig in project helix by apache.

the class YAMLClusterSetup method setupCluster.

/**
 * Set up the cluster by parsing a YAML file.
 * @param input InputStream representing the file
 * @return ClusterConfig Java wrapper of the configuration file
 */
public YAMLClusterConfig setupCluster(InputStream input) {
    // 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;
}
Also used : ParticipantConfig(org.apache.helix.tools.YAMLClusterSetup.YAMLClusterConfig.ParticipantConfig) HashMap(java.util.HashMap) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) Yaml(org.yaml.snakeyaml.Yaml) IdealState(org.apache.helix.model.IdealState) HelixException(org.apache.helix.HelixException) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) StateModelDefinition(org.apache.helix.model.StateModelDefinition) ResourceConfig(org.apache.helix.tools.YAMLClusterSetup.YAMLClusterConfig.ResourceConfig) HelixConfigScope(org.apache.helix.model.HelixConfigScope)

Aggregations

HashMap (java.util.HashMap)1 HelixAdmin (org.apache.helix.HelixAdmin)1 HelixException (org.apache.helix.HelixException)1 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)1 HelixConfigScope (org.apache.helix.model.HelixConfigScope)1 IdealState (org.apache.helix.model.IdealState)1 StateModelDefinition (org.apache.helix.model.StateModelDefinition)1 HelixConfigScopeBuilder (org.apache.helix.model.builder.HelixConfigScopeBuilder)1 ParticipantConfig (org.apache.helix.tools.YAMLClusterSetup.YAMLClusterConfig.ParticipantConfig)1 ResourceConfig (org.apache.helix.tools.YAMLClusterSetup.YAMLClusterConfig.ResourceConfig)1 Yaml (org.yaml.snakeyaml.Yaml)1