public IFuture <IClientMessage> InvokeOnConnection(IClientMessage request, ClientConnection connection, bool bypassHeartbeat = false)
        {
            var clientInvocation = new ClientInvocation(request, connection);

            Send(connection, clientInvocation, bypassHeartbeat);
            return(clientInvocation.Future);
        }
Beispiel #2
0
        private bool ShouldRetryInvocation(ClientInvocation invocation, Exception exception)
        {
            if (invocation.BoundConnection != null)
            {
                return(false);
            }
            if (Clock.CurrentTimeMillis() >= invocation.InvocationTimeMillis + _invocationTimeoutMillis)
            {
                return(false);
            }

            if (invocation.Address != null && exception is TargetNotMemberException &&
                _client.GetClientClusterService().GetMember(invocation.Address) == null)
            {
                //when invocation send over address
                //if exception is target not member and
                //address is not available in member list , don't retry
                return(false);
            }

            //validate exception
            return(exception is IOException ||
                   exception is SocketException ||
                   exception is HazelcastInstanceNotActiveException ||
                   exception is AuthenticationException
                   // above exceptions OR retryable exception case as below
                   || exception is RetryableHazelcastException && (_redoOperations || invocation.Message.IsRetryable()));
        }
        public IFuture <IClientMessage> InvokeOnConnection(IClientMessage request, ClientConnection connection)
        {
            var clientInvocation = new ClientInvocation(request, connection);

            Send(connection, clientInvocation);
            return(clientInvocation.Future);
        }
Beispiel #4
0
        private bool RetryRequest(ClientInvocation invocation, Exception exception)
        {
            var retryNr = invocation.IncrementAndGetRetryCount();

            if (retryNr > _retryCount)
            {
                return(false);
            }

            Logger.Finest("Retry #" + retryNr + " for request " + invocation.Message);

            _client.GetClientExecutionService().SubmitWithDelay(() =>
            {
                if (_isShutDown)
                {
                    FailRequestDueToShutdown(invocation);
                }
                else
                {
                    // get the appropriate connection for retry
                    var connection = GetConnectionForInvocation(invocation);
                    Send(connection, invocation);
                }
            }, _retryWaitTime).ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    var innerException = t.Exception.InnerExceptions.First();
                    Logger.Finest("Retry of request " + invocation.Message + " failed with ", innerException);
                    HandleException(invocation, innerException);
                }
            });
            return(true);
        }
        protected virtual void Send(ClientConnection connection, ClientInvocation clientInvocation)
        {
            var correlationId = NextCorrelationId();

            clientInvocation.Message.SetCorrelationId(correlationId);
            clientInvocation.Message.AddFlag(ClientMessage.BeginAndEndFlags);
            clientInvocation.SentConnection = connection;
            if (clientInvocation.PartitionId != -1)
            {
                clientInvocation.Message.SetPartitionId(clientInvocation.PartitionId);
            }
            if (clientInvocation.MemberUuid != null && clientInvocation.MemberUuid != connection.GetMember().GetUuid())
            {
                HandleException(clientInvocation,
                                new TargetNotMemberException(
                                    "The member UUID on the invocation doesn't match the member UUID on the connection."));
            }

            if (_isShutDown)
            {
                FailRequestDueToShutdown(clientInvocation);
            }
            else
            {
                RegisterInvocation(correlationId, clientInvocation);

                //enqueue to write queue
                if (!connection.WriteAsync((ISocketWritable)clientInvocation.Message))
                {
                    UnregisterCall(correlationId);
                    RemoveEventHandler(correlationId);
                    FailRequest(connection, clientInvocation);
                }
            }
        }
Beispiel #6
0
        private ClientConnection GetConnectionForInvocation(ClientInvocation invocation)
        {
            if (invocation.BoundConnection != null)
            {
                if (!invocation.BoundConnection.Live)
                {
                    throw new HazelcastException(invocation.BoundConnection + " is no longer available");
                }
                return(invocation.BoundConnection);
            }

            Address address = null;

            if (invocation.Address != null)
            {
                address = invocation.Address;
            }
            else if (invocation.MemberUuid != null)
            {
                var member = _client.GetClientClusterService().GetMember(invocation.MemberUuid);
                if (member == null)
                {
                    throw new InvalidOperationException("Could not find a member with UUID " + invocation.MemberUuid);
                }
                address = member.GetAddress();
            }

            else if (invocation.PartitionId != -1)
            {
                address = _client.GetClientPartitionService().GetPartitionOwner(invocation.PartitionId);
            }
            return(GetConnection(address));
        }
Beispiel #7
0
        private IFuture <IClientMessage> SendToOwner(ClientInvocation invocation)
        {
            var clusterService = Client.GetClientClusterService();
            var address        = clusterService.GetOwnerConnectionAddress();

            return(Invoke(invocation, address));
        }
        private void HandleException(ClientInvocation invocation, Exception exception)
        {
            Logger.Finest("Got exception for request " + invocation.Message + ":" + exception);

            if (exception is IOException ||
                exception is SocketException ||
                exception is HazelcastInstanceNotActiveException ||
                exception is AuthenticationException)
            {
                if (RetryRequest(invocation))
                {
                    return;
                }
            }

            if (exception is RetryableHazelcastException)
            {
                if (_redoOperations || invocation.Message.IsRetryable())
                {
                    if (RetryRequest(invocation))
                    {
                        return;
                    }
                }
            }
            invocation.Future.Exception = exception;
        }
Beispiel #9
0
        public IFuture <IClientMessage> InvokeOnConnection(IClientMessage request, ClientConnection connection)
        {
            var clientInvocation = new ClientInvocation(request, connection);

            InvokeInternal(clientInvocation, null, connection);
            return(clientInvocation.Future);
        }
        public IFuture <IClientMessage> InvokeListenerOnConnection(IClientMessage request,
                                                                   DistributedEventHandler eventHandler, ClientConnection connection)
        {
            var clientInvocation = new ClientInvocation(request, connection, eventHandler);

            InvokeInternal(clientInvocation, null, connection);
            return(clientInvocation.Future);
        }
Beispiel #11
0
        private void InvokeInternal(ClientInvocation invocation, Address address = null, ClientConnection connection = null)
        {
            try
            {
                if (connection == null)
                {
                    if (address == null)
                    {
                        address = GetRandomAddress();
                    }
                    connection = GetConnection(address);
                    if (connection == null)
                    {
                        if (address != null && _client.GetClientClusterService().GetMember(address) == null)
                        {
                            throw new TargetNotMemberException(string.Format("Target {0} is not a member.", address));
                        }

                        //Create an async connection and send the invocation afterward.
                        _clientConnectionManager.GetOrConnectAsync(address).ContinueWith(t =>
                        {
                            if (t.IsFaulted)
                            {
                                HandleInvocationException(invocation, t.Exception.Flatten().InnerExceptions.First());
                            }
                            else
                            {
                                InvokeInternal(invocation, address, t.Result);
                            }
                        })
                        .ContinueWith(t =>
                        {
                            HandleInvocationException(invocation, t.Exception.Flatten().InnerExceptions.First());
                        }, TaskContinuationOptions.OnlyOnFaulted |
                                      TaskContinuationOptions.ExecuteSynchronously);
                        return;
                    }
                }
                //Sending Invocation via connection
                UpdateInvocation(invocation, connection);
                ValidateInvocation(invocation, connection);

                if (!TrySend(invocation, connection))
                {
                    //Sending failed.
                    if (_client.GetConnectionManager().Live)
                    {
                        throw new TargetDisconnectedException(connection.GetAddress(), "Error writing to socket.");
                    }
                    throw new HazelcastException("Client is shut down.");
                }
                //Successfully sent.
            }
            catch (Exception e)
            {
                HandleInvocationException(invocation, e);
            }
        }
        private void RegisterInvocation(long correlationId, ClientInvocation request)
        {
            _invocations.TryAdd(correlationId, request);

            var listenerInvocation = request as ClientListenerInvocation;

            if (listenerInvocation != null)
            {
                _listenerInvocations.TryAdd(correlationId, listenerInvocation);
            }
        }
 private void FailRequest(ClientConnection connection, ClientInvocation invocation)
 {
     if (_client.GetConnectionManager().Live)
     {
         HandleException(invocation,
                         new TargetDisconnectedException(connection.GetAddress()));
     }
     else
     {
         FailRequestDueToShutdown(invocation);
     }
 }
Beispiel #14
0
        private void UpdateInvocation(ClientInvocation clientInvocation, ClientConnection connection)
        {
            var correlationId = NextCorrelationId();

            clientInvocation.Message.SetCorrelationId(correlationId);
            clientInvocation.Message.AddFlag(ClientMessage.BeginAndEndFlags);
            clientInvocation.SentConnection = connection;
            if (clientInvocation.PartitionId != -1)
            {
                clientInvocation.Message.SetPartitionId(clientInvocation.PartitionId);
            }
        }
Beispiel #15
0
 private void ValidateInvocation(ClientInvocation clientInvocation, ClientConnection connection)
 {
     if (clientInvocation.MemberUuid != null && clientInvocation.MemberUuid != connection.Member.GetUuid())
     {
         throw new TargetNotMemberException(
                   "The member UUID on the invocation doesn't match the member UUID on the connection.");
     }
     if (_isShutDown)
     {
         throw new HazelcastException("Client is shut down.");
     }
 }
Beispiel #16
0
 protected IFuture <IClientMessage> Invoke(ClientInvocation invocation, Address address = null)
 {
     try
     {
         var connection = GetConnection(address);
         Send(connection, invocation);
     }
     catch (Exception e)
     {
         HandleException(invocation, e);
     }
     return(invocation.Future);
 }
 private void HandleInvocationException(ClientInvocation invocation, Exception exception)
 {
     try
     {
         //Should it retry?
         if (ShouldRetryInvocation(invocation, exception))
         {
             try
             {
                 _client.GetClientExecutionService()
                 .Schedule(() =>
                 {
                     var address = GetNewInvocationAddress(invocation);
                     InvokeInternal(invocation, address);
                 }, RetryWaitTime, TimeUnit.Milliseconds)
                 .ContinueWith(t =>
                 {
                     HandleInvocationException(invocation, t.Exception.Flatten().InnerExceptions.First());
                 }, TaskContinuationOptions.OnlyOnFaulted |
                               TaskContinuationOptions.ExecuteSynchronously);
                 return;
             }
             catch (Exception e)
             {
                 HandleInvocationException(invocation, e);
             }
         }
         //Fail with exception
         try
         {
             invocation.Future.Exception = exception;
         }
         catch (InvalidOperationException e)
         {
             if (Logger.IsFinestEnabled())
             {
                 Logger.Finest("Invocation already completed:", e);
             }
         }
     }
     catch (Exception ex)
     {
         if (Logger.IsFinestEnabled())
         {
             Logger.Finest("HandleInvocationException missed an exception:", ex);
         }
         throw ex;
     }
 }
 private void FailRequestDueToShutdown(ClientInvocation invocation)
 {
     if (Logger.IsFinestEnabled())
     {
         var connectionId = invocation.SentConnection == null
             ? "unknown"
             : invocation.SentConnection.Id.ToString();
         Logger.Finest("Aborting request on connection " + connectionId + ": " + invocation.Message +
                       " due to shutdown.");
     }
     if (!invocation.Future.IsComplete)
     {
         invocation.Future.Exception = new HazelcastException("Client is shutting down.");
     }
 }
        private bool TryRegisterInvocation(long correlationId, ClientInvocation request)
        {
            if (!_invocations.TryAdd(correlationId, request))
            {
                return(false);
            }

            if (request.EventHandler != null &&
                !_clientListenerService.AddEventHandler(correlationId, request.EventHandler))
            {
                _invocations.TryRemove(correlationId, out request);
                return(false);
            }
            return(true);
        }
Beispiel #20
0
        private bool TryRegisterInvocation(long correlationId, ClientInvocation request)
        {
            if (!_invocations.TryAdd(correlationId, request))
            {
                return(false);
            }
            var listenerInvocation = request as ClientListenerInvocation;

            if (listenerInvocation != null)
            {
                if (!_listenerInvocations.TryAdd(correlationId, listenerInvocation))
                {
                    _invocations.TryRemove(correlationId, out request);
                }
            }
            return(true);
        }
        protected IFuture <IClientMessage> Invoke(ClientInvocation invocation, Address address = null)
        {
            try
            {
                if (address == null)
                {
                    address = GetRandomAddress();
                }

                // try to get an existing connection, if not establish a new connection asyncronously
                var connection = GetConnection(address);
                if (connection != null)
                {
                    Send(connection, invocation);
                }
                else
                {
                    _clientConnectionManager.GetOrConnectAsync(address).ContinueWith(t =>
                    {
                        if (t.IsFaulted)
                        {
                            var innerException = t.Exception.InnerExceptions.First();
                            HandleException(invocation, innerException);
                        }
                        else
                        {
                            try
                            {
                                Send(t.Result, invocation);
                            }
                            catch (Exception e)
                            {
                                HandleException(invocation, e);
                            }
                        }
                    });
                }
            }
            catch (Exception e)
            {
                HandleException(invocation, e);
            }
            return(invocation.Future);
        }
        protected virtual void Send(ClientConnection connection, ClientInvocation clientInvocation, bool bypassHeartbeat = false)
        {
            var correlationId = NextCorrelationId();

            clientInvocation.Message.SetCorrelationId(correlationId);
            clientInvocation.Message.AddFlag(ClientMessage.BeginAndEndFlags);
            clientInvocation.SentConnection = connection;
            if (clientInvocation.PartitionId != -1)
            {
                clientInvocation.Message.SetPartitionId(clientInvocation.PartitionId);
            }
            if (clientInvocation.MemberUuid != null && clientInvocation.MemberUuid != connection.Member.GetUuid())
            {
                HandleException(clientInvocation,
                                new TargetNotMemberException(
                                    "The member UUID on the invocation doesn't match the member UUID on the connection."));
                return;
            }
            if (!connection.IsHeartBeating && !bypassHeartbeat)
            {
                HandleException(clientInvocation, new TargetDisconnectedException(connection.GetAddress() + " has stopped heartbeating."));
            }
            else if (_isShutDown)
            {
                FailRequestDueToShutdown(clientInvocation);
            }
            else
            {
                RegisterInvocation(correlationId, clientInvocation);

                //enqueue to write queue
                if (!connection.WriteAsync((ISocketWritable)clientInvocation.Message))
                {
                    if (Logger.IsFinestEnabled())
                    {
                        Logger.Finest("Unable to write request " + clientInvocation.Message + " to connection " +
                                      connection);
                    }
                    UnregisterCall(correlationId);
                    RemoveEventHandler(correlationId);
                    FailRequest(connection, clientInvocation, "Error writing to socket.");
                }
            }
        }
Beispiel #23
0
        private void ValidateInvocation(ClientInvocation clientInvocation, ClientConnection connection,
                                        bool bypassHeartbeat)
        {
            if (clientInvocation.MemberUuid != null && clientInvocation.MemberUuid != connection.Member.GetUuid())
            {
                throw new TargetNotMemberException(
                          "The member UUID on the invocation doesn't match the member UUID on the connection.");
            }

            if (!connection.IsHeartBeating && !bypassHeartbeat)
            {
                throw new TargetDisconnectedException(connection.GetAddress() + " has stopped heartbeating.");
            }

            if (_isShutDown)
            {
                throw new HazelcastException("Client is shut down.");
            }
        }
Beispiel #24
0
        private bool ShouldRetryInvocation(ClientInvocation invocation, Exception exception)
        {
            if (invocation.BoundConnection != null)
            {
                return(false);
            }
            if (Clock.CurrentTimeMillis() >= invocation.InvocationTimeMillis + _invocationTimeoutMillis)
            {
                return(false);
            }

            //validate exception
            return(exception is IOException ||
                   exception is SocketException ||
                   exception is HazelcastInstanceNotActiveException ||
                   exception is AuthenticationException
                   // above exceptions OR retryable excaption case as below
                   || exception is RetryableHazelcastException && (_redoOperations || invocation.Message.IsRetryable()));
        }
        private void ReinvokeInvocation(ClientInvocation invocation)
        {
            Address address = null;

            if (invocation.Address != null)
            {
                address = invocation.Address;
            }
            else if (invocation.MemberUuid != null)
            {
                var member = _client.GetClientClusterService().GetMember(invocation.MemberUuid);
                if (member == null)
                {
                    throw new InvalidOperationException("Could not find a member with UUID " + invocation.MemberUuid);
                }
                address = member.GetAddress();
            }
            else if (invocation.PartitionId != -1)
            {
                address = _client.GetClientPartitionService().GetPartitionOwner(invocation.PartitionId);
            }
            Invoke(invocation, address);
        }
        private bool RetryRequest(ClientInvocation invocation)
        {
            if (invocation.BoundConnection != null)
            {
                return(false);
            }

            if (Clock.CurrentTimeMillis() >= (invocation.InvocationTimeMillis + _invocationTimeoutMillis))
            {
                return(false);
            }

            Logger.Finest("Retry for request " + invocation.Message);

            _client.GetClientExecutionService().Schedule(() =>
            {
                if (_isShutDown)
                {
                    FailRequestDueToShutdown(invocation);
                }
                else
                {
                    // get the appropriate connection for retry
                    ReinvokeInvocation(invocation);
                }
            }, RetryWaitTime, TimeUnit.Milliseconds).ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    var innerException = t.Exception.InnerExceptions.First();
                    Logger.Finest("Retry of request " + invocation.Message + " failed with ", innerException);
                    HandleException(invocation, innerException);
                }
            });
            return(true);
        }
Beispiel #27
0
        private Address GetNewInvocationAddress(ClientInvocation invocation)
        {
            Address newAddress = null;

            if (invocation.Address != null)
            {
                newAddress = invocation.Address;
            }
            else if (invocation.MemberUuid != null)
            {
                var member = _client.GetClientClusterService().GetMember(invocation.MemberUuid);
                if (member == null)
                {
                    Logger.Finest("Could not find a member with UUID " + invocation.MemberUuid);
                    throw new InvalidOperationException("Could not find a member with UUID " + invocation.MemberUuid);
                }
                newAddress = member.GetAddress();
            }
            else if (invocation.PartitionId != -1)
            {
                newAddress = _client.GetClientPartitionService().GetPartitionOwner(invocation.PartitionId);
            }
            return(newAddress);
        }
Beispiel #28
0
        private bool TrySend(ClientInvocation clientInvocation, ClientConnection connection)
        {
            var correlationId = clientInvocation.Message.GetCorrelationId();

            if (!TryRegisterInvocation(correlationId, clientInvocation))
            {
                return(false);
            }

            //enqueue to write queue
            if (connection.WriteAsync((ISocketWritable)clientInvocation.Message))
            {
                return(true);
            }

            //Rollback sending failed
            if (Logger.IsFinestEnabled())
            {
                Logger.Finest("Unable to write request " + clientInvocation.Message + " to connection " + connection);
            }
            UnregisterInvocation(correlationId);
            RemoveEventHandler(correlationId);
            return(false);
        }
        private void ValidateInvocation(ClientInvocation clientInvocation, ClientConnection connection,
            bool bypassHeartbeat)
        {
            if (clientInvocation.MemberUuid != null && clientInvocation.MemberUuid != connection.Member.GetUuid())
            {
                throw new TargetNotMemberException(
                    "The member UUID on the invocation doesn't match the member UUID on the connection.");
            }

            if (!connection.IsHeartBeating && !bypassHeartbeat)
            {
                throw new TargetDisconnectedException(connection.GetAddress() + " has stopped heartbeating.");
            }

            if (_isShutDown)
            {
                throw new HazelcastException("Client is shut down.");
            }
        }
 public IFuture<IClientMessage> InvokeOnConnection(IClientMessage request, ClientConnection connection)
 {
     var clientInvocation = new ClientInvocation(request, connection);
     Send(connection, clientInvocation);
     return clientInvocation.Future;
 }
        protected IFuture<IClientMessage> Invoke(ClientInvocation invocation, Address address = null)
        {
            try
            {
                if (address == null)
                {
                    address = GetRandomAddress();
                }

                // try to get an existing connection, if not establish a new connection asyncronously
                var connection = GetConnection(address);
                if (connection != null)
                {
                    Send(connection, invocation);
                }
                else
                {
                    _clientConnectionManager.GetOrConnectAsync(address).ContinueWith(t =>
                    {
                        if (t.IsFaulted)
                        {
                            var innerException = t.Exception.InnerExceptions.First();
                            HandleException(invocation, innerException);
                        }
                        else
                        {
                            try
                            {
                                Send(t.Result, invocation);
                            }
                            catch (Exception e)
                            {
                                HandleException(invocation, e);
                            }
                        }
                    });
                }
            }
            catch (Exception e)
            {
                HandleException(invocation, e);
            }
            return invocation.Future;
        }
 private IFuture<IClientMessage> SendToOwner(ClientInvocation invocation)
 {
     var clusterService = Client.GetClientClusterService();
     var address = clusterService.GetOwnerConnectionAddress();
     return Invoke(invocation, address);
 }
 private Address GetNewInvocationAddress(ClientInvocation invocation)
 {
     Address newAddress = null;
     if (invocation.Address != null)
     {
         newAddress = invocation.Address;
     }
     else if (invocation.MemberUuid != null)
     {
         var member = _client.GetClientClusterService().GetMember(invocation.MemberUuid);
         if (member == null)
         {
             Logger.Finest("Could not find a member with UUID " + invocation.MemberUuid);
             throw new InvalidOperationException("Could not find a member with UUID " + invocation.MemberUuid);
         }
         newAddress = member.GetAddress();
     }
     else if (invocation.PartitionId != -1)
     {
         newAddress = _client.GetClientPartitionService().GetPartitionOwner(invocation.PartitionId);
     }
     return newAddress;
 }
        private void InvokeInternal(ClientInvocation invocation, Address address = null,
            ClientConnection connection = null, bool bypassHeartbeat = false)
        {
            try
            {
                if (connection == null)
                {
                    if (address == null)
                    {
                        address = GetRandomAddress();
                    }
                    connection = GetConnection(address);
                    if (connection == null)
                    {
                        //Create an async conneciion and send the invocation afterward.
                        _clientConnectionManager.GetOrConnectAsync(address).ContinueWith(t =>
                            {
                                if (t.IsFaulted)
                                {
                                    HandleInvocationException(invocation, t.Exception.Flatten().InnerExceptions.First());
                                }
                                else
                                {
                                    InvokeInternal(invocation, address, t.Result);
                                }
                            })
                            .ContinueWith(t =>
                                {
                                    HandleInvocationException(invocation, t.Exception.Flatten().InnerExceptions.First());
                                }, TaskContinuationOptions.OnlyOnFaulted |
                                   TaskContinuationOptions.ExecuteSynchronously);
                        return;
                    }
                }
                //Sending Invocation via connection
                UpdateInvocation(invocation, connection);
                ValidateInvocation(invocation, connection, bypassHeartbeat);

                if (!TrySend(invocation, connection))
                {
                    //Sending failed.
                    if (_client.GetConnectionManager().Live)
                    {
                        throw new TargetDisconnectedException(connection.GetAddress(), "Error writing to socket.");
                    }
                    throw new HazelcastException("Client is shut down.");
                }
                //Successfully sended.
            }
            catch (Exception e)
            {
                HandleInvocationException(invocation, e);
            }
        }
 private bool TryRegisterInvocation(long correlationId, ClientInvocation request)
 {
     if (!_invocations.TryAdd(correlationId, request)) return false;
     var listenerInvocation = request as ClientListenerInvocation;
     if (listenerInvocation != null)
     {
         if (!_listenerInvocations.TryAdd(correlationId, listenerInvocation))
         {
             _invocations.TryRemove(correlationId, out request);
         }
     }
     return true;
 }
        private void HandleException(ClientInvocation invocation, Exception exception)
        {
            Logger.Finest("Got exception for request " + invocation.Message + ":" + exception);

            if (exception is IOException
                || exception is SocketException
                || exception is HazelcastInstanceNotActiveException
                || exception is AuthenticationException)
            {
                if (RetryRequest(invocation)) return;
            }

            if (exception is RetryableHazelcastException)
            {
                if (_redoOperations || invocation.Message.IsRetryable())
                {
                    if (RetryRequest(invocation))
                        return;
                }
            }
            invocation.Future.Exception = exception;
        }
Beispiel #37
0
 protected IFuture <IClientMessage> Invoke(ClientInvocation invocation, Address address = null)
 {
     InvokeInternal(invocation, address);
     return(invocation.Future);
 }
        private void RegisterInvocation(long correlationId, ClientInvocation request)
        {
            _invocations.TryAdd(correlationId, request);

            var listenerInvocation = request as ClientListenerInvocation;
            if (listenerInvocation != null)
            {
                _listenerInvocations.TryAdd(correlationId, listenerInvocation);
            }
        }
 private void ReinvokeInvocation(ClientInvocation invocation)
 {
     Address address = null;
     if (invocation.Address != null)
     {
         address = invocation.Address;
     }
     else if (invocation.MemberUuid != null)
     {
         var member = _client.GetClientClusterService().GetMember(invocation.MemberUuid);
         if (member == null)
         {
             throw new InvalidOperationException("Could not find a member with UUID " + invocation.MemberUuid);
         }
         address = member.GetAddress();
     }
     else if (invocation.PartitionId != -1)
     {
         address = _client.GetClientPartitionService().GetPartitionOwner(invocation.PartitionId);
     }
     Invoke(invocation, address);
 }
        private bool RetryRequest(ClientInvocation invocation)
        {
            if (invocation.BoundConnection != null)
            {
                return false;
            }

            if (Clock.CurrentTimeMillis() >= (invocation.InvocationTimeMillis + _invocationTimeoutMillis))
            {
                return false;
            }

            Logger.Finest("Retry for request " + invocation.Message);

            _client.GetClientExecutionService().Schedule(() =>
            {
                if (_isShutDown) FailRequestDueToShutdown(invocation);
                else
                {
                    // get the appropriate connection for retry
                    ReinvokeInvocation(invocation);
                }
            }, RetryWaitTime, TimeUnit.Milliseconds).ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    var innerException = t.Exception.InnerExceptions.First();
                    Logger.Finest("Retry of request " + invocation.Message + " failed with ", innerException);
                    HandleException(invocation, innerException);
                }
            });
            return true;
        }
 private void UpdateInvocation(ClientInvocation clientInvocation, ClientConnection connection)
 {
     var correlationId = NextCorrelationId();
     clientInvocation.Message.SetCorrelationId(correlationId);
     clientInvocation.Message.AddFlag(ClientMessage.BeginAndEndFlags);
     clientInvocation.SentConnection = connection;
     if (clientInvocation.PartitionId != -1)
     {
         clientInvocation.Message.SetPartitionId(clientInvocation.PartitionId);
     }
 }
        private bool TrySend(ClientInvocation clientInvocation, ClientConnection connection)
        {
            var correlationId = clientInvocation.Message.GetCorrelationId();
            if (!TryRegisterInvocation(correlationId, clientInvocation)) return false;

            //enqueue to write queue
            if (connection.WriteAsync((ISocketWritable) clientInvocation.Message))
            {
                return true;
            }

            //Rollback sending failed
            if (Logger.IsFinestEnabled())
            {
                Logger.Finest("Unable to write request " + clientInvocation.Message + " to connection " + connection);
            }
            UnregisterInvocation(correlationId);
            RemoveEventHandler(correlationId);
            return false;
        }
 private void FailRequestDueToShutdown(ClientInvocation invocation)
 {
     if (Logger.IsFinestEnabled())
     {
         var connectionId = invocation.SentConnection == null
             ? "unknown"
             : invocation.SentConnection.Id.ToString();
         Logger.Finest("Aborting request on connection " + connectionId + ": " + invocation.Message +
                       " due to shutdown.");
     }
     if (!invocation.Future.IsComplete)
     {
         invocation.Future.Exception = new HazelcastException("Client is shutting down.");
     }
 }
        private bool ShouldRetryInvocation(ClientInvocation invocation, Exception exception)
        {
            if (invocation.BoundConnection != null)
            {
                return false;
            }
            if (Clock.CurrentTimeMillis() >= invocation.InvocationTimeMillis + _invocationTimeoutMillis)
            {
                return false;
            }

            //validate exception
            return exception is IOException
                   || exception is SocketException
                   || exception is HazelcastInstanceNotActiveException
                   || exception is AuthenticationException
            // above exceptions OR retryable excaption case as below
                   || exception is RetryableHazelcastException && (_redoOperations || invocation.Message.IsRetryable());
        }
 private void FailRequest(ClientConnection connection, ClientInvocation invocation)
 {
     if (_client.GetConnectionManager().Live)
     {
         HandleException(invocation,
             new TargetDisconnectedException(connection.GetAddress()));
     }
     else
     {
         FailRequestDueToShutdown(invocation);
     }
 }
 private void HandleInvocationException(ClientInvocation invocation, Exception exception)
 {
     try
     {
         //Should it retry?
         if (ShouldRetryInvocation(invocation, exception))
         {
             try
             {
                 _client.GetClientExecutionService()
                     .Schedule(() =>
                     {
                         var address = GetNewInvocationAddress(invocation);
                         InvokeInternal(invocation, address);
                     }, RetryWaitTime, TimeUnit.Milliseconds)
                     .ContinueWith(t =>
                         {
                             HandleInvocationException(invocation, t.Exception.Flatten().InnerExceptions.First());
                         }, TaskContinuationOptions.OnlyOnFaulted |
                            TaskContinuationOptions.ExecuteSynchronously);
                 return;
             }
             catch (Exception e)
             {
                 HandleInvocationException(invocation, e);
             }
         }
         //Fail with exception
         if (!invocation.Future.IsComplete)
         {
             invocation.Future.Exception = exception;
         }
     }
     catch (Exception ex)
     {
         if (Logger.IsFinestEnabled())
         {
             Logger.Finest("HandleInvocationException missed an exception:", ex);
         }
         throw ex;
     }
 }
        protected virtual void Send(ClientConnection connection, ClientInvocation clientInvocation)
        {
            var correlationId = NextCorrelationId();
            clientInvocation.Message.SetCorrelationId(correlationId);
            clientInvocation.Message.AddFlag(ClientMessage.BeginAndEndFlags);
            clientInvocation.SentConnection = connection;
            if (clientInvocation.PartitionId != -1)
            {
                clientInvocation.Message.SetPartitionId(clientInvocation.PartitionId);
            }
            if (clientInvocation.MemberUuid != null && clientInvocation.MemberUuid != connection.GetMember().GetUuid())
            {
                HandleException(clientInvocation,
                    new TargetNotMemberException(
                        "The member UUID on the invocation doesn't match the member UUID on the connection."));
            }

            if (_isShutDown)
            {
                FailRequestDueToShutdown(clientInvocation);
            }
            else
            {
                RegisterInvocation(correlationId, clientInvocation);

                //enqueue to write queue
                if (!connection.WriteAsync((ISocketWritable) clientInvocation.Message))
                {
                    UnregisterCall(correlationId);
                    RemoveEventHandler(correlationId);
                    FailRequest(connection, clientInvocation);
                }
            }
        }
 public IFuture<IClientMessage> InvokeOnConnection(IClientMessage request, ClientConnection connection,
     bool bypassHeartbeat = false)
 {
     var clientInvocation = new ClientInvocation(request, connection);
     InvokeInternal(clientInvocation, null, connection, bypassHeartbeat);
     return clientInvocation.Future;
 }
 protected IFuture<IClientMessage> Invoke(ClientInvocation invocation, Address address = null)
 {
     InvokeInternal(invocation, address);
     return invocation.Future;
 }