use of org.apache.ignite.network.NetworkMessage in project ignite-3 by apache.
the class RaftServerImpl method handleActionRequest.
/**
* Handle action request.
*
* @param sender The sender.
* @param req The request.
* @param corellationId Corellation id.
* @param queue The queue.
* @param lsnr The listener.
* @param <T> Command type.
*/
private <T extends Command> void handleActionRequest(NetworkAddress sender, ActionRequest req, Long corellationId, BlockingQueue<CommandClosureEx<T>> queue, RaftGroupListener lsnr) {
if (!queue.offer(new CommandClosureEx<>() {
@Override
public RaftGroupListener listener() {
return lsnr;
}
@Override
public T command() {
return (T) req.command();
}
@Override
public void result(Serializable res) {
NetworkMessage msg;
if (res instanceof Throwable) {
msg = clientMsgFactory.sMErrorResponse().error(new SMCompactedThrowable((Throwable) res)).build();
} else {
msg = clientMsgFactory.actionResponse().result(res).build();
}
service.messagingService().respond(sender, msg, corellationId);
}
})) {
// Queue out of capacity.
sendError(sender, corellationId, RaftError.EBUSY);
}
}
use of org.apache.ignite.network.NetworkMessage in project ignite-3 by apache.
the class ItLozaTest method testRaftServiceUsingSharedExecutor.
/**
* Tests that RaftGroupServiceImpl uses shared executor for retrying RaftGroupServiceImpl#sendWithRetry().
*/
@Test
public void testRaftServiceUsingSharedExecutor(TestInfo testInfo) throws Exception {
ClusterService service = null;
Loza loza = null;
RaftGroupService[] grpSrvcs = new RaftGroupService[5];
try {
service = spy(clusterService(testInfo, PORT, List.of()));
MessagingService messagingServiceMock = spy(service.messagingService());
when(service.messagingService()).thenReturn(messagingServiceMock);
CompletableFuture<NetworkMessage> exception = CompletableFuture.failedFuture(new Exception(new IOException()));
loza = new Loza(service, dataPath);
loza.start();
for (int i = 0; i < grpSrvcs.length; i++) {
// return an error on first invocation
doReturn(exception).doAnswer(invocation -> {
assertThat(Thread.currentThread().getName(), containsString(Loza.CLIENT_POOL_NAME));
return exception;
}).doCallRealMethod().when(messagingServiceMock).invoke(any(NetworkAddress.class), any(), anyLong());
grpSrvcs[i] = startClient(Integer.toString(i), service.topologyService().localMember(), loza);
verify(messagingServiceMock, times(3 * (i + 1))).invoke(any(NetworkAddress.class), any(), anyLong());
}
} finally {
for (RaftGroupService srvc : grpSrvcs) {
srvc.shutdown();
loza.stopRaftGroup(srvc.groupId());
}
if (loza != null) {
loza.stop();
}
if (service != null) {
service.stop();
}
}
}
use of org.apache.ignite.network.NetworkMessage in project ignite-3 by apache.
the class TxManagerImpl method finishRemote.
/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<Void> finishRemote(NetworkAddress addr, Timestamp ts, boolean commit, Set<String> groups) {
assert groups != null && !groups.isEmpty();
TxFinishRequest req = FACTORY.txFinishRequest().timestamp(ts).groups(groups).commit(commit).build();
CompletableFuture<NetworkMessage> fut = clusterService.messagingService().invoke(addr, req, TIMEOUT);
// Submit response to a dedicated pool to avoid deadlocks. TODO: IGNITE-15389
return fut.thenApplyAsync(resp -> ((TxFinishResponse) resp).errorMessage()).thenCompose(msg -> msg == null ? completedFuture(null) : failedFuture(new TransactionException(msg)));
}
use of org.apache.ignite.network.NetworkMessage in project ignite-3 by apache.
the class TxManagerImpl method onReceived.
/**
* {@inheritDoc}
*/
@Override
public void onReceived(NetworkMessage message, NetworkAddress senderAddr, @Nullable Long correlationId) {
// Support raft and transactions interop.
if (message instanceof TxFinishRequest) {
TxFinishRequest req = (TxFinishRequest) message;
Set<String> groups = req.groups();
CompletableFuture[] futs = new CompletableFuture[groups.size()];
int i = 0;
// Finish a tx for enlisted groups.
for (String grp : groups) {
futs[i++] = finish(grp, req.timestamp(), req.commit());
}
CompletableFuture.allOf(futs).thenCompose(ignored -> req.commit() ? commitAsync(req.timestamp()) : rollbackAsync(req.timestamp())).handle(new BiFunction<Void, Throwable, Void>() {
@Override
public Void apply(Void ignored, Throwable err) {
TxFinishResponseBuilder resp = FACTORY.txFinishResponse();
if (err != null) {
resp.errorMessage(err.getMessage());
}
clusterService.messagingService().respond(senderAddr, resp.build(), correlationId);
return null;
}
});
}
}
use of org.apache.ignite.network.NetworkMessage in project ignite-3 by apache.
the class ItScaleCubeNetworkMessagingTest method testInvokeDuringStop.
/**
* Tests that if the network component is stopped while waiting for a response to an "invoke" call, the corresponding future completes
* exceptionally.
*/
@Test
public void testInvokeDuringStop(TestInfo testInfo) throws InterruptedException {
testCluster = new Cluster(2, testInfo);
testCluster.startAwait();
ClusterService member0 = testCluster.members.get(0);
ClusterService member1 = testCluster.members.get(1);
// we don't register a message listener on the receiving side, so all "invoke"s should timeout
// perform two invokes to test that multiple requests can get cancelled
CompletableFuture<NetworkMessage> invoke0 = member0.messagingService().invoke(member1.topologyService().localMember(), messageFactory.testMessage().build(), 1000);
CompletableFuture<NetworkMessage> invoke1 = member0.messagingService().invoke(member1.topologyService().localMember(), messageFactory.testMessage().build(), 1000);
member0.stop();
ExecutionException e = assertThrows(ExecutionException.class, () -> invoke0.get(1, TimeUnit.SECONDS));
assertThat(e.getCause(), instanceOf(NodeStoppingException.class));
e = assertThrows(ExecutionException.class, () -> invoke1.get(1, TimeUnit.SECONDS));
assertThat(e.getCause(), instanceOf(NodeStoppingException.class));
}
Aggregations