Couchbase.ObserveHandler.HandleMasterPersistence C# (CSharp) Method

HandleMasterPersistence() public method

Handle the scenario when PersistTo == 1
public HandleMasterPersistence ( ICouchbaseServerPool pool ) : IObserveOperationResult
pool ICouchbaseServerPool
return IObserveOperationResult
        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 };
            }
        }

Usage Example

Example #1
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));
            }
        }