use of org.apache.ignite.internal.network.serialization.SerializationService in project ignite-3 by apache.
the class InboundDecoderTest method testPartialReadWithReuseBuffer.
/**
* Tests that an {@link InboundDecoder} can handle a {@link ByteBuf} where reader index is not {@code 0} at the start of the {@link
* InboundDecoder#decode}.
*
* @throws Exception If failed.
*/
@Test
public void testPartialReadWithReuseBuffer() throws Exception {
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
var channel = new EmbeddedChannel();
Mockito.doReturn(channel).when(ctx).channel();
var serializationService = new SerializationService(registry, mock(UserObjectSerializationContext.class));
var perSessionSerializationService = new PerSessionSerializationService(serializationService);
final var decoder = new InboundDecoder(perSessionSerializationService);
final var list = new ArrayList<>();
var writer = new DirectMessageWriter(perSessionSerializationService, ConnectionManager.DIRECT_PROTOCOL_VERSION);
var msg = new TestMessagesFactory().testMessage().msg("abcdefghijklmn").build();
MessageSerializer<NetworkMessage> serializer = registry.createSerializer(msg.groupType(), msg.messageType());
ByteBuffer nioBuffer = ByteBuffer.allocate(10_000);
writer.setBuffer(nioBuffer);
// Write message to the ByteBuffer.
boolean fullyWritten = serializer.writeMessage(msg, writer);
assertTrue(fullyWritten);
nioBuffer.flip();
ByteBuf buffer = allocator.buffer();
// Write first 3 bytes of a message.
for (int i = 0; i < 3; i++) {
buffer.writeByte(nioBuffer.get());
}
decoder.decode(ctx, buffer, list);
// At this point a header and a first byte of a message have been decoded.
assertEquals(0, list.size());
// Write next 3 bytes of a message.
for (int i = 0; i < 3; i++) {
buffer.writeByte(nioBuffer.get());
}
// Reader index of a buffer is not zero and it must be handled correctly by the InboundDecoder.
decoder.decode(ctx, buffer, list);
// Check if reader index has been tracked correctly.
assertEquals(6, buffer.readerIndex());
assertEquals(0, list.size());
buffer.writeBytes(nioBuffer);
decoder.decode(ctx, buffer, list);
assertEquals(1, list.size());
TestMessage actualMessage = (TestMessage) list.get(0);
assertEquals(msg, actualMessage);
}
use of org.apache.ignite.internal.network.serialization.SerializationService 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.internal.network.serialization.SerializationService 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.internal.network.serialization.SerializationService 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;
}
use of org.apache.ignite.internal.network.serialization.SerializationService in project ignite-3 by apache.
the class ItRecoveryHandshakeTest method startManager.
/**
* Starts a {@link ConnectionManager} adding it to the {@link #startedManagers} list.
*
* @param port Port for the server.
* @param serverHandshakeFailAt At what stage to fail server handshake.
* @param clientHandshakeFailAt At what stage to fail client handshake.
* @return Connection manager.
*/
private ConnectionManager startManager(int port, ServerStageFail serverHandshakeFailAt, ClientStageFail clientHandshakeFailAt) {
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 FailingRecoveryServerHandshakeManager(launchId, consistentId, serverHandshakeFailAt, messageFactory), () -> new FailingRecoveryClientHandshakeManager(launchId, consistentId, clientHandshakeFailAt, messageFactory), bootstrapFactory);
manager.start();
startedManagers.add(manager);
return manager;
}
Aggregations