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());
}
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());
}
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);
}
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);
}
}
};
}
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;
}
Aggregations