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

The following examples show how to use org.apache.helix.manager.zk.ZKHelixAdmin#getInstancesInCluster() . 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: PistachiosFormatter.java    From Pistachio with Apache License 2.0 6 votes vote down vote up
public static PistachioClusterInfo getClusterInfo() {
    try {
        String zookeeperConnStr = ConfigurationManager.getConfiguration().getString("Pistachio.ZooKeeper.Server");
        ZKHelixAdmin admin = new ZKHelixAdmin(zookeeperConnStr);
        IdealState idealState = admin.getResourceIdealState("PistachiosCluster", "PistachiosResource");
        PistachioClusterInfo info = new PistachioClusterInfo();
        info.numPartitions = idealState.getNumPartitions();
        info.numReplicas = Integer.parseInt(idealState.getReplicas());
        info.hostList = admin.getInstancesInCluster("PistachiosCluster");

        logger.info("num partitions: {}, num Replicas: {}, hostList: {}.", info.numPartitions,
            info.numReplicas, Joiner.on(",").join(info.hostList.toArray()));

        return info;
    } catch (Exception e) {
        logger.info("error getting cluster info", e);
        return null;
    }
}
 
Example 2
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 3
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 4
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 5
Source File: HelixBootstrapUpgradeToolTest.java    From ambry with Apache License 2.0 4 votes vote down vote up
/**
 * Test listing sealed partitions in Helix cluster.
 * @throws Exception
 */
@Test
public void testListSealedPartitions() throws Exception {
  assumeTrue(!dcStr.equals("DC1"));
  String clusterName = CLUSTER_NAME_PREFIX + CLUSTER_NAME_IN_STATIC_CLUSTER_MAP;
  // Test regular bootstrap
  long expectedResourceCount =
      (testPartitionLayout.getPartitionLayout().getPartitionCount() - 1) / DEFAULT_MAX_PARTITIONS_PER_RESOURCE + 1;
  writeBootstrapOrUpgrade(expectedResourceCount, false);
  Set<String> sealedPartitions =
      HelixBootstrapUpgradeUtil.listSealedPartition(hardwareLayoutPath, partitionLayoutPath, zkLayoutPath,
          CLUSTER_NAME_PREFIX, dcStr);
  assertEquals("Sealed partition set should be empty initially", Collections.emptySet(), sealedPartitions);
  // randomly choose 20 partitions to mark as sealed
  int[] intArray = new Random().ints(0, 100).distinct().limit(20).toArray();
  Set<String> selectedSealedPartitionSet = new HashSet<>();
  for (int id : intArray) {
    selectedSealedPartitionSet.add(String.valueOf(id));
  }
  // update the sealed lists in Helix
  for (ZkInfo zkInfo : dcsToZkInfo.values()) {
    ZKHelixAdmin admin = new ZKHelixAdmin("localhost:" + zkInfo.getPort());
    for (String instanceName : admin.getInstancesInCluster(clusterName)) {
      InstanceConfig instanceConfig = admin.getInstanceConfig(clusterName, instanceName);
      Set<String> localReplicas = new HashSet<>();
      for (Map<String, String> diskInfo : instanceConfig.getRecord().getMapFields().values()) {
        String replicaStrs = diskInfo.get(REPLICAS_STR);
        for (String replicaStr : replicaStrs.split(REPLICAS_DELIM_STR)) {
          localReplicas.add(replicaStr.split(REPLICAS_STR_SEPARATOR)[0]);
        }
      }
      // derive the intersection of localReplicas set and selectedSealedPartitionSet
      localReplicas.retainAll(selectedSealedPartitionSet);
      instanceConfig.getRecord().setListField(SEALED_STR, new ArrayList<>(localReplicas));
      admin.setInstanceConfig(clusterName, instanceName, instanceConfig);
    }
  }
  // query sealed partition in Helix again
  sealedPartitions =
      HelixBootstrapUpgradeUtil.listSealedPartition(hardwareLayoutPath, partitionLayoutPath, zkLayoutPath,
          CLUSTER_NAME_PREFIX, dcStr);
  assertEquals("Mismatch in sealed partition set", selectedSealedPartitionSet, sealedPartitions);
}