Search in sources :

Example 6 with AsyncInterceptorChain

use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.

the class ClusteredGetCommand method invokeAsync.

/**
 * Invokes a logical "get(key)" on a remote cache and returns results.
 * @return
 */
@Override
public CompletionStage<?> invokeAsync(ComponentRegistry componentRegistry) throws Throwable {
    // make sure the get command doesn't perform a remote call
    // as our caller is already calling the ClusteredGetCommand on all the relevant nodes
    // CACHE_MODE_LOCAL is not used as it can be used when we want to ignore the ownership with respect to reads
    long flagBitSet = EnumUtil.bitSetOf(Flag.SKIP_REMOTE_LOOKUP);
    int segmentToUse;
    if (segment != null) {
        segmentToUse = segment;
    } else {
        segmentToUse = componentRegistry.getComponent(KeyPartitioner.class).getSegment(key);
    }
    // This code and the Flag can be removed when https://issues.redhat.com/browse/ISPN-12332 is complete
    if (isWrite) {
        TransactionConfiguration transactionConfiguration = componentRegistry.getConfiguration().transaction();
        if (transactionConfiguration.transactionMode() == TransactionMode.TRANSACTIONAL) {
            if (transactionConfiguration.lockingMode() == LockingMode.PESSIMISTIC) {
                flagBitSet = EnumUtil.mergeBitSets(flagBitSet, FlagBitSets.ALREADY_HAS_LOCK);
            }
        }
    }
    GetCacheEntryCommand command = componentRegistry.getCommandsFactory().buildGetCacheEntryCommand(key, segmentToUse, EnumUtil.mergeBitSets(flagBitSet, getFlagsBitSet()));
    command.setTopologyId(topologyId);
    InvocationContextFactory icf = componentRegistry.getInvocationContextFactory().running();
    InvocationContext invocationContext = icf.createRemoteInvocationContextForCommand(command, getOrigin());
    AsyncInterceptorChain invoker = componentRegistry.getInterceptorChain().running();
    return invoker.invokeAsync(invocationContext, command).thenApply(rv -> {
        if (log.isTraceEnabled())
            log.tracef("Return value for key=%s is %s", key, rv);
        // this might happen if the value was fetched from a cache loader
        if (rv instanceof MVCCEntry) {
            MVCCEntry<?, ?> mvccEntry = (MVCCEntry<?, ?>) rv;
            InternalCacheValue<?> icv = componentRegistry.getInternalEntryFactory().wired().createValue(mvccEntry);
            icv.setInternalMetadata(mvccEntry.getInternalMetadata());
            return icv;
        } else if (rv instanceof InternalCacheEntry) {
            InternalCacheEntry<?, ?> internalCacheEntry = (InternalCacheEntry<?, ?>) rv;
            return internalCacheEntry.toInternalCacheValue();
        } else {
            // null or Response
            return rv;
        }
    });
}
Also used : TransactionConfiguration(org.infinispan.configuration.cache.TransactionConfiguration) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) MVCCEntry(org.infinispan.container.entries.MVCCEntry) InvocationContext(org.infinispan.context.InvocationContext) GetCacheEntryCommand(org.infinispan.commands.read.GetCacheEntryCommand) InvocationContextFactory(org.infinispan.context.InvocationContextFactory)

Example 7 with AsyncInterceptorChain

use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.

the class BaseDistSyncL1Test method addBlockingInterceptor.

protected BlockingInterceptor addBlockingInterceptor(Cache<?, ?> cache, final CyclicBarrier barrier, Class<? extends VisitableCommand> commandClass, Class<? extends AsyncInterceptor> interceptorPosition, boolean blockAfterCommand) {
    BlockingInterceptor bi = new BlockingInterceptor<>(barrier, commandClass, blockAfterCommand, false);
    AsyncInterceptorChain interceptorChain = cache.getAdvancedCache().getAsyncInterceptorChain();
    assertTrue(interceptorChain.addInterceptorBefore(bi, interceptorPosition));
    return bi;
}
Also used : AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain)

Example 8 with AsyncInterceptorChain

use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.

the class OngoingTransactionsAndJoinTest method testRehashOnJoin.

public void testRehashOnJoin() throws InterruptedException {
    Cache<Object, Object> firstNode = cache(0);
    final CountDownLatch txsStarted = new CountDownLatch(3), txsReady = new CountDownLatch(3), joinEnded = new CountDownLatch(1), rehashStarted = new CountDownLatch(1);
    wrapInboundInvocationHandler(firstNode, original -> new ListeningHandler(original, txsReady, joinEnded, rehashStarted));
    for (int i = 0; i < 10; i++) firstNode.put("OLD" + i, "value");
    UnpreparedDuringRehashTask ut = new UnpreparedDuringRehashTask(firstNode, txsStarted, txsReady, joinEnded, rehashStarted);
    PrepareDuringRehashTask pt = new PrepareDuringRehashTask(firstNode, txsStarted, txsReady, joinEnded, rehashStarted);
    CommitDuringRehashTask ct = new CommitDuringRehashTask(firstNode, txsStarted, txsReady, joinEnded, rehashStarted);
    AsyncInterceptorChain ic = firstNode.getAdvancedCache().getAsyncInterceptorChain();
    ic.addInterceptorAfter(pt, TxInterceptor.class);
    ic.addInterceptorAfter(ct, TxInterceptor.class);
    Set<Thread> threads = new HashSet<>();
    threads.add(new Thread(ut, "Worker-UnpreparedDuringRehashTask"));
    threads.add(new Thread(pt, "Worker-PrepareDuringRehashTask"));
    threads.add(new Thread(ct, "Worker-CommitDuringRehashTask"));
    for (Thread t : threads) t.start();
    txsStarted.await(10, SECONDS);
    // we don't have a hook for the start of the rehash any more
    delayedExecutor.schedule(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            rehashStarted.countDown();
            return null;
        }
    }, 10, TimeUnit.MILLISECONDS);
    // start a new node!
    addClusterEnabledCacheManager(configuration);
    ListeningHandler listeningHandler2 = new ListeningHandler(extractComponent(firstNode, PerCacheInboundInvocationHandler.class), txsReady, joinEnded, rehashStarted);
    replaceComponent(cache(1), PerCacheInboundInvocationHandler.class, listeningHandler2, true);
    Cache<?, ?> joiner = cache(1);
    for (Thread t : threads) t.join();
    TestingUtil.waitForNoRebalance(cache(0), cache(1));
    for (int i = 0; i < 10; i++) {
        Object key = "OLD" + i;
        Object value = joiner.get(key);
        log.infof(" TEST: Key %s is %s", key, value);
        assert "value".equals(value) : "Couldn't see key " + key + " on joiner!";
    }
    for (Object key : Arrays.asList(ut.key(), pt.key(), ct.key())) {
        Object value = joiner.get(key);
        log.infof(" TEST: Key %s is %s", key, value);
        assert "value".equals(value) : "Couldn't see key " + key + " on joiner!";
    }
}
Also used : PerCacheInboundInvocationHandler(org.infinispan.remoting.inboundhandler.PerCacheInboundInvocationHandler) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet)

Example 9 with AsyncInterceptorChain

use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.

the class StaleLocksWithCommitDuringStateTransferTest method doTestSuspect.

/**
 * Check that the transaction commit/rollback recovers if the remote node dies during the RPC
 */
private void doTestSuspect(boolean commit) throws Exception {
    MagicKey k1 = new MagicKey("k1", c1);
    MagicKey k2 = new MagicKey("k2", c2);
    tm(c1).begin();
    c1.put(k1, "v1");
    c1.put(k2, "v2");
    // We split the transaction commit in two phases by calling the TransactionCoordinator methods directly
    TransactionTable txTable = TestingUtil.extractComponent(c1, TransactionTable.class);
    TransactionCoordinator txCoordinator = TestingUtil.extractComponent(c1, TransactionCoordinator.class);
    // Execute the prepare on both nodes
    LocalTransaction localTx = txTable.getLocalTransaction(tm(c1).getTransaction());
    CompletionStages.join(txCoordinator.prepare(localTx));
    // Delay the commit on the remote node. Can't used blockNewTransactions because we don't want a StateTransferInProgressException
    AsyncInterceptorChain c2ic = c2.getAdvancedCache().getAsyncInterceptorChain();
    c2ic.addInterceptorBefore(new DelayCommandInterceptor(), StateTransferInterceptor.class);
    // Schedule the remote node to stop on another thread since the main thread will be busy with the commit call
    Thread worker = new Thread("RehasherSim,StaleLocksWithCommitDuringStateTransferTest") {

        @Override
        public void run() {
            try {
                // should be much larger than the lock acquisition timeout
                Thread.sleep(1000);
                manager(c2).stop();
            // stLock.unblockNewTransactions(1000);
            } catch (InterruptedException e) {
                log.errorf(e, "Error stopping cache");
            }
        }
    };
    worker.start();
    try {
        // finally commit or rollback the transaction
        if (commit) {
            CompletionStages.join(txCoordinator.commit(localTx, false));
        } else {
            CompletionStages.join(txCoordinator.rollback(localTx));
        }
        // make the transaction manager forget about our tx so that we don't get rollback exceptions in the log
        tm(c1).suspend();
    } finally {
        // don't leak threads
        worker.join();
    }
    // test that we don't leak locks
    assertEventuallyNotLocked(c1, k1);
    assertEventuallyNotLocked(c1, k2);
}
Also used : LocalTransaction(org.infinispan.transaction.impl.LocalTransaction) TransactionTable(org.infinispan.transaction.impl.TransactionTable) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) TransactionCoordinator(org.infinispan.transaction.impl.TransactionCoordinator) MagicKey(org.infinispan.distribution.MagicKey)

Example 10 with AsyncInterceptorChain

use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.

the class BaseClusteredExtendedStatisticTest method getExtendedStatistic.

private ExtendedStatisticInterceptor getExtendedStatistic(Cache<?, ?> cache) {
    AsyncInterceptorChain interceptorChain = cache.getAdvancedCache().getAsyncInterceptorChain();
    ExtendedStatisticInterceptor extendedStatisticInterceptor = interceptorChain.findInterceptorExtending(ExtendedStatisticInterceptor.class);
    if (extendedStatisticInterceptor != null) {
        extendedStatisticInterceptor.resetStatistics();
    }
    return extendedStatisticInterceptor;
}
Also used : AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) ExtendedStatisticInterceptor(org.infinispan.extendedstats.wrappers.ExtendedStatisticInterceptor)

Aggregations

AsyncInterceptorChain (org.infinispan.interceptors.AsyncInterceptorChain)53 InvocationContext (org.infinispan.context.InvocationContext)15 CompletableFuture (java.util.concurrent.CompletableFuture)13 VisitableCommand (org.infinispan.commands.VisitableCommand)9 AsyncInterceptor (org.infinispan.interceptors.AsyncInterceptor)9 Future (java.util.concurrent.Future)8 SingleKeyNonTxInvocationContext (org.infinispan.context.impl.SingleKeyNonTxInvocationContext)8 BaseAsyncInterceptor (org.infinispan.interceptors.BaseAsyncInterceptor)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 TestException (org.infinispan.test.TestException)7 CompletableFutures (org.infinispan.util.concurrent.CompletableFutures)7 SECONDS (java.util.concurrent.TimeUnit.SECONDS)6 ExecutionException (java.util.concurrent.ExecutionException)5 TimeoutException (java.util.concurrent.TimeoutException)5 LockControlCommand (org.infinispan.commands.control.LockControlCommand)5 Test (org.testng.annotations.Test)5 BasicComponentRegistry (org.infinispan.factories.impl.BasicComponentRegistry)4 EntryWrappingInterceptor (org.infinispan.interceptors.impl.EntryWrappingInterceptor)4 AbstractInfinispanTest (org.infinispan.test.AbstractInfinispanTest)4 AssertJUnit.assertEquals (org.testng.AssertJUnit.assertEquals)4