Search in sources :

Example 11 with NetworkMessage

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);
    }
}
Also used : Serializable(java.io.Serializable) SMCompactedThrowable(org.apache.ignite.raft.jraft.rpc.impl.SMCompactedThrowable) SMCompactedThrowable(org.apache.ignite.raft.jraft.rpc.impl.SMCompactedThrowable) NetworkMessage(org.apache.ignite.network.NetworkMessage)

Example 12 with NetworkMessage

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();
        }
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ClusterServiceTestUtils(org.apache.ignite.utils.ClusterServiceTestUtils) CompletableFuture(java.util.concurrent.CompletableFuture) RaftGroupListener(org.apache.ignite.raft.client.service.RaftGroupListener) Mockito.spy(org.mockito.Mockito.spy) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Mockito.doReturn(org.mockito.Mockito.doReturn) Path(java.nio.file.Path) MessagingService(org.apache.ignite.network.MessagingService) NetworkMessage(org.apache.ignite.network.NetworkMessage) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) TestInfo(org.junit.jupiter.api.TestInfo) Mockito.verify(org.mockito.Mockito.verify) WorkDirectory(org.apache.ignite.internal.testframework.WorkDirectory) ClusterNode(org.apache.ignite.network.ClusterNode) NetworkAddress(org.apache.ignite.network.NetworkAddress) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) ClusterService(org.apache.ignite.network.ClusterService) WorkDirectoryExtension(org.apache.ignite.internal.testframework.WorkDirectoryExtension) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) ClusterService(org.apache.ignite.network.ClusterService) NetworkAddress(org.apache.ignite.network.NetworkAddress) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) IOException(java.io.IOException) NetworkMessage(org.apache.ignite.network.NetworkMessage) IOException(java.io.IOException) MessagingService(org.apache.ignite.network.MessagingService) Test(org.junit.jupiter.api.Test)

Example 13 with NetworkMessage

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)));
}
Also used : TransactionException(org.apache.ignite.tx.TransactionException) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) BiFunction(java.util.function.BiFunction) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) IgniteStringFormatter.format(org.apache.ignite.lang.IgniteStringFormatter.format) TxFinishRequest(org.apache.ignite.internal.tx.message.TxFinishRequest) ByteBuffer(java.nio.ByteBuffer) TxState(org.apache.ignite.internal.tx.TxState) Map(java.util.Map) TxMessageGroup(org.apache.ignite.internal.tx.message.TxMessageGroup) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) NetworkMessageHandler(org.apache.ignite.network.NetworkMessageHandler) TxFinishResponse(org.apache.ignite.internal.tx.message.TxFinishResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) NetworkMessage(org.apache.ignite.network.NetworkMessage) Timestamp(org.apache.ignite.internal.tx.Timestamp) TxManager(org.apache.ignite.internal.tx.TxManager) TxFinishResponseBuilder(org.apache.ignite.internal.tx.message.TxFinishResponseBuilder) NetworkAddress(org.apache.ignite.network.NetworkAddress) Objects(java.util.Objects) TestOnly(org.jetbrains.annotations.TestOnly) CompletableFuture.failedFuture(java.util.concurrent.CompletableFuture.failedFuture) Nullable(org.jetbrains.annotations.Nullable) LockException(org.apache.ignite.internal.tx.LockException) LockManager(org.apache.ignite.internal.tx.LockManager) TxMessagesFactory(org.apache.ignite.internal.tx.message.TxMessagesFactory) ClusterService(org.apache.ignite.network.ClusterService) IgniteUuid(org.apache.ignite.lang.IgniteUuid) TxFinishResponse(org.apache.ignite.internal.tx.message.TxFinishResponse) TxFinishRequest(org.apache.ignite.internal.tx.message.TxFinishRequest) TransactionException(org.apache.ignite.tx.TransactionException) NetworkMessage(org.apache.ignite.network.NetworkMessage)

Example 14 with NetworkMessage

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;
            }
        });
    }
}
Also used : TransactionException(org.apache.ignite.tx.TransactionException) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) BiFunction(java.util.function.BiFunction) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) IgniteStringFormatter.format(org.apache.ignite.lang.IgniteStringFormatter.format) TxFinishRequest(org.apache.ignite.internal.tx.message.TxFinishRequest) ByteBuffer(java.nio.ByteBuffer) TxState(org.apache.ignite.internal.tx.TxState) Map(java.util.Map) TxMessageGroup(org.apache.ignite.internal.tx.message.TxMessageGroup) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) NetworkMessageHandler(org.apache.ignite.network.NetworkMessageHandler) TxFinishResponse(org.apache.ignite.internal.tx.message.TxFinishResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) NetworkMessage(org.apache.ignite.network.NetworkMessage) Timestamp(org.apache.ignite.internal.tx.Timestamp) TxManager(org.apache.ignite.internal.tx.TxManager) TxFinishResponseBuilder(org.apache.ignite.internal.tx.message.TxFinishResponseBuilder) NetworkAddress(org.apache.ignite.network.NetworkAddress) Objects(java.util.Objects) TestOnly(org.jetbrains.annotations.TestOnly) CompletableFuture.failedFuture(java.util.concurrent.CompletableFuture.failedFuture) Nullable(org.jetbrains.annotations.Nullable) LockException(org.apache.ignite.internal.tx.LockException) LockManager(org.apache.ignite.internal.tx.LockManager) TxMessagesFactory(org.apache.ignite.internal.tx.message.TxMessagesFactory) ClusterService(org.apache.ignite.network.ClusterService) IgniteUuid(org.apache.ignite.lang.IgniteUuid) TxFinishRequest(org.apache.ignite.internal.tx.message.TxFinishRequest) CompletableFuture(java.util.concurrent.CompletableFuture) TxFinishResponseBuilder(org.apache.ignite.internal.tx.message.TxFinishResponseBuilder)

Example 15 with NetworkMessage

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));
}
Also used : ClusterService(org.apache.ignite.network.ClusterService) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) NetworkMessage(org.apache.ignite.network.NetworkMessage) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

NetworkMessage (org.apache.ignite.network.NetworkMessage)19 CompletableFuture (java.util.concurrent.CompletableFuture)8 ByteBuffer (java.nio.ByteBuffer)6 Test (org.junit.jupiter.api.Test)6 ClusterService (org.apache.ignite.network.ClusterService)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 NetworkAddress (org.apache.ignite.network.NetworkAddress)4 OutNetworkObject (org.apache.ignite.network.OutNetworkObject)4 TestMessage (org.apache.ignite.network.TestMessage)4 ByteBuf (io.netty.buffer.ByteBuf)3 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3 TimeUnit (java.util.concurrent.TimeUnit)3 DirectMessageWriter (org.apache.ignite.internal.network.direct.DirectMessageWriter)3 SerializationService (org.apache.ignite.internal.network.serialization.SerializationService)3 UserObjectSerializationContext (org.apache.ignite.internal.network.serialization.UserObjectSerializationContext)3 Nullable (org.jetbrains.annotations.Nullable)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Objects (java.util.Objects)2