use of org.apache.ignite.network.OutNetworkObject 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;
}
use of org.apache.ignite.network.OutNetworkObject 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.OutNetworkObject in project ignite-3 by apache.
the class ItConnectionManagerTest method testReuseIncomingConnection.
/**
* Tests that incoming connection is reused for sending messages.
*
* @throws Exception If failed.
*/
@Test
public void testReuseIncomingConnection() throws Exception {
final String msgText = "test";
TestMessage testMessage = messageFactory.testMessage().msg("test").build();
int port1 = 4000;
int port2 = 4001;
ConnectionManager manager1 = startManager(port1).get1();
ConnectionManager manager2 = startManager(port2).get1();
var fut = new CompletableFuture<NetworkMessage>();
manager1.addListener((obj) -> fut.complete(obj.message()));
NettySender senderFrom1to2 = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
// Ensure a handshake has finished on both sides by sending a message.
// TODO: IGNITE-14085 When the recovery protocol is implemented replace this with simple
// CompletableFuture#get called on the send future.
var messageReceivedOn2 = new CompletableFuture<Void>();
// If the message is received, that means that the handshake was successfully performed.
manager2.addListener((message) -> messageReceivedOn2.complete(null));
senderFrom1to2.send(new OutNetworkObject(testMessage, Collections.emptyList()));
messageReceivedOn2.get(3, TimeUnit.SECONDS);
NettySender senderFrom2to1 = manager2.channel(manager1.consistentId(), new InetSocketAddress(port1)).get(3, TimeUnit.SECONDS);
InetSocketAddress clientLocalAddress = (InetSocketAddress) senderFrom1to2.channel().localAddress();
InetSocketAddress clientRemoteAddress = (InetSocketAddress) senderFrom2to1.channel().remoteAddress();
assertEquals(clientLocalAddress, clientRemoteAddress);
senderFrom2to1.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.OutNetworkObject in project ignite-3 by apache.
the class ItConnectionManagerTest method testSentSuccessfully.
/**
* Tests that a message is sent successfully using the ConnectionManager.
*
* @throws Exception If failed.
*/
@Test
public void testSentSuccessfully() throws Exception {
String msgText = "test";
int port1 = 4000;
int port2 = 4001;
ConnectionManager manager1 = startManager(port1).get1();
ConnectionManager manager2 = startManager(port2).get1();
var fut = new CompletableFuture<NetworkMessage>();
manager2.addListener((obj) -> fut.complete(obj.message()));
NettySender sender = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
TestMessage testMessage = messageFactory.testMessage().msg(msgText).build();
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.OutNetworkObject 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());
}
Aggregations