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

The following examples show how to use org.apache.helix.model.InstanceConfig#getInstanceName() . 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
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 2
Source File: ServerInstance.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
/**
 * By default (auto joined instances), server instance name is of format: {@code Server_<hostname>_<port>}, e.g.
 * {@code Server_localhost_12345}, hostname is of format: {@code Server_<hostname>}, e.g. {@code Server_localhost}.
 */
public ServerInstance(InstanceConfig instanceConfig) {
  String hostname = instanceConfig.getHostName();
  if (hostname != null) {
    if (hostname.startsWith(Helix.PREFIX_OF_SERVER_INSTANCE)) {
      _hostname = hostname.substring(SERVER_INSTANCE_PREFIX_LENGTH);
    } else {
      _hostname = hostname;
    }
    _port = Integer.parseInt(instanceConfig.getPort());
  } else {
    // Hostname might be null in some tests (InstanceConfig created by calling the constructor instead of fetching
    // from ZK), directly parse the instance name
    String instanceName = instanceConfig.getInstanceName();
    String[] hostnameAndPort = instanceName.split(Helix.PREFIX_OF_SERVER_INSTANCE)[1].split(HOSTNAME_PORT_DELIMITER);
    _hostname = hostnameAndPort[0];
    _port = Integer.parseInt(hostnameAndPort[1]);
  }
}
 
Example 3
Source File: CloudToStoreReplicationManager.java    From ambry with Apache License 2.0 6 votes vote down vote up
@Override
public void onInstanceConfigChange(List<InstanceConfig> instanceConfigs, NotificationContext context) {
  logger.info("Instance config change notification received with instanceConfigs: {}", instanceConfigs);
  Set<CloudDataNode> newVcrNodes = new HashSet<>();
  ConcurrentHashMap<String, CloudDataNode> newInstanceNameToCloudDataNode = new ConcurrentHashMap<>();

  // create a new list of available vcr nodes.
  for (InstanceConfig instanceConfig : instanceConfigs) {
    String instanceName = instanceConfig.getInstanceName();
    Port sslPort =
        getSslPortStr(instanceConfig) == null ? null : new Port(getSslPortStr(instanceConfig), PortType.SSL);
    Port http2Port =
        getHttp2PortStr(instanceConfig) == null ? null : new Port(getHttp2PortStr(instanceConfig), PortType.HTTP2);
    CloudDataNode cloudDataNode = new CloudDataNode(instanceConfig.getHostName(),
        new Port(Integer.parseInt(instanceConfig.getPort()), PortType.PLAINTEXT), sslPort, http2Port,
        clusterMapConfig.clustermapVcrDatacenterName, clusterMapConfig);
    newInstanceNameToCloudDataNode.put(instanceName, cloudDataNode);
    newVcrNodes.add(cloudDataNode);
  }

  synchronized (notificationLock) {
    instanceNameToCloudDataNode.set(newInstanceNameToCloudDataNode);
    handleChangeInVcrNodes(newVcrNodes);
  }
}
 
Example 4
Source File: AssignableNode.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Computes the fault zone id based on the domain and fault zone type when topology is enabled.
 * For example, when
 * the domain is "zone=2, instance=testInstance" and the fault zone type is "zone", this function
 * returns "2".
 * If cannot find the fault zone type, this function leaves the fault zone id as the instance name.
 * Note the WAGED rebalancer does not require full topology tree to be created. So this logic is
 * simpler than the CRUSH based rebalancer.
 */
private String computeFaultZone(ClusterConfig clusterConfig, InstanceConfig instanceConfig) {
  if (!clusterConfig.isTopologyAwareEnabled()) {
    // Instance name is the default fault zone if topology awareness is false.
    return instanceConfig.getInstanceName();
  }
  String topologyStr = clusterConfig.getTopology();
  String faultZoneType = clusterConfig.getFaultZoneType();
  if (topologyStr == null || faultZoneType == null) {
    LOG.debug("Topology configuration is not complete. Topology define: {}, Fault Zone Type: {}",
        topologyStr, faultZoneType);
    // Use the instance name, or the deprecated ZoneId field (if exists) as the default fault
    // zone.
    String zoneId = instanceConfig.getZoneId();
    return zoneId == null ? instanceConfig.getInstanceName() : zoneId;
  } else {
    // Get the fault zone information from the complete topology definition.
    String[] topologyKeys = topologyStr.trim().split("/");
    if (topologyKeys.length == 0 || Arrays.stream(topologyKeys)
        .noneMatch(type -> type.equals(faultZoneType))) {
      throw new HelixException(
          "The configured topology definition is empty or does not contain the fault zone type.");
    }

    Map<String, String> domainAsMap = instanceConfig.getDomainAsMap();
    StringBuilder faultZoneStringBuilder = new StringBuilder();
    for (String key : topologyKeys) {
      if (!key.isEmpty()) {
        // if a key does not exist in the instance domain config, apply the default domain value.
        faultZoneStringBuilder.append(domainAsMap.getOrDefault(key, "Default_" + key));
        if (key.equals(faultZoneType)) {
          break;
        } else {
          faultZoneStringBuilder.append('/');
        }
      }
    }
    return faultZoneStringBuilder.toString();
  }
}
 
Example 5
Source File: Replicator.java    From helix with Apache License 2.0 5 votes vote down vote up
public void start() throws Exception {
  // System.out.println("Starting replication for ");
  isReplicationInitiated.set(true);

  List<InstanceConfig> instances = getInstances(resourceName, partition, "MASTER");
  if (instances.size() > 0) {
    if (instances.size() == 1) {
      InstanceConfig newMasterConfig = instances.get(0);
      String master = newMasterConfig.getInstanceName();
      if (currentMasterConfig == null
          || !master.equalsIgnoreCase(currentMasterConfig.getInstanceName())) {
        System.out.println("Found new master:" + newMasterConfig.getInstanceName());
        if (currentMasterConfig != null) {
          stop();
        }
        currentMasterConfig = newMasterConfig;
        startReplication(currentMasterConfig);
      } else {
        System.out.println("Already replicating from " + master);
      }
    } else {
      System.out.println("Invalid number of masters found:" + instances);
    }
  } else {
    System.out.println("No master found");
  }
}
 
Example 6
Source File: ZKHelixAdmin.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public void dropInstance(String clusterName, InstanceConfig instanceConfig) {
  logger.info("Drop instance {} from cluster {}.", instanceConfig.getInstanceName(), clusterName);
  String instanceName = instanceConfig.getInstanceName();

  String instanceConfigPath = PropertyPathBuilder.instanceConfig(clusterName, instanceName);
  if (!_zkClient.exists(instanceConfigPath)) {
    throw new HelixException(
        "Node " + instanceName + " does not exist in config for cluster " + clusterName);
  }

  String instancePath = PropertyPathBuilder.instance(clusterName, instanceName);
  if (!_zkClient.exists(instancePath)) {
    throw new HelixException(
        "Node " + instanceName + " does not exist in instances for cluster " + clusterName);
  }

  String liveInstancePath = PropertyPathBuilder.liveInstance(clusterName, instanceName);
  if (_zkClient.exists(liveInstancePath)) {
    throw new HelixException(
        "Node " + instanceName + " is still alive for cluster " + clusterName + ", can't drop.");
  }

  // delete config path
  String instanceConfigsPath = PropertyPathBuilder.instanceConfig(clusterName);
  ZKUtil.dropChildren(_zkClient, instanceConfigsPath, instanceConfig.getRecord());

  // delete instance path
  int retryCnt = 0;
  while (true) {
    try {
      _zkClient.deleteRecursively(instancePath);
      return;
    } catch (ZkClientException e) {
      if (retryCnt < 3 && e.getCause() instanceof ZkException && e.getCause()
          .getCause() instanceof KeeperException.NotEmptyException) {
        // Racing condition with controller's persisting node history, retryable.
        // We don't need to backoff here as this racing condition only happens once (controller
        // does not repeatedly write instance history)
        logger.warn("Retrying dropping instance {} with exception {}",
            instanceConfig.getInstanceName(), e.getCause().getMessage());
        retryCnt++;
      } else {
        String errorMessage = "Failed to drop instance: " + instanceConfig.getInstanceName()
            + ". Retry times: " + retryCnt;
        logger.error(errorMessage, e);
        throw new HelixException(errorMessage, e);
      }
    }
  }
}
 
Example 7
Source File: InstanceConfigToDataNodeConfigAdapter.java    From ambry with Apache License 2.0 4 votes vote down vote up
/**
 * Exposed for testing.
 * @param instanceConfig the {@link InstanceConfig} to convert to a {@link DataNodeConfig} object.
 * @param clusterMapConfig the {@link ClusterMapConfig} containing any default values that may be needed.
 * @return the {@link DataNodeConfig}, or {@code null} if the {@link InstanceConfig} provided has an unsupported schema
 *         version.
 */
static DataNodeConfig convert(InstanceConfig instanceConfig, ClusterMapConfig clusterMapConfig) {
  int schemaVersion = getSchemaVersion(instanceConfig);
  if (schemaVersion != 0) {
    LOGGER.warn("Unknown InstanceConfig schema version {} in {}. Ignoring.", schemaVersion, instanceConfig);
    return null;
  }
  DataNodeConfig dataNodeConfig = new DataNodeConfig(instanceConfig.getInstanceName(), instanceConfig.getHostName(),
      Integer.parseInt(instanceConfig.getPort()), getDcName(instanceConfig), getSslPortStr(instanceConfig),
      getHttp2PortStr(instanceConfig), getRackId(instanceConfig), getXid(instanceConfig));
  dataNodeConfig.getSealedReplicas().addAll(getSealedReplicas(instanceConfig));
  dataNodeConfig.getStoppedReplicas().addAll(getStoppedReplicas(instanceConfig));
  // TODO uncomment this line once 1534 is merged
  // dataNodeConfig.getDisabledReplicas().addAll(getDisabledReplicas(instanceConfig));
  instanceConfig.getRecord().getMapFields().forEach((mountPath, diskProps) -> {
    if (diskProps.get(DISK_STATE) == null) {
      // Check if this map field actually holds disk properties, since we can't tell from just the field key (the
      // mount path with no special prefix). There may be extra fields when Helix controller adds partitions in ERROR
      // state to InstanceConfig.
      LOGGER.warn("{} field does not contain disk info on {}. Skip it and continue on next one.", mountPath,
          instanceConfig.getInstanceName());
    } else {
      DataNodeConfig.DiskConfig disk = new DataNodeConfig.DiskConfig(
          diskProps.get(DISK_STATE).equals(AVAILABLE_STR) ? HardwareState.AVAILABLE : HardwareState.UNAVAILABLE,
          Long.parseLong(diskProps.get(DISK_CAPACITY_STR)));
      String replicasStr = diskProps.get(REPLICAS_STR);
      if (!replicasStr.isEmpty()) {
        for (String replicaStr : replicasStr.split(REPLICAS_DELIM_STR)) {
          String[] replicaStrParts = replicaStr.split(REPLICAS_STR_SEPARATOR);
          // partition name and replica name are the same.
          String partitionName = replicaStrParts[0];
          long replicaCapacity = Long.parseLong(replicaStrParts[1]);
          String partitionClass =
              replicaStrParts.length > 2 ? replicaStrParts[2] : clusterMapConfig.clusterMapDefaultPartitionClass;
          disk.getReplicaConfigs()
              .put(partitionName, new DataNodeConfig.ReplicaConfig(replicaCapacity, partitionClass));
        }
      }
      dataNodeConfig.getDiskConfigs().put(mountPath, disk);
    }
  });
  return dataNodeConfig;
}