Search in sources :

Example 6 with ZNRecordBucketizer

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);
    }
}
Also used : ZNRecordBucketizer(org.apache.helix.ZNRecordBucketizer) ArrayList(java.util.ArrayList) StateModelDefinition(org.apache.helix.model.StateModelDefinition) CurrentState(org.apache.helix.model.CurrentState) DataUpdater(org.I0Itec.zkclient.DataUpdater) ZNRecord(org.apache.helix.ZNRecord)

Aggregations

ZNRecordBucketizer (org.apache.helix.ZNRecordBucketizer)6 ArrayList (java.util.ArrayList)5 ZNRecord (org.apache.helix.ZNRecord)5 PropertyKey (org.apache.helix.PropertyKey)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HelixDataAccessor (org.apache.helix.HelixDataAccessor)2 HelixException (org.apache.helix.HelixException)2 HelixRollbackException (org.apache.helix.HelixRollbackException)2 PropertyType (org.apache.helix.PropertyType)2 ZNRecordDelta (org.apache.helix.ZNRecordDelta)2 CurrentState (org.apache.helix.model.CurrentState)2 StateTransitionError (org.apache.helix.participant.statemachine.StateTransitionError)2 List (java.util.List)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DataUpdater (org.I0Itec.zkclient.DataUpdater)1 HelixProperty (org.apache.helix.HelixProperty)1 Builder (org.apache.helix.PropertyKey.Builder)1 HelixMetaDataAccessException (org.apache.helix.api.exceptions.HelixMetaDataAccessException)1 StateModelDefinition (org.apache.helix.model.StateModelDefinition)1