Search in sources :

Example 1 with NettyBootstrapFactory

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

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

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

the class ItConnectionManagerTest method startManager.

/**
 * Creates and starts a {@link ConnectionManager} listening on the given port, configured with the provided serialization registry.
 *
 * @param port     Port for the connection manager to listen on.
 * @param registry Serialization registry.
 * @return Connection manager.
 */
private IgniteBiTuple<ConnectionManager, NettyBootstrapFactory> startManager(int port, MessageSerializationRegistry registry) {
    UUID launchId = UUID.randomUUID();
    String consistentId = UUID.randomUUID().toString();
    var messageFactory = new NetworkMessagesFactory();
    networkConfiguration.port().update(port).join();
    NetworkView cfg = networkConfiguration.value();
    NettyBootstrapFactory bootstrapFactory = new NettyBootstrapFactory(networkConfiguration, consistentId);
    bootstrapFactory.start();
    startedBootstrapFactories.add(bootstrapFactory);
    var manager = new ConnectionManager(cfg, new SerializationService(registry, mock(UserObjectSerializationContext.class)), consistentId, () -> new RecoveryServerHandshakeManager(launchId, consistentId, messageFactory), () -> new RecoveryClientHandshakeManager(launchId, consistentId, messageFactory), bootstrapFactory);
    manager.start();
    startedManagers.add(manager);
    return new IgniteBiTuple<>(manager, bootstrapFactory);
}
Also used : NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) NetworkMessagesFactory(org.apache.ignite.internal.network.NetworkMessagesFactory) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) UUID(java.util.UUID) NetworkView(org.apache.ignite.configuration.schemas.network.NetworkView) RecoveryClientHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryClientHandshakeManager) RecoveryServerHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryServerHandshakeManager)

Example 4 with NettyBootstrapFactory

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

the class ClusterServiceTestUtils method clusterService.

/**
 * Creates a cluster service and required node configuration manager beneath it. Populates node configuration with specified port.
 * Manages configuration manager lifecycle: on cluster service start starts node configuration manager, on cluster service stop - stops
 * node configuration manager.
 *
 * @param testInfo                 Test info.
 * @param port                     Local port.
 * @param nodeFinder               Node finder.
 * @param clusterSvcFactory        Cluster service factory.
 */
public static ClusterService clusterService(TestInfo testInfo, int port, NodeFinder nodeFinder, TestScaleCubeClusterServiceFactory clusterSvcFactory) {
    var registry = new MessageSerializationRegistryImpl();
    REGISTRY_INITIALIZERS.forEach(c -> {
        try {
            c.invoke(c.getDeclaringClass(), registry);
        } catch (Throwable e) {
            throw new RuntimeException("Failed to invoke registry initializer", e);
        }
    });
    var ctx = new ClusterLocalConfiguration(testNodeName(testInfo, port), registry);
    ConfigurationManager nodeConfigurationMgr = new ConfigurationManager(Collections.singleton(NetworkConfiguration.KEY), Map.of(), new TestConfigurationStorage(ConfigurationType.LOCAL), List.of(), List.of());
    NetworkConfiguration configuration = nodeConfigurationMgr.configurationRegistry().getConfiguration(NetworkConfiguration.KEY);
    var bootstrapFactory = new NettyBootstrapFactory(configuration, ctx.getName());
    var clusterSvc = clusterSvcFactory.createClusterService(ctx, configuration, bootstrapFactory);
    assert nodeFinder instanceof StaticNodeFinder : "Only StaticNodeFinder is supported at the moment";
    return new ClusterService() {

        @Override
        public TopologyService topologyService() {
            return clusterSvc.topologyService();
        }

        @Override
        public MessagingService messagingService() {
            return clusterSvc.messagingService();
        }

        @Override
        public ClusterLocalConfiguration localConfiguration() {
            return clusterSvc.localConfiguration();
        }

        @Override
        public boolean isStopped() {
            return clusterSvc.isStopped();
        }

        @Override
        public void start() {
            nodeConfigurationMgr.start();
            NetworkConfiguration configuration = nodeConfigurationMgr.configurationRegistry().getConfiguration(NetworkConfiguration.KEY);
            configuration.change(netCfg -> netCfg.changePort(port).changeNodeFinder(c -> c.changeType(NodeFinderType.STATIC.toString()).changeNetClusterNodes(nodeFinder.findNodes().stream().map(NetworkAddress::toString).toArray(String[]::new)))).join();
            bootstrapFactory.start();
            clusterSvc.start();
        }

        @Override
        public void stop() {
            try {
                clusterSvc.stop();
                bootstrapFactory.stop();
                nodeConfigurationMgr.stop();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
}
Also used : IntStream(java.util.stream.IntStream) ClassGraph(io.github.classgraph.ClassGraph) NodeFinderType(org.apache.ignite.configuration.schemas.network.NodeFinderType) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) ConfigurationType(org.apache.ignite.configuration.annotation.ConfigurationType) IgniteTestUtils.testNodeName(org.apache.ignite.internal.testframework.IgniteTestUtils.testNodeName) Collectors.toUnmodifiableList(java.util.stream.Collectors.toUnmodifiableList) ArrayList(java.util.ArrayList) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) NetworkConfiguration(org.apache.ignite.configuration.schemas.network.NetworkConfiguration) Map(java.util.Map) NodeFinder(org.apache.ignite.network.NodeFinder) ScanResult(io.github.classgraph.ScanResult) Method(java.lang.reflect.Method) MessagingService(org.apache.ignite.network.MessagingService) MessageSerializationRegistryInitializer(org.apache.ignite.network.serialization.MessageSerializationRegistryInitializer) TopologyService(org.apache.ignite.network.TopologyService) ClusterLocalConfiguration(org.apache.ignite.network.ClusterLocalConfiguration) ClassInfo(io.github.classgraph.ClassInfo) ConfigurationManager(org.apache.ignite.internal.configuration.ConfigurationManager) TestInfo(org.junit.jupiter.api.TestInfo) NetworkAddress(org.apache.ignite.network.NetworkAddress) List(java.util.List) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) MessageSerializationRegistry(org.apache.ignite.network.serialization.MessageSerializationRegistry) TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) ClusterService(org.apache.ignite.network.ClusterService) NetworkMessagesSerializationRegistryInitializer(org.apache.ignite.internal.network.NetworkMessagesSerializationRegistryInitializer) MessageSerializationRegistryImpl(org.apache.ignite.network.MessageSerializationRegistryImpl) Collections(java.util.Collections) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) ClusterService(org.apache.ignite.network.ClusterService) MessageSerializationRegistryImpl(org.apache.ignite.network.MessageSerializationRegistryImpl) NetworkConfiguration(org.apache.ignite.configuration.schemas.network.NetworkConfiguration) ClusterLocalConfiguration(org.apache.ignite.network.ClusterLocalConfiguration) ConfigurationManager(org.apache.ignite.internal.configuration.ConfigurationManager)

Example 5 with NettyBootstrapFactory

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

the class ItRecoveryHandshakeTest method startManager.

/**
 * Starts a {@link ConnectionManager} with a normal handshake manager adding it to the {@link #startedManagers} list.
 *
 * @param port Port.
 * @return Connection manager
 */
private ConnectionManager startManager(int port) {
    var registry = new TestMessageSerializationRegistryImpl();
    var serializationService = new SerializationService(registry, mock(UserObjectSerializationContext.class));
    var messageFactory = new NetworkMessagesFactory();
    UUID launchId = UUID.randomUUID();
    String consistentId = UUID.randomUUID().toString();
    networkConfiguration.port().update(port).join();
    NetworkView cfg = networkConfiguration.value();
    NettyBootstrapFactory bootstrapFactory = new NettyBootstrapFactory(networkConfiguration, consistentId);
    bootstrapFactory.start();
    startedBootstrapFactories.add(bootstrapFactory);
    var manager = new ConnectionManager(cfg, serializationService, consistentId, () -> new RecoveryServerHandshakeManager(launchId, consistentId, messageFactory), () -> new RecoveryClientHandshakeManager(launchId, consistentId, messageFactory), bootstrapFactory);
    manager.start();
    startedManagers.add(manager);
    return manager;
}
Also used : TestMessageSerializationRegistryImpl(org.apache.ignite.network.TestMessageSerializationRegistryImpl) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) ConnectionManager(org.apache.ignite.internal.network.netty.ConnectionManager) NetworkMessagesFactory(org.apache.ignite.internal.network.NetworkMessagesFactory) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) UUID(java.util.UUID) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) NetworkView(org.apache.ignite.configuration.schemas.network.NetworkView)

Aggregations

NettyBootstrapFactory (org.apache.ignite.network.NettyBootstrapFactory)9 SerializationService (org.apache.ignite.internal.network.serialization.SerializationService)5 UUID (java.util.UUID)4 NetworkConfiguration (org.apache.ignite.configuration.schemas.network.NetworkConfiguration)4 NetworkView (org.apache.ignite.configuration.schemas.network.NetworkView)4 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)4 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 NetworkMessagesFactory (org.apache.ignite.internal.network.NetworkMessagesFactory)3 ConnectionManager (org.apache.ignite.internal.network.netty.ConnectionManager)3 UserObjectSerializationContext (org.apache.ignite.internal.network.serialization.UserObjectSerializationContext)3 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeUnit (java.util.concurrent.TimeUnit)2 ConfigurationManager (org.apache.ignite.internal.configuration.ConfigurationManager)2 NetworkMessage (org.apache.ignite.network.NetworkMessage)2 TestMessageSerializationRegistryImpl (org.apache.ignite.network.TestMessageSerializationRegistryImpl)2 MessageSerializationRegistry (org.apache.ignite.network.serialization.MessageSerializationRegistry)2