Search in sources :

Example 1 with HandshakeManager

use of org.apache.ignite.internal.network.handshake.HandshakeManager in project ignite-3 by apache.

the class NettyServerTest method testHandshakeManagerInvoked.

/**
 * Tests that handshake manager is invoked upon a client connecting to a server.
 *
 * @throws Exception If failed.
 */
@Test
public void testHandshakeManagerInvoked() throws Exception {
    HandshakeManager handshakeManager = mock(HandshakeManager.class);
    when(handshakeManager.handshakeFuture()).thenReturn(CompletableFuture.completedFuture(mock(NettySender.class)));
    HandshakeResult noOp = HandshakeResult.noOp();
    when(handshakeManager.init(any())).thenReturn(noOp);
    when(handshakeManager.onConnectionOpen(any())).thenReturn(noOp);
    when(handshakeManager.onMessage(any(), any())).thenReturn(noOp);
    MessageSerializationRegistry registry = mock(MessageSerializationRegistry.class);
    when(registry.createDeserializer(anyShort(), anyShort())).thenReturn(new MessageDeserializer<>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean readMessage(MessageReader reader) throws MessageMappingException {
            return true;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Class<NetworkMessage> klass() {
            return NetworkMessage.class;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public NetworkMessage getMessage() {
            return mock(NetworkMessage.class);
        }
    });
    bootstrapFactory = new NettyBootstrapFactory(serverCfg, "");
    bootstrapFactory.start();
    server = new NettyServer(serverCfg.value(), () -> handshakeManager, sender -> {
    }, (message) -> {
    }, new SerializationService(registry, mock(UserObjectSerializationContext.class)), bootstrapFactory);
    server.start().get(3, TimeUnit.SECONDS);
    CompletableFuture<Channel> connectFut = NettyUtils.toChannelCompletableFuture(new Bootstrap().channel(NioSocketChannel.class).group(new NioEventLoopGroup()).handler(new ChannelInitializer<>() {

        /**
         * {@inheritDoc}
         */
        @Override
        protected void initChannel(Channel ch) throws Exception {
        // No-op.
        }
    }).connect(server.address()));
    Channel channel = connectFut.get(3, TimeUnit.SECONDS);
    ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
    // One message only.
    for (int i = 0; i < (NetworkMessage.MSG_TYPE_SIZE_BYTES + 1); i++) {
        buffer.writeByte(1);
    }
    channel.writeAndFlush(buffer).get(3, TimeUnit.SECONDS);
    channel.close().get(3, TimeUnit.SECONDS);
    InOrder order = Mockito.inOrder(handshakeManager);
    order.verify(handshakeManager, timeout()).init(any());
    order.verify(handshakeManager, timeout()).handshakeFuture();
    order.verify(handshakeManager, timeout()).onConnectionOpen(any());
    order.verify(handshakeManager, timeout()).onMessage(any(), any());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) CompletableFuture(java.util.concurrent.CompletableFuture) MessageReader(org.apache.ignite.network.serialization.MessageReader) ArgumentMatchers.anyShort(org.mockito.ArgumentMatchers.anyShort) InjectConfiguration(org.apache.ignite.internal.configuration.testframework.InjectConfiguration) HandshakeResult(org.apache.ignite.internal.network.handshake.HandshakeResult) ByteBuf(io.netty.buffer.ByteBuf) ChannelPromise(io.netty.channel.ChannelPromise) NetworkConfiguration(org.apache.ignite.configuration.schemas.network.NetworkConfiguration) MessageMappingException(org.apache.ignite.network.serialization.MessageMappingException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ConfigurationExtension(org.apache.ignite.internal.configuration.testframework.ConfigurationExtension) MessageDeserializer(org.apache.ignite.network.serialization.MessageDeserializer) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) InOrder(org.mockito.InOrder) ChannelInitializer(io.netty.channel.ChannelInitializer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NetworkMessage(org.apache.ignite.network.NetworkMessage) Mockito.when(org.mockito.Mockito.when) ServerChannel(io.netty.channel.ServerChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) VerificationMode(org.mockito.verification.VerificationMode) Bootstrap(io.netty.bootstrap.Bootstrap) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) MessageSerializationRegistry(org.apache.ignite.network.serialization.MessageSerializationRegistry) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HandshakeManager(org.apache.ignite.internal.network.handshake.HandshakeManager) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) Mockito.mock(org.mockito.Mockito.mock) HandshakeResult(org.apache.ignite.internal.network.handshake.HandshakeResult) MessageMappingException(org.apache.ignite.network.serialization.MessageMappingException) InOrder(org.mockito.InOrder) MessageSerializationRegistry(org.apache.ignite.network.serialization.MessageSerializationRegistry) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) MessageReader(org.apache.ignite.network.serialization.MessageReader) ByteBuf(io.netty.buffer.ByteBuf) MessageMappingException(org.apache.ignite.network.serialization.MessageMappingException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) HandshakeManager(org.apache.ignite.internal.network.handshake.HandshakeManager) Bootstrap(io.netty.bootstrap.Bootstrap) NetworkMessage(org.apache.ignite.network.NetworkMessage) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test)

Example 2 with HandshakeManager

use of org.apache.ignite.internal.network.handshake.HandshakeManager in project ignite-3 by apache.

the class NettyServer method start.

/**
 * Starts the server.
 *
 * @return Future that resolves when the server is successfully started.
 */
public CompletableFuture<Void> start() {
    synchronized (startStopLock) {
        if (stopped) {
            throw new IgniteInternalException("Attempted to start an already stopped server");
        }
        if (serverStartFuture != null) {
            throw new IgniteInternalException("Attempted to start an already started server");
        }
        ServerBootstrap bootstrap = bootstrapFactory.createServerBootstrap();
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void initChannel(SocketChannel ch) {
                var sessionSerializationService = new PerSessionSerializationService(serializationService);
                // Get handshake manager for the new channel.
                HandshakeManager manager = handshakeManager.get();
                ch.pipeline().addLast(/*
                                     * Decoder that uses the MessageReader
                                     * to read chunked data.
                                     */
                new InboundDecoder(sessionSerializationService), // Handshake handler.
                new HandshakeHandler(manager, (consistentId) -> new MessageHandler(messageListener, consistentId, sessionSerializationService)), /*
                                     * Encoder that uses the MessageWriter
                                     * to write chunked data.
                                     */
                new ChunkedWriteHandler(), // Converts NetworkMessage to a ChunkedNetworkMessageInput
                new OutboundEncoder(sessionSerializationService), new IoExceptionSuppressingHandler());
                manager.handshakeFuture().thenAccept(newConnectionListener);
            }
        });
        int port = configuration.port();
        int portRange = configuration.portRange();
        var bindFuture = new CompletableFuture<Channel>();
        tryBind(bootstrap, port, port + portRange, port, bindFuture);
        serverStartFuture = bindFuture.handle((channel, err) -> {
            synchronized (startStopLock) {
                if (channel != null) {
                    serverCloseFuture = NettyUtils.toCompletableFuture(channel.closeFuture());
                }
                this.channel = (ServerChannel) channel;
                if (err != null || stopped) {
                    Throwable stopErr = err != null ? err : new CancellationException("Server was stopped");
                    return CompletableFuture.<Void>failedFuture(stopErr);
                } else {
                    return CompletableFuture.<Void>completedFuture(null);
                }
            }
        }).thenCompose(Function.identity());
        return serverStartFuture;
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) CompletableFuture(java.util.concurrent.CompletableFuture) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) CancellationException(java.util.concurrent.CancellationException) HandshakeManager(org.apache.ignite.internal.network.handshake.HandshakeManager)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)2 HandshakeManager (org.apache.ignite.internal.network.handshake.HandshakeManager)2 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelPromise (io.netty.channel.ChannelPromise)1 ServerChannel (io.netty.channel.ServerChannel)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)1 CancellationException (java.util.concurrent.CancellationException)1 TimeUnit (java.util.concurrent.TimeUnit)1 NetworkConfiguration (org.apache.ignite.configuration.schemas.network.NetworkConfiguration)1