Search in sources :

Example 1 with NettySender

use of org.apache.ignite.internal.network.netty.NettySender in project ignite-3 by apache.

the class ItRecoveryHandshakeTest method testHandshakeWithMultipleClients.

/**
 * Tests that one server can handle multiple incoming connections and perform the handshake operation successfully.
 *
 * @throws Exception If failed.
 */
@Test
public void testHandshakeWithMultipleClients() throws Exception {
    ConnectionManager server = startManager(4000);
    List<ConnectionManager> clients = new ArrayList<>();
    int clientCount = 10;
    for (int i = 0; i < clientCount; i++) {
        clients.add(startManager(4001 + i));
    }
    // A key is the client's consistent id, a value is the channel between the client and the server
    var channelsToServer = new HashMap<String, NettySender>();
    for (ConnectionManager client : clients) {
        channelsToServer.put(client.consistentId(), client.channel(server.consistentId(), server.getLocalAddress()).get(3, TimeUnit.SECONDS));
    }
    assertTrue(waitForCondition(() -> server.channels().size() == clientCount, TimeUnit.SECONDS.toMillis(3)));
    Map<String, NettySender> channels = server.channels();
    // Assert that server's channels are inbound connections opened from clients
    channelsToServer.forEach((consistentId, toServer) -> {
        NettySender toClient = channels.get(consistentId);
        assertNotNull(toClient);
        assertEquals(toServer.channel().localAddress(), toClient.channel().remoteAddress());
    });
}
Also used : ConnectionManager(org.apache.ignite.internal.network.netty.ConnectionManager) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NettySender(org.apache.ignite.internal.network.netty.NettySender) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with NettySender

use of org.apache.ignite.internal.network.netty.NettySender in project ignite-3 by apache.

the class ItRecoveryHandshakeTest method testHandshakeFailsOnServerWhenClientResponded.

/**
 * Tests special handshake scenario: the client assumes a handshake has been finished, but the server fails on client's response. The
 * server will then close a connection and the client should get the "connection closed event".
 *
 * @throws Exception If failed.
 */
@Test
public void testHandshakeFailsOnServerWhenClientResponded() throws Exception {
    ConnectionManager manager1 = startManager(4000, SERVER_CLIENT_RESPONDED, CLIENT_DOESNT_FAIL);
    ConnectionManager manager2 = startManager(4001, SERVER_DOESNT_FAIL, CLIENT_DOESNT_FAIL);
    NettySender from2to1 = manager2.channel(manager1.consistentId(), manager1.getLocalAddress()).get(3, TimeUnit.SECONDS);
    from2to1.channel().closeFuture().get(3, TimeUnit.SECONDS);
}
Also used : ConnectionManager(org.apache.ignite.internal.network.netty.ConnectionManager) NettySender(org.apache.ignite.internal.network.netty.NettySender) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with NettySender

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

Example 4 with NettySender

use of org.apache.ignite.internal.network.netty.NettySender in project ignite-3 by apache.

the class RecoveryClientHandshakeManager method onMessage.

/**
 * {@inheritDoc}
 */
@Override
public HandshakeResult onMessage(Channel channel, NetworkMessage message) {
    if (message instanceof HandshakeStartMessage) {
        HandshakeStartMessage msg = (HandshakeStartMessage) message;
        UUID remoteLaunchId = msg.launchId();
        String remoteConsistentId = msg.consistentId();
        HandshakeStartResponseMessage response = messageFactory.handshakeStartResponseMessage().launchId(launchId).consistentId(consistentId).receivedCount(0).connectionsCount(0).build();
        ChannelFuture sendFuture = channel.writeAndFlush(new OutNetworkObject(response, Collections.emptyList()));
        NettyUtils.toCompletableFuture(sendFuture).whenComplete((unused, throwable) -> {
            if (throwable != null) {
                handshakeCompleteFuture.completeExceptionally(new HandshakeException("Failed to send handshake response: " + throwable.getMessage(), throwable));
            } else {
                handshakeCompleteFuture.complete(new NettySender(channel, remoteLaunchId.toString(), remoteConsistentId));
            }
        });
        return HandshakeResult.removeHandler(remoteLaunchId, remoteConsistentId);
    }
    handshakeCompleteFuture.completeExceptionally(new HandshakeException("Unexpected message during handshake: " + message.toString()));
    return HandshakeResult.fail();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HandshakeStartMessage(org.apache.ignite.internal.network.recovery.message.HandshakeStartMessage) HandshakeStartResponseMessage(org.apache.ignite.internal.network.recovery.message.HandshakeStartResponseMessage) UUID(java.util.UUID) OutNetworkObject(org.apache.ignite.network.OutNetworkObject) HandshakeException(org.apache.ignite.internal.network.handshake.HandshakeException) NettySender(org.apache.ignite.internal.network.netty.NettySender)

Example 5 with NettySender

use of org.apache.ignite.internal.network.netty.NettySender in project ignite-3 by apache.

the class RecoveryServerHandshakeManager method onMessage.

/**
 * {@inheritDoc}
 */
@Override
public HandshakeResult onMessage(Channel channel, NetworkMessage message) {
    if (message instanceof HandshakeStartResponseMessage) {
        HandshakeStartResponseMessage msg = (HandshakeStartResponseMessage) message;
        UUID remoteLaunchId = msg.launchId();
        String remoteConsistentId = msg.consistentId();
        handshakeCompleteFuture.complete(new NettySender(channel, remoteLaunchId.toString(), remoteConsistentId));
        return HandshakeResult.removeHandler(remoteLaunchId, remoteConsistentId);
    }
    handshakeCompleteFuture.completeExceptionally(new HandshakeException("Unexpected message during handshake: " + message.toString()));
    return HandshakeResult.fail();
}
Also used : HandshakeStartResponseMessage(org.apache.ignite.internal.network.recovery.message.HandshakeStartResponseMessage) UUID(java.util.UUID) HandshakeException(org.apache.ignite.internal.network.handshake.HandshakeException) NettySender(org.apache.ignite.internal.network.netty.NettySender)

Aggregations

NettySender (org.apache.ignite.internal.network.netty.NettySender)5 ConnectionManager (org.apache.ignite.internal.network.netty.ConnectionManager)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 UUID (java.util.UUID)2 HandshakeException (org.apache.ignite.internal.network.handshake.HandshakeException)2 HandshakeStartResponseMessage (org.apache.ignite.internal.network.recovery.message.HandshakeStartResponseMessage)2 OutNetworkObject (org.apache.ignite.network.OutNetworkObject)2 Test (org.junit.jupiter.api.Test)2 ChannelFuture (io.netty.channel.ChannelFuture)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HandshakeStartMessage (org.apache.ignite.internal.network.recovery.message.HandshakeStartMessage)1 TestMessage (org.apache.ignite.network.TestMessage)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1