use of org.apache.ignite.network.NetworkMessage in project ignite-3 by apache.
the class AllTypesMessageGenerator method generate.
/**
* Generate a new {@link AllTypesMessage}.
*
* @param seed Random seed.
* @param nestedMsg {@code true} if nested messages should be generated, {@code false} otherwise.
* @return Message.
*/
public static AllTypesMessage generate(long seed, boolean nestedMsg, boolean fillArrays) {
try {
var random = new Random(seed);
AllTypesMessageBuilder message = new TestMessagesFactory().allTypesMessage();
Field[] fields = AllTypesMessageImpl.builder().getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (!field.getType().isArray() || fillArrays) {
field.set(message, randomValue(random, field, nestedMsg));
}
}
if (nestedMsg) {
message.netMsgArrV(IntStream.range(0, 10).mapToObj(unused -> generate(seed, false, fillArrays)).toArray(NetworkMessage[]::new));
message.netMsgCollW(IntStream.range(0, 10).mapToObj(unused -> generate(seed, false, fillArrays)).collect(toList()));
message.newMsgMapX(IntStream.range(0, 10).boxed().collect(toMap(String::valueOf, unused -> generate(seed, false, fillArrays))));
}
return message.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.ignite.network.NetworkMessage 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.network.NetworkMessage 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.NetworkMessage in project ignite-3 by apache.
the class ScaleCubeDirectMarshallerTransport method fromNetworkMessage.
/**
* Unwrap ScaleCube {@link Message} from {@link NetworkMessage}.
*
* @param networkMessage Network message.
* @return ScaleCube message.
* @throws IgniteInternalException If failed to read ScaleCube message byte array.
*/
@Nullable
private Message fromNetworkMessage(NetworkMessage networkMessage) throws IgniteInternalException {
if (networkMessage instanceof ScaleCubeMessage) {
ScaleCubeMessage msg = (ScaleCubeMessage) networkMessage;
Map<String, String> headers = msg.headers();
Object obj = msg.data();
NetworkMessage message = msg.message();
Object data = obj != null ? obj : message;
return Message.withHeaders(headers).data(data).build();
}
return null;
}
use of org.apache.ignite.network.NetworkMessage 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;
}
Aggregations