Search in sources :

Example 1 with SerializationService

use of org.apache.ignite.internal.network.serialization.SerializationService in project ignite-3 by apache.

the class InboundDecoderTest method testPartialReadWithReuseBuffer.

/**
 * Tests that an {@link InboundDecoder} can handle a {@link ByteBuf} where reader index is not {@code 0} at the start of the {@link
 * InboundDecoder#decode}.
 *
 * @throws Exception If failed.
 */
@Test
public void testPartialReadWithReuseBuffer() throws Exception {
    ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
    var channel = new EmbeddedChannel();
    Mockito.doReturn(channel).when(ctx).channel();
    var serializationService = new SerializationService(registry, mock(UserObjectSerializationContext.class));
    var perSessionSerializationService = new PerSessionSerializationService(serializationService);
    final var decoder = new InboundDecoder(perSessionSerializationService);
    final var list = new ArrayList<>();
    var writer = new DirectMessageWriter(perSessionSerializationService, ConnectionManager.DIRECT_PROTOCOL_VERSION);
    var msg = new TestMessagesFactory().testMessage().msg("abcdefghijklmn").build();
    MessageSerializer<NetworkMessage> serializer = registry.createSerializer(msg.groupType(), msg.messageType());
    ByteBuffer nioBuffer = ByteBuffer.allocate(10_000);
    writer.setBuffer(nioBuffer);
    // Write message to the ByteBuffer.
    boolean fullyWritten = serializer.writeMessage(msg, writer);
    assertTrue(fullyWritten);
    nioBuffer.flip();
    ByteBuf buffer = allocator.buffer();
    // Write first 3 bytes of a message.
    for (int i = 0; i < 3; i++) {
        buffer.writeByte(nioBuffer.get());
    }
    decoder.decode(ctx, buffer, list);
    // At this point a header and a first byte of a message have been decoded.
    assertEquals(0, list.size());
    // Write next 3 bytes of a message.
    for (int i = 0; i < 3; i++) {
        buffer.writeByte(nioBuffer.get());
    }
    // Reader index of a buffer is not zero and it must be handled correctly by the InboundDecoder.
    decoder.decode(ctx, buffer, list);
    // Check if reader index has been tracked correctly.
    assertEquals(6, buffer.readerIndex());
    assertEquals(0, list.size());
    buffer.writeBytes(nioBuffer);
    decoder.decode(ctx, buffer, list);
    assertEquals(1, list.size());
    TestMessage actualMessage = (TestMessage) list.get(0);
    assertEquals(msg, actualMessage);
}
Also used : ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) TestMessagesFactory(org.apache.ignite.network.TestMessagesFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) DirectMessageWriter(org.apache.ignite.internal.network.direct.DirectMessageWriter) TestMessage(org.apache.ignite.network.TestMessage) NetworkMessage(org.apache.ignite.network.NetworkMessage) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with SerializationService

use of org.apache.ignite.internal.network.serialization.SerializationService 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 3 with SerializationService

use of org.apache.ignite.internal.network.serialization.SerializationService in project ignite-3 by apache.

the class ItConnectionManagerTest method startManager.

/**
 * Creates and starts a {@link ConnectionManager} listening on the given port, configured with the provided serialization registry.
 *
 * @param port     Port for the connection manager to listen on.
 * @param registry Serialization registry.
 * @return Connection manager.
 */
private IgniteBiTuple<ConnectionManager, NettyBootstrapFactory> startManager(int port, MessageSerializationRegistry registry) {
    UUID launchId = UUID.randomUUID();
    String consistentId = UUID.randomUUID().toString();
    var messageFactory = new NetworkMessagesFactory();
    networkConfiguration.port().update(port).join();
    NetworkView cfg = networkConfiguration.value();
    NettyBootstrapFactory bootstrapFactory = new NettyBootstrapFactory(networkConfiguration, consistentId);
    bootstrapFactory.start();
    startedBootstrapFactories.add(bootstrapFactory);
    var manager = new ConnectionManager(cfg, new SerializationService(registry, mock(UserObjectSerializationContext.class)), consistentId, () -> new RecoveryServerHandshakeManager(launchId, consistentId, messageFactory), () -> new RecoveryClientHandshakeManager(launchId, consistentId, messageFactory), bootstrapFactory);
    manager.start();
    startedManagers.add(manager);
    return new IgniteBiTuple<>(manager, bootstrapFactory);
}
Also used : NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) NetworkMessagesFactory(org.apache.ignite.internal.network.NetworkMessagesFactory) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) UUID(java.util.UUID) NetworkView(org.apache.ignite.configuration.schemas.network.NetworkView) RecoveryClientHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryClientHandshakeManager) RecoveryServerHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryServerHandshakeManager)

Example 4 with SerializationService

use of org.apache.ignite.internal.network.serialization.SerializationService in project ignite-3 by apache.

the class ItRecoveryHandshakeTest method startManager.

/**
 * Starts a {@link ConnectionManager} with a normal handshake manager adding it to the {@link #startedManagers} list.
 *
 * @param port Port.
 * @return Connection manager
 */
private ConnectionManager startManager(int port) {
    var registry = new TestMessageSerializationRegistryImpl();
    var serializationService = new SerializationService(registry, mock(UserObjectSerializationContext.class));
    var messageFactory = new NetworkMessagesFactory();
    UUID launchId = UUID.randomUUID();
    String consistentId = UUID.randomUUID().toString();
    networkConfiguration.port().update(port).join();
    NetworkView cfg = networkConfiguration.value();
    NettyBootstrapFactory bootstrapFactory = new NettyBootstrapFactory(networkConfiguration, consistentId);
    bootstrapFactory.start();
    startedBootstrapFactories.add(bootstrapFactory);
    var manager = new ConnectionManager(cfg, serializationService, consistentId, () -> new RecoveryServerHandshakeManager(launchId, consistentId, messageFactory), () -> new RecoveryClientHandshakeManager(launchId, consistentId, messageFactory), bootstrapFactory);
    manager.start();
    startedManagers.add(manager);
    return manager;
}
Also used : TestMessageSerializationRegistryImpl(org.apache.ignite.network.TestMessageSerializationRegistryImpl) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) ConnectionManager(org.apache.ignite.internal.network.netty.ConnectionManager) NetworkMessagesFactory(org.apache.ignite.internal.network.NetworkMessagesFactory) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) UUID(java.util.UUID) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) NetworkView(org.apache.ignite.configuration.schemas.network.NetworkView)

Example 5 with SerializationService

use of org.apache.ignite.internal.network.serialization.SerializationService in project ignite-3 by apache.

the class ItRecoveryHandshakeTest method startManager.

/**
 * Starts a {@link ConnectionManager} adding it to the {@link #startedManagers} list.
 *
 * @param port                  Port for the server.
 * @param serverHandshakeFailAt At what stage to fail server handshake.
 * @param clientHandshakeFailAt At what stage to fail client handshake.
 * @return Connection manager.
 */
private ConnectionManager startManager(int port, ServerStageFail serverHandshakeFailAt, ClientStageFail clientHandshakeFailAt) {
    var registry = new TestMessageSerializationRegistryImpl();
    var serializationService = new SerializationService(registry, mock(UserObjectSerializationContext.class));
    var messageFactory = new NetworkMessagesFactory();
    UUID launchId = UUID.randomUUID();
    String consistentId = UUID.randomUUID().toString();
    networkConfiguration.port().update(port).join();
    NetworkView cfg = networkConfiguration.value();
    NettyBootstrapFactory bootstrapFactory = new NettyBootstrapFactory(networkConfiguration, consistentId);
    bootstrapFactory.start();
    startedBootstrapFactories.add(bootstrapFactory);
    var manager = new ConnectionManager(cfg, serializationService, consistentId, () -> new FailingRecoveryServerHandshakeManager(launchId, consistentId, serverHandshakeFailAt, messageFactory), () -> new FailingRecoveryClientHandshakeManager(launchId, consistentId, clientHandshakeFailAt, messageFactory), bootstrapFactory);
    manager.start();
    startedManagers.add(manager);
    return manager;
}
Also used : TestMessageSerializationRegistryImpl(org.apache.ignite.network.TestMessageSerializationRegistryImpl) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) ConnectionManager(org.apache.ignite.internal.network.netty.ConnectionManager) NetworkMessagesFactory(org.apache.ignite.internal.network.NetworkMessagesFactory) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) UUID(java.util.UUID) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) NetworkView(org.apache.ignite.configuration.schemas.network.NetworkView)

Aggregations

SerializationService (org.apache.ignite.internal.network.serialization.SerializationService)8 UserObjectSerializationContext (org.apache.ignite.internal.network.serialization.UserObjectSerializationContext)7 NettyBootstrapFactory (org.apache.ignite.network.NettyBootstrapFactory)5 ByteBuf (io.netty.buffer.ByteBuf)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 UUID (java.util.UUID)4 NetworkView (org.apache.ignite.configuration.schemas.network.NetworkView)4 NetworkMessagesFactory (org.apache.ignite.internal.network.NetworkMessagesFactory)4 ConnectionManager (org.apache.ignite.internal.network.netty.ConnectionManager)3 PerSessionSerializationService (org.apache.ignite.internal.network.serialization.PerSessionSerializationService)3 NetworkMessage (org.apache.ignite.network.NetworkMessage)3 ByteBuffer (java.nio.ByteBuffer)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeUnit (java.util.concurrent.TimeUnit)2 NetworkConfiguration (org.apache.ignite.configuration.schemas.network.NetworkConfiguration)2 DirectMessageWriter (org.apache.ignite.internal.network.direct.DirectMessageWriter)2 RecoveryClientHandshakeManager (org.apache.ignite.internal.network.recovery.RecoveryClientHandshakeManager)2 RecoveryServerHandshakeManager (org.apache.ignite.internal.network.recovery.RecoveryServerHandshakeManager)2 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)2 TestMessageSerializationRegistryImpl (org.apache.ignite.network.TestMessageSerializationRegistryImpl)2