Search in sources :

Example 1 with ZNRecordDelta

use of org.apache.helix.zookeeper.datamodel.ZNRecordDelta in project helix by apache.

the class CustomizedStateProvider method deletePerPartitionCustomizedState.

/**
 * Delete the customized state for a specified resource and a specified partition
 */
public void deletePerPartitionCustomizedState(String customizedStateName, String resourceName, String partitionName) {
    HelixDataAccessor accessor = _helixManager.getHelixDataAccessor();
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    PropertyKey propertyKey = keyBuilder.customizedState(_instanceName, customizedStateName, resourceName);
    CustomizedState existingState = getCustomizedState(customizedStateName, resourceName);
    ZNRecord rec = new ZNRecord(existingState.getId());
    rec.getMapFields().put(partitionName, null);
    ZNRecordDelta delta = new ZNRecordDelta(rec, ZNRecordDelta.MergeOperation.SUBTRACT);
    List<ZNRecordDelta> deltaList = new ArrayList<ZNRecordDelta>();
    deltaList.add(delta);
    existingState.setDeltaList(deltaList);
    if (!accessor.updateProperty(propertyKey, existingState)) {
        throw new HelixException(String.format("Failed to delete customized state %s to zk for instance %s, resource %s, " + "partition %s", customizedStateName, _instanceName, resourceName, partitionName));
    }
}
Also used : HelixException(org.apache.helix.HelixException) HelixDataAccessor(org.apache.helix.HelixDataAccessor) CustomizedState(org.apache.helix.model.CustomizedState) ArrayList(java.util.ArrayList) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) ZNRecordDelta(org.apache.helix.zookeeper.datamodel.ZNRecordDelta)

Example 2 with ZNRecordDelta

use of org.apache.helix.zookeeper.datamodel.ZNRecordDelta in project helix by apache.

the class HelixStateTransitionHandler method postHandleMessage.

void postHandleMessage() {
    HelixTaskResult taskResult = (HelixTaskResult) _notificationContext.get(MapKey.HELIX_TASK_RESULT.toString());
    Exception exception = taskResult.getException();
    String partitionKey = _message.getPartitionName();
    // for zk current state it is OK as we have the per-session current state node
    if (!_message.getTgtSessionId().equals(_manager.getSessionId())) {
        logger.warn("Session id has changed. Skip postExecutionMessage. Old session " + _message.getExecutionSessionId() + " , new session : " + _manager.getSessionId());
        return;
    }
    // Set the INFO property and mark the end time, previous state of the state transition
    _currentStateDelta.setInfo(partitionKey, taskResult.getInfo());
    _currentStateDelta.setEndTime(partitionKey, taskResult.getCompleteTime());
    _currentStateDelta.setPreviousState(partitionKey, _message.getFromState());
    // add host name this state transition is triggered by.
    _currentStateDelta.setTriggerHost(partitionKey, Message.MessageType.RELAYED_MESSAGE.name().equals(_message.getMsgSubType()) ? _message.getRelaySrcHost() : _message.getMsgSrc());
    if (taskResult.isSuccess()) {
        // String fromState = message.getFromState();
        String toState = _message.getToState();
        _currentStateDelta.setState(partitionKey, toState);
        if (toState.equalsIgnoreCase(HelixDefinedState.DROPPED.toString())) {
            // for "OnOfflineToDROPPED" message, we need to remove the resource key record
            // from the current state of the instance because the resource key is dropped.
            // In the state model it will be stayed as "OFFLINE", which is OK.
            ZNRecord rec = new ZNRecord(_currentStateDelta.getId());
            rec.getMapFields().put(partitionKey, null);
            ZNRecordDelta delta = new ZNRecordDelta(rec, MergeOperation.SUBTRACT);
            List<ZNRecordDelta> deltaList = new ArrayList<>();
            deltaList.add(delta);
            _currentStateDelta.setDeltaList(deltaList);
            _stateModelFactory.removeStateModel(_message.getResourceName(), partitionKey);
        } else if (_stateModel.getCurrentState().equals(_message.getFromState())) {
            // if the partition is not to be dropped, update _stateModel to the TO_STATE
            // need this check because TaskRunner may change _stateModel before reach here.
            _stateModel.updateState(toState);
        }
    } else if (!taskResult.isCancelled()) {
        if (exception instanceof HelixStateMismatchException) {
            // if fromState mismatch, set current state on zk to stateModel's current state
            logger.warn("Force CurrentState on Zk to be stateModel's CurrentState. partitionKey: " + partitionKey + ", currentState: " + _stateModel.getCurrentState() + ", message: " + _message);
            _currentStateDelta.setState(partitionKey, _stateModel.getCurrentState());
            updateZKCurrentState();
            return;
        }
        // Handle timeout interrupt.
        if ((exception instanceof InterruptedException) && !_isTimeout) {
            // State transition interrupted but not caused by timeout. Keep the current
            // state in this case
            logger.error("State transition interrupted but not timeout. Not updating state. Partition : " + _message.getPartitionName() + " MsgId : " + _message.getMsgId());
            return;
        }
        // We did early return when Timeout interrupt.
        StateTransitionError error = new StateTransitionError(ErrorType.INTERNAL, exception instanceof InterruptedException ? ErrorCode.TIMEOUT : ErrorCode.ERROR, exception);
        _stateModel.rollbackOnError(_message, _notificationContext, error);
        _currentStateDelta.setState(partitionKey, HelixDefinedState.ERROR.toString());
        _stateModel.updateState(HelixDefinedState.ERROR.toString());
    }
    // NOP if taskResult.isCancelled()
    updateZKCurrentState();
}
Also used : ArrayList(java.util.ArrayList) StateTransitionError(org.apache.helix.participant.statemachine.StateTransitionError) HelixException(org.apache.helix.HelixException) HelixRollbackException(org.apache.helix.HelixRollbackException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) ZNRecordDelta(org.apache.helix.zookeeper.datamodel.ZNRecordDelta)

Aggregations

ArrayList (java.util.ArrayList)2 HelixException (org.apache.helix.HelixException)2 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)2 ZNRecordDelta (org.apache.helix.zookeeper.datamodel.ZNRecordDelta)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HelixDataAccessor (org.apache.helix.HelixDataAccessor)1 HelixRollbackException (org.apache.helix.HelixRollbackException)1 PropertyKey (org.apache.helix.PropertyKey)1 CustomizedState (org.apache.helix.model.CustomizedState)1 StateTransitionError (org.apache.helix.participant.statemachine.StateTransitionError)1