Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
 public void Start()
 {
     if (!_live.CompareAndSet(false, true))
     {
         return;
     }
     _client.GetConnectionManager().AddConnectionListener(this);
     _partitionUpdaterToken = new CancellationTokenSource();
     _client.GetClientExecutionService().ScheduleWithFixedDelay(() => GetPartitions(),
                                                                0, PartitionRefreshPeriod, TimeUnit.Milliseconds, _partitionUpdaterToken.Token);
 }
 private void FailRequest(ClientConnection connection, ClientInvocation invocation)
 {
     if (_client.GetConnectionManager().Live)
     {
         HandleException(invocation,
                         new TargetDisconnectedException(connection.GetAddress()));
     }
     else
     {
         FailRequestDueToShutdown(invocation);
     }
 }
Esempio n. 4
0
        public IClient GetLocalClient()
        {
            var cm = _client.GetConnectionManager();
            var cp = GetPrincipal();
            var ownerConnection = cm.GetConnection(OwnerConnectionAddress);

            var socketAddress = ownerConnection != null?ownerConnection.GetLocalSocketAddress() : null;

            var uuid = cp != null?cp.GetUuid() : null;

            return(new Client(uuid, socketAddress));
        }
        private void InitializeOnServer(ClientProxy clientProxy)
        {
            var initializationTarget = FindNextAddressToCreateARequest();
            var invocationTarget     = initializationTarget;

            if (initializationTarget != null &&
                _client.GetConnectionManager().GetConnection(initializationTarget) == null)
            {
                invocationTarget = _client.GetClientClusterService().GetOwnerConnectionAddress();
            }

            if (invocationTarget == null)
            {
                throw new IOException("Not able to setup owner connection!");
            }

            var request = ClientCreateProxyCodec.EncodeRequest(clientProxy.GetName(), clientProxy.GetServiceName(),
                                                               initializationTarget);

            try
            {
                ThreadUtil.GetResult(_client.GetInvocationService().InvokeOnTarget(request, invocationTarget));
            }
            catch (Exception e)
            {
                throw ExceptionUtil.Rethrow(e);
            }
        }
Esempio n. 6
0
 public ClientMembershipListener(HazelcastClient client)
 {
     _client            = client;
     _connectionManager = (ClientConnectionManager)client.GetConnectionManager();
     _partitionService  = (ClientPartitionService)client.GetClientPartitionService();
     _clusterService    = (ClientClusterService)client.GetClientClusterService();
 }
 public ClientMembershipListener(HazelcastClient client)
 {
     _client = client;
     _connectionManager = (ClientConnectionManager) client.GetConnectionManager();
     _partitionService = (ClientPartitionService) client.GetClientPartitionService();
     _clusterService = (ClientClusterService) client.GetClientClusterService();
 }
Esempio n. 8
0
 private bool GetPartitions()
 {
     if (_updating.CompareAndSet(false, true))
     {
         try
         {
             Logger.Finest("Updating partition list.");
             var clusterService = _client.GetClientClusterService();
             var ownerAddress   = clusterService.GetOwnerConnectionAddress();
             var connection     = _client.GetConnectionManager().GetConnection(ownerAddress);
             if (connection == null)
             {
                 throw new InvalidOperationException(
                           "Owner connection is not available, could not get partitions.");
             }
             var response = GetPartitionsFrom(connection);
             var result   = ProcessPartitionResponse(response);
             Logger.Finest("Partition list updated");
             return(result);
         }
         catch (HazelcastInstanceNotActiveException ignored)
         {
         }
         catch (Exception e)
         {
             Logger.Warning("Error when getting list of partitions", e);
         }
         finally
         {
             _updating.Set(false);
         }
     }
     return(false);
 }
Esempio n. 9
0
        public ClientListenerService(HazelcastClient client)
        {
            _client            = client;
            _connectionManager = client.GetConnectionManager();
            var eventTreadCount    = EnvironmentUtil.ReadInt("hazelcast.client.event.thread.count") ?? DefaultEventThreadCount;
            var eventQueueCapacity =
                EnvironmentUtil.ReadInt("hazelcast.client.event.queue.capacity") ?? DefaultEventQueueCapacity;

            _eventExecutor         = new StripedTaskScheduler(eventTreadCount, eventQueueCapacity, client.GetName() + ".event");
            _registrationScheduler = new StripedTaskScheduler(1, eventQueueCapacity, client.GetName() + ".eventRegistration");
            _registrations         = new ConcurrentDictionary <string, ListenerRegistration>();
            _eventHandlers         = new ConcurrentDictionary <long, DistributedEventHandler>();
            _failedRegistrations   = new ConcurrentDictionary <ClientConnection, ICollection <ListenerRegistration> >();
            IsSmart = client.GetClientConfig().GetNetworkConfig().IsSmartRouting();
        }
        protected ClientInvocationService(HazelcastClient client)
        {
            _client = client;
            _redoOperations = client.GetClientConfig().GetNetworkConfig().IsRedoOperation();

            var eventTreadCount = EnvironmentUtil.ReadEnvironmentVar("hazelcast.client.event.thread.count") ??
                                  DefaultEventThreadCount;
            _taskScheduler = new StripedTaskScheduler(eventTreadCount);

            _invocationTimeoutMillis =
            (EnvironmentUtil.ReadEnvironmentVar("hazelcast.client.invocation.timeout.seconds") ??
             DefaultInvocationTimeout) * 1000;

            _clientConnectionManager = client.GetConnectionManager();
            _clientConnectionManager.AddConnectionListener(this);
        }
        protected ClientInvocationService(HazelcastClient client)
        {
            _client         = client;
            _redoOperations = client.GetClientConfig().GetNetworkConfig().IsRedoOperation();

            var eventTreadCount = EnvironmentUtil.ReadEnvironmentVar("hazelcast.client.event.thread.count") ??
                                  DefaultEventThreadCount;

            _taskScheduler = new StripedTaskScheduler(eventTreadCount);

            _invocationTimeoutMillis =
                (EnvironmentUtil.ReadEnvironmentVar("hazelcast.client.invocation.timeout.seconds") ??
                 DefaultInvocationTimeout) * 1000;

            _clientConnectionManager = (ClientConnectionManager)client.GetConnectionManager();
            _clientConnectionManager.AddConnectionListener(this);
        }
Esempio n. 12
0
        public ClientInvocationService(HazelcastClient client)
        {
            _client         = client;
            _redoOperations = client.GetClientConfig().GetNetworkConfig().IsRedoOperation();

            var eventTreadCount = EnvironmentUtil.ReadEnvironmentVar("hazelcast.client.event.thread.count") ??
                                  DefaultEventThreadCount;

            _taskScheduler = new StripedTaskScheduler(eventTreadCount);

            // TODO: These should be consoliated with the way it's done in the Java client
            _retryCount    = EnvironmentUtil.ReadEnvironmentVar("hazelcast.client.request.retry.count") ?? _retryCount;
            _retryWaitTime = EnvironmentUtil.ReadEnvironmentVar("hazelcast.client.request.retry.wait.time") ??
                             _retryWaitTime;

            _clientConnectionManager = (ClientConnectionManager)client.GetConnectionManager();
            _clientConnectionManager.AddConnectionListener(this);
            _clientConnectionManager.AddConnectionHeartBeatListener(this);
        }
 public void Start()
 {
     _clientConnectionManager = _client.GetConnectionManager();
     _clientListenerService   = _client.GetListenerService();
     _clientConnectionManager.AddConnectionListener(this);
 }
Esempio n. 14
0
 private ClientConnection GetConnection(Address address = null)
 {
     EnsureOwnerConnectionAvailable();
     return(_client.GetConnectionManager().GetOrConnect(address));
 }