Search in sources :

Example 1 with CheckTransactionRpcCommand

use of org.infinispan.commands.remote.CheckTransactionRpcCommand in project infinispan by infinispan.

the class TransactionTable method cleanupTimedOutTransactions.

private void cleanupTimedOutTransactions() {
    if (log.isTraceEnabled())
        log.tracef("About to cleanup remote transactions older than %d ms", configuration.transaction().completedTxTimeout());
    long beginning = timeService.time();
    long cutoffCreationTime = beginning - TimeUnit.MILLISECONDS.toNanos(configuration.transaction().completedTxTimeout());
    List<GlobalTransaction> toKill = new ArrayList<>();
    Map<Address, Collection<GlobalTransaction>> toCheck = new HashMap<>();
    // Check remote transactions.
    for (Map.Entry<GlobalTransaction, RemoteTransaction> e : remoteTransactions.entrySet()) {
        GlobalTransaction gtx = e.getKey();
        RemoteTransaction remoteTx = e.getValue();
        // concurrent map doesn't accept null values
        assert remoteTx != null;
        if (log.isTraceEnabled()) {
            log.tracef("Checking transaction %s", gtx);
        }
        // Check the time.
        long creationTime = remoteTx.getCreationTime();
        if (creationTime - cutoffCreationTime >= 0) {
            // transaction still valid
            continue;
        }
        if (transactionOriginatorChecker.isOriginatorMissing(gtx)) {
            // originator no longer available. Transaction can be rolled back.
            long duration = timeService.timeDuration(creationTime, beginning, TimeUnit.MILLISECONDS);
            log.remoteTransactionTimeout(gtx, duration);
            toKill.add(gtx);
        } else {
            // originator alive or hot rod transaction
            Address orig = gtx.getAddress();
            if (rpcManager.getMembers().contains(orig)) {
                // originator still in view. Check if the transaction is valid.
                Collection<GlobalTransaction> addressCheckList = toCheck.computeIfAbsent(orig, k -> new ArrayList<>());
                addressCheckList.add(gtx);
            }
        // else, it is a hot rod transaction. don't kill it since the server reaper will take appropriate action
        }
    }
    // check if the transaction is running on originator
    for (Map.Entry<Address, Collection<GlobalTransaction>> entry : toCheck.entrySet()) {
        CheckTransactionRpcCommand cmd = commandsFactory.buildCheckTransactionRpcCommand(entry.getValue());
        RpcOptions rpcOptions = rpcManager.getSyncRpcOptions();
        rpcManager.invokeCommand(entry.getKey(), cmd, CheckTransactionRpcCommand.responseCollector(), rpcOptions).thenAccept(this::killAllTransactionsAsync);
    }
    // Rollback the orphaned transactions and release any held locks.
    killAllTransactionsAsync(toKill);
}
Also used : Address(org.infinispan.remoting.transport.Address) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GlobalTransaction(org.infinispan.transaction.xa.GlobalTransaction) CheckTransactionRpcCommand(org.infinispan.commands.remote.CheckTransactionRpcCommand) RpcOptions(org.infinispan.remoting.rpc.RpcOptions) Collection(java.util.Collection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 2 with CheckTransactionRpcCommand

use of org.infinispan.commands.remote.CheckTransactionRpcCommand in project infinispan by infinispan.

the class NoLockLostOnLongTxTest method testCheckTransactionRpcCommand.

public void testCheckTransactionRpcCommand() throws Exception {
    // default cache is transactional
    Cache<String, String> cache0 = cache(0);
    Cache<String, String> cache1 = cache(1);
    CommandsFactory factory = cache0.getAdvancedCache().getComponentRegistry().getCommandsFactory();
    RpcManager rpcManager = cache0.getAdvancedCache().getRpcManager();
    RpcOptions rpcOptions = rpcManager.getSyncRpcOptions();
    ResponseCollector<Collection<GlobalTransaction>> collector = CheckTransactionRpcCommand.responseCollector();
    Address remoteAddress = cache1.getAdvancedCache().getRpcManager().getAddress();
    TransactionTable transactionTable = cache1.getAdvancedCache().getComponentRegistry().getTransactionTable();
    CheckTransactionRpcCommand rpcCommand = factory.buildCheckTransactionRpcCommand(Collections.emptyList());
    Collection<GlobalTransaction> result = rpcManager.invokeCommand(remoteAddress, rpcCommand, collector, rpcOptions).toCompletableFuture().join();
    assertTrue("Expected an empty collection but got: " + result, result.isEmpty());
    TransactionManager tm = cache1.getAdvancedCache().getTransactionManager();
    tm.begin();
    cache1.put("k", "v");
    rpcCommand = factory.buildCheckTransactionRpcCommand(transactionTable.getLocalGlobalTransaction());
    result = rpcManager.invokeCommand(remoteAddress, rpcCommand, collector, rpcOptions).toCompletableFuture().join();
    assertTrue("Expected an empty collection but got: " + result, result.isEmpty());
    tm.commit();
    GlobalTransaction nonExistingGtx = new GlobalTransaction(remoteAddress, false);
    nonExistingGtx.setId(-1);
    Collection<GlobalTransaction> list = Collections.singletonList(nonExistingGtx);
    rpcCommand = factory.buildCheckTransactionRpcCommand(list);
    result = rpcManager.invokeCommand(remoteAddress, rpcCommand, collector, rpcOptions).toCompletableFuture().join();
    assertEquals("Wrong list returned.", list, result);
}
Also used : CommandsFactory(org.infinispan.commands.CommandsFactory) RpcManager(org.infinispan.remoting.rpc.RpcManager) Address(org.infinispan.remoting.transport.Address) TransactionTable(org.infinispan.transaction.impl.TransactionTable) GlobalTransaction(org.infinispan.transaction.xa.GlobalTransaction) CheckTransactionRpcCommand(org.infinispan.commands.remote.CheckTransactionRpcCommand) RpcOptions(org.infinispan.remoting.rpc.RpcOptions) EmbeddedTransactionManager(org.infinispan.transaction.tm.EmbeddedTransactionManager) TransactionManager(javax.transaction.TransactionManager) Collection(java.util.Collection)

Aggregations

Collection (java.util.Collection)2 CheckTransactionRpcCommand (org.infinispan.commands.remote.CheckTransactionRpcCommand)2 RpcOptions (org.infinispan.remoting.rpc.RpcOptions)2 Address (org.infinispan.remoting.transport.Address)2 GlobalTransaction (org.infinispan.transaction.xa.GlobalTransaction)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 TransactionManager (javax.transaction.TransactionManager)1 CommandsFactory (org.infinispan.commands.CommandsFactory)1 RpcManager (org.infinispan.remoting.rpc.RpcManager)1 TransactionTable (org.infinispan.transaction.impl.TransactionTable)1 EmbeddedTransactionManager (org.infinispan.transaction.tm.EmbeddedTransactionManager)1