use of com.ms.silverking.cloud.dht.daemon.storage.protocol.SingleWriterConsistentStorageEntryState.StateTransitionResult in project SilverKing by Morgan-Stanley.
the class SingleWriterConsistentWrite method update.
/**
* Called after a replica state updates
*/
@Override
public void update(DHTKey key, IPAndPort replica, byte storageState, OpResult update, PutVirtualCommunicator pvComm) {
assert replica != null;
if (update == OpResult.SUCCEEDED || update == OpResult.INVALID_VERSION || update == OpResult.MUTATION || update == OpResult.NO_SUCH_NAMESPACE) {
SingleWriterConsistentStorageEntryState entryState;
if (debug) {
System.out.printf("key %s replica %s\tupdate %s\n", KeyUtil.keyToString(key), replica, update);
}
// FUTURE - could refactor code to only perform this lookup once if needed
entryState = getEntryState(key);
synchronized (entryState) {
if (entryState.getCurOpResult() == OpResult.INCOMPLETE) {
TwoPhaseStorageState prevState;
StateTransitionResult transitionResult;
TwoPhaseStorageState nextState;
OpResult opResult;
prevState = TwoPhaseStorageState.values[storageState];
if (update.toOperationState() != OperationState.FAILED) {
nextState = TwoPhaseStorageState.nextState(prevState);
transitionResult = entryState.transitionFromState(replica, prevState);
opResult = nextState.toOpResult();
} else {
entryState.fail(replica, prevState);
transitionResult = StateTransitionResult.COMPLETION;
nextState = TwoPhaseStorageState.FAILED;
opResult = update;
}
if (debug) {
System.out.printf("key %s prevState %s nextState %s transitionResult %s\n", KeyUtil.keyToString(key), prevState, nextState, transitionResult);
}
switch(transitionResult) {
case REPLICA_TRANSITION:
break;
case QUORUM_TRANSITION:
quorumTransition(key, entryState.primaryReplicas(), nextState, update, pvComm);
quorumTransition(key, entryState.secondaryReplicas(), nextState, update, pvComm);
if (!nextState.isComplete()) {
break;
} else {
// fall through if complete
}
case COMPLETION:
int _completeEntries;
pvComm.sendResult(key, opResult);
_completeEntries = completeEntries.incrementAndGet();
if (debug) {
System.out.printf("COMPLETION %s %d %d\n", KeyUtil.keyToString(key), _completeEntries, numEntries);
}
if (_completeEntries >= numEntries) {
setOpResult(OpResult.SUCCEEDED);
}
if (debug) {
System.out.printf("opResult %s\n", getOpResult());
}
break;
case NO_TRANSITION:
// must be old update
break;
default:
throw new RuntimeException("panic");
}
} else {
if (Log.levelMet(Level.FINE)) {
Log.fine("Update for non-incomplete: ", key + " " + replica + " " + update);
}
}
}
} else {
// FUTURE - Consider additional error handling
Log.warning("Unexpected update: ", key + " " + replica + " " + update);
}
}
Aggregations