Java Code Examples for org.apache.helix.manager.zk.ZKHelixAdmin#addInstance()

The following examples show how to use org.apache.helix.manager.zk.ZKHelixAdmin#addInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Quickstart.java    From helix with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: TestMessageThrottle2.java    From helix with Apache License 2.0 6 votes vote down vote up
private static void addInstanceConfig(String instanceName) {
  // add node to cluster if not already added
  ZKHelixAdmin admin = new ZKHelixAdmin(ZK_ADDR);

  InstanceConfig instanceConfig = null;
  try {
    instanceConfig = admin.getInstanceConfig(_clusterName, instanceName);
  } catch (Exception ignored) {
  }
  if (instanceConfig == null) {
    InstanceConfig config = new InstanceConfig(instanceName);
    config.setHostName("localhost");
    config.setInstanceEnabled(true);
    echo("Adding InstanceConfig:" + config);
    admin.addInstance(_clusterName, config);
  }
}
 
Example 3
Source File: Worker.java    From helix with Apache License 2.0 5 votes vote down vote up
@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();
    }
  }
}
 
Example 4
Source File: LockProcess.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
  }
}
 
Example 5
Source File: Consumer.java    From helix with Apache License 2.0 4 votes vote down vote up
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();
    }
  }
}
 
Example 6
Source File: SetupCluster.java    From helix with Apache License 2.0 4 votes vote down vote up
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();
    }
  }
}