Esempio n. 1
0
        /// <summary>
        /// Sends a <see cref="IOperation" /> to the Couchbase Server using the Memcached protocol using async/await.
        /// </summary>
        /// <param name="operation">The <see cref="IOperation" /> to send.</param>
        /// <returns>
        /// An <see cref="Task{IOperationResult}" /> object representing the asynchronous operation.
        /// </returns>
        public async override Task <IOperationResult> SendWithRetryAsync(IOperation operation)
        {
            var tcs = new TaskCompletionSource <IOperationResult>();
            var cts = new CancellationTokenSource(OperationLifeSpan);

            cts.CancelAfter(OperationLifeSpan);

            try
            {
                var keyMapper = ConfigInfo.GetKeyMapper();
                var vBucket   = (IVBucket)keyMapper.MapKey(operation.Key);
                operation.VBucket = vBucket;

                operation.Completed = CallbackFactory.CompletedFuncWithRetryForCouchbase(
                    this, Pending, ClusterController, tcs, cts.Token);

                Pending.TryAdd(operation.Opaque, operation);

                var server = vBucket.LocatePrimary();
                server.SendAsync(operation).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                tcs.TrySetResult(new OperationResult
                {
                    Exception = e,
                    Status    = ResponseStatus.ClientFailure
                });
            }
            return(await tcs.Task);
        }
        /// <summary>
        /// Sends a <see cref="IOperation" /> to the Couchbase Server using the Memcached protocol using async/await.
        /// </summary>
        /// <param name="operation">The <see cref="IOperation" /> to send.</param>
        /// <param name="tcs">The <see cref="TaskCompletionSource{T}"/> the represents the task to await on.</param>
        /// <param name="cts">The <see cref="CancellationTokenSource"/> for cancellation.</param>
        /// <returns>
        /// An <see cref="Task{IOperationResult}" /> object representing the asynchronous operation.
        /// </returns>
        public override async Task <IOperationResult> SendWithRetryAsync(IOperation operation,
                                                                         TaskCompletionSource <IOperationResult> tcs = null,
                                                                         CancellationTokenSource cts = null)
        {
            tcs = tcs ?? new TaskCompletionSource <IOperationResult>();
            cts = cts ?? new CancellationTokenSource(OperationLifeSpan);

            try
            {
                //Is the cluster configured for Data services?
                if (!ConfigInfo.IsDataCapable)
                {
                    tcs.SetException(new ServiceNotSupportedException(
                                         ExceptionUtil.GetMessage(ExceptionUtil.ServiceNotSupportedMsg, "Data")));
                }

                var keyMapper = ConfigInfo.GetKeyMapper();
                var vBucket   = (IVBucket)keyMapper.MapKey(operation.Key, operation.LastConfigRevisionTried);
                operation.VBucket = vBucket;
                operation.LastConfigRevisionTried = vBucket.Rev;

                operation.Completed = CallbackFactory.CompletedFuncWithRetryForCouchbase(
                    this, Pending, ClusterController, tcs, cts.Token);

                Pending.TryAdd(operation.Opaque, operation);

                IServer server;
                var     attempts = 0;
                while ((server = vBucket.LocatePrimary()) == null)
                {
                    if (attempts++ > 10)
                    {
                        throw new TimeoutException("Could not acquire a server.");
                    }
                    await Task.Delay((int)Math.Pow(2, attempts)).ContinueOnAnyContext();
                }
                Log.Debug("Starting send for {0} with {1}", operation.Opaque, server.EndPoint);
                await server.SendAsync(operation).ContinueOnAnyContext();
            }
            catch (Exception e)
            {
                tcs.TrySetResult(new OperationResult
                {
                    Id        = operation.Key,
                    Exception = e,
                    Status    = ResponseStatus.ClientFailure
                });
            }
            return(await tcs.Task.ContinueOnAnyContext());
        }
        /// <summary>
        /// Sends a <see cref="IOperation" /> to the Couchbase Server using the Memcached protocol using async/await.
        /// </summary>
        /// <param name="operation">The <see cref="IOperation" /> to send.</param>
        /// <param name="tcs">The <see cref="TaskCompletionSource{T}"/> the represents the task to await on.</param>
        /// <param name="cts">The <see cref="CancellationTokenSource"/> for cancellation.</param>
        /// <returns>
        /// An <see cref="Task{IOperationResult}" /> object representing the asynchronous operation.
        /// </returns>
        public override Task <IOperationResult> SendWithRetryAsync(IOperation operation,
                                                                   TaskCompletionSource <IOperationResult> tcs = null,
                                                                   CancellationTokenSource cts = null)
        {
            tcs = tcs ?? new TaskCompletionSource <IOperationResult>();
            cts = cts ?? new CancellationTokenSource(OperationLifeSpan);

            //Is the cluster configured for Data services?
            if (!ConfigInfo.IsDataCapable)
            {
                tcs.SetException(
                    new ServiceNotSupportedException("The cluster does not support Data services."));
            }

            try
            {
                var keyMapper = ConfigInfo.GetKeyMapper();
                var vBucket   = (IVBucket)keyMapper.MapKey(operation.Key);
                operation.VBucket = vBucket;

                operation.Completed = CallbackFactory.CompletedFuncWithRetryForCouchbase(
                    this, Pending, ClusterController, tcs, cts.Token);

                Pending.TryAdd(operation.Opaque, operation);

                IServer server;
                var     attempts = 0;
                while ((server = vBucket.LocatePrimary()) == null)
                {
                    if (attempts++ > 10)
                    {
                        throw new TimeoutException("Could not acquire a server.");
                    }
                    Thread.Sleep((int)Math.Pow(2, attempts));
                }
                Log.Debug(m => m("Starting send for {0} with {1}", operation.Opaque, server.EndPoint));
                server.SendAsync(operation).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                tcs.TrySetResult(new OperationResult
                {
                    Exception = e,
                    Status    = ResponseStatus.ClientFailure
                });
            }
            return(tcs.Task);
        }
Esempio n. 4
0
        /// <summary>
        /// Sends a <see cref="IOperation{T}" /> to the Couchbase Server using the Memcached protocol using async/await.
        /// </summary>
        /// <typeparam name="T">The Type of the body of the request.</typeparam>
        /// <param name="operation">The <see cref="IOperation{T}" /> to send.</param>
        /// <param name="tcs">The <see cref="TaskCompletionSource{T}"/> the represents the task to await on.</param>
        /// <param name="cts">The <see cref="CancellationTokenSource"/> for cancellation.</param>
        /// <returns>
        /// An <see cref="Task{IOperationResult}" /> object representing the asynchronous operation.
        /// </returns>
        public override Task <IOperationResult <T> > SendWithRetryAsync <T>(IOperation <T> operation,
                                                                            TaskCompletionSource <IOperationResult <T> > tcs = null,
                                                                            CancellationTokenSource cts = null)
        {
            tcs = tcs ?? new TaskCompletionSource <IOperationResult <T> >();
            cts = cts ?? new CancellationTokenSource(OperationLifeSpan);

            try
            {
                var keyMapper = ConfigInfo.GetKeyMapper();
                var vBucket   = (IVBucket)keyMapper.MapKey(operation.Key);
                operation.VBucket = vBucket;

                operation.Completed = CallbackFactory.CompletedFuncWithRetryForCouchbase(
                    this, Pending, ClusterController, tcs, cts.Token);

                Pending.TryAdd(operation.Opaque, operation);

                IServer server;
                var     attempts = 0;
                while ((server = vBucket.LocatePrimary()) == null)
                {
                    if (attempts++ > 10)
                    {
                        throw new TimeoutException("Could not acquire a server.");
                    }
                    Thread.Sleep((int)Math.Pow(2, attempts));
                }
                server.SendAsync(operation).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                tcs.TrySetResult(new OperationResult <T>
                {
                    Exception = e,
                    Status    = ResponseStatus.ClientFailure
                });
            }
            return(tcs.Task);
        }