Holds the clientSocket to one of the members of Hazelcast ICluster.
Holds the clientSocket to one of the members of Hazelcast ICluster.
 public ClientListenerInvocation(IClientMessage message, DistributedEventHandler handler,
    DecodeStartListenerResponse responseDecoder, ClientConnection connection)
     : base(message, connection)
 {
     _responseDecoder = responseDecoder;
     _handler = handler;
 }
 public void DestroyConnection(ClientConnection connection, Exception cause)
 {
     var address = connection.GetAddress();
     Logger.Finest("Destroying connection " + connection + " due to " + cause.Message);
     if (address != null)
     {
         ClientConnection conn;
         if (_addresses.TryRemove(address, out conn))
         {
             connection.Close();
             FireConnectionListenerEvent(f => f.ConnectionRemoved(connection));
         }
         else
         {
             Logger.Warning("Could not find connection " + connection +
                            " in list of connections, could not destroy connection. Current list of connections are " +
                            string.Join(", ", _addresses.Keys));
             connection.Close();
         }
     }
     else
     {
         // connection has not authenticated yet
         ((ClientInvocationService) _client.GetInvocationService()).CleanUpConnectionResources(connection);
     }
 }
        private ClientConnection InitializeConnection(Address address, Authenticator authenticator)
        {
            CheckLive();
            ClientConnection connection = null;
            var id = _nextConnectionId;
            try
            {
                if (Logger.IsFinestEnabled())
                {
                    Logger.Finest("Creating new connection for " + address + " with id " + id);
                }

                connection = new ClientConnection(this, (ClientInvocationService) _client.GetInvocationService(), id, address, _networkConfig);

                connection.Init(_socketInterceptor);
                authenticator(connection);
                Interlocked.Increment(ref _nextConnectionId);
                Logger.Finest("Authenticated to " + connection);
                return connection;
            }
            catch (Exception e)
            {
                if (Logger.IsFinestEnabled())
                {
                    Logger.Finest("Error connecting to " + address + " with id " + id, e);
                }

                if (connection != null)
                {
                    connection.Close();
                }
                throw ExceptionUtil.Rethrow(e, typeof (IOException), typeof (SocketException),
                    typeof (TargetDisconnectedException));
            }
        }
        private void CleanupEventHandlers(ClientConnection connection)
        {
            var keys = new List<long>();
            foreach (var entry in _listenerInvocations)
            {
                if (entry.Value.SentConnection == connection && entry.Value.BoundConnection == null)
                {
                    keys.Add(entry.Key);
                }
            }

            foreach (var key in keys)
            {
                ClientListenerInvocation invocation;
                _listenerInvocations.TryRemove(key, out invocation);

                if (invocation.Future.IsComplete && invocation.Future.Result != null)
                {
                    //re-register listener on a new node
                    _client.GetClientExecutionService().Submit(() =>
                    {
                        if (!_isShutDown)
                        {
                            ReregisterListener(invocation);
                        }
                    });
                }
            }
        }
 private void FailRequest(ClientConnection connection, ClientInvocation invocation)
 {
     if (_client.GetConnectionManager().Live)
     {
         HandleException(invocation,
             new TargetDisconnectedException(connection.GetAddress()));
     }
     else
     {
         FailRequestDueToShutdown(invocation);
     }
 }
 public void CleanUpConnectionResources(ClientConnection connection)
 {
     Logger.Finest("Cleaning up connection resources for " + connection.Id);
     CleanupInvocations(connection);
     CleanupEventHandlers(connection);
 }
 public IFuture<IClientMessage> InvokeOnConnection(IClientMessage request, ClientConnection connection)
 {
     var clientInvocation = new ClientInvocation(request, connection);
     Send(connection, clientInvocation);
     return clientInvocation.Future;
 }
 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;
 }
        private void CleanupEventHandlers(ClientConnection connection)
        {
            var keys = new List<long>();
            foreach (var entry in _listenerInvocations)
            {
                if (entry.Value.SentConnection == connection && entry.Value.BoundConnection == null)
                {
                    keys.Add(entry.Key);
                }
            }

            foreach (var key in keys)
            {
                ClientListenerInvocation invocation;
                _listenerInvocations.TryRemove(key, out invocation);

                if (invocation.Future.IsComplete && invocation.Future.Result != null)
                {
                    //re-register listener on a new node
                    if (!_isShutDown)
                    {
                        _client.GetClientExecutionService().Schedule(() =>
                            {
                                ReregisterListener(invocation)
                                    .ToTask()
                                    .ContinueWith(t => { Logger.Warning("Cannot reregister listener.", t.Exception); },
                                        TaskContinuationOptions.OnlyOnFaulted |
                                        TaskContinuationOptions.ExecuteSynchronously);
                            }, RetryWaitTime, TimeUnit.Milliseconds);
                    }
                }
            }
        }
 public void HeartBeatStopped(ClientConnection connection)
 {
     if (connection.GetAddress().Equals(_ownerConnectionAddress))
     {
         _connectionManager.DestroyConnection(connection);
     }
 }
        private void ManagerAuthenticator(ClientConnection connection)
        {
            Logger.Finest("Authenticating against the owner node");
            var ss = _client.GetSerializationService();

            string uuid = null;
            string ownerUuid = null;
            if (_principal != null)
            {
                uuid = _principal.GetUuid();
                ownerUuid = _principal.GetOwnerUuid();
            }
            ClientMessage request;
            if (_credentials is UsernamePasswordCredentials)
            {
                var usernamePasswordCr = (UsernamePasswordCredentials) _credentials;
                request = ClientAuthenticationCodec.EncodeRequest(usernamePasswordCr.GetUsername(),
                    usernamePasswordCr.GetPassword(), uuid, ownerUuid, true,
                    ClientTypes.Csharp, _client.GetSerializationService().GetVersion());
            }
            else
            {
                var data = ss.ToData(_credentials);
                request = ClientAuthenticationCustomCodec.EncodeRequest(data, uuid, ownerUuid, false,
                    ClientTypes.Csharp, _client.GetSerializationService().GetVersion());
            }

            connection.Init();
            IClientMessage response;
            try
            {
                var invocationService = (ClientInvocationService) _client.GetInvocationService();
                response = ThreadUtil.GetResult(invocationService.InvokeOnConnection(request, connection));
            }
            catch (Exception e)
            {
                throw ExceptionUtil.Rethrow(e);
            }
            var result = ClientAuthenticationCodec.DecodeResponse(response);

            var member = new Member(result.address, result.ownerUuid);
            _principal = new ClientPrincipal(result.uuid, result.ownerUuid);

            connection.SetRemoteMember(member);
        }
 public void ConnectionRemoved(ClientConnection connection)
 {
     var executionService = (ClientExecutionService) _client.GetClientExecutionService();
     if (Equals(connection.GetAddress(), _ownerConnectionAddress))
     {
         if (_client.GetLifecycleService().IsRunning())
         {
             executionService.SubmitInternal(() =>
             {
                 try
                 {
                     FireConnectionEvent(LifecycleEvent.LifecycleState.ClientDisconnected);
                     ConnectToCluster();
                 }
                 catch (Exception e)
                 {
                     Logger.Warning("Could not re-connect to cluster shutting down the client", e);
                     _client.GetLifecycleService().Shutdown();
                 }
             });
         }
     }
 }
 public ClientInvocation(IClientMessage message, ClientConnection boundConnection)
     : this(message)
 {
     _boundConnection = boundConnection;
 }
Esempio n. 14
0
 private bool Equals(ClientConnection other)
 {
     return(_id == other._id);
 }
 public void ConnectionAdded(ClientConnection connection)
 {
     // noop
 }
 private void CleanupInvocations(ClientConnection connection)
 {
     var keys = new List<long>();
     foreach (var entry in _invocations)
     {
         if (entry.Value.SentConnection == connection)
         {
             keys.Add(entry.Key);
         }
     }
     foreach (var key in keys)
     {
         ClientInvocation invocation;
         if (_invocations.TryRemove(key, out invocation))
         {
             var ex = _client.GetConnectionManager().Live
                 ? new TargetDisconnectedException(connection.GetAddress(), "connection was closed.")
                 : new HazelcastException("Client is shut down.");
             HandleInvocationException(invocation, ex);
         }
     }
 }
 public void ConnectionRemoved(ClientConnection connection)
 {
     CleanUpConnectionResources(connection);
 }
        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);
            }
        }
 public IFuture<IClientMessage> InvokeListenerOnConnection(IClientMessage request,
     DistributedEventHandler handler, DecodeStartListenerResponse responseDecoder, ClientConnection connection)
 {
     var clientInvocation = new ClientListenerInvocation(request, handler, responseDecoder, connection);
     Send(connection, clientInvocation);
     return clientInvocation.Future;
 }
        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;
        }
        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);
                }
            }
        }
 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 void CleanupInvocations(ClientConnection connection)
 {
     var keys = new List<long>();
     foreach (var entry in _invocations)
     {
         if (entry.Value.SentConnection == connection)
         {
             keys.Add(entry.Key);
         }
     }
     foreach (var key in keys)
     {
         ClientInvocation invocation;
         _invocations.TryRemove(key, out invocation);
         FailRequest(connection, invocation);
     }
 }
        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 void DestroyConnection(ClientConnection connection)
 {
     var address = connection.GetAddress();
     // ReSharper disable once InconsistentlySynchronizedField
     Logger.Finest("Destroying connection " + connection);
     if (address != null)
     {
         ClientConnection conn;
         if (_addresses.TryRemove(address, out conn))
         {
             connection.Close();
             FireConnectionListenerEvent(f => f.ConnectionRemoved(connection));
         }
     }
     else
     {
         // connection has not authenticated yet
         ((ClientInvocationService) _client.GetInvocationService()).CleanUpConnectionResources(connection);
     }
 }
 public void HeartBeatStarted(ClientConnection connection)
 {
     // noop
 }
        private void ClusterAuthenticator(ClientConnection connection)
        {
            var ss = _client.GetSerializationService();
            var clusterService = (ClientClusterService) _client.GetClientClusterService();
            var principal = clusterService.GetPrincipal();

            var uuid = principal.GetUuid();
            var ownerUuid = principal.GetOwnerUuid();
            ClientMessage request;

            var usernamePasswordCr = _credentials as UsernamePasswordCredentials;
            if (usernamePasswordCr != null)
            {
                request = ClientAuthenticationCodec.EncodeRequest(usernamePasswordCr.GetUsername(),
                    usernamePasswordCr.GetPassword(), uuid, ownerUuid, false,
                    ClientTypes.Csharp, _client.GetSerializationService().GetVersion());
            }
            else
            {
                var data = ss.ToData(_credentials);
                request = ClientAuthenticationCustomCodec.EncodeRequest(data, uuid, ownerUuid, false,
                    ClientTypes.Csharp, _client.GetSerializationService().GetVersion());
            }

            IClientMessage response;
            try
            {
                var future = ((ClientInvocationService) _client.GetInvocationService()).InvokeOnConnection(request,
                    connection);
                response = ThreadUtil.GetResult(future);
            }
            catch (Exception e)
            {
                throw ExceptionUtil.Rethrow(e);
            }
            var rp = ClientAuthenticationCodec.DecodeResponse(response);

            if (rp.address == null)
            {
                throw new HazelcastException("Could not resolve address for member.");
            }

            var member = _client.GetClientClusterService().GetMember(rp.address);
            if (member == null)
            {
                throw new HazelcastException("Node with address '" + rp.address + "' was not found in the member list");
            }
            connection.Member = member;
        }
 public void HeartBeatStopped(ClientConnection connection)
 {
     CleanupInvocations(connection);
     CleanupEventHandlers(connection);
 }
 private ClientGetPartitionsCodec.ResponseParameters GetPartitionsFrom(ClientConnection connection)
 {
     var request = ClientGetPartitionsCodec.EncodeRequest();
     var task = ((ClientInvocationService) _client.GetInvocationService()).InvokeOnConnection(request, connection);
     var result = ThreadUtil.GetResult(task, PartitionTimeout);
     return ClientGetPartitionsCodec.DecodeResponse(result);
 }
Esempio n. 30
0
        private void Authenticate(ClientConnection connection, bool isOwnerConnection)
        {
            if (Logger.IsFinestEnabled())
            {
                Logger.Finest(string.Format("Authenticating against the {0} node", isOwnerConnection?"owner":"non-owner"));
            }
            string uuid      = null;
            string ownerUuid = null;

            if (ClientPrincipal != null)
            {
                uuid      = ClientPrincipal.GetUuid();
                ownerUuid = ClientPrincipal.GetOwnerUuid();
            }

            var           ss = _client.GetSerializationService();
            ClientMessage request;
            var           credentials = _credentialsFactory.NewCredentials();

            LastCredentials = credentials;
            if (credentials.GetType() == typeof(UsernamePasswordCredentials))
            {
                var usernamePasswordCr = (UsernamePasswordCredentials)credentials;
                request = ClientAuthenticationCodec.EncodeRequest(usernamePasswordCr.Username, usernamePasswordCr.Password, uuid,
                                                                  ownerUuid, isOwnerConnection, ClientTypes.Csharp, ss.GetVersion(), VersionUtil.GetDllVersion());
            }
            else
            {
                var data = ss.ToData(credentials);
                request = ClientAuthenticationCustomCodec.EncodeRequest(data, uuid, ownerUuid, isOwnerConnection,
                                                                        ClientTypes.Csharp, ss.GetVersion(), VersionUtil.GetDllVersion());
            }

            IClientMessage response;

            try
            {
                var invocationService = (ClientInvocationService)_client.GetInvocationService();
                response = ThreadUtil.GetResult(invocationService.InvokeOnConnection(request, connection), _heartbeatTimeout);
            }
            catch (Exception e)
            {
                var ue = ExceptionUtil.Rethrow(e);
                Logger.Finest("Member returned an exception during authentication.", ue);
                throw ue;
            }
            var result = ClientAuthenticationCodec.DecodeResponse(response);

            if (result.address == null)
            {
                throw new HazelcastException("Could not resolve address for member.");
            }
            switch (result.status)
            {
            case AuthenticationStatus.Authenticated:
                if (isOwnerConnection)
                {
                    var member = new Member(result.address, result.ownerUuid);
                    ClientPrincipal   = new ClientPrincipal(result.uuid, result.ownerUuid);
                    connection.Member = member;
                    connection.SetOwner();
                    connection.ConnectedServerVersionStr = result.serverHazelcastVersion;
                }
                else
                {
                    var member = _client.GetClientClusterService().GetMember(result.address);
                    if (member == null)
                    {
                        throw new HazelcastException(string.Format("Node with address '{0}' was not found in the member list",
                                                                   result.address));
                    }
                    connection.Member = member;
                }
                break;

            case AuthenticationStatus.CredentialsFailed:
                throw new AuthenticationException("Invalid credentials! Principal: " + ClientPrincipal);

            case AuthenticationStatus.SerializationVersionMismatch:
                throw new InvalidOperationException("Server serialization version does not match to client");

            default:
                throw new AuthenticationException("Authentication status code not supported. status: " + result.status);
            }
        }