Search in sources :

Example 66 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project helix by apache.

the class Worker method run.

@Override
public void run() {
    ZkClient zkclient = null;
    try {
        // add node to cluster if not already added
        zkclient = new ZkClient(_zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
        ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
        List<String> nodes = admin.getInstancesInCluster(_clusterName);
        if (!nodes.contains(_instanceName)) {
            InstanceConfig config = new InstanceConfig(_instanceName);
            config.setHostName("localhost");
            config.setInstanceEnabled(true);
            admin.addInstance(_clusterName, config);
        }
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                System.out.println("Shutting down " + _instanceName);
                disconnect();
            }
        });
        connect();
    } finally {
        if (zkclient != null) {
            zkclient.close();
        }
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) InstanceConfig(org.apache.helix.model.InstanceConfig) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer)

Example 67 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin 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)

Example 68 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project helix by apache.

the class Quickstart method setup.

public static void setup() {
    admin = new ZKHelixAdmin(ZK_ADDRESS);
    // create cluster
    echo("Creating cluster: " + CLUSTER_NAME);
    admin.addCluster(CLUSTER_NAME, true);
    // Add nodes to the cluster
    echo("Adding " + NUM_NODES + " participants to the cluster");
    for (int i = 0; i < NUM_NODES; i++) {
        admin.addInstance(CLUSTER_NAME, INSTANCE_CONFIG_LIST.get(i));
        echo("\t Added participant: " + INSTANCE_CONFIG_LIST.get(i).getInstanceName());
    }
    // Add a state model
    StateModelDefinition myStateModel = defineStateModel();
    echo("Configuring StateModel: " + "MyStateModel  with 1 Master and 1 Slave");
    admin.addStateModelDef(CLUSTER_NAME, STATE_MODEL_NAME, myStateModel);
    // Add a resource with 6 partitions and 2 replicas
    echo("Adding a resource MyResource: " + "with 6 partitions and 2 replicas");
    admin.addResource(CLUSTER_NAME, RESOURCE_NAME, NUM_PARTITIONS, STATE_MODEL_NAME, "AUTO");
    // this will set up the ideal state, it calculates the preference list for
    // each partition similar to consistent hashing
    admin.rebalance(CLUSTER_NAME, RESOURCE_NAME, NUM_REPLICAS);
}
Also used : ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) StateModelDefinition(org.apache.helix.model.StateModelDefinition)

Example 69 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project databus by linkedin.

the class ClusterCheckpointPersistenceProvider method createCluster.

/**
 * Create a cluster if it doesn't exist Note: This method is not thread-safe
 * as HelixAdmin.addCluster appears to fail on concurrent execution within
 * threads
 *
 * @return true if cluster was created false otherwise
 */
public static boolean createCluster(String zkAddr, String clusterName) {
    boolean created = false;
    ZkClient zkClient = null;
    try {
        zkClient = new ZkClient(zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
        ZKHelixAdmin admin = new ZKHelixAdmin(zkClient);
        admin.addCluster(clusterName, false);
        created = true;
    } catch (HelixException e) {
        LOG.warn("Warn! Cluster might already exist! " + clusterName);
        created = false;
    } finally {
        // close this connection
        if (zkClient != null) {
            zkClient.close();
        }
    }
    return created;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixException(org.apache.helix.HelixException) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer)

Aggregations

ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)69 HelixAdmin (org.apache.helix.HelixAdmin)30 IdealState (org.apache.helix.model.IdealState)25 Test (org.testng.annotations.Test)23 Date (java.util.Date)21 InstanceConfig (org.apache.helix.model.InstanceConfig)16 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)14 ZNRecord (org.apache.helix.ZNRecord)13 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)13 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)12 ZkClient (org.apache.helix.manager.zk.ZkClient)12 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)12 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)11 StateModelDefinition (org.apache.helix.model.StateModelDefinition)11 HashMap (java.util.HashMap)10 HashSet (java.util.HashSet)10 HelixDataAccessor (org.apache.helix.HelixDataAccessor)8 ExternalView (org.apache.helix.model.ExternalView)8 Test (org.junit.Test)8 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)7