void applyApplicationStateLocally( InetAddress addr, EndpointState localStatePtr, EndpointState remoteStatePtr) { Map<ApplicationState, VersionedValue> localAppStateMap = localStatePtr.getApplicationStateMap(); for (Entry<ApplicationState, VersionedValue> remoteEntry : remoteStatePtr.getApplicationStateMap().entrySet()) { ApplicationState remoteKey = remoteEntry.getKey(); VersionedValue remoteValue = remoteEntry.getValue(); VersionedValue localValue = localAppStateMap.get(remoteKey); /* If state doesn't exist locally for this key then just apply it */ if (localValue == null) { localStatePtr.addApplicationState(remoteKey, remoteValue); doNotifications(addr, remoteKey, remoteValue); continue; } int remoteGeneration = remoteStatePtr.getHeartBeatState().getGeneration(); int localGeneration = localStatePtr.getHeartBeatState().getGeneration(); assert remoteGeneration >= localGeneration; // SystemTable makes sure we never generate a smaller generation on // start /* If the remoteGeneration is greater than localGeneration then apply state blindly */ if (remoteGeneration > localGeneration) { localStatePtr.addApplicationState(remoteKey, remoteValue); doNotifications(addr, remoteKey, remoteValue); continue; } /* If the generations are the same then apply state if the remote version is greater than local version. */ if (remoteGeneration == localGeneration) { int remoteVersion = remoteValue.version; int localVersion = localValue.version; if (remoteVersion > localVersion) { localStatePtr.addApplicationState(remoteKey, remoteValue); doNotifications(addr, remoteKey, remoteValue); } } } }
EndpointState getStateForVersionBiggerThan(InetAddress forEndpoint, int version) { EndpointState epState = endpointStateMap_.get(forEndpoint); EndpointState reqdEndpointState = null; if (epState != null) { /* * Here we try to include the Heart Beat state only if it is * greater than the version passed in. It might happen that * the heart beat version maybe lesser than the version passed * in and some application state has a version that is greater * than the version passed in. In this case we also send the old * heart beat and throw it away on the receiver if it is redundant. */ int localHbVersion = epState.getHeartBeatState().getHeartBeatVersion(); if (localHbVersion > version) { reqdEndpointState = new EndpointState(epState.getHeartBeatState()); if (logger_.isTraceEnabled()) logger_.trace( "local heartbeat version " + localHbVersion + " greater than " + version + " for " + forEndpoint); } /* Accumulate all application states whose versions are greater than "version" variable */ for (Entry<ApplicationState, VersionedValue> entry : epState.getApplicationStateMap().entrySet()) { VersionedValue value = entry.getValue(); if (value.version > version) { if (reqdEndpointState == null) { reqdEndpointState = new EndpointState(epState.getHeartBeatState()); } final ApplicationState key = entry.getKey(); if (logger_.isTraceEnabled()) logger_.trace("Adding state " + key + ": " + value.value); reqdEndpointState.addApplicationState(key, value); } } } return reqdEndpointState; }
int getMaxEndpointStateVersion(EndpointState epState) { int maxVersion = epState.getHeartBeatState().getHeartBeatVersion(); for (VersionedValue value : epState.getApplicationStateMap().values()) maxVersion = Math.max(maxVersion, value.version); return maxVersion; }