/// <summary>
        /// Handle the scenario when PersistTo == 0 && ReplicateTo == 0
        /// Primary use case is to check whether key exists without
        /// having to perform a Get + null check
        /// </summary>
        public IObserveOperationResult HandleMasterOnlyInCache(ICouchbaseServerPool pool)
        {
            try
            {
                var commandConfig = SetupObserveOperation(pool);
                var node = commandConfig.CouchbaseNodes[0];
                var result = node.ExecuteObserveOperation(commandConfig.Operation);
                if (Log.IsDebugEnabled) Log.Debug("Node: " + node.EndPoint + ", Result: " + result.KeyState + ", Cas: " + result.Cas + ", Key: " + _settings.Key);

                if ((_settings.Cas == 0 || result.Cas == _settings.Cas) &&
                        (result.KeyState == ObserveKeyState.FoundNotPersisted || result.KeyState == ObserveKeyState.FoundPersisted))
                {
                    result.Pass();
                }
                else
                {
                    result.Fail("Key not found");
                }

                return result;
            }
            catch (ObserveExpectationException ex)
            {
                return new ObserveOperationResult { Success = false, Message = ex.Message };
            }
            catch (Exception ex)
            {
                return new ObserveOperationResult { Success = false, Exception = ex };
            }
        }
Esempio n. 2
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
                });
            }
        }
        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 };
            }
        }
Esempio n. 4
0
 public IObserveOperationResult HandleMasterPersistenceWithReplication(ICouchbaseServerPool pool)
 {
     try
     {
         return(performParallelObserve(pool));
     }
     catch (Exception ex)
     {
         return(new ObserveOperationResult {
             Success = false, Exception = ex
         });
     }
 }
Esempio n. 5
0
        /// <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
                });
            }
        }
Esempio n. 6
0
        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);
        }
        /// <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 };
            }
        }
Esempio n. 8
0
        protected CouchbaseClient(ICouchbaseServerPool pool, ICouchbaseClientConfiguration configuration)
            : base(pool,
                   configuration.CreateKeyTransformer(),
                   configuration.CreateTranscoder(),
                   configuration.CreatePerformanceMonitor())
        {
            this.documentNameTransformer = configuration.CreateDesignDocumentNameTransformer();
            this.poolInstance            = (ICouchbaseServerPool)this.Pool;
            observeTimeout = configuration.ObserveTimeout;

            StoreOperationResultFactory  = new DefaultStoreOperationResultFactory();
            GetOperationResultFactory    = new DefaultGetOperationResultFactory();
            MutateOperationResultFactory = new DefaultMutateOperationResultFactory();
            ConcatOperationResultFactory = new DefaultConcatOperationResultFactory();
            RemoveOperationResultFactory = new DefaultRemoveOperationResultFactory();
        }
Esempio n. 9
0
        private ObserveOperationSetup setupObserveOperation(ICouchbaseServerPool pool)
        {
            var vbucket = pool.GetVBucket(_settings.Key);

            // Check to see if our persistence requirements can be satisfied
            if (((int)_settings.ReplicateTo > vbucket.Replicas.Length + 1) ||
                ((int)_settings.PersistTo > vbucket.Replicas.Length + 1))
            {
                throw new ObserveExpectationException(
                          "Requested replication or persistence to more nodes than are currently " +
                          "configured");
            }
            var command = pool.OperationFactory.Observe(_settings.Key, vbucket.Index, _settings.Cas);

            var workingNodes          = pool.GetWorkingNodes().ToArray();
            var masterAndReplicaNodes = new List <CouchbaseNode>();

            masterAndReplicaNodes.Add(workingNodes[vbucket.Master] as CouchbaseNode);

            for (var i = 0; i < vbucket.Replicas.Length; i++)
            {
                int replicaIndex = vbucket.Replicas[i];
                if (replicaIndex < 0)
                {
                    continue;
                }
                masterAndReplicaNodes.Add(workingNodes[replicaIndex] as CouchbaseNode);
            }

            if (masterAndReplicaNodes.Count < (int)_settings.PersistTo ||
                masterAndReplicaNodes.Count - 1 < (int)_settings.ReplicateTo)
            {
                throw new ObserveExpectationException(
                          "Requested replication or persistence to more nodes than are currently " +
                          "online");
            }

            return(new ObserveOperationSetup
            {
                VBucket = vbucket,
                CouchbaseNodes = masterAndReplicaNodes.ToArray(),
                Operation = command
            });
        }
Esempio n. 10
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
         });
     }
 }
Esempio n. 11
0
        /// <summary>
        /// Handle the scenario when PersistTo == 0 && ReplicateTo == 0
        /// Primary use case is to check whether key exists without
        /// having to perform a Get + null check
        /// </summary>
        public IObserveOperationResult HandleMasterOnlyInCache(ICouchbaseServerPool pool)
        {
            try
            {
                var commandConfig = setupObserveOperation(pool);
                var node          = commandConfig.CouchbaseNodes[0] as CouchbaseNode;
                var result        = node.ExecuteObserveOperation(commandConfig.Operation);
                if (log.IsDebugEnabled)
                {
                    log.Debug("Node: " + node.EndPoint + ", Result: " + result.KeyState + ", Cas: " + result.Cas + ", Key: " + _settings.Key);
                }

                if ((_settings.Cas == 0 || result.Cas == _settings.Cas) &&
                    (result.KeyState == ObserveKeyState.FoundNotPersisted || result.KeyState == ObserveKeyState.FoundPersisted))
                {
                    result.Pass();
                }
                else
                {
                    result.Fail("Key not found");
                }

                return(result);
            }
            catch (ObserveExpectationException ex)
            {
                return(new ObserveOperationResult {
                    Success = false, Message = ex.Message
                });
            }
            catch (Exception ex)
            {
                return(new ObserveOperationResult {
                    Success = false, Exception = ex
                });
            }
        }
 public CouchbaseClientExtended(ICouchbaseServerPool pool, CouchbaseClientConfiguration config) : base(pool, config)
 {
     LoadBaseConfig(config);
 }
        private Tuple<VBucket, CouchbaseNode[], IObserveOperation> setupObserveOperation(ICouchbaseServerPool pool)
        {
            var vbucket = pool.GetVBucket(_settings.Key);

            // Check to see if our persistence requirements can be satisfied
            if (((int)_settings.ReplicateTo > vbucket.Replicas.Length + 1) ||
                 ((int)_settings.PersistTo > vbucket.Replicas.Length + 1))
            {
                throw new ObserveExpectationException(
                    "Requested replication or persistence to more nodes than are currently " +
                    "available");
            }
            var command = pool.OperationFactory.Observe(_settings.Key, vbucket.Index, _settings.Cas);

            var workingNodes = pool.GetWorkingNodes().ToArray();
            var masterAndReplicaNodes = new List<CouchbaseNode>();
            masterAndReplicaNodes.Add(workingNodes[vbucket.Master] as CouchbaseNode);

            for (var i = 0; i < vbucket.Replicas.Length; i++)
            {
                int replicaIndex = vbucket.Replicas[i];
                if (replicaIndex < 0)
                {
                    continue;
                }
                masterAndReplicaNodes.Add(workingNodes[replicaIndex] as CouchbaseNode);
            }

            return Tuple.Create(vbucket, masterAndReplicaNodes.ToArray(), command);
        }
        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;
        }
 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 };
     }
 }
Esempio n. 16
0
        private Tuple <VBucket, CouchbaseNode[], IObserveOperation> setupObserveOperation(ICouchbaseServerPool pool)
        {
            var vbucket = pool.GetVBucket(_settings.Key);
            var command = pool.OperationFactory.Observe(_settings.Key, vbucket.Index, _settings.Cas);

            var workingNodes = pool.GetWorkingNodes().ToArray();

            var masterAndReplicaNodes = new CouchbaseNode[vbucket.Replicas.Count() + 1];

            masterAndReplicaNodes[0] = workingNodes[vbucket.Master] as CouchbaseNode;

            for (var i = 0; i < vbucket.Replicas.Length; i++)
            {
                masterAndReplicaNodes[i + 1] = workingNodes[vbucket.Replicas[i]] as CouchbaseNode;
            }

            return(Tuple.Create(vbucket, masterAndReplicaNodes, command));
        }
        private Tuple<VBucket, CouchbaseNode[], IObserveOperation> setupObserveOperation(ICouchbaseServerPool pool)
        {
            var vbucket = pool.GetVBucket(_settings.Key);
            var command = pool.OperationFactory.Observe(_settings.Key, vbucket.Index, _settings.Cas);

            var workingNodes = pool.GetWorkingNodes().ToArray();

            var masterAndReplicaNodes = new CouchbaseNode[vbucket.Replicas.Count() + 1];

            masterAndReplicaNodes[0] = workingNodes[vbucket.Master] as CouchbaseNode;

            for (var i = 0; i < vbucket.Replicas.Length; i++)
            {
                masterAndReplicaNodes[i + 1] = workingNodes[vbucket.Replicas[i]] as CouchbaseNode;
            }

            return Tuple.Create(vbucket, masterAndReplicaNodes, command);
        }
        private ObserveOperationSetup SetupObserveOperation(ICouchbaseServerPool pool)
        {
            var vbucket = pool.GetVBucket(_settings.Key);

            // Check to see if our persistence requirements can be satisfied
            if (((int)_settings.ReplicateTo > vbucket.Replicas.Length + 1) ||
                 ((int)_settings.PersistTo > vbucket.Replicas.Length + 1))
            {
                throw new ObserveExpectationException(
                    "Requested replication or persistence to more nodes than are currently " +
                    "configured");
            }
            var command = pool.OperationFactory.Observe(_settings.Key, vbucket.Index, _settings.Cas);

            var workingNodes = pool.GetWorkingNodes().Cast<ICouchbaseNode>().ToArray();
            var masterAndReplicaNodes = new List<ICouchbaseNode> {workingNodes[vbucket.Master]};

            for (var i = 0; i < vbucket.Replicas.Length; i++)
            {
                int replicaIndex = vbucket.Replicas[i];
                if (replicaIndex < 0)
                {
                    continue;
                }
                masterAndReplicaNodes.Add(workingNodes[replicaIndex]);
            }

            if (masterAndReplicaNodes.Count < (int)_settings.PersistTo ||
                masterAndReplicaNodes.Count - 1 < (int)_settings.ReplicateTo)
            {
                throw new ObserveExpectationException(
                    "Requested replication or persistence to more nodes than are currently " +
                    "online");
            }

            return new ObserveOperationSetup
            {
                VBucket = vbucket,
                CouchbaseNodes = masterAndReplicaNodes.ToArray(),
                Operation = command
            };
        }
 public IObserveOperationResult HandleMasterPersistenceWithReplication(ICouchbaseServerPool pool)
 {
     try
     {
         return performParallelObserve(pool);
     }
     catch (Exception ex)
     {
         return new ObserveOperationResult { Success = false, Exception = ex };
     }
 }