public ClientListenerInvocation(IClientMessage message, DistributedEventHandler handler, DecodeStartListenerResponse responseDecoder, Address address) : base(message, address) { _responseDecoder = responseDecoder; _handler = handler; }
public static int CalculateDataSize(string name, string serviceName, Address target) { var dataSize = ClientMessage.HeaderSize; dataSize += ParameterUtil.CalculateDataSize(name); dataSize += ParameterUtil.CalculateDataSize(serviceName); dataSize += AddressCodec.CalculateDataSize(target); return dataSize; }
public Member(Address address, string uuid, IDictionary<string, string> attributes, bool liteMember) { this.address = address; this.uuid = uuid; _liteMember = liteMember; foreach (var kv in attributes) { _attributes.TryAdd(kv.Key, kv.Value); } }
public Member(Address address, string uuid, IDictionary<string, string> attributes, bool liteMember) { _logger = Logger.GetLogger(typeof (Member) + ":" + address); _address = address; _uuid = uuid; _liteMember = liteMember; foreach (var kv in attributes) { _attributes.TryAdd(kv.Key, kv.Value); } }
public static ClientMessage EncodeRequest(string name, string serviceName, Address target) { var requiredDataSize = RequestParameters.CalculateDataSize(name, serviceName, target); var clientMessage = ClientMessage.CreateForEncode(requiredDataSize); clientMessage.SetMessageType((int) RequestType); clientMessage.SetRetryable(Retryable); clientMessage.Set(name); clientMessage.Set(serviceName); AddressCodec.Encode(target, clientMessage); clientMessage.UpdateFrameLength(); return clientMessage; }
public static void Encode(ClientMessage clientMessage, Hazelcast.IO.Address address) { clientMessage.Add(BeginFrame.Copy()); var initialFrame = new Frame(new byte[InitialFrameSize]); EncodeInt(initialFrame.Content, PortFieldOffset, address.Port); clientMessage.Add(initialFrame); StringCodec.Encode(clientMessage, address.Host); clientMessage.Add(EndFrame.Copy()); }
private bool Equals(Address other) { return _port == other._port && string.Equals(_host, other._host) && _type == other._type; }
internal virtual void ListenMembershipEvents(Address ownerConnectionAddress) { Logger.Finest("Starting to listen for membership events from " + ownerConnectionAddress); _initialListFetched = new ManualResetEventSlim(); try { var clientMessage = ClientAddMembershipListenerCodec.EncodeRequest(false); DistributedEventHandler handler = m => ClientAddMembershipListenerCodec.AbstractEventHandler .Handle(m, HandleMember, HandleMemberCollection, HandleMemberAttributeChange); try { var connection = _connectionManager.GetConnection(ownerConnectionAddress); if (connection == null) { throw new InvalidOperationException( "Can not load initial members list because owner connection is null. Address " + ownerConnectionAddress); } var invocationService = (ClientInvocationService)_client.GetInvocationService(); var response = ThreadUtil.GetResult(invocationService.InvokeListenerOnConnection(clientMessage, handler, m => ClientAddMembershipListenerCodec.DecodeResponse(m).response, connection)); //registraiton id is ignored as this listener will never be removed var registirationId = ClientAddMembershipListenerCodec.DecodeResponse(response).response; WaitInitialMemberListFetched(); } catch (Exception e) { throw ExceptionUtil.Rethrow(e); } } catch (Exception e) { if (_client.GetLifecycleService().IsRunning()) { if (Logger.IsFinestEnabled()) { Logger.Warning("Error while registering to cluster events! -> " + ownerConnectionAddress, e); } else { Logger.Warning("Error while registering to cluster events! -> " + ownerConnectionAddress + ", Error: " + e); } } } }
/// <exception cref="System.IO.IOException"></exception> public ClientConnection(ClientConnectionManager clientConnectionManager, ClientInvocationService invocationService, int id, Address address, ClientNetworkConfig clientNetworkConfig) { _clientConnectionManager = clientConnectionManager; _id = id; var isa = address.GetInetSocketAddress(); var socketOptions = clientNetworkConfig.GetSocketOptions(); var socketFactory = socketOptions.GetSocketFactory() ?? new DefaultSocketFactory(); _clientSocket = socketFactory.CreateSocket(); try { _clientSocket = new Socket(isa.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); var lingerOption = new LingerOption(true, 5); if (socketOptions.GetLingerSeconds() > 0) { lingerOption.LingerTime = socketOptions.GetLingerSeconds(); } _clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption); _clientSocket.NoDelay = socketOptions.IsTcpNoDelay(); _clientSocket.ReceiveTimeout = socketOptions.GetTimeout() > 0 ? socketOptions.GetTimeout() : -1; var bufferSize = socketOptions.GetBufferSize() * 1024; if (bufferSize < 0) { bufferSize = BufferSize; } _clientSocket.SendBufferSize = bufferSize; _clientSocket.ReceiveBufferSize = bufferSize; var connectionTimeout = clientNetworkConfig.GetConnectionTimeout() > -1 ? clientNetworkConfig.GetConnectionTimeout() : ConnectionTimeout; var socketResult = _clientSocket.BeginConnect(address.GetHost(), address.GetPort(), null, null); if (!socketResult.AsyncWaitHandle.WaitOne(connectionTimeout, true) || !_clientSocket.Connected) { // NOTE, MUST CLOSE THE SOCKET _clientSocket.Close(); throw new IOException("Failed to connect to " + address); } _sendBuffer = ByteBuffer.Allocate(BufferSize); _receiveBuffer = ByteBuffer.Allocate(BufferSize); _builder = new ClientMessageBuilder(invocationService.HandleClientMessage); var networkStream = new NetworkStream(_clientSocket, false); if (clientNetworkConfig.GetSSLConfig().IsEnabled()) { var sslStream = new SslStream(networkStream, false, (sender, certificate, chain, sslPolicyErrors) => RemoteCertificateValidationCallback(sender, certificate, chain, sslPolicyErrors, clientNetworkConfig), null); var certificateName = clientNetworkConfig.GetSSLConfig().GetCertificateName() ?? ""; sslStream.AuthenticateAsClient(certificateName); _stream = sslStream; } else { _stream = networkStream; } _live = new AtomicBoolean(true); } catch (Exception e) { _clientSocket.Close(); if (_stream != null) { _stream.Close(); } throw new IOException("Cannot connect! Socket error:" + e.Message); } }
/// <exception cref="System.Exception" /> private bool Connect(ICollection<IPEndPoint> triedAddresses) { ICollection<IPEndPoint> socketAddresses = GetEndpoints(); foreach (var inetSocketAddress in socketAddresses) { try { triedAddresses.Add(inetSocketAddress); var address = new Address(inetSocketAddress); if (Logger.IsFinestEnabled()) { Logger.Finest("Trying to connect to " + address); } var connection = _connectionManager.GetOrConnect(address, ManagerAuthenticator); FireConnectionEvent(LifecycleEvent.LifecycleState.ClientConnected); _ownerConnectionAddress = connection.GetAddress(); return true; } catch (Exception e) { var level = e is AuthenticationException ? LogLevel.Warning : LogLevel.Finest; Logger.Log(level, "Exception during initial connection to " + inetSocketAddress, e); } } return false; }
public override IFuture<IClientMessage> InvokeListenerOnTarget(IClientMessage request, Address target, DistributedEventHandler handler, DecodeStartListenerResponse responseDecoder) { return Invoke(new ClientListenerInvocation(request, handler, responseDecoder, target), target); }
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 Member(Address address, string uuid) : this(address, uuid, new Dictionary<string, string>(), false) { }
public abstract IFuture<IClientMessage> InvokeOnTarget(IClientMessage request, Address target);
public static void Encode(Address address, ClientMessage clientMessage) { clientMessage.Set(address.GetHost()).Set(address.GetPort()); }
public static int CalculateDataSize(Address address) { var dataSize = ParameterUtil.CalculateDataSize(address.GetHost()); dataSize += Bits.IntSizeInBytes; return dataSize; }
private void ConnectToOne() { _ownerConnectionAddress = null; var networkConfig = GetClientConfig().GetNetworkConfig(); var connAttemptLimit = networkConfig.GetConnectionAttemptLimit(); var connectionAttemptPeriod = networkConfig.GetConnectionAttemptPeriod(); var connectionAttemptLimit = connAttemptLimit == 0 ? int.MaxValue : connAttemptLimit; var attempt = 0; ICollection<IPEndPoint> triedAddresses = new HashSet<IPEndPoint>(); while (attempt < connectionAttemptLimit) { if (!_client.GetLifecycleService().IsRunning()) { if (Logger.IsFinestEnabled()) { Logger.Finest("Giving up on retrying to connect to cluster since client is shutdown"); } break; } attempt++; var nextTry = Clock.CurrentTimeMillis() + connectionAttemptPeriod; var isConnected = Connect(triedAddresses); if (isConnected) { return; } var remainingTime = nextTry - Clock.CurrentTimeMillis(); Logger.Warning( string.Format("Unable to get alive cluster connection, try in {0} ms later, attempt {1} of {2}.", Math.Max(0, remainingTime), attempt, connectionAttemptLimit)); if (remainingTime > 0) { try { Thread.Sleep((int) remainingTime); } catch (Exception) { break; } } } throw new InvalidOperationException("Unable to connect to any address in the config! " + "The following addresses were tried:" + string.Join(", ", triedAddresses)); }
public TargetDisconnectedException(Address address) : base("Target[" + address + "] disconnected.") { }
/// <exception cref="System.IO.IOException"></exception> public ClientConnection GetOrConnect(Address address, Authenticator authenticator) { if (address == null) { throw new ArgumentException("address"); } lock (_connectionMutex) { ClientConnection connection; if (!_addresses.ContainsKey(address)) { connection = InitializeConnection(address, authenticator); FireConnectionListenerEvent(f => f.ConnectionAdded(connection)); _addresses.TryAdd(connection.GetAddress(), connection); Logger.Finest("Active list of connections: " + string.Join(", ", _addresses.Values)); } else connection = _addresses[address]; if (connection == null) { Logger.Severe("CONNECTION Cannot be NULL here"); } return connection; } }
public Member(Address address) : this(address, null) { }
public Task<ClientConnection> GetOrConnectAsync(Address address) { lock (_pendingConnections) { if (!_pendingConnections.ContainsKey(address)) { var task = _client.GetClientExecutionService().Submit(() => GetOrConnect(address)); task.ContinueWith(t => { if (t.IsFaulted) { if (Logger.IsFinestEnabled()) { Logger.Finest("Exception in async pending connection:", t.Exception); } } lock (_pendingConnections) { _pendingConnections.Remove(address); } }); _pendingConnections[address] = task; } return _pendingConnections[address]; } }
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 IFuture<IClientMessage> Invoke(ClientInvocation invocation, Address address = null) { InvokeInternal(invocation, address); return invocation.Future; }
/// <summary> /// Gets an existing connection for the given address /// </summary> /// <param name="address"></param> /// <returns></returns> public ClientConnection GetConnection(Address address) { ClientConnection connection; return _addresses.TryGetValue(address, out connection) ? connection : null; }
private ClientConnection GetConnection(Address address) { EnsureOwnerConnectionAvailable(); return _client.GetConnectionManager().GetConnection(address); }
/// <summary> /// Gets the connection for the address. If there is no connection, a new one will be created /// </summary> /// <param name="target"></param> /// <returns></returns> public ClientConnection GetOrConnect(Address target) { return GetOrConnect(target, ClusterAuthenticator); }
public abstract IFuture<IClientMessage> InvokeListenerOnTarget(IClientMessage request, Address target, DistributedEventHandler handler, DecodeStartListenerResponse responseDecoder);
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)); } }
public override IFuture<IClientMessage> InvokeOnTarget(IClientMessage request, Address target) { return SendToOwner(new ClientInvocation(request)); }
public override IFuture<IClientMessage> InvokeOnTarget(IClientMessage request, Address target) { return Invoke(new ClientInvocation(request, target), target); }
public Task<ClientConnection> GetOrConnectAsync(Address address) { lock (_pendingConnections) { if (!_pendingConnections.ContainsKey(address)) { var task = _client.GetClientExecutionService().Submit(() => GetOrConnect(address)); task.ContinueWith(t => { lock (_pendingConnections) { _pendingConnections.Remove(address); } }); _pendingConnections[address] = task; } return _pendingConnections[address]; } }