Search in sources :

Example 1 with ProcessedMessageState

use of org.apache.helix.monitoring.mbeans.ParticipantMessageMonitor.ProcessedMessageState in project helix by apache.

the class HelixTaskExecutor method cancelNotStartedStateTransition.

// Try to cancel this state transition that has not been started yet.
// Three Types of Cancellation: 1. Message arrived with previous state transition
// 2. Message handled but task not started
// 3. Message handled and task already started
// This method tries to handle the first two cases, it returns true if no further cancellation is needed,
// false if not been able to cancel the state transition (i.e, further cancellation is needed).
private boolean cancelNotStartedStateTransition(Message message, Map<String, MessageHandler> stateTransitionHandlers, HelixDataAccessor accessor, String instanceName) {
    String targetMessageName = getMessageTarget(message.getResourceName(), message.getPartitionName());
    ProcessedMessageState messageState;
    Message targetStateTransitionMessage;
    // State transition message and cancel message are in same batch
    if (stateTransitionHandlers.containsKey(targetMessageName)) {
        targetStateTransitionMessage = stateTransitionHandlers.get(targetMessageName).getMessage();
        if (isCancelingSameStateTransition(targetStateTransitionMessage, message)) {
            stateTransitionHandlers.remove(targetMessageName);
            messageState = ProcessedMessageState.COMPLETED;
        } else {
            messageState = ProcessedMessageState.DISCARDED;
        }
    } else if (_messageTaskMap.containsKey(targetMessageName)) {
        // Cancel the from future without interrupt ->  Cancel the task future without
        // interruptting the state transition that is already started.  If the state transition
        // is already started, we should call cancel in the state model.
        String taskId = _messageTaskMap.get(targetMessageName);
        HelixTask task = (HelixTask) _taskMap.get(taskId).getTask();
        Future<HelixTaskResult> future = _taskMap.get(taskId).getFuture();
        targetStateTransitionMessage = task.getMessage();
        if (isCancelingSameStateTransition(task.getMessage(), message)) {
            boolean success = task.cancel();
            if (!success) {
                // the state transition is already started, need further cancellation.
                return false;
            }
            future.cancel(false);
            _messageTaskMap.remove(targetMessageName);
            _taskMap.remove(taskId);
            messageState = ProcessedMessageState.COMPLETED;
        } else {
            messageState = ProcessedMessageState.DISCARDED;
        }
    } else {
        return false;
    }
    // remove the original state-transition message been cancelled.
    removeMessageFromZK(accessor, targetStateTransitionMessage, instanceName);
    _monitor.reportProcessedMessage(targetStateTransitionMessage, ParticipantMessageMonitor.ProcessedMessageState.DISCARDED);
    // remove the state transition cancellation message
    reportAndRemoveMessage(message, accessor, instanceName, messageState);
    return true;
}
Also used : Message(org.apache.helix.model.Message) ProcessedMessageState(org.apache.helix.monitoring.mbeans.ParticipantMessageMonitor.ProcessedMessageState) Future(java.util.concurrent.Future)

Aggregations

Future (java.util.concurrent.Future)1 Message (org.apache.helix.model.Message)1 ProcessedMessageState (org.apache.helix.monitoring.mbeans.ParticipantMessageMonitor.ProcessedMessageState)1