Search in sources :

Example 61 with ZKHelixAdmin

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

the class SetupCluster method main.

public static void main(String[] args) {
    if (args.length < 2) {
        System.err.println("USAGE: java SetupCluster zookeeperAddress(e.g. localhost:2181) numberOfNodes");
        System.exit(1);
    }
    final String zkAddr = args[0];
    final int numNodes = Integer.parseInt(args[1]);
    final String clusterName = DEFAULT_CLUSTER_NAME;
    ZkClient zkclient = null;
    try {
        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 state model definition
        StateModelConfigGenerator generator = new StateModelConfigGenerator();
        admin.addStateModelDef(clusterName, DEFAULT_STATE_MODEL, new StateModelDefinition(generator.generateConfigForOnlineOffline()));
        // addNodes
        for (int i = 0; i < numNodes; i++) {
            String port = "" + (12001 + i);
            String serverId = "localhost_" + port;
            InstanceConfig config = new InstanceConfig(serverId);
            config.setHostName("localhost");
            config.setPort(port);
            config.setInstanceEnabled(true);
            admin.addInstance(clusterName, config);
        }
        // add resource "repository" which has 1 partition
        String resourceName = DEFAULT_RESOURCE_NAME;
        admin.addResource(clusterName, resourceName, DEFAULT_PARTITION_NUMBER, DEFAULT_STATE_MODEL, RebalanceMode.SEMI_AUTO.toString());
        admin.rebalance(clusterName, resourceName, 1);
    } 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) StateModelDefinition(org.apache.helix.model.StateModelDefinition) StateModelConfigGenerator(org.apache.helix.tools.StateModelConfigGenerator) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer)

Example 62 with ZKHelixAdmin

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

the class LockManagerDemo method main.

/**
 * LockManagerDemo clusterName, numInstances, lockGroupName, numLocks
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    final String zkAddress = "localhost:2199";
    final String clusterName = "lock-manager-demo";
    final String lockGroupName = "lock-group";
    final int numInstances = 3;
    final int numPartitions = 12;
    final boolean startController = false;
    HelixManager controllerManager = null;
    Thread[] processArray;
    processArray = new Thread[numInstances];
    try {
        startLocalZookeeper(2199);
        HelixAdmin admin = new ZKHelixAdmin(zkAddress);
        admin.addCluster(clusterName, true);
        StateModelConfigGenerator generator = new StateModelConfigGenerator();
        admin.addStateModelDef(clusterName, "OnlineOffline", new StateModelDefinition(generator.generateConfigForOnlineOffline()));
        admin.addResource(clusterName, lockGroupName, numPartitions, "OnlineOffline", RebalanceMode.FULL_AUTO.toString());
        admin.rebalance(clusterName, lockGroupName, 1);
        for (int i = 0; i < numInstances; i++) {
            final String instanceName = "localhost_" + (12000 + i);
            processArray[i] = new Thread(new Runnable() {

                @Override
                public void run() {
                    LockProcess lockProcess = null;
                    try {
                        lockProcess = new LockProcess(clusterName, zkAddress, instanceName, startController);
                        lockProcess.start();
                        Thread.currentThread().join();
                    } catch (InterruptedException e) {
                        System.out.println(instanceName + "Interrupted");
                        if (lockProcess != null) {
                            lockProcess.stop();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            processArray[i].start();
        }
        Thread.sleep(3000);
        controllerManager = HelixControllerMain.startHelixController(zkAddress, clusterName, "controller", HelixControllerMain.STANDALONE);
        Thread.sleep(5000);
        printStatus(admin, clusterName, lockGroupName);
        System.out.println("Stopping localhost_12000");
        processArray[0].interrupt();
        Thread.sleep(3000);
        printStatus(admin, clusterName, lockGroupName);
        Thread.currentThread().join();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (controllerManager != null) {
            controllerManager.disconnect();
        }
        for (Thread process : processArray) {
            if (process != null) {
                process.interrupt();
            }
        }
    }
}
Also used : HelixManager(org.apache.helix.HelixManager) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) StateModelDefinition(org.apache.helix.model.StateModelDefinition) StateModelConfigGenerator(org.apache.helix.tools.StateModelConfigGenerator)

Example 63 with ZKHelixAdmin

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

the class LockProcess method configureInstance.

/**
 * Configure the instance, the configuration of each node is available to
 * other nodes.
 * @param instanceName
 */
private void configureInstance(String instanceName) {
    ZKHelixAdmin helixAdmin = new ZKHelixAdmin(zkAddress);
    List<String> instancesInCluster = helixAdmin.getInstancesInCluster(clusterName);
    if (instancesInCluster == null || !instancesInCluster.contains(instanceName)) {
        InstanceConfig config = new InstanceConfig(instanceName);
        config.setHostName("localhost");
        config.setPort("12000");
        helixAdmin.addInstance(clusterName, config);
    }
}
Also used : ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) InstanceConfig(org.apache.helix.model.InstanceConfig)

Example 64 with ZKHelixAdmin

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

the class Consumer method main.

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.err.println("USAGE: java Consumer zookeeperAddress (e.g. localhost:2181) consumerId (0-2), rabbitmqServer (e.g. localhost)");
        System.exit(1);
    }
    final String zkAddr = args[0];
    final String clusterName = SetupConsumerCluster.DEFAULT_CLUSTER_NAME;
    final String consumerId = args[1];
    final String mqServer = args[2];
    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("consumer_" + consumerId)) {
            InstanceConfig config = new InstanceConfig("consumer_" + consumerId);
            config.setHostName("localhost");
            config.setInstanceEnabled(true);
            admin.addInstance(clusterName, config);
        }
        // start consumer
        final Consumer consumer = new Consumer(zkAddr, clusterName, "consumer_" + consumerId, mqServer);
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                System.out.println("Shutting down consumer_" + consumerId);
                consumer.disconnect();
            }
        });
        consumer.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 65 with ZKHelixAdmin

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

the class SetupConsumerCluster method main.

public static void main(String[] args) {
    if (args.length < 1) {
        System.err.println("USAGE: java SetupConsumerCluster zookeeperAddress (e.g. localhost:2181)");
        System.exit(1);
    }
    final String zkAddr = args[0];
    final String clusterName = DEFAULT_CLUSTER_NAME;
    ZkClient zkclient = null;
    try {
        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 state model definition
        admin.addStateModelDef(clusterName, DEFAULT_STATE_MODEL, new StateModelDefinition(StateModelConfigGenerator.generateConfigForOnlineOffline()));
        // add resource "topic" which has 6 partitions
        String resourceName = DEFAULT_RESOURCE_NAME;
        admin.addResource(clusterName, resourceName, DEFAULT_PARTITION_NUMBER, DEFAULT_STATE_MODEL, RebalanceMode.FULL_AUTO.toString());
        admin.rebalance(clusterName, resourceName, 1);
    } finally {
        if (zkclient != null) {
            zkclient.close();
        }
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) StateModelDefinition(org.apache.helix.model.StateModelDefinition) 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