Search in sources :

Example 1 with HelixRollbackException

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

the class HelixStateTransitionHandler method handleMessage.

@Override
public HelixTaskResult handleMessage() {
    NotificationContext context = _notificationContext;
    Message message = _message;
    synchronized (_stateModel) {
        HelixTaskResult taskResult = new HelixTaskResult();
        HelixManager manager = context.getManager();
        _statusUpdateUtil.logInfo(message, HelixStateTransitionHandler.class, "Message handling task begin execute", manager);
        message.setExecuteStartTimeStamp(new Date().getTime());
        try {
            preHandleMessage();
            invoke(manager, context, taskResult, message);
        } catch (HelixStateMismatchException e) {
            // Simply log error and return from here if State mismatch.
            // The current state of the state model is intact.
            taskResult.setSuccess(false);
            taskResult.setMessage(e.toString());
            taskResult.setException(e);
        } catch (Exception e) {
            String errorMessage = "Exception while executing a state transition task " + message.getPartitionName();
            logger.error(errorMessage, e);
            if (e.getCause() != null && e.getCause() instanceof InterruptedException) {
                e = (InterruptedException) e.getCause();
            }
            if (e instanceof HelixRollbackException || (e.getCause() != null && e.getCause() instanceof HelixRollbackException)) {
                // TODO : Support cancel to any state
                logger.info("Rollback happened of state transition on resource \"" + _message.getResourceName() + "\" partition \"" + _message.getPartitionName() + "\" from \"" + _message.getFromState() + "\" to \"" + _message.getToState() + "\"");
                taskResult.setCancelled(true);
            } else {
                _statusUpdateUtil.logError(message, HelixStateTransitionHandler.class, e, errorMessage, manager);
                taskResult.setSuccess(false);
                taskResult.setMessage(e.toString());
                taskResult.setException(e);
                taskResult.setInterrupted(e instanceof InterruptedException);
            }
        }
        taskResult.setCompleteTime(System.currentTimeMillis());
        // add task result to context for postHandling
        context.add(MapKey.HELIX_TASK_RESULT.toString(), taskResult);
        postHandleMessage();
        return taskResult;
    }
}
Also used : NotificationContext(org.apache.helix.NotificationContext) HelixManager(org.apache.helix.HelixManager) Message(org.apache.helix.model.Message) HelixRollbackException(org.apache.helix.HelixRollbackException) Date(java.util.Date) HelixException(org.apache.helix.HelixException) HelixRollbackException(org.apache.helix.HelixRollbackException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with HelixRollbackException

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

the class HelixStateTransitionHandler method invoke.

private void invoke(HelixManager manager, NotificationContext context, HelixTaskResult taskResult, Message message) throws IllegalAccessException, InvocationTargetException, InterruptedException, HelixRollbackException {
    _statusUpdateUtil.logInfo(message, HelixStateTransitionHandler.class, "Message handling invoking", manager);
    // by default, we invoke state transition function in state model
    Method methodToInvoke = null;
    String fromState = message.getFromState();
    String toState = message.getToState();
    methodToInvoke = _transitionMethodFinder.getMethodForTransition(_stateModel.getClass(), fromState, toState, new Class[] { Message.class, NotificationContext.class });
    if (methodToInvoke != null) {
        logger.info(String.format("Instance %s, partition %s received state transition from %s to %s on session %s, message id: %s", message.getTgtName(), message.getPartitionName(), message.getFromState(), message.getToState(), message.getTgtSessionId(), message.getMsgId()));
        if (_cancelled) {
            throw new HelixRollbackException(String.format("Instance %s, partition %s state transition from %s to %s on session %s has been cancelled, message id: %s", message.getTgtName(), message.getPartitionName(), message.getFromState(), message.getToState(), message.getTgtSessionId(), message.getMsgId()));
        }
        if (_cancelled) {
            throw new HelixRollbackException(String.format("Instance %s, partition %s state transition from %s to %s on session %s has been cancelled", message.getTgtName(), message.getPartitionName(), message.getFromState(), message.getToState(), message.getTgtSessionId()));
        }
        Object result = methodToInvoke.invoke(_stateModel, new Object[] { message, context });
        taskResult.setSuccess(true);
        String resultStr;
        if (result == null || result instanceof Void) {
            resultStr = "";
        } else {
            resultStr = result.toString();
        }
        taskResult.setInfo(resultStr);
    } else {
        String errorMessage = "Unable to find method for transition from " + fromState + " to " + toState + " in " + _stateModel.getClass();
        logger.error(errorMessage);
        taskResult.setSuccess(false);
        _statusUpdateUtil.logError(message, HelixStateTransitionHandler.class, errorMessage, manager);
    }
}
Also used : NotificationContext(org.apache.helix.NotificationContext) Message(org.apache.helix.model.Message) HelixRollbackException(org.apache.helix.HelixRollbackException) Method(java.lang.reflect.Method)

Aggregations

HelixRollbackException (org.apache.helix.HelixRollbackException)2 NotificationContext (org.apache.helix.NotificationContext)2 Message (org.apache.helix.model.Message)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Date (java.util.Date)1 HelixException (org.apache.helix.HelixException)1 HelixManager (org.apache.helix.HelixManager)1