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;
}
});
}
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;
}
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!";
}
}
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);
}
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;
}
Aggregations