internal async override Task SendAsync(IOperation op, CancellationToken token = default, TimeSpan?timeout = null)
            {
                var mockConnection = new Mock <IConnection>();

                mockConnection.SetupGet(x => x.IsDead).Returns(false);
                mockConnection
                .Setup(x => x.SendAsync(It.IsAny <ReadOnlyMemory <byte> >(), It.IsAny <Func <SocketAsyncState, Task> >()))
                .Returns(Task.CompletedTask);

                var clusterNode = new ClusterNode(new ClusterContext())
                {
                    Connection = mockConnection.Object
                };
                await clusterNode.ExecuteOp(op, token, timeout);

                if (_statuses.TryDequeue(out ResponseStatus status))
                {
                    await op.Completed(new SocketAsyncState
                    {
                        Status = status
                    });
                }
                else
                {
                    throw new InvalidOperationException();
                }

                // return Task.CompletedTask;
            }
Exemplo n.º 2
0
        /// <summary>
        /// Asynchrounously executes an operation for a given key.
        /// </summary>
        /// <param name="operation">The <see cref="IOperation{T}" /> being executed.</param>
        /// <param name="connection">The <see cref="IConnection" /> the operation is using.</param>
        /// <returns>
        /// An <see cref="IOperationResult" /> representing the result of operation.
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        /// <remarks>
        /// This overload is used to perform authentication on the connection if it has not already been authenticated.
        /// </remarks>
        public async Task ExecuteAsync(IOperation operation, IConnection connection)
        {
            ExceptionDispatchInfo capturedException = null;

            try
            {
                var request = await operation.WriteAsync().ContinueOnAnyContext();

#pragma warning disable CS4014 // We don't want to wait here - the dedicated read thread will get the result
                connection.SendAsync(request, operation.Completed);
#pragma warning restore CS4014
            }
            catch (Exception e)
            {
                capturedException = ExceptionDispatchInfo.Capture(e);
            }

            if (capturedException != null)
            {
                await operation.Completed(new SocketAsyncState
                {
                    Exception = capturedException.SourceException,
                    Opaque    = operation.Opaque
                }).ContinueOnAnyContext();
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Asynchrounously executes an operation for a given key.
 /// </summary>
 /// <param name="operation">The <see cref="IOperation{T}" /> being executed.</param>
 /// <returns>
 /// An <see cref="IOperationResult" /> representing the result of operation.
 /// </returns>
 /// <remarks>
 /// This overload is used to perform authentication on the connection if it has not already been authenticated.
 /// </remarks>
 public async Task ExecuteAsync(IOperation operation)
 {
     try
     {
         var connection = _connectionPool.Acquire();
         if (!connection.IsAuthenticated)
         {
             lock (_syncObj)
             {
                 Authenticate(connection);
                 EnableEnhancedDurability(connection);
             }
         }
         await ExecuteAsync(operation, connection);
     }
     catch (Exception e)
     {
         Log.Debug(e);
         operation.Completed(new SocketAsyncState
         {
             Exception = e,
             Opaque    = operation.Opaque,
             Status    = (e is SocketException) ?
                         ResponseStatus.TransportFailure :
                         ResponseStatus.ClientFailure
         });
     }
 }
 public async Task ExecuteAsync(IOperation operation)
 {
     try
     {
         if (!_connection.IsAuthenticated)
         {
             lock (_syncObj)
             {
                 Authenticate(_connection);
                 EnableEnhancedDurability(_connection);
             }
         }
         await ExecuteAsync(operation, _connection);
     }
     catch (Exception e)
     {
         Log.DebugFormat("Endpoint: {0} - {1} {2}", EndPoint, _identity, e);
         operation.Completed(new SocketAsyncState
         {
             Exception = e,
             Opaque    = operation.Opaque,
             Status    = (e is SocketException) ?
                         ResponseStatus.TransportFailure :
                         ResponseStatus.ClientFailure
         });
     }
 }
        public async Task ExecuteAsync <T>(IOperation <T> operation, IConnection connection)
        {
            try
            {
                var request = await operation.WriteAsync().ContinueOnAnyContext();

                connection.SendAsync(request, operation.Completed);
            }
            catch (Exception e)
            {
                Log.DebugFormat("Endpoint: {0} - {1} {2}", EndPoint, _identity, e);
                operation.Completed(new SocketAsyncState
                {
                    Exception = e,
                    Opaque    = operation.Opaque,
                    Status    = (e is SocketException) ?
                                ResponseStatus.TransportFailure :
                                ResponseStatus.ClientFailure
                });
            }
            finally
            {
                //need better error handling
                if (connection.IsDead)
                {
                    _connectionPool.Release(connection);
                    _connection = _connectionPool.Acquire();
                }
            }
        }
 async Task HandleException(ExceptionDispatchInfo capturedException, IOperation operation)
 {
     var sourceException = capturedException.SourceException;
     await operation.Completed(new SocketAsyncState
     {
         Exception = sourceException,
         Opaque    = operation.Opaque,
         Status    = (sourceException is SocketException) ?
                     ResponseStatus.TransportFailure :
                     ResponseStatus.ClientFailure
     }).ContinueOnAnyContext();
 }
            public Task Send(IOperation op, TaskCompletionSource <IMemoryOwner <byte> > tcs)
            {
                if (_statuses.TryDequeue(out ResponseStatus status))
                {
                    op.Completed(new SocketAsyncState
                    {
                        Status = status
                    });
                }
                else
                {
                    throw new InvalidOperationException();
                }

                return(Task.CompletedTask);
            }
        /// <summary>
        /// Asynchrounously executes an operation for a given key.
        /// </summary>
        /// <param name="operation">The <see cref="IOperation{T}" /> being executed.</param>
        /// <param name="connection">The <see cref="IConnection" /> the operation is using.</param>
        /// <returns>
        /// An <see cref="IOperationResult" /> representing the result of operation.
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        /// <remarks>
        /// This overload is used to perform authentication on the connection if it has not already been authenticated.
        /// </remarks>
        public async Task ExecuteAsync(IOperation operation, IConnection connection)
        {
            try
            {
                var request = await operation.WriteAsync().ContinueOnAnyContext();

                connection.SendAsync(request, operation.Completed);
            }
            catch (Exception e)
            {
                operation.Completed(new SocketAsyncState
                {
                    Exception = e
                });
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Sends a key/value operation that contains no body to it's mapped server asynchronously.
        /// </summary>
        /// <param name="operation">The <see cref="IOperation" /> to send.</param>
        /// <returns>
        /// A <see cref="Task" /> representing the asynchronous operation.
        /// </returns>
        public async Task SendAsync(IOperation operation)
        {
            if (_isDown)
            {
                var msg = ExceptionUtil.GetNodeUnavailableMsg(EndPoint,
                                                              _clientConfiguration.NodeAvailableCheckInterval);

                operation.Completed(new SocketAsyncState
                {
                    Exception = new ServerException(msg),
                    Opaque    = operation.Opaque,
                    Status    = ResponseStatus.NodeUnavailable
                });
                return;
            }
            await _ioStrategy.ExecuteAsync(operation);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Sends a key/value operation that contains no body to it's mapped server asynchronously.
        /// </summary>
        /// <param name="operation">The <see cref="IOperation" /> to send.</param>
        /// <returns>
        /// A <see cref="Task" /> representing the asynchronous operation.
        /// </returns>
        public async Task SendAsync(IOperation operation)
        {
            operation.CurrentHost = EndPoint;
            if (_isDown)
            {
                var msg = ExceptionUtil.GetNodeUnavailableMsg(EndPoint,
                                                              _clientConfiguration.NodeAvailableCheckInterval);

                await operation.Completed(new SocketAsyncState
                {
                    Exception = new NodeUnavailableException(msg),
                    Opaque    = operation.Opaque,
                    Status    = ResponseStatus.NodeUnavailable
                }).ContinueOnAnyContext();

                return;
            }
            await _ioService.ExecuteAsync(operation).ContinueOnAnyContext();
        }
Exemplo n.º 11
0
        /// <summary>
        /// Asynchrounously executes an operation for a given key.
        /// </summary>
        /// <param name="operation">The <see cref="IOperation{T}" /> being executed.</param>
        /// <param name="connection">The <see cref="IConnection" /> the operation is using.</param>
        /// <returns>
        /// An <see cref="IOperationResult" /> representing the result of operation.
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        /// <remarks>
        /// This overload is used to perform authentication on the connection if it has not already been authenticated.
        /// </remarks>
        public async Task ExecuteAsync(IOperation operation, IConnection connection)
        {
            try
            {
                var request = await operation.WriteAsync().ContinueOnAnyContext();

                connection.SendAsync(request, operation.Completed);
            }
            catch (Exception e)
            {
                Log.Debug(e);
                operation.Completed(new SocketAsyncState
                {
                    Exception = e,
                    Opaque    = operation.Opaque,
                    Status    = (e is SocketException) ?
                                ResponseStatus.TransportFailure :
                                ResponseStatus.ClientFailure
                });
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Handles the exception.
        /// </summary>
        /// <param name="capturedException">The captured exception.</param>
        /// <param name="operation">The operation.</param>
        /// <returns></returns>
        protected static async Task HandleException(ExceptionDispatchInfo capturedException, IOperation operation)
        {
            var sourceException = capturedException.SourceException;
            var status          = ResponseStatus.ClientFailure;

            if (sourceException is SocketException)
            {
                status = ResponseStatus.TransportFailure;
            }
            else if (sourceException is AuthenticationException)
            {
                status = ResponseStatus.AuthenticationError;
            }

            await operation.Completed(new SocketAsyncState
            {
                Exception = sourceException,
                Opaque    = operation.Opaque,
                Status    = status
            }).ContinueOnAnyContext();
        }
Exemplo n.º 13
0
        /// <summary>
        /// Asynchrounously executes an operation for a given key.
        /// </summary>
        /// <param name="operation">The <see cref="IOperation{T}" /> being executed.</param>
        /// <param name="connection">The <see cref="IConnection" /> the operation is using.</param>
        /// <returns>
        /// An <see cref="IOperationResult" /> representing the result of operation.
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        /// <remarks>
        /// This overload is used to perform authentication on the connection if it has not already been authenticated.
        /// </remarks>
        public async Task ExecuteAsync(IOperation operation, IConnection connection)
        {
            ExceptionDispatchInfo capturedException = null;

            try
            {
                var request = await operation.WriteAsync().ContinueOnAnyContext();

                connection.SendAsync(request, operation.Completed);
            }
            catch (Exception e)
            {
                capturedException = ExceptionDispatchInfo.Capture(e);
            }

            if (capturedException != null)
            {
                await operation.Completed(new SocketAsyncState
                {
                    Exception = capturedException.SourceException,
                    Opaque    = operation.Opaque
                }).ContinueOnAnyContext();
            }
        }