Search in sources :

Example 41 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class InstanceValidationUtil method hasErrorPartitions.

/**
 * Method to check if the instance has error partitions
 * @param dataAccessor
 * @param clusterId
 * @param instanceName
 * @return
 */
public static boolean hasErrorPartitions(HelixDataAccessor dataAccessor, String clusterId, String instanceName) {
    PropertyKey.Builder propertyKeyBuilder = new PropertyKey.Builder(clusterId);
    PropertyKey liveInstanceKey = propertyKeyBuilder.liveInstance(instanceName);
    LiveInstance liveInstance = dataAccessor.getProperty(liveInstanceKey);
    if (liveInstance != null) {
        String sessionId = liveInstance.getEphemeralOwner();
        PropertyKey currentStatesKey = propertyKeyBuilder.currentStates(instanceName, sessionId);
        List<String> resourceNames = dataAccessor.getChildNames(currentStatesKey);
        for (String resourceName : resourceNames) {
            PropertyKey key = propertyKeyBuilder.currentState(instanceName, sessionId, resourceName);
            CurrentState currentState = dataAccessor.getProperty(key);
            if (currentState != null && currentState.getPartitionStateMap().containsValue(HelixDefinedState.ERROR.name())) {
                _logger.warn("The instance {} has error partitions on it.", instanceName);
                return true;
            }
        }
    }
    return false;
}
Also used : LiveInstance(org.apache.helix.model.LiveInstance) CurrentState(org.apache.helix.model.CurrentState) PropertyKey(org.apache.helix.PropertyKey)

Example 42 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class InstanceValidationUtil method isResourceAssigned.

/**
 * Method to check if the instance is assigned at least 1 resource, not in a idle state;
 * Independent of the instance alive/enabled status
 * @param dataAccessor
 * @param instanceName
 * @return
 */
public static boolean isResourceAssigned(HelixDataAccessor dataAccessor, String instanceName) {
    PropertyKey.Builder propertyKeyBuilder = dataAccessor.keyBuilder();
    LiveInstance liveInstance = dataAccessor.getProperty(propertyKeyBuilder.liveInstance(instanceName));
    if (liveInstance != null) {
        String sessionId = liveInstance.getEphemeralOwner();
        List<String> resourceNames = dataAccessor.getChildNames(propertyKeyBuilder.currentStates(instanceName, sessionId));
        for (String resourceName : resourceNames) {
            PropertyKey currentStateKey = propertyKeyBuilder.currentState(instanceName, sessionId, resourceName);
            CurrentState currentState = dataAccessor.getProperty(currentStateKey);
            if (currentState != null && currentState.getPartitionStateMap().size() > 0) {
                return true;
            }
        }
    }
    _logger.warn(String.format("The instance %s does not have resource assigned on it.", instanceName));
    return false;
}
Also used : LiveInstance(org.apache.helix.model.LiveInstance) CurrentState(org.apache.helix.model.CurrentState) PropertyKey(org.apache.helix.PropertyKey)

Example 43 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class InstanceValidationUtil method hasValidConfig.

/**
 * Method to check if the instance has valid configuration.
 * Instance stability check requires PERSIST_INTERMEDIATE_ASSIGNMENT turned on!
 *
 * @param dataAccessor
 * @param clusterId
 * @param instanceName
 * @return
 */
public static boolean hasValidConfig(HelixDataAccessor dataAccessor, String clusterId, String instanceName) {
    PropertyKey.Builder keyBuilder = dataAccessor.keyBuilder();
    ClusterConfig clusterConfig = dataAccessor.getProperty(keyBuilder.clusterConfig());
    if (clusterConfig == null) {
        _logger.error("Cluster config is missing in cluster " + clusterId);
        return false;
    }
    if (!clusterConfig.isPersistIntermediateAssignment()) {
        _logger.error("Cluster config {} is not turned on, which is required for instance stability check.", ClusterConfig.ClusterConfigProperty.PERSIST_INTERMEDIATE_ASSIGNMENT.toString());
        return false;
    }
    PropertyKey propertyKey = keyBuilder.instanceConfig(instanceName);
    InstanceConfig instanceConfig = dataAccessor.getProperty(propertyKey);
    return instanceConfig != null && instanceConfig.isValid();
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig) PropertyKey(org.apache.helix.PropertyKey) ClusterConfig(org.apache.helix.model.ClusterConfig)

Example 44 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class TaskRunner method setZKCurrentState.

/**
 * Request a state change for a specific task.
 *
 * @param accessor  connected Helix data accessor
 * @param instance  the instance serving the task
 * @param sessionId the current session of the instance
 * @param resource  the job name
 * @param partition the task partition name
 * @param state     the requested state
 * @return true if the request was persisted, false otherwise
 */
private boolean setZKCurrentState(HelixDataAccessor accessor, String instance, String sessionId, String resource, String partition, TaskPartitionState state) {
    LOG.debug(String.format("Updating current state %s for partition %s.", state, partition));
    try {
        PropertyKey.Builder keyBuilder = accessor.keyBuilder();
        PropertyKey key = Boolean.getBoolean(SystemPropertyKeys.TASK_CURRENT_STATE_PATH_DISABLED) ? keyBuilder.currentState(instance, sessionId, resource) : keyBuilder.taskCurrentState(instance, sessionId, resource);
        String prevState = _stateModel.getCurrentState();
        CurrentState currentStateDelta = new CurrentState(resource);
        currentStateDelta.setSessionId(sessionId);
        currentStateDelta.setStateModelDefRef(TaskConstants.STATE_MODEL_NAME);
        currentStateDelta.setState(partition, state.name());
        currentStateDelta.setInfo(partition, _result.getInfo());
        currentStateDelta.setPreviousState(partition, prevState);
        return accessor.updateProperty(key, currentStateDelta);
    } catch (Exception e) {
        LOG.error(String.format("Error when updating current state  to %s for partition %s.", state, partition), e);
        return false;
    }
}
Also used : CurrentState(org.apache.helix.model.CurrentState) PropertyKey(org.apache.helix.PropertyKey) HelixException(org.apache.helix.HelixException)

Example 45 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class TaskUtil method removeWorkflowJobConfig.

/**
 * Remove workflow or job config.
 * @param accessor
 * @param workflowJobResource the workflow or job name
 */
private static boolean removeWorkflowJobConfig(HelixDataAccessor accessor, String workflowJobResource) {
    PropertyKey cfgKey = accessor.keyBuilder().resourceConfig(workflowJobResource);
    if (accessor.getPropertyStat(cfgKey) != null) {
        if (!accessor.removeProperty(cfgKey)) {
            LOG.warn("Error occurred while trying to remove config for {}. Failed to remove node {}.", workflowJobResource, cfgKey);
            return false;
        }
    }
    LOG.info("removed job config {}.", cfgKey.getPath());
    return true;
}
Also used : PropertyKey(org.apache.helix.PropertyKey)

Aggregations

PropertyKey (org.apache.helix.PropertyKey)134 HelixDataAccessor (org.apache.helix.HelixDataAccessor)60 ArrayList (java.util.ArrayList)33 Test (org.testng.annotations.Test)33 CurrentState (org.apache.helix.model.CurrentState)31 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)28 Message (org.apache.helix.model.Message)27 HelixManager (org.apache.helix.HelixManager)25 LiveInstance (org.apache.helix.model.LiveInstance)23 Map (java.util.Map)20 HelixException (org.apache.helix.HelixException)20 Builder (org.apache.helix.PropertyKey.Builder)20 HashMap (java.util.HashMap)18 HashSet (java.util.HashSet)18 IdealState (org.apache.helix.model.IdealState)16 InstanceConfig (org.apache.helix.model.InstanceConfig)15 ExternalView (org.apache.helix.model.ExternalView)12 List (java.util.List)11 HelixProperty (org.apache.helix.HelixProperty)11 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)10