Search in sources :

Example 1 with ErrorCode

use of org.apache.helix.messaging.handling.MessageHandler.ErrorCode in project helix by apache.

the class HelixTask method call.

@Override
public HelixTaskResult call() {
    HelixTaskResult taskResult = null;
    ErrorType type = null;
    ErrorCode code = null;
    long start = System.currentTimeMillis();
    logger.info("handling task: " + getTaskId() + " begin, at: " + start);
    HelixDataAccessor accessor = _manager.getHelixDataAccessor();
    _statusUpdateUtil.logInfo(_message, HelixTask.class, "Message handling task begin execute", _manager);
    _message.setExecuteStartTimeStamp(new Date().getTime());
    // partitionName -> csUpdate
    if (_message.getBatchMessageMode() == true) {
        _notificationContext.add(MapKey.CURRENT_STATE_UPDATE.toString(), new ConcurrentHashMap<String, CurrentStateUpdate>());
    }
    // Handle the message
    try {
        setStarted();
        taskResult = _handler.handleMessage();
    } catch (InterruptedException e) {
        taskResult = new HelixTaskResult();
        taskResult.setException(e);
        taskResult.setInterrupted(true);
        _statusUpdateUtil.logError(_message, HelixTask.class, e, "State transition interrupted, timeout:" + _isTimeout, _manager);
        logger.info("Message " + _message.getMsgId() + " is interrupted");
    } catch (Exception e) {
        taskResult = new HelixTaskResult();
        taskResult.setException(e);
        taskResult.setMessage(e.getMessage());
        String errorMessage = "Exception while executing a message. " + e + " msgId: " + _message.getMsgId() + " type: " + _message.getMsgType();
        logger.error(errorMessage, e);
        _statusUpdateUtil.logError(_message, HelixTask.class, e, errorMessage, _manager);
    }
    // cancel timeout task
    _executor.cancelTimeoutTask(this);
    Exception exception = null;
    try {
        if (taskResult.isSuccess()) {
            _statusUpdateUtil.logInfo(_message, _handler.getClass(), "Message handling task completed successfully", _manager);
            logger.info("Message " + _message.getMsgId() + " completed.");
            _executor.getParticipantMonitor().reportProcessedMessage(_message, ParticipantMessageMonitor.ProcessedMessageState.COMPLETED);
        } else {
            type = ErrorType.INTERNAL;
            if (taskResult.isInterrupted()) {
                logger.info("Message " + _message.getMsgId() + " is interrupted");
                code = _isTimeout ? ErrorCode.TIMEOUT : ErrorCode.CANCEL;
                if (_isTimeout) {
                    int retryCount = _message.getRetryCount();
                    logger.info("Message timeout, retry count: " + retryCount + " msgId:" + _message.getMsgId());
                    _statusUpdateUtil.logInfo(_message, _handler.getClass(), "Message handling task timeout, retryCount:" + retryCount, _manager);
                    // we should retry the execution of the message by re-schedule it in
                    if (retryCount > 0) {
                        _message.setRetryCount(retryCount - 1);
                        HelixTask task = new HelixTask(_message, _notificationContext, _handler, _executor);
                        _executor.scheduleTask(task);
                        return taskResult;
                    }
                }
                _executor.getParticipantMonitor().reportProcessedMessage(_message, ParticipantMessageMonitor.ProcessedMessageState.DISCARDED);
            } else if (taskResult.isCancelled()) {
                type = null;
                _statusUpdateUtil.logInfo(_message, _handler.getClass(), "Cancellation completed successfully", _manager);
                _executor.getParticipantMonitor().reportProcessedMessage(_message, ParticipantMessageMonitor.ProcessedMessageState.DISCARDED);
            } else {
                // logging for errors
                code = ErrorCode.ERROR;
                String errorMsg = "Message execution failed. msgId: " + getTaskId() + ", errorMsg: " + taskResult.getMessage();
                logger.error(errorMsg);
                _statusUpdateUtil.logError(_message, _handler.getClass(), errorMsg, _manager);
                _executor.getParticipantMonitor().reportProcessedMessage(_message, ParticipantMessageMonitor.ProcessedMessageState.FAILED);
            }
        }
        // forward relay messages attached to this message to other participants
        if (taskResult.isSuccess()) {
            try {
                forwardRelayMessages(accessor, _message, taskResult.getCompleteTime());
            } catch (Exception e) {
                // Fail to send relay message should not result in a task execution failure
                // Currently we don't log error to ZK to reduce writes as when accessor throws
                // exception, ZK might not be in good condition.
                logger.warn("Failed to send relay messages.", e);
            }
        }
        if (_message.getAttribute(Attributes.PARENT_MSG_ID) == null) {
            removeMessageFromZk(accessor, _message);
            reportMessageStat(_manager, _message, taskResult);
            sendReply(accessor, _message, taskResult);
            _executor.finishTask(this);
        }
    } catch (Exception e) {
        exception = e;
        type = ErrorType.FRAMEWORK;
        code = ErrorCode.ERROR;
        String errorMessage = "Exception after executing a message, msgId: " + _message.getMsgId() + e;
        logger.error(errorMessage, e);
        _statusUpdateUtil.logError(_message, HelixTask.class, errorMessage, _manager);
    } finally {
        long end = System.currentTimeMillis();
        logger.info("msg: " + _message.getMsgId() + " handling task completed, results:" + taskResult.isSuccess() + ", at: " + end + ", took:" + (end - start));
        // the handler have chance to finally cleanup
        if (type == ErrorType.INTERNAL) {
            _handler.onError(taskResult.getException(), code, type);
        } else if (type == ErrorType.FRAMEWORK) {
            _handler.onError(exception, code, type);
        }
    }
    return taskResult;
}
Also used : Date(java.util.Date) HelixRollbackException(org.apache.helix.HelixRollbackException) StateTransitionDataPoint(org.apache.helix.monitoring.StateTransitionDataPoint) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ErrorType(org.apache.helix.messaging.handling.MessageHandler.ErrorType) ErrorCode(org.apache.helix.messaging.handling.MessageHandler.ErrorCode)

Aggregations

Date (java.util.Date)1 HelixDataAccessor (org.apache.helix.HelixDataAccessor)1 HelixRollbackException (org.apache.helix.HelixRollbackException)1 ErrorCode (org.apache.helix.messaging.handling.MessageHandler.ErrorCode)1 ErrorType (org.apache.helix.messaging.handling.MessageHandler.ErrorType)1 StateTransitionDataPoint (org.apache.helix.monitoring.StateTransitionDataPoint)1