public IObserveOperationResult HandleMasterPersistence(ICouchbaseServerPool pool, ObserveKeyState passingState = ObserveKeyState.FoundPersisted) { try { var commandConfig = setupObserveOperation(pool); var node = commandConfig.CouchbaseNodes[0] as CouchbaseNode; IObserveOperationResult result = new ObserveOperationResult(); do { var are = new AutoResetEvent(false); var timer = new Timer(state => { result = node.ExecuteObserveOperation(commandConfig.Operation); if (log.IsDebugEnabled) { log.Debug("Node: " + node.EndPoint + ", Result: " + result.KeyState + ", Cas: " + result.Cas + ", Key: " + _settings.Key); } if (result.Success && result.Cas != _settings.Cas && result.Cas > 0 && passingState == ObserveKeyState.FoundPersisted) //don't check CAS for deleted items { result.Fail(ObserveOperationConstants.MESSAGE_MODIFIED); are.Set(); } else if (result.KeyState == passingState || (result.KeyState == ObserveKeyState.FoundPersisted && passingState == ObserveKeyState.FoundNotPersisted)) //if checking in memory, on disk should pass too { //though in memory checks are supported in this condition //a miss will require a timeout result.Pass(); are.Set(); } }, are, 0, 500); if (!are.WaitOne(_settings.Timeout)) { timer.Change(-1, -1); result.Fail(ObserveOperationConstants.MESSAGE_TIMEOUT, new TimeoutException()); break; } timer.Change(-1, -1); } while (result.Message == string.Empty && result.KeyState != passingState); return(result); } catch (ObserveExpectationException ex) { return(new ObserveOperationResult { Success = false, Message = ex.Message }); } catch (Exception ex) { return(new ObserveOperationResult { Success = false, Exception = ex }); } }
/// <summary> /// Handle the scenario when PersistTo == 1 /// </summary> public IObserveOperationResult HandleMasterPersistence(ICouchbaseServerPool pool) { try { var commandConfig = setupObserveOperation(pool); var node = commandConfig.Item2[0] as CouchbaseNode; IObserveOperationResult result = new ObserveOperationResult(); do { var are = new AutoResetEvent(false); var timer = new Timer(state => { result = node.ExecuteObserveOperation(commandConfig.Item3); if (log.IsDebugEnabled) { log.Debug("Node: " + node.EndPoint + ", Result: " + result.KeyState + ", Cas: " + result.Cas + ", Key: " + _settings.Key); } if (result.Success && result.Cas != _settings.Cas && result.Cas > 0) { result.Fail(ObserveOperationConstants.MESSAGE_MODIFIED); are.Set(); } else if (result.KeyState == ObserveKeyState.FoundPersisted) { result.Pass(); are.Set(); } }, are, 0, 500); if (!are.WaitOne(_settings.Timeout)) { timer.Change(-1, -1); result.Fail(ObserveOperationConstants.MESSAGE_TIMEOUT, new TimeoutException()); break; } timer.Change(-1, -1); } while (result.Message == string.Empty && result.KeyState != ObserveKeyState.FoundPersisted); return(result); } catch (Exception ex) { return(new ObserveOperationResult { Success = false, Exception = ex }); } }
private IObserveOperationResult performParallelObserve(ICouchbaseServerPool pool) { var commandConfig = setupObserveOperation(pool); var observedNodes = commandConfig.Item2.Select(n => new ObservedNode { Node = n as CouchbaseNode, IsMaster = n == commandConfig.Item2[0] }).ToArray(); var replicaFoundCount = 0; var replicaPersistedCount = 0; var isKeyPersistedToMaster = false; IObserveOperationResult result = new ObserveOperationResult(); do { var are = new AutoResetEvent(false); var timer = new Timer(state => { result = checkNodesForKey(observedNodes, commandConfig.Item3, ref isKeyPersistedToMaster, ref replicaFoundCount, ref replicaPersistedCount); if (result.Message == ObserveOperationConstants.MESSAGE_MODIFIED) { are.Set(); result.Fail(ObserveOperationConstants.MESSAGE_MODIFIED); } else if (isInExpectedState(replicaFoundCount, replicaPersistedCount, isKeyPersistedToMaster)) { are.Set(); result.Pass(); } }, are, 0, 500); if (!are.WaitOne(_settings.Timeout)) { timer.Change(-1, -1); result.Fail(ObserveOperationConstants.MESSAGE_TIMEOUT, new TimeoutException()); return(result); } if (result.Success) { timer.Change(-1, -1); } } while (result.Message == string.Empty && !isInExpectedState(replicaFoundCount, replicaPersistedCount, isKeyPersistedToMaster)); return(result); }
public IObserveOperationResult ExecuteObserveOperation(IObserveOperation op) { var readResult = new ObserveOperationResult(); var result = this.Acquire(); if (result.Success && result.HasValue) { try { var socket = result.Value; var b = op.GetBuffer(); socket.Write(b); readResult = op.ReadResponse(socket) as ObserveOperationResult; if (readResult.Success) { readResult.Pass(); } else { readResult.InnerResult = result; readResult.Fail("Failed to read response, see inner result for details"); } return(readResult); } catch (IOException e) { log.Error(e); readResult.Fail("Exception reading response", e); return(readResult); } finally { ((IDisposable)result.Value).Dispose(); } } else { readResult.Fail("Failed to obtain socket from pool"); return(readResult); } }
protected override IOperationResult ReadResponse(PooledSocket socket) { var response = new ObserveResponse(); var result = new ObserveOperationResult(); var retval = false; if (response.Read(socket)) { retval = true; result.Cas = response.Cas; result.StatusCode = StatusCode; result.Key = response.Key; result.KeyState = response.KeyState; result.PersistenceStats = response.PersistenceStats; result.ReplicationStats = response.ReplicationStats; } this.StatusCode = response.StatusCode; result.PassOrFail(retval, ResultHelper.ProcessResponseData(response.Data, "Failed: ")); return(result); }
private IObserveOperationResult checkNodesForKey(ObservedNode[] nodes, IObserveOperation command, ref bool isMasterInExpectedState, ref int replicaFoundCount, ref int replicaPersistedCount) { var tmpReplicaFoundCount = 0; var tmpReplicaPersistedCount = 0; var tmpIsPersistedToMaster = false; var result = new ObserveOperationResult(); var lockObject = new object(); foreach (var node in nodes) { lock (lockObject) { var opResult = node.Node.ExecuteObserveOperation(command); if (log.IsDebugEnabled) { log.Debug("Node: " + node.Node.EndPoint + ", Result: " + opResult.KeyState + ", Master: " + node.IsMaster + ", Cas: " + opResult.Cas + ", Key: " + _settings.Key); } if (!opResult.Success) //Probably an IO Exception { break; } else if (node.IsMaster && opResult.Cas != _settings.Cas) { result.Success = false; result.Message = ObserveOperationConstants.MESSAGE_MODIFIED; break; } else if (opResult.KeyState == ObserveKeyState.FoundPersisted) { node.KeyIsPersisted = true; if (node.IsMaster) { tmpIsPersistedToMaster = true; } else { tmpReplicaPersistedCount++; } } else if (opResult.KeyState == ObserveKeyState.FoundNotPersisted) { if (!node.IsMaster) { tmpReplicaFoundCount++; } } } } isMasterInExpectedState = tmpIsPersistedToMaster; replicaFoundCount = tmpReplicaFoundCount; replicaPersistedCount = tmpReplicaPersistedCount; if (log.IsDebugEnabled) { log.Debug("Master Persisted: " + tmpIsPersistedToMaster + ", Replica Found: " + replicaFoundCount + ", Replica Persisted: " + tmpReplicaPersistedCount); } return(result); }