public void Add(AsyncCommand command, int timeout)
        {
            queue.Enqueue(command);

            if (timeout < sleepInterval)
            {
                sleepInterval = timeout;
                thread.Interrupt();
            }
        }
 public AsyncCommand(AsyncCommand other)
 {
     // Retry constructor.
     this.sequence            = other.sequence;
     this.cluster             = other.cluster;
     this.policy              = other.policy;
     this.partition           = other.partition;
     this.node                = other.node;
     this.eventArgs           = other.eventArgs;
     this.eventArgs.UserToken = this;
     this.segmentOrig         = other.segmentOrig;
     this.segment             = other.segment;
     this.watch               = other.watch;
     this.iteration           = other.iteration + 1;
     this.commandSentCounter  = other.commandSentCounter;
     this.isRead              = other.isRead;
     this.usingSocketTimeout  = other.usingSocketTimeout;
 }
Ejemplo n.º 3
0
        private void Retry(AerospikeException ae)
        {
            // Prepare for retry.
            if (!PrepareRetry(ae.Result != ResultCode.SERVER_NOT_AVAILABLE))
            {
                try
                {
                    // Batch may be retried in separate commands.
                    if (RetryBatch())
                    {
                        // Batch was retried in separate commands.  Complete this command.
                        return;
                    }
                }
                catch (Exception e)
                {
                    NotifyFailure(new AerospikeException("Batch split retry failed", e));
                    return;
                }
            }

            AsyncCommand command = CloneCommand();

            if (command != null)
            {
                // Command should only be added to AsyncTimeoutQueue once.
                // CheckTimeout() will verify both socketTimeout and totalTimeout.
                if (socketTimeout > 0)
                {
                    command.socketWatch = Stopwatch.StartNew();
                    AsyncTimeoutQueue.Instance.Add(command, socketTimeout);
                }
                else if (totalTimeout > 0)
                {
                    int remain = (int)(totalTimeout - totalWatch.ElapsedMilliseconds);
                    AsyncTimeoutQueue.Instance.Add(command, remain);
                }
                command.ExecuteCommand();
            }
            else
            {
                FailCommand(ae);
            }
        }
Ejemplo n.º 4
0
        protected internal virtual void Retry(AerospikeException ae)
        {
            // Prepare for retry.
            AsyncCommand command = CloneCommand();

            if (command != null)
            {
                if (policy.totalTimeout > 0)
                {
                    AsyncTimeoutQueue.Instance.Add(command, policy.totalTimeout);
                }
                else if (policy.socketTimeout > 0)
                {
                    command.watch = Stopwatch.StartNew();
                    AsyncTimeoutQueue.Instance.Add(command, policy.socketTimeout);
                }
                command.ExecuteCommand();
            }
            else
            {
                FailCommand(ae);
            }
        }
        private void Retry(AerospikeException ae)
        {
            // Prepare for retry.
            AsyncCommand command = CloneCommand();

            if (command != null)
            {
                if (policy.totalTimeout > 0)
                {
                    AsyncTimeoutQueue.Instance.Add(command, policy.totalTimeout);
                }
                else if (policy.socketTimeout > 0)
                {
                    command.watch = Stopwatch.StartNew();
                    AsyncTimeoutQueue.Instance.Add(command, policy.socketTimeout);
                }
                command.ExecuteCommand();
            }
            else
            {
                PutBackArgsOnError();
                NotifyFailure(ae);
            }
        }
Ejemplo n.º 6
0
            private void HandleSocketEvent(object sender, SocketAsyncEventArgs args)
            {
                AsyncCommand command = args.UserToken as AsyncCommand;

                if (args.SocketError != SocketError.Success)
                {
                    command.ConnectionFailed(command.GetAerospikeException(args.SocketError));
                    return;
                }

                try
                {
                    switch (args.LastOperation)
                    {
                    case SocketAsyncOperation.Receive:
                        command.ReceiveEvent();
                        break;

                    case SocketAsyncOperation.Send:
                        command.SendEvent();
                        break;

                    case SocketAsyncOperation.Connect:
                        command.ConnectionCreated();
                        break;

                    default:
                        command.FailOnApplicationError(new AerospikeException("Invalid socket operation: " + args.LastOperation));
                        break;
                    }
                }
                catch (AerospikeException.Connection ac)
                {
                    command.ConnectionFailed(ac);
                }
                catch (AerospikeException ae)
                {
                    // Fail without retry on non-network errors.
                    if (ae.Result == ResultCode.TIMEOUT)
                    {
                        // Create server timeout exception.
                        ae = new AerospikeException.Timeout(command.policy, false);
                    }
                    command.FailOnApplicationError(ae);
                }
                catch (SocketException se)
                {
                    command.ConnectionFailed(command.GetAerospikeException(se.SocketErrorCode));
                }
                catch (ObjectDisposedException ode)
                {
                    // This exception occurs because socket is being used after timeout thread closes socket.
                    // Retry when this happens.
                    command.ConnectionFailed(new AerospikeException(ode));
                }
                catch (Exception e)
                {
                    // Fail without retry on unknown errors.
                    command.FailOnApplicationError(new AerospikeException(e));
                }
            }
Ejemplo n.º 7
0
 public void ScheduleCommandExecution(AsyncCommand command)
 {
     commandQueue.ScheduleCommand(command);
 }
Ejemplo n.º 8
0
 public void SetBatchRetry(AsyncCommand other)
 {
     // This batch retry command will be added to the timeout queue in ExecuteCore().
     this.iteration  = other.iteration;
     this.totalWatch = other.totalWatch;
 }
 // Schedules a command for later execution.
 public override bool ScheduleCommand(AsyncCommand command)
 {
     command.ExecuteInline(_argsQueue.Take());
     return(true);
 }
 // Schedules a command for later execution, and returns a boolean indicating if the command has been executed or is actually going be executed.
 public abstract bool ScheduleCommand(AsyncCommand command);
Ejemplo n.º 11
0
 // Schedules a command for later execution.
 // Throws <see cref="AerospikeException.CommandRejected"/> if command is rejected.
 public abstract void ScheduleCommand(AsyncCommand command);
 // Schedules a command for later execution.
 public override void ScheduleCommand(AsyncCommand command)
 {
     // Block until EventArgs becomes available.
     command.ExecuteInline(argsQueue.Take());
 }
        private void RetryAfterInit(AerospikeException ae)
        {
            if (iterations < policy.maxRetries && (policy.retryOnTimeout || watch == null || watch.ElapsedMilliseconds < policy.timeout))
            {
                int status = Interlocked.CompareExchange(ref state, RETRY, IN_PROGRESS);

                if (status == IN_PROGRESS)
                {
                    // Prepare for retry.
                    AsyncCommand command = CloneCommand();

                    if (command != null)
                    {
                        CloseConnection();

                        if (policy.timeout > 0)
                        {
                            if (policy.retryOnTimeout)
                            {
                                command.watch = Stopwatch.StartNew();
                            }
                            else
                            {
                                command.watch = this.watch;
                            }
                            AsyncTimeoutQueue.Instance.Add(command, policy.timeout);
                        }

                        try
                        {
                            command.ExecuteCommand();
                        }
                        catch (Exception)
                        {
                            // Command has already been cleaned up.
                            // Notify user of original exception.
                            OnFailure(ae);
                        }
                    }
                    else
                    {
                        CloseOnError();
                        OnFailure(ae);
                    }
                }
                else
                {
                    AlreadyCompleted(status);
                }
            }
            else
            {
                int status = Interlocked.CompareExchange(ref state, FAIL_NETWORK_ERROR, IN_PROGRESS);

                if (status == IN_PROGRESS)
                {
                    CloseOnError();
                    OnFailure(ae);
                }
                else
                {
                    AlreadyCompleted(status);
                }
            }
        }