Search in sources :

Example 1 with NetworkMessage

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

the class AllTypesMessageGenerator method generate.

/**
 * Generate a new {@link AllTypesMessage}.
 *
 * @param seed      Random seed.
 * @param nestedMsg {@code true} if nested messages should be generated, {@code false} otherwise.
 * @return Message.
 */
public static AllTypesMessage generate(long seed, boolean nestedMsg, boolean fillArrays) {
    try {
        var random = new Random(seed);
        AllTypesMessageBuilder message = new TestMessagesFactory().allTypesMessage();
        Field[] fields = AllTypesMessageImpl.builder().getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (!field.getType().isArray() || fillArrays) {
                field.set(message, randomValue(random, field, nestedMsg));
            }
        }
        if (nestedMsg) {
            message.netMsgArrV(IntStream.range(0, 10).mapToObj(unused -> generate(seed, false, fillArrays)).toArray(NetworkMessage[]::new));
            message.netMsgCollW(IntStream.range(0, 10).mapToObj(unused -> generate(seed, false, fillArrays)).collect(toList()));
            message.newMsgMapX(IntStream.range(0, 10).boxed().collect(toMap(String::valueOf, unused -> generate(seed, false, fillArrays))));
        }
        return message.build();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Field(java.lang.reflect.Field) Random(java.util.Random) TestMessagesFactory(org.apache.ignite.network.TestMessagesFactory) NetworkMessage(org.apache.ignite.network.NetworkMessage)

Example 2 with NetworkMessage

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

use of org.apache.ignite.network.NetworkMessage 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 4 with NetworkMessage

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

the class ScaleCubeDirectMarshallerTransport method fromNetworkMessage.

/**
 * Unwrap ScaleCube {@link Message} from {@link NetworkMessage}.
 *
 * @param networkMessage Network message.
 * @return ScaleCube message.
 * @throws IgniteInternalException If failed to read ScaleCube message byte array.
 */
@Nullable
private Message fromNetworkMessage(NetworkMessage networkMessage) throws IgniteInternalException {
    if (networkMessage instanceof ScaleCubeMessage) {
        ScaleCubeMessage msg = (ScaleCubeMessage) networkMessage;
        Map<String, String> headers = msg.headers();
        Object obj = msg.data();
        NetworkMessage message = msg.message();
        Object data = obj != null ? obj : message;
        return Message.withHeaders(headers).data(data).build();
    }
    return null;
}
Also used : ScaleCubeMessage(org.apache.ignite.internal.network.message.ScaleCubeMessage) NetworkMessage(org.apache.ignite.network.NetworkMessage) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with NetworkMessage

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

the class MarshallableTest method write.

/**
 * Writes a map to a buffer through the {@link MessageWithMarshallable}.
 */
private ByteBuffer write(Map<String, SimpleSerializableObject> testMap) throws Exception {
    var serializers = new Serialization();
    var writer = new DirectMessageWriter(serializers.perSessionSerializationService, ConnectionManager.DIRECT_PROTOCOL_VERSION);
    MessageWithMarshallable msg = msgFactory.messageWithMarshallable().marshallableMap(testMap).build();
    IntSet ids = new IntOpenHashSet();
    msg.prepareMarshal(ids, serializers.userObjectSerializer);
    MessageSerializer<NetworkMessage> serializer = registry.createSerializer(msg.groupType(), msg.messageType());
    var catcher = new OutboundByteBufCatcher();
    var channel = new EmbeddedChannel(catcher, new ChunkedWriteHandler(), new OutboundEncoder(serializers.perSessionSerializationService));
    List<ClassDescriptorMessage> classDescriptorsMessages = PerSessionSerializationService.createClassDescriptorsMessages(ids, serializers.descriptorRegistry);
    channel.writeAndFlush(new OutNetworkObject(msg, classDescriptorsMessages));
    channel.flushOutbound();
    ByteBuffer nioBuffer = catcher.buf;
    return nioBuffer;
}
Also used : IntSet(it.unimi.dsi.fastutil.ints.IntSet) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuffer(java.nio.ByteBuffer) ClassDescriptorMessage(org.apache.ignite.internal.network.message.ClassDescriptorMessage) IntOpenHashSet(it.unimi.dsi.fastutil.ints.IntOpenHashSet) DirectMessageWriter(org.apache.ignite.internal.network.direct.DirectMessageWriter) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) OutboundEncoder(org.apache.ignite.internal.network.netty.OutboundEncoder) NetworkMessage(org.apache.ignite.network.NetworkMessage) OutNetworkObject(org.apache.ignite.network.OutNetworkObject)

Aggregations

NetworkMessage (org.apache.ignite.network.NetworkMessage)19 CompletableFuture (java.util.concurrent.CompletableFuture)8 ByteBuffer (java.nio.ByteBuffer)6 Test (org.junit.jupiter.api.Test)6 ClusterService (org.apache.ignite.network.ClusterService)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 NetworkAddress (org.apache.ignite.network.NetworkAddress)4 OutNetworkObject (org.apache.ignite.network.OutNetworkObject)4 TestMessage (org.apache.ignite.network.TestMessage)4 ByteBuf (io.netty.buffer.ByteBuf)3 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3 TimeUnit (java.util.concurrent.TimeUnit)3 DirectMessageWriter (org.apache.ignite.internal.network.direct.DirectMessageWriter)3 SerializationService (org.apache.ignite.internal.network.serialization.SerializationService)3 UserObjectSerializationContext (org.apache.ignite.internal.network.serialization.UserObjectSerializationContext)3 Nullable (org.jetbrains.annotations.Nullable)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Objects (java.util.Objects)2