Search in sources :

Example 1 with TxFinishRequest

use of org.apache.ignite.internal.tx.message.TxFinishRequest 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 2 with TxFinishRequest

use of org.apache.ignite.internal.tx.message.TxFinishRequest 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 3 with TxFinishRequest

use of org.apache.ignite.internal.tx.message.TxFinishRequest in project ignite-3 by apache.

the class MessagingServiceTestUtils method mockMessagingService.

/**
 * Prepares messaging service mock.
 *
 * @param txManager Transaction manager.
 * @return Messaging service mock.
 */
public static MessagingService mockMessagingService(TxManager txManager) {
    MessagingService messagingService = Mockito.mock(MessagingService.class, RETURNS_DEEP_STUBS);
    doAnswer(invocationClose -> {
        assert invocationClose.getArgument(1) instanceof TxFinishRequest;
        TxFinishRequest txFinishRequest = invocationClose.getArgument(1);
        if (txFinishRequest.commit()) {
            txManager.commitAsync(txFinishRequest.timestamp()).get();
        } else {
            txManager.rollbackAsync(txFinishRequest.timestamp()).get();
        }
        return CompletableFuture.completedFuture(mock(TxFinishResponse.class));
    }).when(messagingService).invoke(any(NetworkAddress.class), any(TxFinishRequest.class), anyLong());
    return messagingService;
}
Also used : TxFinishResponse(org.apache.ignite.internal.tx.message.TxFinishResponse) TxFinishRequest(org.apache.ignite.internal.tx.message.TxFinishRequest) NetworkAddress(org.apache.ignite.network.NetworkAddress) MessagingService(org.apache.ignite.network.MessagingService)

Aggregations

TxFinishRequest (org.apache.ignite.internal.tx.message.TxFinishRequest)3 TxFinishResponse (org.apache.ignite.internal.tx.message.TxFinishResponse)3 NetworkAddress (org.apache.ignite.network.NetworkAddress)3 ByteBuffer (java.nio.ByteBuffer)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Set (java.util.Set)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)2 CompletableFuture.failedFuture (java.util.concurrent.CompletableFuture.failedFuture)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BiFunction (java.util.function.BiFunction)2 InternalTransaction (org.apache.ignite.internal.tx.InternalTransaction)2 LockException (org.apache.ignite.internal.tx.LockException)2 LockManager (org.apache.ignite.internal.tx.LockManager)2 Timestamp (org.apache.ignite.internal.tx.Timestamp)2 TxManager (org.apache.ignite.internal.tx.TxManager)2 TxState (org.apache.ignite.internal.tx.TxState)2 TxFinishResponseBuilder (org.apache.ignite.internal.tx.message.TxFinishResponseBuilder)2