Search in sources :

Example 1 with CurrentState

use of org.apache.helix.model.CurrentState in project pinot by linkedin.

the class PinotHelixResourceManager method dropInstance.

/**
   * Drop the instance from helix cluster. Instance will not be dropped if:
   * - It is a live instance.
   * - Has at least one ONLINE segment.
   *
   * @param instanceName: Name of the instance to be dropped.
   * @return
   */
public PinotResourceManagerResponse dropInstance(String instanceName) {
    if (!instanceExists(instanceName)) {
        return new PinotResourceManagerResponse("Instance " + instanceName + " does not exist.", false);
    }
    HelixDataAccessor helixDataAccessor = _helixZkManager.getHelixDataAccessor();
    LiveInstance liveInstance = helixDataAccessor.getProperty(_keyBuilder.liveInstance(instanceName));
    if (liveInstance != null) {
        PropertyKey currentStatesKey = _keyBuilder.currentStates(instanceName, liveInstance.getSessionId());
        List<CurrentState> currentStates = _helixDataAccessor.getChildValues(currentStatesKey);
        if (currentStates != null) {
            for (CurrentState currentState : currentStates) {
                for (String state : currentState.getPartitionStateMap().values()) {
                    if (state.equalsIgnoreCase(SegmentOnlineOfflineStateModel.ONLINE)) {
                        return new PinotResourceManagerResponse(("Instance " + instanceName + " has online partitions"), false);
                    }
                }
            }
        } else {
            return new PinotResourceManagerResponse("Cannot drop live instance " + instanceName + " please stop the instance first.", false);
        }
    }
    // Disable the instance first.
    toogleInstance(instanceName, false, 10);
    _helixAdmin.dropInstance(_helixClusterName, getHelixInstanceConfig(instanceName));
    return new PinotResourceManagerResponse("Instance " + instanceName + " dropped.", true);
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) CurrentState(org.apache.helix.model.CurrentState) PropertyKey(org.apache.helix.PropertyKey)

Example 2 with CurrentState

use of org.apache.helix.model.CurrentState in project pinot by linkedin.

the class PinotHelixResourceManager method toogleInstance.

/**
   * Toggle the status of an Instance between OFFLINE and ONLINE.
   * Keeps checking until ideal-state is successfully updated or times out.
   *
   * @param instanceName: Name of Instance for which the status needs to be toggled.
   * @param toggle: 'True' for ONLINE 'False' for OFFLINE.
   * @param timeOutInSeconds: Time-out for setting ideal-state.
   * @return
   */
public PinotResourceManagerResponse toogleInstance(String instanceName, boolean toggle, int timeOutInSeconds) {
    if (!instanceExists(instanceName)) {
        return new PinotResourceManagerResponse("Instance " + instanceName + " does not exist.", false);
    }
    _helixAdmin.enableInstance(_helixClusterName, instanceName, toggle);
    long deadline = System.currentTimeMillis() + 1000 * timeOutInSeconds;
    boolean toggleSucceed = false;
    String beforeToggleStates = (toggle) ? SegmentOnlineOfflineStateModel.OFFLINE : SegmentOnlineOfflineStateModel.ONLINE;
    while (System.currentTimeMillis() < deadline) {
        toggleSucceed = true;
        PropertyKey liveInstanceKey = _keyBuilder.liveInstance(instanceName);
        LiveInstance liveInstance = _helixDataAccessor.getProperty(liveInstanceKey);
        if (liveInstance == null) {
            if (toggle) {
                return PinotResourceManagerResponse.FAILURE_RESPONSE;
            } else {
                return PinotResourceManagerResponse.SUCCESS_RESPONSE;
            }
        }
        PropertyKey instanceCurrentStatesKey = _keyBuilder.currentStates(instanceName, liveInstance.getSessionId());
        List<CurrentState> instanceCurrentStates = _helixDataAccessor.getChildValues(instanceCurrentStatesKey);
        if (instanceCurrentStates == null) {
            return PinotResourceManagerResponse.SUCCESS_RESPONSE;
        } else {
            for (CurrentState currentState : instanceCurrentStates) {
                for (String state : currentState.getPartitionStateMap().values()) {
                    if (beforeToggleStates.equals(state)) {
                        toggleSucceed = false;
                    }
                }
            }
        }
        if (toggleSucceed) {
            return (toggle) ? new PinotResourceManagerResponse("Instance " + instanceName + " enabled.", true) : new PinotResourceManagerResponse("Instance " + instanceName + " disabled.", true);
        } else {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
        }
    }
    return new PinotResourceManagerResponse("Instance enable/disable failed, timeout.", false);
}
Also used : LiveInstance(org.apache.helix.model.LiveInstance) CurrentState(org.apache.helix.model.CurrentState) PropertyKey(org.apache.helix.PropertyKey)

Aggregations

PropertyKey (org.apache.helix.PropertyKey)2 CurrentState (org.apache.helix.model.CurrentState)2 LiveInstance (org.apache.helix.model.LiveInstance)2 HelixDataAccessor (org.apache.helix.HelixDataAccessor)1