Java Code Examples for org.apache.helix.model.InstanceConfig#setHostName()

The following examples show how to use org.apache.helix.model.InstanceConfig#setHostName() . 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: HdfsManagerTest.java    From terrapin with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
  MockitoAnnotations.initMocks(this);
  this.testRoutingTableProvider = new TestRoutingTableProvider(null);
  this.hdfsManager = new HdfsManager(new PropertiesConfiguration(),
      mockZkManager,
      CLUSTER_NAME,
      mockHelixAdmin,
      mockDfsClient,
      testRoutingTableProvider);
  hostConfig1 = new InstanceConfig("host1");
  hostConfig1.setHostName("host1");
  hostConfig2 = new InstanceConfig("host2");
  hostConfig2.setHostName("host2");
  hostConfig3 = new InstanceConfig("host3");
  hostConfig3.setHostName("host3");
  hostConfig4 = new InstanceConfig("host4");
  hostConfig4.setHostName("host4");
}
 
Example 2
Source File: Quickstart.java    From helix with Apache License 2.0 6 votes vote down vote up
private static void addNode() throws Exception {

    NUM_NODES = NUM_NODES + 1;
    int port = 12000 + NUM_NODES - 1;
    InstanceConfig instanceConfig = new InstanceConfig("localhost_" + port);
    instanceConfig.setHostName("localhost");
    instanceConfig.setPort("" + port);
    instanceConfig.setInstanceEnabled(true);
    echo("ADDING NEW NODE :" + instanceConfig.getInstanceName()
        + ". Partitions will move from old nodes to the new node.");
    admin.addInstance(CLUSTER_NAME, instanceConfig);
    INSTANCE_CONFIG_LIST.add(instanceConfig);
    MyProcess process = new MyProcess(instanceConfig.getInstanceName());
    PROCESS_LIST.add(process);
    admin.rebalance(CLUSTER_NAME, RESOURCE_NAME, 3);
    process.start();
  }
 
Example 3
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 4
Source File: ManagerControllerHelix.java    From uReplicator with Apache License 2.0 6 votes vote down vote up
public synchronized void start() throws Exception{
  LOGGER.info("Trying to start ManagerControllerHelix!");
  _helixZkManager = HelixManagerFactory.getZKHelixManager(_helixClusterName,
      _instanceId,
      InstanceType.PARTICIPANT,
      _helixZkURL);
  _helixZkManager.getStateMachineEngine().registerStateModelFactory("OnlineOffline",
      new ControllerStateModelFactory(this));
  try {
    _helixZkManager.connect();
    InstanceConfig instanceConfig = new InstanceConfig(_instanceId);
    instanceConfig.setHostName(_hostname);
    instanceConfig.setPort(_port);
    instanceConfig.setInstanceEnabled(true);
    _helixZkManager.getConfigAccessor().setInstanceConfig(_helixClusterName, _instanceId,
        instanceConfig);
  } catch (Exception e) {
    LOGGER.error("Failed to start ManagerControllerHelix " + _helixClusterName, e);
    throw e;
  }
}
 
Example 5
Source File: HelixParticipantTest.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * Generate {@link InstanceConfig} for given data node.
 * @param clusterMap {@link MockClusterMap} to use
 * @param dataNode the data node associated with InstanceConfig.
 * @param sealedReplicas the sealed replicas that should be placed into sealed list. This can be null.
 * @return {@link InstanceConfig} of given data node.
 */
private InstanceConfig generateInstanceConfig(MockClusterMap clusterMap, MockDataNodeId dataNode,
    List<String> sealedReplicas) {
  String instanceName = ClusterMapUtils.getInstanceName(dataNode.getHostname(), dataNode.getPort());
  InstanceConfig instanceConfig = new InstanceConfig(instanceName);
  instanceConfig.setHostName(dataNode.getHostname());
  instanceConfig.setPort(Integer.toString(dataNode.getPort()));
  instanceConfig.getRecord().setSimpleField(DATACENTER_STR, dataNode.getDatacenterName());
  instanceConfig.getRecord().setSimpleField(RACKID_STR, dataNode.getRackId());
  instanceConfig.getRecord().setSimpleField(SCHEMA_VERSION_STR, Integer.toString(CURRENT_SCHEMA_VERSION));
  Map<String, SortedSet<ReplicaId>> mountPathToReplicas = new HashMap<>();
  for (ReplicaId replicaId : clusterMap.getReplicaIds(dataNode)) {
    mountPathToReplicas.computeIfAbsent(replicaId.getMountPath(),
        k -> new TreeSet<>(Comparator.comparing(ReplicaId::getPartitionId))).add(replicaId);
  }
  Map<String, Map<String, String>> mountPathToDiskInfos = new HashMap<>();
  for (Map.Entry<String, SortedSet<ReplicaId>> entry : mountPathToReplicas.entrySet()) {
    String mountPath = entry.getKey();
    StringBuilder replicaStrBuilder = new StringBuilder();
    DiskId diskId = null;
    for (ReplicaId replica : entry.getValue()) {
      replicaStrBuilder.append(replica.getPartitionId().toPathString())
          .append(REPLICAS_STR_SEPARATOR)
          .append(replica.getCapacityInBytes())
          .append(REPLICAS_STR_SEPARATOR)
          .append(replica.getPartitionId().getPartitionClass())
          .append(REPLICAS_DELIM_STR);
      diskId = replica.getDiskId();
    }
    Map<String, String> diskInfo = new HashMap<>();
    diskInfo.put(REPLICAS_STR, replicaStrBuilder.toString());
    diskInfo.put(DISK_CAPACITY_STR, String.valueOf(diskId.getRawCapacityInBytes()));
    diskInfo.put(DISK_STATE, AVAILABLE_STR);
    mountPathToDiskInfos.put(mountPath, diskInfo);
  }
  instanceConfig.getRecord().setMapFields(mountPathToDiskInfos);
  instanceConfig.getRecord()
      .setListField(ClusterMapUtils.SEALED_STR, sealedReplicas == null ? new ArrayList<>() : sealedReplicas);
  return instanceConfig;
}
 
Example 6
Source File: YAMLClusterSetup.java    From helix with Apache License 2.0 5 votes vote down vote up
private static InstanceConfig getInstanceCfg(ParticipantConfig participant) {
  if (participant == null || participant.name == null || participant.host == null
      || participant.port == null) {
    throw new HelixException("Participant must have a specified name, host, and port!");
  }
  InstanceConfig instanceCfg = new InstanceConfig(participant.name);
  instanceCfg.setHostName(participant.host);
  instanceCfg.setPort(participant.port.toString());
  return instanceCfg;
}
 
Example 7
Source File: HelixUtil.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Compose the config for an instance
 * @param instanceName
 * @return InstanceConfig
 */
public static InstanceConfig composeInstanceConfig(String instanceName) {
  InstanceConfig instanceConfig = new InstanceConfig(instanceName);
  String hostName = instanceName;
  String port = "";
  int lastPos = instanceName.lastIndexOf("_");
  if (lastPos > 0) {
    hostName = instanceName.substring(0, lastPos);
    port = instanceName.substring(lastPos + 1);
  }
  instanceConfig.setHostName(hostName);
  instanceConfig.setPort(port);
  instanceConfig.setInstanceEnabled(true);
  return instanceConfig;
}
 
Example 8
Source File: BaseStageTest.java    From helix with Apache License 2.0 5 votes vote down vote up
protected void setupInstances(int numInstances) {
  // setup liveInstances
  for (int i = 0; i < numInstances; i++) {
    String instance = HOSTNAME_PREFIX + i;
    InstanceConfig config = new InstanceConfig(instance);
    config.setHostName(instance);
    config.setPort("12134");
    admin.addInstance(manager.getClusterName(), config);
  }
}
 
Example 9
Source File: ValidationManagerTest.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Test
public void testRebuildBrokerResourceWhenBrokerAdded()
    throws Exception {
  // Check that the first table we added doesn't need to be rebuilt(case where ideal state brokers and brokers in broker resource are the same.
  String partitionName = _offlineTableConfig.getTableName();
  HelixAdmin helixAdmin = _helixManager.getClusterManagmentTool();

  IdealState idealState = HelixHelper.getBrokerIdealStates(helixAdmin, getHelixClusterName());
  // Ensure that the broker resource is not rebuilt.
  Assert.assertTrue(idealState.getInstanceSet(partitionName)
      .equals(_helixResourceManager.getAllInstancesForBrokerTenant(TagNameUtils.DEFAULT_TENANT_NAME)));
  _helixResourceManager.rebuildBrokerResourceFromHelixTags(partitionName);

  // Add another table that needs to be rebuilt
  TableConfig offlineTableConfigTwo = new TableConfigBuilder(TableType.OFFLINE).setTableName(TEST_TABLE_TWO).build();
  _helixResourceManager.addTable(offlineTableConfigTwo);
  String partitionNameTwo = offlineTableConfigTwo.getTableName();

  // Add a new broker manually such that the ideal state is not updated and ensure that rebuild broker resource is called
  final String brokerId = "Broker_localhost_2";
  InstanceConfig instanceConfig = new InstanceConfig(brokerId);
  instanceConfig.setInstanceEnabled(true);
  instanceConfig.setHostName("Broker_localhost");
  instanceConfig.setPort("2");
  helixAdmin.addInstance(getHelixClusterName(), instanceConfig);
  helixAdmin.addInstanceTag(getHelixClusterName(), instanceConfig.getInstanceName(),
      TagNameUtils.getBrokerTagForTenant(TagNameUtils.DEFAULT_TENANT_NAME));
  idealState = HelixHelper.getBrokerIdealStates(helixAdmin, getHelixClusterName());
  // Assert that the two don't equal before the call to rebuild the broker resource.
  Assert.assertTrue(!idealState.getInstanceSet(partitionNameTwo)
      .equals(_helixResourceManager.getAllInstancesForBrokerTenant(TagNameUtils.DEFAULT_TENANT_NAME)));
  _helixResourceManager.rebuildBrokerResourceFromHelixTags(partitionNameTwo);
  idealState = HelixHelper.getBrokerIdealStates(helixAdmin, getHelixClusterName());
  // Assert that the two do equal after being rebuilt.
  Assert.assertTrue(idealState.getInstanceSet(partitionNameTwo)
      .equals(_helixResourceManager.getAllInstancesForBrokerTenant(TagNameUtils.DEFAULT_TENANT_NAME)));
}
 
Example 10
Source File: ZkTestBase.java    From helix with Apache License 2.0 5 votes vote down vote up
protected void setupInstances(String clusterName, int[] instances) {
  HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
  for (int i = 0; i < instances.length; i++) {
    String instance = "localhost_" + instances[i];
    InstanceConfig instanceConfig = new InstanceConfig(instance);
    instanceConfig.setHostName("localhost");
    instanceConfig.setPort("" + instances[i]);
    instanceConfig.setInstanceEnabled(true);
    admin.addInstance(clusterName, instanceConfig);
  }
}
 
Example 11
Source File: TestPropertyCache.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test(enabled = false, description = "First set the property cache and mutate the object from caller")
public void testDefensiveCopyOnDataMutate() {
  // init
  @SuppressWarnings("unchecked")
  PropertyCache<InstanceConfig> propertyCache = new PropertyCache<>(MOCK_CONTROL_CONTEXT_PROVIDER,
      "mock property cache", mock(PropertyCache.PropertyCacheKeyFuncs.class), false);
  InstanceConfig instanceConfig = new InstanceConfig("id");
  Map<String, InstanceConfig> propertyConfigMap = ImmutableMap.of("id", instanceConfig);

  propertyCache.setPropertyMap(propertyConfigMap);
  // mutate the property from outside
  instanceConfig.setHostName("fakeHost");
  String hostName = propertyCache.getPropertyByName("id").getHostName();
  Assert.assertTrue(hostName.isEmpty());
}
 
Example 12
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 13
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 14
Source File: TestTopology.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateClusterTopologyWithDefaultTopology() {
  ClusterConfig clusterConfig = new ClusterConfig("Test_Cluster");
  clusterConfig.setTopologyAwareEnabled(true);

  List<String> allNodes = new ArrayList<String>();
  List<String> liveNodes = new ArrayList<String>();
  Map<String, InstanceConfig> instanceConfigMap = new HashMap<String, InstanceConfig>();

  Map<String, Integer> nodeToWeightMap = new HashMap<String, Integer>();

  for (int i = 0; i < 100; i++) {
    String instance = "localhost_" + i;
    InstanceConfig config = new InstanceConfig(instance);
    String zoneId = "rack_" + i / 10;
    config.setZoneId(zoneId);
    config.setHostName(instance);
    config.setPort("9000");
    allNodes.add(instance);

    int weight = 0;
    if (i % 10 != 0) {
      liveNodes.add(instance);
      weight = 1000;
      if (i % 3 == 0) {
        // set random instance weight.
        weight = (i + 1) * 100;
        config.setWeight(weight);
      }
    }

    instanceConfigMap.put(instance, config);

    if (!nodeToWeightMap.containsKey(zoneId)) {
      nodeToWeightMap.put(zoneId, 0);
    }
    nodeToWeightMap.put(zoneId, nodeToWeightMap.get(zoneId) + weight);
  }

  Topology topo = new Topology(allNodes, liveNodes, instanceConfigMap, clusterConfig);

  Assert.assertTrue(topo.getEndNodeType().equals(Topology.Types.INSTANCE.name()));
  Assert.assertTrue(topo.getFaultZoneType().equals(Topology.Types.ZONE.name()));

  List<Node> faultZones = topo.getFaultZones();
  Assert.assertEquals(faultZones.size(), 10);

  Node root = topo.getRootNode();

  Assert.assertEquals(root.getChildrenCount(Topology.Types.ZONE.name()), 10);
  Assert.assertEquals(root.getChildrenCount(topo.getEndNodeType()), 100);

  // validate weights.
  for (Node rack : root.getChildren()) {
    Assert.assertEquals(rack.getWeight(), (long) nodeToWeightMap.get(rack.getName()));
  }
}
 
Example 15
Source File: HelixServerStarter.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
private void updateInstanceConfigIfNeeded(String host, int port) {
  InstanceConfig instanceConfig = _helixAdmin.getInstanceConfig(_helixClusterName, _instanceId);
  boolean needToUpdateInstanceConfig = false;

  // Add default instance tags if not exist
  List<String> instanceTags = instanceConfig.getTags();
  if (instanceTags == null || instanceTags.size() == 0) {
    if (ZKMetadataProvider.getClusterTenantIsolationEnabled(_helixManager.getHelixPropertyStore())) {
      instanceConfig.addTag(TagNameUtils.getOfflineTagForTenant(null));
      instanceConfig.addTag(TagNameUtils.getRealtimeTagForTenant(null));
    } else {
      instanceConfig.addTag(UNTAGGED_SERVER_INSTANCE);
    }
    needToUpdateInstanceConfig = true;
  }

  // Update host and port if needed
  if (!host.equals(instanceConfig.getHostName())) {
    instanceConfig.setHostName(host);
    needToUpdateInstanceConfig = true;
  }
  String portStr = Integer.toString(port);
  if (!portStr.equals(instanceConfig.getPort())) {
    instanceConfig.setPort(portStr);
    needToUpdateInstanceConfig = true;
  }

  if (needToUpdateInstanceConfig) {
    LOGGER.info("Updating instance config for instance: {} with instance tags: {}, host: {}, port: {}", _instanceId,
        instanceTags, host, port);
  } else {
    LOGGER.info("Instance config for instance: {} has instance tags: {}, host: {}, port: {}, no need to update",
        _instanceId, instanceTags, host, port);
    return;
  }

  // NOTE: Use HelixDataAccessor.setProperty() instead of HelixAdmin.setInstanceConfig() because the latter explicitly
  // forbids instance host/port modification
  HelixDataAccessor helixDataAccessor = _helixManager.getHelixDataAccessor();
  Preconditions.checkState(
      helixDataAccessor.setProperty(helixDataAccessor.keyBuilder().instanceConfig(_instanceId), instanceConfig),
      "Failed to update instance config");
}
 
Example 16
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();
    }
  }
}
 
Example 17
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 18
Source File: TestTopology.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateClusterTopology() {
  ClusterConfig clusterConfig = new ClusterConfig("Test_Cluster");

  String topology = "/Rack/Sub-Rack/Host/Instance";
  clusterConfig.setTopology(topology);
  clusterConfig.setFaultZoneType("Sub-Rack");
  clusterConfig.setTopologyAwareEnabled(true);

  List<String> allNodes = new ArrayList<String>();
  List<String> liveNodes = new ArrayList<String>();
  Map<String, InstanceConfig> instanceConfigMap = new HashMap<String, InstanceConfig>();

  Map<String, Integer> nodeToWeightMap = new HashMap<String, Integer>();

  for (int i = 0; i < 100; i++) {
    String instance = "localhost_" + i;
    InstanceConfig config = new InstanceConfig(instance);
    String rack_id = "rack_" + i/25;
    String sub_rack_id = "subrack-" + i/5;

    String domain =
        String.format("Rack=%s, Sub-Rack=%s, Host=%s", rack_id, sub_rack_id, instance);
    config.setDomain(domain);
    config.setHostName(instance);
    config.setPort("9000");
    allNodes.add(instance);

    int weight = 0;
    if (i % 10 != 0) {
      liveNodes.add(instance);
      weight = 1000;
      if (i % 3 == 0) {
        // set random instance weight.
        weight = (i+1) * 100;
        config.setWeight(weight);
      }
    }

    instanceConfigMap.put(instance, config);

    if (!nodeToWeightMap.containsKey(rack_id)) {
      nodeToWeightMap.put(rack_id, 0);
    }
    nodeToWeightMap.put(rack_id, nodeToWeightMap.get(rack_id) + weight);
    if (!nodeToWeightMap.containsKey(sub_rack_id)) {
      nodeToWeightMap.put(sub_rack_id, 0);
    }
    nodeToWeightMap.put(sub_rack_id, nodeToWeightMap.get(sub_rack_id) + weight);
  }

  Topology topo = new Topology(allNodes, liveNodes, instanceConfigMap, clusterConfig);

  Assert.assertTrue(topo.getEndNodeType().equals("Instance"));
  Assert.assertTrue(topo.getFaultZoneType().equals("Sub-Rack"));

  List<Node> faultZones = topo.getFaultZones();
  Assert.assertEquals(faultZones.size(), 20);

  Node root = topo.getRootNode();

  Assert.assertEquals(root.getChildrenCount("Rack"), 4);
  Assert.assertEquals(root.getChildrenCount("Sub-Rack"), 20);
  Assert.assertEquals(root.getChildrenCount("Host"), 100);
  Assert.assertEquals(root.getChildrenCount("Instance"), 100);


  // validate weights.
  for (Node rack : root.getChildren()) {
    Assert.assertEquals(rack.getWeight(), (long)nodeToWeightMap.get(rack.getName()));
    for (Node subRack : rack.getChildren()) {
      Assert.assertEquals(subRack.getWeight(), (long)nodeToWeightMap.get(subRack.getName()));
    }
  }
}
 
Example 19
Source File: TestZkHelixAdmin.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetResourcesWithTag() {
  String TEST_TAG = "TestTAG";

  String className = TestHelper.getTestClassName();
  String methodName = TestHelper.getTestMethodName();
  String clusterName = className + "_" + methodName;

  HelixAdmin tool = new ZKHelixAdmin(_gZkClient);
  tool.addCluster(clusterName, true);
  Assert.assertTrue(ZKUtil.isClusterSetup(clusterName, _gZkClient));

  tool.addStateModelDef(clusterName, "OnlineOffline",
      new StateModelDefinition(StateModelConfigGenerator.generateConfigForOnlineOffline()));

  for (int i = 0; i < 4; i++) {
    String instanceName = "host" + i + "_9999";
    InstanceConfig config = new InstanceConfig(instanceName);
    config.setHostName("host" + i);
    config.setPort("9999");
    // set tag to two instances
    if (i < 2) {
      config.addTag(TEST_TAG);
    }
    tool.addInstance(clusterName, config);
    tool.enableInstance(clusterName, instanceName, true);
    String path = PropertyPathBuilder.instance(clusterName, instanceName);
    AssertJUnit.assertTrue(_gZkClient.exists(path));
  }

  for (int i = 0; i < 4; i++) {
    String resourceName = "database_" + i;
    IdealState is = new IdealState(resourceName);
    is.setStateModelDefRef("OnlineOffline");
    is.setNumPartitions(2);
    is.setRebalanceMode(IdealState.RebalanceMode.FULL_AUTO);
    is.setReplicas("1");
    is.enable(true);
    if (i < 2) {
      is.setInstanceGroupTag(TEST_TAG);
    }
    tool.addResource(clusterName, resourceName, is);
  }

  List<String> allResources = tool.getResourcesInCluster(clusterName);
  List<String> resourcesWithTag = tool.getResourcesInClusterWithTag(clusterName, TEST_TAG);
  AssertJUnit.assertEquals(allResources.size(), 4);
  AssertJUnit.assertEquals(resourcesWithTag.size(), 2);

  tool.dropCluster(clusterName);
}
 
Example 20
Source File: ClusterChangeHandlerTest.java    From ambry with Apache License 2.0 4 votes vote down vote up
/**
 * Test that {@link DynamicClusterChangeHandler} is able to handle invalid info entry in the InstanceConfig at runtime
 * or during initialization.
 */
@Test
public void instanceConfigInvalidInfoEntryTest() {
  Properties properties = new Properties();
  properties.putAll(props);
  properties.setProperty("clustermap.cluster.change.handler.type", "DynamicClusterChangeHandler");
  ClusterMapConfig clusterMapConfig = new ClusterMapConfig(new VerifiableProperties(properties));
  HelixClusterManager.HelixClusterManagerCallback mockManagerCallback =
      Mockito.mock(HelixClusterManager.HelixClusterManagerCallback.class);
  HelixClusterManager.ClusterChangeHandlerCallback mockHandlerCallback =
      Mockito.mock(HelixClusterManager.ClusterChangeHandlerCallback.class);
  doAnswer(returnsFirstArg()).when(mockHandlerCallback).addPartitionIfAbsent(any(), anyLong());

  Counter initFailureCount = new Counter();
  DynamicClusterChangeHandler dynamicChangeHandler =
      new DynamicClusterChangeHandler(clusterMapConfig, localDc, selfInstanceName, Collections.emptyMap(),
          mockManagerCallback, mockHandlerCallback,
          new HelixClusterManagerMetrics(new MetricRegistry(), mockManagerCallback), e -> initFailureCount.inc(),
          new AtomicLong());
  // create an InstanceConfig with invalid entry that mocks error info added by Helix controller
  PartitionId selectedPartition = testPartitionLayout.getPartitionLayout().getPartitions(null).get(0);
  Replica testReplica = (Replica) selectedPartition.getReplicaIds().get(0);
  DataNode testNode = (DataNode) testReplica.getDataNodeId();
  InstanceConfig instanceConfig = new InstanceConfig(getInstanceName(testNode.getHostname(), testNode.getPort()));
  instanceConfig.setHostName(testNode.getHostname());
  instanceConfig.setPort(Integer.toString(testNode.getPort()));
  instanceConfig.getRecord().setSimpleField(ClusterMapUtils.DATACENTER_STR, testNode.getDatacenterName());
  instanceConfig.getRecord().setSimpleField(ClusterMapUtils.RACKID_STR, testNode.getRackId());
  instanceConfig.getRecord()
      .setSimpleField(ClusterMapUtils.SCHEMA_VERSION_STR, Integer.toString(ClusterMapUtils.CURRENT_SCHEMA_VERSION));
  instanceConfig.getRecord().setListField(ClusterMapUtils.SEALED_STR, Collections.emptyList());
  instanceConfig.getRecord().setListField(ClusterMapUtils.STOPPED_REPLICAS_STR, Collections.emptyList());

  Map<String, Map<String, String>> diskInfos = new HashMap<>();
  assertNotNull("testReplica should not be null", testReplica);
  Map<String, String> diskInfo = new HashMap<>();
  diskInfo.put(ClusterMapUtils.DISK_CAPACITY_STR, Long.toString(testReplica.getDiskId().getRawCapacityInBytes()));
  diskInfo.put(ClusterMapUtils.DISK_STATE, ClusterMapUtils.AVAILABLE_STR);
  String replicasStrBuilder =
      testReplica.getPartition().getId() + ClusterMapUtils.REPLICAS_STR_SEPARATOR + testReplica.getCapacityInBytes()
          + ClusterMapUtils.REPLICAS_STR_SEPARATOR + testReplica.getPartition().getPartitionClass()
          + ClusterMapUtils.REPLICAS_DELIM_STR;
  diskInfo.put(ClusterMapUtils.REPLICAS_STR, replicasStrBuilder);
  diskInfos.put(testReplica.getDiskId().getMountPath(), diskInfo);
  // add an invalid entry at the end of diskInfos
  Map<String, String> invalidEntry = new HashMap<>();
  invalidEntry.put("INVALID_KEY", "INVALID_VALUE");
  diskInfos.put("INVALID_MOUNT_PATH", invalidEntry);
  instanceConfig.getRecord().setMapFields(diskInfos);
  // we call onInstanceConfigChange() twice
  // 1st call, to verify initialization code path
  dynamicChangeHandler.onDataNodeConfigChange(
      Collections.singleton(InstanceConfigToDataNodeConfigAdapter.convert(instanceConfig, clusterMapConfig)));
  // 2nd call, to verify dynamic update code path
  dynamicChangeHandler.onDataNodeConfigChange(
      Collections.singletonList(InstanceConfigToDataNodeConfigAdapter.convert(instanceConfig, clusterMapConfig)));
  assertEquals("There shouldn't be initialization errors", 0, initFailureCount.getCount());
}