Search in sources :

Example 76 with ZkClient

use of org.apache.helix.manager.zk.ZkClient 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 77 with ZkClient

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

Example 78 with ZkClient

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

the class FileStore method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.err.println("USAGE: java FileStore zookeeperAddress(e.g. localhost:2181) serverId(host_port)");
        System.exit(1);
    }
    final String zkAddr = args[0];
    final String clusterName = SetupCluster.DEFAULT_CLUSTER_NAME;
    final String serverId = args[1];
    ZkClient zkclient = null;
    try {
        // start consumer
        final FileStore store = new FileStore(zkAddr, clusterName, serverId);
        Runtime.getRuntime().addShutdownHook(new Thread() {

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

Example 79 with ZkClient

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

the class ZkChildResource method delete.

@Override
public Representation delete() {
    String zkPath = getZKPath();
    try {
        ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
        List<String> childNames = zkClient.getChildren(zkPath);
        if (childNames != null) {
            for (String childName : childNames) {
                String childPath = zkPath.equals("/") ? "/" + childName : zkPath + "/" + childName;
                zkClient.deleteRecursively(childPath);
            }
        }
        getResponse().setStatus(Status.SUCCESS_OK);
    } catch (Exception e) {
        getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
        getResponse().setStatus(Status.SUCCESS_OK);
        LOG.error("Error in delete zkChild: " + zkPath, e);
    }
    return null;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient)

Example 80 with ZkClient

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

the class ZkChildResource method get.

@Override
public Representation get() {
    StringRepresentation presentation = null;
    String zkPath = getZKPath();
    try {
        ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
        ZNRecord result = readZkChild(zkPath, zkClient);
        presentation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(result), MediaType.APPLICATION_JSON);
    } catch (Exception e) {
        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
        presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
        LOG.error("Error in read zkPath: " + zkPath, e);
    }
    return presentation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) ZNRecord(org.apache.helix.ZNRecord)

Aggregations

ZkClient (org.apache.helix.manager.zk.ZkClient)109 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)39 ZNRecord (org.apache.helix.ZNRecord)29 StringRepresentation (org.restlet.representation.StringRepresentation)29 ClusterSetup (org.apache.helix.tools.ClusterSetup)26 HelixException (org.apache.helix.HelixException)23 Builder (org.apache.helix.PropertyKey.Builder)18 IOException (java.io.IOException)17 HelixDataAccessor (org.apache.helix.HelixDataAccessor)15 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)15 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)15 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)12 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)10 Test (org.testng.annotations.Test)10 Date (java.util.Date)9 BeforeClass (org.testng.annotations.BeforeClass)9 PropertyKey (org.apache.helix.PropertyKey)8 IdealState (org.apache.helix.model.IdealState)8 InstanceConfig (org.apache.helix.model.InstanceConfig)8 StateModelDefinition (org.apache.helix.model.StateModelDefinition)7