Search in sources :

Example 1 with RebalanceMode

use of org.apache.helix.model.IdealState.RebalanceMode in project helix by apache.

the class IdealStateExample method main.

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.err.println("USAGE: IdealStateExample zkAddress clusterName idealStateMode (FULL_AUTO, SEMI_AUTO, or CUSTOMIZED) idealStateJsonFile (required for CUSTOMIZED mode)");
        System.exit(1);
    }
    final String zkAddr = args[0];
    final String clusterName = args[1];
    final String idealStateRebalancerModeStr = args[2].toUpperCase();
    String idealStateJsonFile = null;
    RebalanceMode idealStateRebalancerMode = RebalanceMode.valueOf(idealStateRebalancerModeStr);
    if (idealStateRebalancerMode == RebalanceMode.CUSTOMIZED) {
        if (args.length < 4) {
            System.err.println("Missng idealStateJsonFile for CUSTOMIZED ideal state mode");
            System.exit(1);
        }
        idealStateJsonFile = args[3];
    }
    // add cluster {clusterName}
    ZkClient zkclient = new ZkClient(zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
    ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
    admin.addCluster(clusterName, true);
    // add MasterSlave state mode definition
    StateModelConfigGenerator generator = new StateModelConfigGenerator();
    admin.addStateModelDef(clusterName, "MasterSlave", new StateModelDefinition(generator.generateConfigForMasterSlave()));
    // add 3 participants: "localhost:{12918, 12919, 12920}"
    for (int i = 0; i < 3; 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 resource "TestDB" which has 4 partitions and uses MasterSlave state model
    String resourceName = "TestDB";
    if (idealStateRebalancerMode == RebalanceMode.SEMI_AUTO || idealStateRebalancerMode == RebalanceMode.FULL_AUTO) {
        admin.addResource(clusterName, resourceName, 4, "MasterSlave", idealStateRebalancerModeStr);
        // rebalance resource "TestDB" using 3 replicas
        admin.rebalance(clusterName, resourceName, 3);
    } else if (idealStateRebalancerMode == RebalanceMode.CUSTOMIZED) {
        admin.addIdealState(clusterName, resourceName, idealStateJsonFile);
    }
    // start helix controller
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                HelixControllerMain.main(new String[] { "--zkSvr", zkAddr, "--cluster", clusterName });
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }).start();
    // start 3 dummy participants
    for (int i = 0; i < 3; i++) {
        int port = 12918 + i;
        final String instanceName = "localhost_" + port;
        new Thread(new Runnable() {

            @Override
            public void run() {
                DummyParticipant.main(new String[] { zkAddr, clusterName, instanceName });
            }
        }).start();
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) InstanceConfig(org.apache.helix.model.InstanceConfig) StateModelDefinition(org.apache.helix.model.StateModelDefinition) RebalanceMode(org.apache.helix.model.IdealState.RebalanceMode) StateModelConfigGenerator(org.apache.helix.tools.StateModelConfigGenerator) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer)

Example 2 with RebalanceMode

use of org.apache.helix.model.IdealState.RebalanceMode in project helix by apache.

the class ClusterSetup method createIdealStateForResourceGroup.

/**
 * Create an IdealState for a resource that belongs to a resource group We use
 * "resourceGroupName$resourceInstanceTag" as the IdealState znode name to differetiate different
 * resources from the same resourceGroup.
 */
public IdealState createIdealStateForResourceGroup(String resourceGroupName, String resourceTag, int numPartition, int replica, String rebalanceMode, String stateModelDefName) {
    String idealStateId = genIdealStateNameWithResourceTag(resourceGroupName, resourceTag);
    IdealState idealState = new IdealState(idealStateId);
    idealState.setNumPartitions(numPartition);
    idealState.setStateModelDefRef(stateModelDefName);
    IdealState.RebalanceMode mode = idealState.rebalanceModeFromString(rebalanceMode, IdealState.RebalanceMode.SEMI_AUTO);
    idealState.setRebalanceMode(mode);
    idealState.setReplicas("" + replica);
    idealState.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
    idealState.setResourceGroupName(resourceGroupName);
    idealState.setInstanceGroupTag(resourceTag);
    idealState.enableGroupRouting(true);
    return idealState;
}
Also used : RebalanceMode(org.apache.helix.model.IdealState.RebalanceMode) IdealState(org.apache.helix.model.IdealState)

Example 3 with RebalanceMode

use of org.apache.helix.model.IdealState.RebalanceMode in project helix by apache.

the class ZKHelixAdmin method addResource.

@Override
public void addResource(String clusterName, String resourceName, int partitions, String stateModelRef, String rebalancerMode, String rebalanceStrategy, int bucketSize, int maxPartitionsPerInstance) {
    if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
        throw new HelixException("cluster " + clusterName + " is not setup yet");
    }
    IdealState idealState = new IdealState(resourceName);
    idealState.setNumPartitions(partitions);
    idealState.setStateModelDefRef(stateModelRef);
    RebalanceMode mode = idealState.rebalanceModeFromString(rebalancerMode, RebalanceMode.SEMI_AUTO);
    idealState.setRebalanceMode(mode);
    idealState.setRebalanceStrategy(rebalanceStrategy);
    idealState.setReplicas("" + 0);
    idealState.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
    if (maxPartitionsPerInstance > 0 && maxPartitionsPerInstance < Integer.MAX_VALUE) {
        idealState.setMaxPartitionsPerInstance(maxPartitionsPerInstance);
    }
    if (bucketSize > 0) {
        idealState.setBucketSize(bucketSize);
    }
    addResource(clusterName, resourceName, idealState);
}
Also used : HelixException(org.apache.helix.HelixException) RebalanceMode(org.apache.helix.model.IdealState.RebalanceMode) IdealState(org.apache.helix.model.IdealState)

Example 4 with RebalanceMode

use of org.apache.helix.model.IdealState.RebalanceMode in project helix by apache.

the class IdealStateBuilderExample method main.

public static void main(String[] args) {
    if (args.length < 3) {
        System.err.println("USAGE: java IdealStateBuilderExample zkAddress clusterName idealStateMode" + " (FULL_AUTO, SEMI_AUTO, CUSTOMIZED, or USER_DEFINED)");
        System.exit(1);
    }
    final String zkAddr = args[0];
    final String clusterName = args[1];
    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:
            {
                SemiAutoModeISBuilder builder = new SemiAutoModeISBuilder(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:
            {
                FullAutoModeISBuilder builder = new FullAutoModeISBuilder(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() {
            try {
                HelixControllerMain.main(new String[] { "--zkSvr", zkAddr, "--cluster", clusterName });
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }).start();
    // start dummy participants
    for (int i = 0; i < n; i++) {
        int port = 12918 + i;
        final String instanceName = "localhost_" + port;
        new Thread(new Runnable() {

            @Override
            public void run() {
                DummyParticipant.main(new String[] { zkAddr, clusterName, instanceName });
            }
        }).start();
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) CustomModeISBuilder(org.apache.helix.model.builder.CustomModeISBuilder) SemiAutoModeISBuilder(org.apache.helix.model.builder.SemiAutoModeISBuilder) FullAutoModeISBuilder(org.apache.helix.model.builder.FullAutoModeISBuilder) IdealState(org.apache.helix.model.IdealState) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) InstanceConfig(org.apache.helix.model.InstanceConfig) StateModelDefinition(org.apache.helix.model.StateModelDefinition) RebalanceMode(org.apache.helix.model.IdealState.RebalanceMode) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer)

Aggregations

RebalanceMode (org.apache.helix.model.IdealState.RebalanceMode)4 IdealState (org.apache.helix.model.IdealState)3 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)2 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)2 ZkClient (org.apache.helix.manager.zk.ZkClient)2 InstanceConfig (org.apache.helix.model.InstanceConfig)2 StateModelDefinition (org.apache.helix.model.StateModelDefinition)2 HelixException (org.apache.helix.HelixException)1 CustomModeISBuilder (org.apache.helix.model.builder.CustomModeISBuilder)1 FullAutoModeISBuilder (org.apache.helix.model.builder.FullAutoModeISBuilder)1 SemiAutoModeISBuilder (org.apache.helix.model.builder.SemiAutoModeISBuilder)1 StateModelConfigGenerator (org.apache.helix.tools.StateModelConfigGenerator)1