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

The following examples show how to use org.apache.helix.manager.zk.ZKHelixAdmin#addCluster() . 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: TestConfigAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetRestConfig() {
  String className = TestHelper.getTestClassName();
  String methodName = TestHelper.getTestMethodName();
  String clusterName = className + "_" + methodName;

  ZKHelixAdmin admin = new ZKHelixAdmin(ZK_ADDR);
  admin.addCluster(clusterName, true);
  ConfigAccessor configAccessor = new ConfigAccessor(ZK_ADDR);
  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.REST).forCluster(clusterName).build();
  Assert.assertNull(configAccessor.getRESTConfig(clusterName));

  RESTConfig restConfig = new RESTConfig(clusterName);
  restConfig.set(RESTConfig.SimpleFields.CUSTOMIZED_HEALTH_URL, "TEST_URL");
  configAccessor.setRESTConfig(clusterName, restConfig);
  Assert.assertEquals(restConfig, configAccessor.getRESTConfig(clusterName));
}
 
Example 3
Source File: SetupConsumerCluster.java    From helix with Apache License 2.0 5 votes vote down vote up
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();
    }
  }
}
 
Example 4
Source File: WeightAwareRebalanceUtilExample.java    From helix with Apache License 2.0 4 votes vote down vote up
private static void rebalanceUtilUsageWithZkBasedDataProvider() {
  System.out.println(String.format("Start rebalancing using WeightAwareRebalanceUtil and ZK based Capacity/Weight data providers for %d resources.", nResources));

  // Init a zkserver & cluster nodes for this example
  ZkServer zkServer = ExampleHelper.startZkServer(ZK_ADDRESS);
  admin = new ZKHelixAdmin(ZK_ADDRESS);
  admin.addCluster(CLUSTER_NAME, true);

  /**
   * In order to avoid re-construct capacity / usage information every time, user can choose to use ZK based providers.
   * In this example, we assume the evaluating metrics are QPS and memory.
   * In this case, 2 sets of constraints are needed.
   *
   * Init and persistent ZkBasedDataProvider.
   * 1. Create ZK based providers and init with capacity / weight information.
   * 2. Persist providers in to HelixPropertyStore.
   * 3. Read from ZK when necessary.
   */
  // For QPS
  ZkBasedPartitionWeightProvider qpsWeightProvider =
      new ZkBasedPartitionWeightProvider(ZK_ADDRESS, CLUSTER_NAME, "QPS");
  // Note that user can specify more detailed weight info for each partition.
  qpsWeightProvider.updateWeights(Collections.EMPTY_MAP, Collections.EMPTY_MAP, resourceWeight);
  ZkBasedCapacityProvider qpsCapacityProvider =
      new ZkBasedCapacityProvider(ZK_ADDRESS, CLUSTER_NAME, "QPS");
  qpsCapacityProvider.updateCapacity(Collections.EMPTY_MAP, Collections.EMPTY_MAP, defaultCapacity);
  // For Memory
  ZkBasedPartitionWeightProvider memoryWeightProvider =
      new ZkBasedPartitionWeightProvider(ZK_ADDRESS, CLUSTER_NAME, "MEM");
  // Note that user can specify more detailed capacity and usage info for each participant.
  memoryWeightProvider.updateWeights(Collections.EMPTY_MAP, Collections.EMPTY_MAP, resourceWeight);
  ZkBasedCapacityProvider memoryCapacityProvider =
      new ZkBasedCapacityProvider(ZK_ADDRESS, CLUSTER_NAME, "MEM");
  memoryCapacityProvider.updateCapacity(Collections.EMPTY_MAP, Collections.EMPTY_MAP, defaultCapacity);

  // Persist providers
  qpsCapacityProvider.persistCapacity();
  qpsWeightProvider.persistWeights();
  memoryCapacityProvider.persistCapacity();
  memoryWeightProvider.persistWeights();

  /**
   * Init constraints with ZkBasedDataProvider
   * 1. Read providers from ZK by constructing the object with same ZK address, cluster name, and dimension name
   * 2. Specify constraints with the provider. Only use soft constraint here for simplifying.
   */
  qpsWeightProvider =
      new ZkBasedPartitionWeightProvider(ZK_ADDRESS, CLUSTER_NAME, "QPS");
  qpsCapacityProvider =
      new ZkBasedCapacityProvider(ZK_ADDRESS, CLUSTER_NAME, "QPS");
  memoryWeightProvider =
      new ZkBasedPartitionWeightProvider(ZK_ADDRESS, CLUSTER_NAME, "MEM");
  memoryCapacityProvider =
      new ZkBasedCapacityProvider(ZK_ADDRESS, CLUSTER_NAME, "MEM");

  // !WARNING! Don't put providers that are not providing same type of data
  PartitionWeightAwareEvennessConstraint qpsConstraint =
      new PartitionWeightAwareEvennessConstraint(qpsWeightProvider, qpsCapacityProvider);
  PartitionWeightAwareEvennessConstraint memoryConstraint =
      new PartitionWeightAwareEvennessConstraint(memoryWeightProvider, memoryCapacityProvider);

  List<AbstractRebalanceSoftConstraint> softConstraints = new ArrayList<>();
  softConstraints.add(qpsConstraint);
  softConstraints.add(memoryConstraint);

  /**
   * Call util to calculate partition assignment.
   * Here, use the same simple config set for example. User can always customize the configs.
   */
  WeightAwareRebalanceUtil util = new WeightAwareRebalanceUtil(clusterConfig, instanceConfigs);
  ResourcesStateMap assignment =
      util.buildIncrementalRebalanceAssignment(resourceConfigs, null, Collections.EMPTY_LIST,
          softConstraints);

  ExampleHelper.stopZkServer(zkServer);

  System.out.println(String.format("Finished rebalancing using WeightAwareRebalanceUtil and ZK based Capacity/Weight data providers for %d resources.", nResources));
  printAssignmentInfo(assignment);
}
 
Example 5
Source File: TestHelper.java    From helix with Apache License 2.0 4 votes vote down vote up
public static void setupEmptyCluster(HelixZkClient zkClient, String clusterName) {
  ZKHelixAdmin admin = new ZKHelixAdmin(zkClient);
  admin.addCluster(clusterName, true);
}
 
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();
    }
  }
}