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));
}
}
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();
}
Aggregations