Search in sources :

Example 1 with OutNetworkObject

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

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

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

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

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

the class ItRecoveryHandshakeTest method testHandshakeScenario.

/**
 * Tests handshake scenarios in which some of the parts of handshake protocol can fail.
 *
 * @param scenario Handshake scenario.
 * @throws Exception If failed.
 */
@ParameterizedTest
@MethodSource("handshakeScenarios")
public void testHandshakeScenario(HandshakeScenario scenario) throws Exception {
    ConnectionManager manager1 = startManager(4000, scenario.serverFailAt, CLIENT_DOESNT_FAIL);
    ConnectionManager manager2 = startManager(4001, SERVER_DOESNT_FAIL, scenario.clientFailAt);
    NettySender from2to1;
    try {
        from2to1 = manager2.channel(manager1.consistentId(), manager1.getLocalAddress()).get(3, TimeUnit.SECONDS);
    } catch (Exception e) {
        if (scenario.clientFailAt == CLIENT_DOESNT_FAIL && scenario.serverFailAt == SERVER_DOESNT_FAIL) {
            Assertions.fail(e);
        }
        return;
    }
    if (scenario.clientFailAt != CLIENT_DOESNT_FAIL || scenario.serverFailAt != SERVER_DOESNT_FAIL) {
        Assertions.fail("Handshake should've failed");
    }
    assertNotNull(from2to1);
    // Ensure the handshake has finished on both sides.
    TestMessage msg = messageFactory.testMessage().msg("test").build();
    from2to1.send(new OutNetworkObject(msg, Collections.emptyList())).get(3, TimeUnit.SECONDS);
    NettySender from1to2 = manager1.channel(manager2.consistentId(), manager2.getLocalAddress()).get(3, TimeUnit.SECONDS);
    assertNotNull(from1to2);
    assertEquals(from2to1.channel().localAddress(), from1to2.channel().remoteAddress());
}
Also used : ConnectionManager(org.apache.ignite.internal.network.netty.ConnectionManager) TestMessage(org.apache.ignite.network.TestMessage) OutNetworkObject(org.apache.ignite.network.OutNetworkObject) NettySender(org.apache.ignite.internal.network.netty.NettySender) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

OutNetworkObject (org.apache.ignite.network.OutNetworkObject)7 NetworkMessage (org.apache.ignite.network.NetworkMessage)4 TestMessage (org.apache.ignite.network.TestMessage)4 InetSocketAddress (java.net.InetSocketAddress)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Test (org.junit.jupiter.api.Test)3 ChannelFuture (io.netty.channel.ChannelFuture)2 HandshakeException (org.apache.ignite.internal.network.handshake.HandshakeException)2 NettySender (org.apache.ignite.internal.network.netty.NettySender)2 HandshakeStartMessage (org.apache.ignite.internal.network.recovery.message.HandshakeStartMessage)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 DecoderException (io.netty.handler.codec.DecoderException)1 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)1 IntOpenHashSet (it.unimi.dsi.fastutil.ints.IntOpenHashSet)1 IntSet (it.unimi.dsi.fastutil.ints.IntSet)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 UUID (java.util.UUID)1 ExecutionException (java.util.concurrent.ExecutionException)1