Search in sources :

Example 1 with DirectMessageWriter

use of org.apache.ignite.internal.network.direct.DirectMessageWriter 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 DirectMessageWriter

use of org.apache.ignite.internal.network.direct.DirectMessageWriter 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)

Example 3 with DirectMessageWriter

use of org.apache.ignite.internal.network.direct.DirectMessageWriter in project ignite-3 by apache.

the class InboundDecoderTest method sendAndReceive.

/**
 * Serializes and then deserializes the given message.
 */
private <T extends NetworkMessage> T sendAndReceive(T msg) {
    var serializationService = new SerializationService(registry, mock(UserObjectSerializationContext.class));
    var perSessionSerializationService = new PerSessionSerializationService(serializationService);
    var channel = new EmbeddedChannel(new InboundDecoder(perSessionSerializationService));
    var writer = new DirectMessageWriter(perSessionSerializationService, ConnectionManager.DIRECT_PROTOCOL_VERSION);
    MessageSerializer<NetworkMessage> serializer = registry.createSerializer(msg.groupType(), msg.messageType());
    ByteBuffer buf = ByteBuffer.allocate(10_000);
    T received;
    do {
        buf.clear();
        writer.setBuffer(buf);
        serializer.writeMessage(msg, writer);
        buf.flip();
        ByteBuf buffer = allocator.buffer(buf.limit());
        buffer.writeBytes(buf);
        channel.writeInbound(buffer);
    } while ((received = channel.readInbound()) == null);
    return received;
}
Also used : DirectMessageWriter(org.apache.ignite.internal.network.direct.DirectMessageWriter) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) NetworkMessage(org.apache.ignite.network.NetworkMessage) ByteBuf(io.netty.buffer.ByteBuf) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) ByteBuffer(java.nio.ByteBuffer)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 ByteBuffer (java.nio.ByteBuffer)3 DirectMessageWriter (org.apache.ignite.internal.network.direct.DirectMessageWriter)3 NetworkMessage (org.apache.ignite.network.NetworkMessage)3 ByteBuf (io.netty.buffer.ByteBuf)2 PerSessionSerializationService (org.apache.ignite.internal.network.serialization.PerSessionSerializationService)2 SerializationService (org.apache.ignite.internal.network.serialization.SerializationService)2 UserObjectSerializationContext (org.apache.ignite.internal.network.serialization.UserObjectSerializationContext)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)1 IntOpenHashSet (it.unimi.dsi.fastutil.ints.IntOpenHashSet)1 IntSet (it.unimi.dsi.fastutil.ints.IntSet)1 ArrayList (java.util.ArrayList)1 ClassDescriptorMessage (org.apache.ignite.internal.network.message.ClassDescriptorMessage)1 OutboundEncoder (org.apache.ignite.internal.network.netty.OutboundEncoder)1 OutNetworkObject (org.apache.ignite.network.OutNetworkObject)1 TestMessage (org.apache.ignite.network.TestMessage)1 TestMessagesFactory (org.apache.ignite.network.TestMessagesFactory)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1