Search in sources :

Example 6 with NetworkMessage

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

the class ItConnectionManagerTest method testCanReconnectAfterFail.

/**
 * Tests that after a channel was closed, a new channel is opened upon a request.
 *
 * @throws Exception If failed.
 */
@Test
public void testCanReconnectAfterFail() throws Exception {
    String msgText = "test";
    int port1 = 4000;
    int port2 = 4001;
    ConnectionManager manager1 = startManager(port1).get1();
    IgniteBiTuple<ConnectionManager, NettyBootstrapFactory> manager2 = startManager(port2);
    NettySender sender = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
    TestMessage testMessage = messageFactory.testMessage().msg(msgText).build();
    manager2.get1().stop();
    manager2.get2().stop();
    final NettySender finalSender = sender;
    assertThrows(ClosedChannelException.class, () -> {
        try {
            finalSender.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
        } catch (Exception e) {
            throw e.getCause();
        }
    });
    manager2 = startManager(port2);
    var fut = new CompletableFuture<NetworkMessage>();
    manager2.get1().addListener((obj) -> fut.complete(obj.message()));
    sender = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
    sender.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
    NetworkMessage receivedMessage = fut.get(3, TimeUnit.SECONDS);
    assertEquals(msgText, ((TestMessage) receivedMessage).msg());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DecoderException(io.netty.handler.codec.DecoderException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) CompletableFuture(java.util.concurrent.CompletableFuture) TestMessage(org.apache.ignite.network.TestMessage) OutNetworkObject(org.apache.ignite.network.OutNetworkObject) NetworkMessage(org.apache.ignite.network.NetworkMessage) Test(org.junit.jupiter.api.Test)

Example 7 with NetworkMessage

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

the class ItConnectionManagerTest method testReuseIncomingConnection.

/**
 * Tests that incoming connection is reused for sending messages.
 *
 * @throws Exception If failed.
 */
@Test
public void testReuseIncomingConnection() throws Exception {
    final String msgText = "test";
    TestMessage testMessage = messageFactory.testMessage().msg("test").build();
    int port1 = 4000;
    int port2 = 4001;
    ConnectionManager manager1 = startManager(port1).get1();
    ConnectionManager manager2 = startManager(port2).get1();
    var fut = new CompletableFuture<NetworkMessage>();
    manager1.addListener((obj) -> fut.complete(obj.message()));
    NettySender senderFrom1to2 = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
    // Ensure a handshake has finished on both sides by sending a message.
    // TODO: IGNITE-14085 When the recovery protocol is implemented replace this with simple
    // CompletableFuture#get called on the send future.
    var messageReceivedOn2 = new CompletableFuture<Void>();
    // If the message is received, that means that the handshake was successfully performed.
    manager2.addListener((message) -> messageReceivedOn2.complete(null));
    senderFrom1to2.send(new OutNetworkObject(testMessage, Collections.emptyList()));
    messageReceivedOn2.get(3, TimeUnit.SECONDS);
    NettySender senderFrom2to1 = manager2.channel(manager1.consistentId(), new InetSocketAddress(port1)).get(3, TimeUnit.SECONDS);
    InetSocketAddress clientLocalAddress = (InetSocketAddress) senderFrom1to2.channel().localAddress();
    InetSocketAddress clientRemoteAddress = (InetSocketAddress) senderFrom2to1.channel().remoteAddress();
    assertEquals(clientLocalAddress, clientRemoteAddress);
    senderFrom2to1.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
    NetworkMessage receivedMessage = fut.get(3, TimeUnit.SECONDS);
    assertEquals(msgText, ((TestMessage) receivedMessage).msg());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) TestMessage(org.apache.ignite.network.TestMessage) OutNetworkObject(org.apache.ignite.network.OutNetworkObject) NetworkMessage(org.apache.ignite.network.NetworkMessage) Test(org.junit.jupiter.api.Test)

Example 8 with NetworkMessage

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

the class ItConnectionManagerTest method testSentSuccessfully.

/**
 * Tests that a message is sent successfully using the ConnectionManager.
 *
 * @throws Exception If failed.
 */
@Test
public void testSentSuccessfully() throws Exception {
    String msgText = "test";
    int port1 = 4000;
    int port2 = 4001;
    ConnectionManager manager1 = startManager(port1).get1();
    ConnectionManager manager2 = startManager(port2).get1();
    var fut = new CompletableFuture<NetworkMessage>();
    manager2.addListener((obj) -> fut.complete(obj.message()));
    NettySender sender = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
    TestMessage testMessage = messageFactory.testMessage().msg(msgText).build();
    sender.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
    NetworkMessage receivedMessage = fut.get(3, TimeUnit.SECONDS);
    assertEquals(msgText, ((TestMessage) receivedMessage).msg());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) TestMessage(org.apache.ignite.network.TestMessage) OutNetworkObject(org.apache.ignite.network.OutNetworkObject) NetworkMessage(org.apache.ignite.network.NetworkMessage) Test(org.junit.jupiter.api.Test)

Example 9 with NetworkMessage

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

Example 10 with NetworkMessage

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

the class MessageHandler method channelRead.

/**
 * {@inheritDoc}
 */
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    NetworkMessage message = (NetworkMessage) msg;
    messageListener.accept(new InNetworkObject(message, consistentId, serializationService.compositeDescriptorRegistry()));
}
Also used : NetworkMessage(org.apache.ignite.network.NetworkMessage)

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