Search in sources :

Example 31 with Message

use of org.apache.helix.model.Message in project helix by apache.

the class HelixTaskExecutor method cancelTask.

@Override
public boolean cancelTask(MessageTask task) {
    Message message = task.getMessage();
    NotificationContext notificationContext = task.getNotificationContext();
    String taskId = task.getTaskId();
    synchronized (_lock) {
        if (_taskMap.containsKey(taskId)) {
            MessageTaskInfo taskInfo = _taskMap.get(taskId);
            // cancel timeout task
            if (taskInfo._timerTask != null) {
                taskInfo._timerTask.cancel();
            }
            // cancel task
            Future<HelixTaskResult> future = taskInfo.getFuture();
            removeMessageFromTaskAndFutureMap(message);
            _statusUpdateUtil.logInfo(message, HelixTaskExecutor.class, "Canceling task: " + taskId, notificationContext.getManager());
            // return if it is interrupted.
            if (future.cancel(true)) {
                _statusUpdateUtil.logInfo(message, HelixTaskExecutor.class, "Canceled task: " + taskId, notificationContext.getManager());
                _taskMap.remove(taskId);
                return true;
            } else {
                _statusUpdateUtil.logInfo(message, HelixTaskExecutor.class, "fail to cancel task: " + taskId, notificationContext.getManager());
            }
        } else {
            _statusUpdateUtil.logWarning(message, HelixTaskExecutor.class, "fail to cancel task: " + taskId + ", future not found", notificationContext.getManager());
        }
    }
    return false;
}
Also used : NotificationContext(org.apache.helix.NotificationContext) Message(org.apache.helix.model.Message)

Example 32 with Message

use of org.apache.helix.model.Message in project helix by apache.

the class HelixTaskExecutor method syncSessionToController.

private void syncSessionToController(HelixManager manager) {
    if (_lastSessionSyncTime == null || System.currentTimeMillis() - _lastSessionSyncTime > SESSION_SYNC_INTERVAL) {
        // > delay since last sync
        HelixDataAccessor accessor = manager.getHelixDataAccessor();
        PropertyKey key = new Builder(manager.getClusterName()).controllerMessage(SESSION_SYNC);
        if (accessor.getProperty(key) == null) {
            LOG.info(String.format("Participant %s syncs session with controller", manager.getInstanceName()));
            Message msg = new Message(MessageType.PARTICIPANT_SESSION_CHANGE, SESSION_SYNC);
            msg.setSrcName(manager.getInstanceName());
            msg.setTgtSessionId("*");
            msg.setMsgState(MessageState.NEW);
            msg.setMsgId(SESSION_SYNC);
            Criteria cr = new Criteria();
            cr.setRecipientInstanceType(InstanceType.CONTROLLER);
            cr.setSessionSpecific(false);
            manager.getMessagingService().send(cr, msg);
            _lastSessionSyncTime = System.currentTimeMillis();
        }
    }
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) Message(org.apache.helix.model.Message) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) Builder(org.apache.helix.PropertyKey.Builder) Criteria(org.apache.helix.Criteria) PropertyKey(org.apache.helix.PropertyKey)

Example 33 with Message

use of org.apache.helix.model.Message 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)

Example 34 with Message

use of org.apache.helix.model.Message in project helix by apache.

the class HelixTaskExecutor method updateMessageState.

private void updateMessageState(List<Message> readMsgs, HelixDataAccessor accessor, String instanceName) {
    Builder keyBuilder = accessor.keyBuilder();
    List<PropertyKey> readMsgKeys = new ArrayList<>();
    for (Message msg : readMsgs) {
        readMsgKeys.add(msg.getKey(keyBuilder, instanceName));
        _knownMessageIds.add(msg.getId());
    }
    accessor.setChildren(readMsgKeys, readMsgs);
}
Also used : Message(org.apache.helix.model.Message) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) PropertyKey(org.apache.helix.PropertyKey)

Example 35 with Message

use of org.apache.helix.model.Message in project helix by apache.

the class HelixTaskExecutor method finishTask.

@Override
public void finishTask(MessageTask task) {
    Message message = task.getMessage();
    String taskId = task.getTaskId();
    LOG.info("message finished: " + taskId + ", took " + (new Date().getTime() - message.getExecuteStartTimeStamp()));
    synchronized (_lock) {
        if (_taskMap.containsKey(taskId)) {
            MessageTaskInfo info = _taskMap.remove(taskId);
            removeMessageFromTaskAndFutureMap(message);
            if (info._timerTask != null) {
                // ok to cancel multiple times
                info._timerTask.cancel();
            }
        } else {
            LOG.warn("message " + taskId + " not found in task map");
        }
    }
}
Also used : Message(org.apache.helix.model.Message) Date(java.util.Date)

Aggregations

Message (org.apache.helix.model.Message)116 Test (org.testng.annotations.Test)53 ArrayList (java.util.ArrayList)36 HelixDataAccessor (org.apache.helix.HelixDataAccessor)30 Builder (org.apache.helix.PropertyKey.Builder)28 HelixManager (org.apache.helix.HelixManager)22 ZNRecord (org.apache.helix.ZNRecord)22 Criteria (org.apache.helix.Criteria)21 Date (java.util.Date)19 HashMap (java.util.HashMap)18 Partition (org.apache.helix.model.Partition)18 PropertyKey (org.apache.helix.PropertyKey)17 LiveInstance (org.apache.helix.model.LiveInstance)13 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)12 NotificationContext (org.apache.helix.NotificationContext)11 CurrentState (org.apache.helix.model.CurrentState)10 HelixException (org.apache.helix.HelixException)9 Resource (org.apache.helix.model.Resource)9 StringWriter (java.io.StringWriter)8 List (java.util.List)8