use of org.infinispan.commands.functional.ReadWriteKeyCommand in project infinispan by infinispan.
the class AbstractFunctionalTest method expectReadWriteKeyCommand.
protected CountDownLatch expectReadWriteKeyCommand(AdvancedCache cache, Predicate<Object> valuePredicate, int numUpdates) {
if (!cacheMode.isInvalidation()) {
CountDownLatch latch = new CountDownLatch(numUpdates);
ExpectingInterceptor.get(cache).when((ctx, cmd) -> cmd instanceof ReadWriteKeyCommand && valuePredicate.test(((ReadWriteKeyCommand) cmd).getFunction())).countDown(latch);
cleanup.add(() -> ExpectingInterceptor.cleanup(cache));
return latch;
} else {
return new CountDownLatch(0);
}
}
use of org.infinispan.commands.functional.ReadWriteKeyCommand in project infinispan by infinispan.
the class EntityCollectionInvalidationTest method testAll.
@Test
public void testAll() throws Exception {
log.infof(name.getMethodName());
assertEmptyCaches();
assertTrue(remoteListener.isEmpty());
assertTrue(localListener.isEmpty());
log.debug("Create node 0");
IdContainer ids = createCustomer(localFactory);
assertTrue(remoteListener.isEmpty());
assertTrue(localListener.isEmpty());
// The create customer above removes the collection from the cache (see CollectionRecreateAction)
// and therefore the putFromLoad in getCustomer could be considered stale if executed too soon.
timeService.advance(1);
CountDownLatch remoteCollectionLoadLatch = null;
if (!cacheMode.isInvalidation()) {
remoteCollectionLoadLatch = new CountDownLatch(1);
ExpectingInterceptor.get(remoteCollectionCache).when((ctx, cmd) -> cmd instanceof ReadWriteKeyCommand).countDown(remoteCollectionLoadLatch);
}
log.debug("Find node 0");
// This actually brings the collection into the cache
getCustomer(ids.customerId, localFactory);
// Now the collection is in the cache so, the 2nd "get"
// should read everything from the cache
log.debug("Find(2) node 0");
localListener.clear();
getCustomer(ids.customerId, localFactory);
// Check the read came from the cache
log.debug("Check cache 0");
assertLoadedFromCache(localListener, ids.customerId, ids.contactIds);
if (remoteCollectionLoadLatch != null) {
log.debug("Wait for remote collection put from load to complete");
assertTrue(remoteCollectionLoadLatch.await(2, TimeUnit.SECONDS));
ExpectingInterceptor.cleanup(remoteCollectionCache);
}
log.debug("Find node 1");
// This actually brings the collection into the cache since invalidation is in use
getCustomer(ids.customerId, remoteFactory);
// Now the collection is in the cache so, the 2nd "get"
// should read everything from the cache
log.debug("Find(2) node 1");
remoteListener.clear();
getCustomer(ids.customerId, remoteFactory);
// Check the read came from the cache
log.debug("Check cache 1");
assertLoadedFromCache(remoteListener, ids.customerId, ids.contactIds);
// Modify customer in remote
remoteListener.clear();
CountDownLatch modifyLatch = null;
if (!cacheMode.isInvalidation() && accessType != AccessType.NONSTRICT_READ_WRITE) {
modifyLatch = new CountDownLatch(1);
ExpectingInterceptor.get(localCustomerCache).when(this::isFutureUpdate).countDown(modifyLatch);
}
ids = modifyCustomer(ids.customerId, remoteFactory);
assertLoadedFromCache(remoteListener, ids.customerId, ids.contactIds);
if (modifyLatch != null) {
assertTrue(modifyLatch.await(2, TimeUnit.SECONDS));
ExpectingInterceptor.cleanup(localCustomerCache);
}
assertEquals(0, localCollectionRegion.getElementCountInMemory());
if (cacheMode.isInvalidation()) {
// After modification, local cache should have been invalidated and hence should be empty
assertEquals(0, localCustomerRegion.getElementCountInMemory());
} else {
// Replicated cache is updated, not invalidated
assertEquals(1, localCustomerRegion.getElementCountInMemory());
}
}
use of org.infinispan.commands.functional.ReadWriteKeyCommand in project infinispan by infinispan.
the class AbstractPartialUpdateTest method testPartialUpdate.
@Test
public void testPartialUpdate() throws Exception {
final AsyncInterceptor failureInterceptor = addFailureInducingInterceptor(remoteCustomerCache);
try {
// Remote update latch
CountDownLatch remoteLatch = new CountDownLatch(2);
ExpectingInterceptor.get(remoteCustomerCache).when((ctx, cmd) -> cmd instanceof ReadWriteKeyCommand).countDown(remoteLatch);
try {
Statistics statsNode0 = getStatistics(localFactory);
withTxSession(localFactory, s -> {
Customer customer = new Customer();
customer.setName("JBoss");
s.persist(customer);
});
assertEquals(1, statsNode0.getSecondLevelCachePutCount());
assertEquals(0, statsNode0.getSecondLevelCacheMissCount());
assertEquals(0, statsNode0.getSecondLevelCacheHitCount());
// Wait for value to be applied remotely
assertTrue(remoteLatch.await(2, TimeUnit.SECONDS));
} finally {
ExpectingInterceptor.cleanup(remoteCustomerCache);
}
Statistics statsNode1 = getStatistics(remoteFactory);
withSession(remoteFactory.withOptions(), s -> {
Customer customer = s.load(Customer.class, 1);
assertEquals("JBoss", customer.getName());
});
assertEquals(0, statsNode1.getSecondLevelCachePutCount());
assertEquals(0, statsNode1.getSecondLevelCacheMissCount());
assertEquals(1, statsNode1.getSecondLevelCacheHitCount());
final boolean updated = doUpdate();
if (updated) {
withSession(localFactory.withOptions(), s -> {
Customer customer = s.load(Customer.class, 1);
assertEquals("JBoss, a division of Red Hat", customer.getName());
});
withSession(remoteFactory.withOptions(), s -> {
Customer customer = s.load(Customer.class, 1);
assertEquals("JBoss, a division of Red Hat", customer.getName());
});
}
} finally {
remoteCustomerCache.getAsyncInterceptorChain().removeInterceptor(failureInterceptor.getClass());
}
}
use of org.infinispan.commands.functional.ReadWriteKeyCommand in project infinispan by infinispan.
the class SingleKeyFunctionalBackupWriteCommand method createWriteCommand.
@Override
WriteCommand createWriteCommand() {
switch(operation) {
case READ_WRITE:
// noinspection unchecked
return new ReadWriteKeyCommand(key, (Function) function, segmentId, getCommandInvocationId(), MATCH_ALWAYS, params, keyDataConversion, valueDataConversion);
case READ_WRITE_KEY_VALUE:
// noinspection unchecked
ReadWriteKeyValueCommand cmd = new ReadWriteKeyValueCommand(key, value, (BiFunction) function, segmentId, getCommandInvocationId(), MATCH_ALWAYS, params, keyDataConversion, valueDataConversion);
cmd.setPrevValueAndMetadata(prevValue, prevMetadata);
return cmd;
case WRITE_ONLY:
// noinspection unchecked
return new WriteOnlyKeyCommand(key, (Consumer) function, segmentId, getCommandInvocationId(), MATCH_ALWAYS, params, keyDataConversion, valueDataConversion);
case WRITE_ONLY_KEY_VALUE:
// noinspection unchecked
return new WriteOnlyKeyValueCommand(key, value, (BiConsumer) function, segmentId, getCommandInvocationId(), MATCH_ALWAYS, params, keyDataConversion, valueDataConversion);
default:
throw new IllegalStateException("Unknown operation " + operation);
}
}
use of org.infinispan.commands.functional.ReadWriteKeyCommand in project infinispan by infinispan.
the class AbstractRegionAccessStrategyTest method expectPutFromLoad.
protected CountDownLatch expectPutFromLoad(InfinispanBaseRegion region, Object key) {
Predicate<Object> functionPredicate = accessType == AccessType.NONSTRICT_READ_WRITE ? VersionedEntry.class::isInstance : TombstoneUpdate.class::isInstance;
CountDownLatch latch = new CountDownLatch(1);
if (!isUsingInvalidation()) {
ExpectingInterceptor.get(region.getCache()).when((ctx, cmd) -> isExpectedReadWriteKey(key, cmd) && functionPredicate.test(((ReadWriteKeyCommand) cmd).getFunction())).countDown(latch);
cleanup.add(() -> ExpectingInterceptor.cleanup(region.getCache()));
} else {
if (transactional) {
expectPutFromLoadEndInvalidating(region, key, latch);
} else {
expectInvalidateCommand(region, latch);
}
}
log.debugf("Create latch for putFromLoad: %s", latch);
return latch;
}
Aggregations