Beispiel #1
0
        public override bool Execute()
        {
            IObserveOperationResult res = client.Observe(
                Key, Cas, options.Persist, options.Replicate);

            if (!res.Success)
            {
                FailCommand(res);
                return(false);
            }

            string pretty = String.Format("Observe Code: (0x{0:x})",
                                          (int)res.KeyState);

            foreach (string name in Enum.GetNames(typeof(ObserveKeyState)))
            {
                ObserveKeyState v = (ObserveKeyState)
                                    Enum.Parse(typeof(ObserveKeyState), name);
                if (res.KeyState == v)
                {
                    pretty += " " + name;
                    break;
                }
            }

            Console.WriteLine(pretty);

            pretty = String.Format(
                "Timings: Persist [{0}], Replicate [{1}]",
                res.PersistenceStats.ToString(),
                res.ReplicationStats.ToString());
            Console.WriteLine(pretty);

            return(true);
        }
Beispiel #2
0
        public IObserveOperationResult Observe(string key, ulong cas, PersistTo persistTo, ReplicateTo replicateTo,
                                               ObserveKeyState persistedKeyState = ObserveKeyState.FoundPersisted,
                                               ObserveKeyState replicatedState   = ObserveKeyState.FoundNotPersisted)
        {
            var hashedKey = this.KeyTransformer.Transform(key);
            var vbucket   = this.poolInstance.GetVBucket(key);
            var nodes     = this.poolInstance.GetWorkingNodes().ToArray();
            var command   = this.poolInstance.OperationFactory.Observe(hashedKey, vbucket.Index, cas);
            var runner    = new ObserveHandler(new ObserveSettings
            {
                PersistTo   = persistTo,
                ReplicateTo = replicateTo,
                Key         = hashedKey,
                Cas         = cas,
                Timeout     = observeTimeout
            });

            //Master only persistence
            if (replicateTo == ReplicateTo.Zero && persistTo == PersistTo.One)
            {
                return(runner.HandleMasterPersistence(poolInstance, persistedKeyState));
            }
            else if (replicateTo == ReplicateTo.Zero && persistTo == PersistTo.Zero)             //used for key exists checks
            {
                return(runner.HandleMasterOnlyInCache(poolInstance));
            }
            else
            {
                return(runner.HandleMasterPersistenceWithReplication(poolInstance, persistedKeyState, replicatedState));
            }
        }
        public IObserveOperationResult HandleMasterPersistence(ICouchbaseServerPool pool, ObserveKeyState passingState = ObserveKeyState.FoundPersisted)
        {
            try
            {
                var commandConfig = SetupObserveOperation(pool);
                var node = commandConfig.CouchbaseNodes[0];
                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 };
            }
        }
Beispiel #4
0
        public unsafe bool Read(PooledSocket socket)
        {
            this.StatusCode = -1;

            if (!socket.IsAlive)
            {
                return(false);
            }

            var header = new byte[HeaderLength];

            socket.Read(header, 0, header.Length);

            int dataLength, extraLength;

            DeserializeHeader(header, out dataLength, out extraLength);

            var keyHeader = new byte[4];

            socket.Read(keyHeader, 0, 4);
            var vbucket = BinaryConverter.DecodeUInt16(keyHeader, 0);
            var keylen  = BinaryConverter.DecodeUInt16(keyHeader, 2);

            var keyData = new byte[keylen];

            socket.Read(keyData, 0, keylen);
            Key = BinaryConverter.DecodeKey(keyData);

            var keyStateData = new byte[1];

            socket.Read(keyStateData, 0, keyStateData.Length);
            KeyState = (ObserveKeyState)keyStateData[0];

            var casData = new byte[8];

            socket.Read(casData, 0, casData.Length);
            Cas = BinaryConverter.DecodeUInt64(casData, 0);

            return(this.StatusCode == 0);
        }
 public IObserveOperationResult Observe(string key, ulong cas, PersistTo persistTo, ReplicateTo replicateTo,
                                        ObserveKeyState persistedKeyState = ObserveKeyState.FoundPersisted, ObserveKeyState replicatedState = ObserveKeyState.FoundNotPersisted)
 {
     throw new NotImplementedException();
 }
        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;
        }
 public IObserveOperationResult HandleMasterPersistenceWithReplication(ICouchbaseServerPool pool, ObserveKeyState persistedKeyState, ObserveKeyState replicatedKeyState)
 {
     try
     {
         return performParallelObserve(pool, persistedKeyState, replicatedKeyState);
     }
     catch (ObserveExpectationException ex)
     {
         return new ObserveOperationResult { Success = false, Message = ex.Message };
     }
     catch (Exception ex)
     {
         return new ObserveOperationResult { Success = false, Exception = ex };
     }
 }
Beispiel #9
0
        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
                });
            }
        }
Beispiel #10
0
        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);
        }
Beispiel #11
0
        private IObserveOperationResult performParallelObserve(ICouchbaseServerPool pool, ObserveKeyState persistedKeyState, ObserveKeyState replicatedKeyState)
        {
            var commandConfig = setupObserveOperation(pool);
            var observedNodes = commandConfig.CouchbaseNodes.Select(n => new ObservedNode
            {
                Node     = n as CouchbaseNode,
                IsMaster = n == commandConfig.CouchbaseNodes[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.Operation, 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);
        }
Beispiel #12
0
 public IObserveOperationResult HandleMasterPersistenceWithReplication(ICouchbaseServerPool pool, ObserveKeyState persistedKeyState, ObserveKeyState replicatedKeyState)
 {
     try
     {
         return(performParallelObserve(pool, persistedKeyState, replicatedKeyState));
     }
     catch (ObserveExpectationException ex)
     {
         return(new ObserveOperationResult {
             Success = false, Message = ex.Message
         });
     }
     catch (Exception ex)
     {
         return(new ObserveOperationResult {
             Success = false, Exception = ex
         });
     }
 }