Search in sources :

Example 1 with Channel

use of com.hazelcast.internal.networking.Channel in project hazelcast by hazelcast.

the class TcpClientConnectionManager method createSocketConnection.

@SuppressWarnings("unchecked")
protected TcpClientConnection createSocketConnection(Address target) {
    CandidateClusterContext currentClusterContext = clusterDiscoveryService.current();
    SocketChannel socketChannel = null;
    try {
        socketChannel = SocketChannel.open();
        Socket socket = socketChannel.socket();
        bindSocketToPort(socket);
        Channel channel = networking.register(currentClusterContext.getChannelInitializer(), socketChannel, true);
        channel.attributeMap().put(Address.class, target);
        InetSocketAddress inetSocketAddress = new InetSocketAddress(target.getInetAddress(), target.getPort());
        channel.connect(inetSocketAddress, connectionTimeoutMillis);
        TcpClientConnection connection = new TcpClientConnection(client, connectionIdGen.incrementAndGet(), channel);
        socketChannel.configureBlocking(true);
        SocketInterceptor socketInterceptor = currentClusterContext.getSocketInterceptor();
        if (socketInterceptor != null) {
            socketInterceptor.onConnect(socket);
        }
        channel.start();
        return connection;
    } catch (Exception e) {
        closeResource(socketChannel);
        logger.finest(e);
        throw rethrow(e);
    }
}
Also used : CandidateClusterContext(com.hazelcast.client.impl.clientside.CandidateClusterContext) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) SocketChannel(java.nio.channels.SocketChannel) Channel(com.hazelcast.internal.networking.Channel) SocketInterceptor(com.hazelcast.nio.SocketInterceptor) Socket(java.net.Socket) HazelcastException(com.hazelcast.core.HazelcastException) HazelcastClientNotActiveException(com.hazelcast.client.HazelcastClientNotActiveException) IOException(java.io.IOException) AuthenticationException(com.hazelcast.client.AuthenticationException) HazelcastClientOfflineException(com.hazelcast.client.HazelcastClientOfflineException) EOFException(java.io.EOFException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ClientNotAllowedInClusterException(com.hazelcast.client.ClientNotAllowedInClusterException) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException)

Example 2 with Channel

use of com.hazelcast.internal.networking.Channel in project hazelcast by hazelcast.

the class NioNetworking method shutdown.

@Override
public void shutdown() {
    if (!started.compareAndSet(true, false)) {
        return;
    }
    metricsRegistry.deregisterDynamicMetricsProvider(this);
    // if there are any channels left, we close them.
    for (Channel channel : channels) {
        if (!channel.isClosed()) {
            closeResource(channel);
        }
    }
    // and clear them to prevent memory leaks.
    channels.clear();
    // we unregister the publish future to prevent memory leaks.
    if (publishFuture != null) {
        publishFuture.cancel(false);
        publishFuture = null;
    }
    ioBalancer.stop();
    if (logger.isFinestEnabled()) {
        logger.finest("Shutting down IO Threads... Total: " + (inputThreads.length + outputThreads.length));
    }
    shutdown(inputThreads);
    inputThreads = null;
    shutdown(outputThreads);
    outputThreads = null;
    closeListenerExecutor.shutdown();
    closeListenerExecutor = null;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) Channel(com.hazelcast.internal.networking.Channel)

Example 3 with Channel

use of com.hazelcast.internal.networking.Channel in project hazelcast by hazelcast.

the class NioNetworking method provideDynamicMetrics.

@Override
public void provideDynamicMetrics(MetricDescriptor descriptor, MetricsCollectionContext context) {
    for (Channel channel : channels) {
        String pipelineId = channel.localSocketAddress() + "->" + channel.remoteSocketAddress();
        MetricDescriptor descriptorIn = descriptor.copy().withPrefix(TCP_PREFIX_CONNECTION_IN).withDiscriminator(TCP_DISCRIMINATOR_PIPELINEID, pipelineId);
        context.collect(descriptorIn, channel.inboundPipeline());
        MetricDescriptor descriptorOut = descriptor.copy().withPrefix(TCP_PREFIX_CONNECTION_OUT).withDiscriminator(TCP_DISCRIMINATOR_PIPELINEID, pipelineId);
        context.collect(descriptorOut, channel.outboundPipeline());
    }
    NioThread[] inputThreads = this.inputThreads;
    if (inputThreads != null) {
        for (NioThread nioThread : inputThreads) {
            MetricDescriptor descriptorInThread = descriptor.copy().withPrefix(TCP_PREFIX_INPUTTHREAD).withDiscriminator(TCP_DISCRIMINATOR_THREAD, nioThread.getName());
            context.collect(descriptorInThread, nioThread);
        }
    }
    NioThread[] outputThreads = this.outputThreads;
    if (outputThreads != null) {
        for (NioThread nioThread : outputThreads) {
            MetricDescriptor descriptorOutThread = descriptor.copy().withPrefix(TCP_PREFIX_OUTPUTTHREAD).withDiscriminator(TCP_DISCRIMINATOR_THREAD, nioThread.getName());
            context.collect(descriptorOutThread, nioThread);
        }
    }
    IOBalancer ioBalancer = this.ioBalancer;
    if (ioBalancer != null) {
        MetricDescriptor descriptorBalancer = descriptor.copy().withPrefix(TCP_PREFIX_BALANCER);
        context.collect(descriptorBalancer, ioBalancer);
    }
    MetricDescriptor descriptorTcp = descriptor.copy().withPrefix(TCP_PREFIX);
    context.collect(descriptorTcp, this);
}
Also used : MetricDescriptor(com.hazelcast.internal.metrics.MetricDescriptor) IOBalancer(com.hazelcast.internal.networking.nio.iobalancer.IOBalancer) SocketChannel(java.nio.channels.SocketChannel) Channel(com.hazelcast.internal.networking.Channel)

Example 4 with Channel

use of com.hazelcast.internal.networking.Channel in project hazelcast by hazelcast.

the class TcpServerConnectionManager method newChannel.

Channel newChannel(SocketChannel socketChannel, boolean clientMode) throws IOException {
    Networking networking = server.getNetworking();
    ChannelInitializer channelInitializer = channelInitializerFn.apply(endpointQualifier);
    assert channelInitializer != null : "Found NULL channel initializer for endpoint-qualifier " + endpointQualifier;
    Channel channel = networking.register(channelInitializer, socketChannel, clientMode);
    // Advanced Network
    if (endpointConfig != null) {
        setChannelOptions(channel, endpointConfig);
    }
    acceptedChannels.add(channel);
    return channel;
}
Also used : Networking(com.hazelcast.internal.networking.Networking) SocketChannel(java.nio.channels.SocketChannel) Channel(com.hazelcast.internal.networking.Channel) ChannelInitializer(com.hazelcast.internal.networking.ChannelInitializer)

Example 5 with Channel

use of com.hazelcast.internal.networking.Channel in project hazelcast by hazelcast.

the class TcpServerControlTest method setup.

@Before
public void setup() throws IllegalAccessException {
    HazelcastInstance hz = factory.newHazelcastInstance(createConfig());
    serializationService = getSerializationService(hz);
    Node node = getNode(hz);
    connectionManager = (TcpServerConnectionManager) node.getServer().getConnectionManager(EndpointQualifier.resolve(protocolType, protocolIdentifier));
    tcpServerControl = getFieldValueReflectively(connectionManager, "serverControl");
    lifecycleListener = getFieldValueReflectively(connectionManager, "connectionLifecycleListener");
    addressRegistry = node.getLocalAddressRegistry();
    // setup mock channel & socket
    Socket socket = mock(Socket.class);
    when(socket.getRemoteSocketAddress()).thenReturn(CLIENT_SOCKET_ADDRESS);
    channel = mock(Channel.class);
    ConcurrentMap channelAttributeMap = new ConcurrentHashMap();
    when(channel.attributeMap()).thenReturn(channelAttributeMap);
    when(channel.socket()).thenReturn(socket);
    when(channel.remoteSocketAddress()).thenReturn(CLIENT_SOCKET_ADDRESS);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Node(com.hazelcast.instance.impl.Node) Accessors.getNode(com.hazelcast.test.Accessors.getNode) Channel(com.hazelcast.internal.networking.Channel) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Socket(java.net.Socket) Before(org.junit.Before)

Aggregations

Channel (com.hazelcast.internal.networking.Channel)5 SocketChannel (java.nio.channels.SocketChannel)4 Socket (java.net.Socket)2 AuthenticationException (com.hazelcast.client.AuthenticationException)1 ClientNotAllowedInClusterException (com.hazelcast.client.ClientNotAllowedInClusterException)1 HazelcastClientNotActiveException (com.hazelcast.client.HazelcastClientNotActiveException)1 HazelcastClientOfflineException (com.hazelcast.client.HazelcastClientOfflineException)1 CandidateClusterContext (com.hazelcast.client.impl.clientside.CandidateClusterContext)1 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)1 HazelcastException (com.hazelcast.core.HazelcastException)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 Node (com.hazelcast.instance.impl.Node)1 MetricDescriptor (com.hazelcast.internal.metrics.MetricDescriptor)1 ChannelInitializer (com.hazelcast.internal.networking.ChannelInitializer)1 Networking (com.hazelcast.internal.networking.Networking)1 IOBalancer (com.hazelcast.internal.networking.nio.iobalancer.IOBalancer)1 SocketInterceptor (com.hazelcast.nio.SocketInterceptor)1 TargetDisconnectedException (com.hazelcast.spi.exception.TargetDisconnectedException)1 Accessors.getNode (com.hazelcast.test.Accessors.getNode)1 EOFException (java.io.EOFException)1