Search in sources :

Example 1 with MessageDeserializer

use of org.apache.ignite.network.serialization.MessageDeserializer 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 MessageDeserializer

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

the class SerializationFactoryGenerator method generateFactory.

/**
 * Generates a {@link MessageSerializationFactory} class for the given network message type.
 *
 * @param message      network message
 * @param serializer   {@link MessageSerializer}, generated for the given {@code message}
 * @param deserializer {@link MessageDeserializer}, generated for the given {@code message}
 * @return {@code TypeSpec} of the generated {@code MessageSerializationFactory}
 */
public TypeSpec generateFactory(MessageClass message, TypeSpec serializer, TypeSpec deserializer) {
    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Generating a MessageSerializationFactory", message.element());
    ClassName messageFactoryClassName = messageGroup.messageFactoryClassName();
    FieldSpec messageFactoryField = FieldSpec.builder(messageFactoryClassName, "messageFactory").addModifiers(Modifier.PRIVATE, Modifier.FINAL).build();
    return TypeSpec.classBuilder(message.simpleName() + "SerializationFactory").addModifiers(Modifier.PUBLIC).addSuperinterface(ParameterizedTypeName.get(ClassName.get(MessageSerializationFactory.class), message.className())).addField(messageFactoryField).addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(messageFactoryClassName, "messageFactory").addStatement("this.$N = messageFactory", messageFactoryField).build()).addMethod(MethodSpec.methodBuilder("createDeserializer").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(ParameterizedTypeName.get(ClassName.get(MessageDeserializer.class), message.className())).addStatement("return new $N($N)", deserializer, messageFactoryField).build()).addMethod(MethodSpec.methodBuilder("createSerializer").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(ParameterizedTypeName.get(ClassName.get(MessageSerializer.class), message.className())).addStatement("return new $N()", serializer).build()).addOriginatingElement(message.element()).addOriginatingElement(messageGroup.element()).build();
}
Also used : MessageDeserializer(org.apache.ignite.network.serialization.MessageDeserializer) ClassName(com.squareup.javapoet.ClassName) MessageSerializationFactory(org.apache.ignite.network.serialization.MessageSerializationFactory) FieldSpec(com.squareup.javapoet.FieldSpec)

Example 3 with MessageDeserializer

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

the class InboundDecoder method decode.

/**
 * {@inheritDoc}
 */
@Override
public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    ByteBuffer buffer = in.nioBuffer();
    Attribute<MessageReader> readerAttr = ctx.channel().attr(READER_KEY);
    MessageReader reader = readerAttr.get();
    if (reader == null) {
        reader = new DirectMessageReader(serializationService, ConnectionManager.DIRECT_PROTOCOL_VERSION);
        readerAttr.set(reader);
    }
    Attribute<MessageDeserializer<NetworkMessage>> messageAttr = ctx.channel().attr(DESERIALIZER_KEY);
    while (buffer.hasRemaining()) {
        int initialNioBufferPosition = buffer.position();
        MessageDeserializer<NetworkMessage> msg = messageAttr.get();
        try {
            // Read message type.
            if (msg == null) {
                if (buffer.remaining() >= NetworkMessage.MSG_TYPE_SIZE_BYTES) {
                    msg = serializationService.createMessageDeserializer(DirectMarshallingUtils.getShort(buffer), DirectMarshallingUtils.getShort(buffer));
                } else {
                    break;
                }
            }
            boolean finished = false;
            // Read message if buffer has remaining data.
            if (msg != null && buffer.hasRemaining()) {
                reader.setCurrentReadClass(msg.klass());
                reader.setBuffer(buffer);
                finished = msg.readMessage(reader);
            }
            int readBytes = buffer.position() - initialNioBufferPosition;
            // Set read position to Netty's ByteBuf.
            in.readerIndex(in.readerIndex() + readBytes);
            if (finished) {
                reader.reset();
                messageAttr.set(null);
                NetworkMessage message = msg.getMessage();
                if (message instanceof ClassDescriptorListMessage) {
                    onClassDescriptorMessage((ClassDescriptorListMessage) message);
                } else {
                    out.add(message);
                }
            } else {
                messageAttr.set(msg);
            }
            if (readBytes == 0) {
                // less than N bytes available.
                break;
            }
        } catch (Throwable e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("Failed to read message [msg=%s, buf=%s, reader=%s]: %s", msg, buffer, reader, e.getMessage()), e);
            }
            throw e;
        }
    }
}
Also used : ClassDescriptorListMessage(org.apache.ignite.internal.network.message.ClassDescriptorListMessage) DirectMessageReader(org.apache.ignite.internal.network.direct.DirectMessageReader) MessageDeserializer(org.apache.ignite.network.serialization.MessageDeserializer) MessageReader(org.apache.ignite.network.serialization.MessageReader) DirectMessageReader(org.apache.ignite.internal.network.direct.DirectMessageReader) NetworkMessage(org.apache.ignite.network.NetworkMessage) ByteBuffer(java.nio.ByteBuffer)

Aggregations

MessageDeserializer (org.apache.ignite.network.serialization.MessageDeserializer)3 NetworkMessage (org.apache.ignite.network.NetworkMessage)2 MessageReader (org.apache.ignite.network.serialization.MessageReader)2 ClassName (com.squareup.javapoet.ClassName)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 Bootstrap (io.netty.bootstrap.Bootstrap)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 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 ByteBuffer (java.nio.ByteBuffer)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 NetworkConfiguration (org.apache.ignite.configuration.schemas.network.NetworkConfiguration)1