Java Code Examples for org.apache.helix.model.LiveInstance#getEphemeralOwner()

The following examples show how to use org.apache.helix.model.LiveInstance#getEphemeralOwner() . 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: CurrentStateCache.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
protected Set<PropertyKey> PopulateParticipantKeys(HelixDataAccessor accessor,
    Map<String, LiveInstance> liveInstanceMap) {
  Set<PropertyKey> participantStateKeys = new HashSet<>();
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();
  for (String instanceName : liveInstanceMap.keySet()) {
    LiveInstance liveInstance = liveInstanceMap.get(instanceName);
    String sessionId = liveInstance.getEphemeralOwner();
    List<String> currentStateNames =
        accessor.getChildNames(keyBuilder.currentStates(instanceName, sessionId));
    for (String currentStateName : currentStateNames) {
      participantStateKeys
          .add(keyBuilder.currentState(instanceName, sessionId, currentStateName));
    }
  }
  return participantStateKeys;
}
 
Example 2
Source File: SchedulerTasksResource.java    From helix with Apache License 2.0 6 votes vote down vote up
StringRepresentation getSchedulerTasksRepresentation() throws JsonGenerationException,
    JsonMappingException, IOException {
  String clusterName = (String) getRequest().getAttributes().get("clusterName");
  String instanceName = (String) getRequest().getAttributes().get("instanceName");
  ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
  ClusterSetup setupTool = new ClusterSetup(zkClient);
  List<String> instances =
      setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);

  HelixDataAccessor accessor =
      ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
  LiveInstance liveInstance =
      accessor.getProperty(accessor.keyBuilder().liveInstance(instanceName));
  String sessionId = liveInstance.getEphemeralOwner();

  StringRepresentation representation = new StringRepresentation("");// (ClusterRepresentationUtil.ObjectToJson(instanceConfigs),
                                                                     // MediaType.APPLICATION_JSON);

  return representation;
}
 
Example 3
Source File: RoutingTable.java    From helix with Apache License 2.0 5 votes vote down vote up
private void refresh(Map<String, Map<String, Map<String, CurrentState>>> currentStateMap) {
  Map<String, InstanceConfig> instanceConfigMap = new HashMap<>();
  if (currentStateMap != null && !currentStateMap.isEmpty()) {
    for (InstanceConfig config : _instanceConfigs) {
      instanceConfigMap.put(config.getId(), config);
    }
    for (LiveInstance liveInstance : _liveInstances) {
      String instanceName = liveInstance.getInstanceName();
      String sessionId = liveInstance.getEphemeralOwner();
      InstanceConfig instanceConfig = instanceConfigMap.get(instanceName);
      if (instanceConfig == null) {
        logger.warn(
            "Participant {} is not found with proper configuration information. It might already be removed from the cluster. "
                + "Skip recording partition assignments that are related to this instance.",
            instanceName);
        continue;
      }

      Map<String, CurrentState> currentStates = Collections.emptyMap();
      if (currentStateMap.containsKey(instanceName)
          && currentStateMap.get(instanceName).containsKey(sessionId)) {
        currentStates = currentStateMap.get(instanceName).get(sessionId);
      }

      for (CurrentState currentState : currentStates.values()) {
        String resourceName = currentState.getResourceName();
        Map<String, String> stateMap = currentState.getPartitionStateMap();

        for (String partitionName : stateMap.keySet()) {
          String state = stateMap.get(partitionName);
          addEntry(resourceName, partitionName, state, instanceConfig);
        }
      }
    }
  }
}
 
Example 4
Source File: ZKHelixManager.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isLeader() {
  String warnLogPrefix = String
      .format("Instance %s is not leader of cluster %s due to", _instanceName, _clusterName);
  if (_instanceType != InstanceType.CONTROLLER
      && _instanceType != InstanceType.CONTROLLER_PARTICIPANT) {
    LOG.warn(String
        .format("%s instance type %s does not match to CONTROLLER/CONTROLLER_PARTICIPANT",
            warnLogPrefix, _instanceType.name()));
    return false;
  }

  if (!isConnected()) {
    LOG.warn(String.format("%s HelixManager is not connected", warnLogPrefix));
    return false;
  }

  try {
    LiveInstance leader = _dataAccessor.getProperty(_keyBuilder.controllerLeader());
    if (leader != null) {
      String leaderName = leader.getInstanceName();
      String sessionId = leader.getEphemeralOwner();
      if (leaderName != null && leaderName.equals(_instanceName) && sessionId
          .equals(_sessionId)) {
        return true;
      }
      LOG.warn(String
          .format("%s current session %s does not match leader session %s", warnLogPrefix,
              _sessionId, sessionId));
    } else {
      LOG.warn(String.format("%s leader ZNode is null", warnLogPrefix));
    }
  } catch (Exception e) {
    LOG.warn(String.format("%s exception happen when session check", warnLogPrefix), e);
  }
  return false;
}
 
Example 5
Source File: DistributedLeaderElection.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * @return true if the current manager created a new controller node successfully.
 */
private boolean tryCreateController(HelixManager manager) {
  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  Builder keyBuilder = accessor.keyBuilder();

  LiveInstance newLeader = new LiveInstance(manager.getInstanceName());
  newLeader.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName());
  newLeader.setSessionId(manager.getSessionId());
  newLeader.setHelixVersion(manager.getVersion());
  try {
    if (accessor.createControllerLeader(newLeader)) {
      updateHistory(manager);
      return true;
    } else {
      LOG.info(
          "Unable to become leader probably because some other controller became the leader.");
    }
  } catch (Exception e) {
    LOG.error(
        "Exception when trying to updating leader record in cluster:" + manager.getClusterName()
            + ". Need to check again whether leader node has been created or not.", e);
  }

  LiveInstance currentLeader = accessor.getProperty(keyBuilder.controllerLeader());
  if (currentLeader != null) {
    String currentSession = currentLeader.getEphemeralOwner();
    LOG.info("Leader exists for cluster: " + manager.getClusterName() + ", currentLeader: "
        + currentLeader.getInstanceName() + ", leaderSessionId: " + currentSession);
    if (currentSession != null && currentSession.equals(newLeader.getEphemeralOwner())) {
      return true;
    } else {
      LOG.warn("The existing leader has a different session. Expected session Id: " + newLeader
          .getEphemeralOwner());
    }
  }
  return false;
}
 
Example 6
Source File: CurrentStateComputationStage.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public void process(ClusterEvent event) throws Exception {
  _eventId = event.getEventId();
  BaseControllerDataProvider cache = event.getAttribute(AttributeName.ControllerDataProvider.name());
  final Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
  final Map<String, Resource> resourceToRebalance =
      event.getAttribute(AttributeName.RESOURCES_TO_REBALANCE.name());

  if (cache == null || resourceMap == null) {
    throw new StageException("Missing attributes in event:" + event
        + ". Requires DataCache|RESOURCE");
  }

  Map<String, LiveInstance> liveInstances = cache.getLiveInstances();
  final CurrentStateOutput currentStateOutput = new CurrentStateOutput();

  for (LiveInstance instance : liveInstances.values()) {
    String instanceName = instance.getInstanceName();
    String instanceSessionId = instance.getEphemeralOwner();

    // update pending messages
    Map<String, Message> messages = cache.getMessages(instanceName);
    Map<String, Message> relayMessages = cache.getRelayMessages(instanceName);
    updatePendingMessages(instance, messages.values(), currentStateOutput, relayMessages.values(), resourceMap);

    // update current states.
    Map<String, CurrentState> currentStateMap = cache.getCurrentState(instanceName,
        instanceSessionId);
    updateCurrentStates(instance, currentStateMap.values(), currentStateOutput, resourceMap);
  }
  event.addAttribute(AttributeName.CURRENT_STATE.name(), currentStateOutput);

  final ClusterStatusMonitor clusterStatusMonitor =
      event.getAttribute(AttributeName.clusterStatusMonitor.name());
  if (clusterStatusMonitor != null && cache instanceof ResourceControllerDataProvider) {
    final ResourceControllerDataProvider dataProvider = (ResourceControllerDataProvider) cache;
    reportInstanceCapacityMetrics(clusterStatusMonitor, dataProvider, resourceToRebalance,
        currentStateOutput);
    reportResourcePartitionCapacityMetrics(dataProvider.getAsyncTasksThreadPool(),
        clusterStatusMonitor, dataProvider.getResourceConfigMap().values());
  }
}
 
Example 7
Source File: CurrentStateComputationStage.java    From helix with Apache License 2.0 4 votes vote down vote up
private void updateCurrentStates(LiveInstance instance, Collection<CurrentState> currentStates,
    CurrentStateOutput currentStateOutput, Map<String, Resource> resourceMap) {
  String instanceName = instance.getInstanceName();
  String instanceSessionId = instance.getEphemeralOwner();

  for (CurrentState currentState : currentStates) {
    if (!instanceSessionId.equals(currentState.getSessionId())) {
      continue;
    }
    String resourceName = currentState.getResourceName();
    String stateModelDefName = currentState.getStateModelDefRef();
    Resource resource = resourceMap.get(resourceName);
    if (resource == null) {
      continue;
    }
    if (stateModelDefName != null) {
      currentStateOutput.setResourceStateModelDef(resourceName, stateModelDefName);
    }

    currentStateOutput.setBucketSize(resourceName, currentState.getBucketSize());

    Map<String, String> partitionStateMap = currentState.getPartitionStateMap();
    for (String partitionName : partitionStateMap.keySet()) {
      Partition partition = resource.getPartition(partitionName);
      if (partition != null) {
        currentStateOutput.setCurrentState(resourceName, partition, instanceName,
            currentState.getState(partitionName));
        currentStateOutput.setEndTime(resourceName, partition, instanceName,
            currentState.getEndTime(partitionName));
        String info = currentState.getInfo(partitionName);
        // This is to avoid null value entries in the map, and reduce memory usage by avoiding extra empty entries in the map.
        if (info != null) {
          currentStateOutput.setInfo(resourceName, partition, instanceName, info);
        }
        String requestState = currentState.getRequestedState(partitionName);
        if (requestState != null) {
          currentStateOutput
              .setRequestedState(resourceName, partition, instanceName, requestState);
        }
      }
    }
  }
}
 
Example 8
Source File: InstanceServiceImpl.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public InstanceInfo getInstanceInfo(String clusterId, String instanceName,
    List<HealthCheck> healthChecks) {
  InstanceInfo.Builder instanceInfoBuilder = new InstanceInfo.Builder(instanceName);

  InstanceConfig instanceConfig =
      _dataAccessor.getProperty(_dataAccessor.keyBuilder().instanceConfig(instanceName));
  LiveInstance liveInstance =
      _dataAccessor.getProperty(_dataAccessor.keyBuilder().liveInstance(instanceName));
  if (instanceConfig != null) {
    instanceInfoBuilder.instanceConfig(instanceConfig.getRecord());
  } else {
    LOG.warn("Missing instance config for {}", instanceName);
  }
  if (liveInstance != null) {
    instanceInfoBuilder.liveInstance(liveInstance.getRecord());
    String sessionId = liveInstance.getEphemeralOwner();

    List<String> resourceNames = _dataAccessor
        .getChildNames(_dataAccessor.keyBuilder().currentStates(instanceName, sessionId));
    instanceInfoBuilder.resources(resourceNames);
    List<String> partitions = new ArrayList<>();
    for (String resourceName : resourceNames) {
      CurrentState currentState = _dataAccessor.getProperty(
          _dataAccessor.keyBuilder().currentState(instanceName, sessionId, resourceName));
      if (currentState != null && currentState.getPartitionStateMap() != null) {
        partitions.addAll(currentState.getPartitionStateMap().keySet());
      } else {
        LOG.warn(
            "Current state is either null or partitionStateMap is missing. InstanceName: {}, SessionId: {}, ResourceName: {}",
            instanceName, sessionId, resourceName);
      }
    }
    instanceInfoBuilder.partitions(partitions);
  } else {
    LOG.warn("Missing live instance for {}", instanceName);
  }
  try {
    Map<String, Boolean> healthStatus =
        getInstanceHealthStatus(clusterId, instanceName, healthChecks);
    instanceInfoBuilder.healthStatus(healthStatus);
  } catch (HelixException ex) {
    LOG.error(
        "Exception while getting health status. Cluster: {}, Instance: {}, reporting health status as unHealth",
        clusterId, instanceName, ex);
    instanceInfoBuilder.healthStatus(false);
  }

  return instanceInfoBuilder.build();
}