use of org.apache.helix.ZNRecordBucketizer in project helix by apache.
the class ParticipantManager method carryOverPreviousCurrentState.
/**
* carry over current-states from last sessions
* set to initial state for current session only when state doesn't exist in current session
*/
private void carryOverPreviousCurrentState() {
List<String> sessions = _dataAccessor.getChildNames(_keyBuilder.sessions(_instanceName));
for (String session : sessions) {
if (session.equals(_sessionId)) {
continue;
}
List<CurrentState> lastCurStates = _dataAccessor.getChildValues(_keyBuilder.currentStates(_instanceName, session));
for (CurrentState lastCurState : lastCurStates) {
LOG.info("Carrying over old session: " + session + ", resource: " + lastCurState.getId() + " to current session: " + _sessionId);
String stateModelDefRef = lastCurState.getStateModelDefRef();
if (stateModelDefRef == null) {
LOG.error("skip carry-over because previous current state doesn't have a state model definition. previous current-state: " + lastCurState);
continue;
}
StateModelDefinition stateModel = _dataAccessor.getProperty(_keyBuilder.stateModelDef(stateModelDefRef));
BaseDataAccessor<ZNRecord> baseAccessor = _dataAccessor.getBaseDataAccessor();
String curStatePath = _keyBuilder.currentState(_instanceName, _sessionId, lastCurState.getResourceName()).getPath();
String initState = stateModel.getInitialState();
if (lastCurState.getBucketSize() > 0) {
// update parent node
ZNRecord metaRecord = new ZNRecord(lastCurState.getId());
metaRecord.setSimpleFields(lastCurState.getRecord().getSimpleFields());
DataUpdater<ZNRecord> metaRecordUpdater = new CurStateCarryOverUpdater(_sessionId, initState, new CurrentState(metaRecord));
boolean success = baseAccessor.update(curStatePath, metaRecordUpdater, AccessOption.PERSISTENT);
if (success) {
// update current state buckets
ZNRecordBucketizer bucketizer = new ZNRecordBucketizer(lastCurState.getBucketSize());
Map<String, ZNRecord> map = bucketizer.bucketize(lastCurState.getRecord());
List<String> paths = new ArrayList<String>();
List<DataUpdater<ZNRecord>> updaters = new ArrayList<DataUpdater<ZNRecord>>();
for (String bucketName : map.keySet()) {
paths.add(curStatePath + "/" + bucketName);
updaters.add(new CurStateCarryOverUpdater(_sessionId, initState, new CurrentState(map.get(bucketName))));
}
baseAccessor.updateChildren(paths, updaters, AccessOption.PERSISTENT);
}
} else {
_dataAccessor.getBaseDataAccessor().update(curStatePath, new CurStateCarryOverUpdater(_sessionId, initState, lastCurState), AccessOption.PERSISTENT);
}
}
}
/**
* remove previous current state parent nodes
*/
for (String session : sessions) {
if (session.equals(_sessionId)) {
continue;
}
String path = _keyBuilder.currentStates(_instanceName, session).getPath();
LOG.info("Removing current states from previous sessions. path: " + path);
_zkclient.deleteRecursively(path);
}
}
Aggregations