Inheritance: Enyim.Caching.Memcached.Results.OperationResultBase, IObserveOperationResult
        /// <summary>
        /// Handle the scenario when PersistTo == 1
        /// </summary>
        public IObserveOperationResult HandleMasterPersistence(ICouchbaseServerPool pool, ObserveKeyState passingState = ObserveKeyState.FoundPersisted)
        {
            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 && passingState == ObserveKeyState.FoundPersisted) //don't check CAS for deleted items
                        {
                            result.Fail(ObserveOperationConstants.MESSAGE_MODIFIED);
                            are.Set();
                        }
                        else if (result.KeyState == passingState)
                        {
                            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 };
            }
        }
Exemplo n.º 2
0
        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.StatusCode = StatusCode.UnspecifiedError;
                    readResult.Fail("Exception reading response", e);
                    return readResult;
                }
                finally
                {
                    ((IDisposable)result.Value).Dispose();
                }
            }
            else
            {
                result.Combine(readResult);
                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.ToStatusCode();

            result.PassOrFail(retval, ResultHelper.ProcessResponseData(response.Data, "Failed: "));
            return result;
        }
        private IObserveOperationResult performParallelObserve(ICouchbaseServerPool pool, ObserveKeyState persistedKeyState, ObserveKeyState replicatedKeyState)
        {
            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, persistedKeyState, replicatedKeyState);

                    if (result.Message == ObserveOperationConstants.MESSAGE_MODIFIED)
                    {
                        are.Set();
                        result.Fail(ObserveOperationConstants.MESSAGE_MODIFIED);
                    }
                    else if (isInExpectedState(replicaFoundCount, replicaPersistedCount, isKeyPersistedToMaster))
                    {
                        result.Pass();
                        are.Set();
                    }

                }, 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;
        }
        private IObserveOperationResult checkNodesForKey(ObservedNode[] nodes, IObserveOperation command, ref bool isMasterInExpectedState, ref int replicaFoundCount, ref int replicaPersistedCount, ObserveKeyState persistedKeyState, ObserveKeyState replicatedKeyState)
        {
            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 &&
                             (persistedKeyState == ObserveKeyState.FoundPersisted ||
                              replicatedKeyState == ObserveKeyState.FoundNotPersisted))
                    {
                        result.Success = false;
                        result.Message = ObserveOperationConstants.MESSAGE_MODIFIED;
                        break;
                    }
                    else if (opResult.KeyState == persistedKeyState)
                    {
                        node.KeyIsPersisted = true;
                        if (node.IsMaster)
                        {
                            tmpIsPersistedToMaster = true;
                        }
                        else
                        {
                            tmpReplicaPersistedCount++;
                        }
                    }
                    else if (opResult.KeyState == replicatedKeyState)
                    {
                        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;
        }