use of org.infinispan.functional.FunctionalMap.ReadWriteMap in project infinispan by infinispan.
the class FunctionalDistributionTest method iterate.
private void iterate(Object key, ReadWriteMap<Object, Integer> rw, int expectedValue) throws Exception {
List<AdvancedCache<Object, Object>> owners = cacheManagers.stream().map(cm -> cm.getCache(DIST).getAdvancedCache()).filter(cache -> cache.getDistributionManager().getCacheTopology().isWriteOwner(key)).collect(Collectors.toList());
CyclicBarrier barrier = new CyclicBarrier(numDistOwners + 1);
for (AdvancedCache cache : owners) {
BlockingInterceptor bi = new BlockingInterceptor<>(barrier, ReadWriteKeyCommand.class, true, false);
cache.getAsyncInterceptorChain().addInterceptorBefore(bi, EntryWrappingInterceptor.class);
}
// While the command execution could be async the BlockingInterceptor would block us on primary owner == originator
// On backup == originator the blocking interceptor is not hit until
Future<Void> f = fork(() -> rw.eval(key, entry -> entry.set(entry.find().orElse(0) + 1)).join());
barrier.await(10, TimeUnit.SECONDS);
for (AdvancedCache cache : owners) {
cache.getAsyncInterceptorChain().findInterceptorWithClass(BlockingInterceptor.class).suspend(true);
}
barrier.await(10, TimeUnit.SECONDS);
for (AdvancedCache cache : owners) {
cache.getAsyncInterceptorChain().removeInterceptor(BlockingInterceptor.class);
}
f.get(10, TimeUnit.SECONDS);
// we want to ensure that each of the owners executes the function only once:
Assert.assertEquals(owners.stream().map(cache -> cache.getDataContainer().get(key).getValue()).collect(Collectors.toList()), Collections.nCopies(numDistOwners, expectedValue));
}
use of org.infinispan.functional.FunctionalMap.ReadWriteMap in project infinispan by infinispan.
the class CacheOperationsTest method testFunctional.
private void testFunctional(String site) throws Exception {
FunctionalMapImpl<Object, Object> fmap = FunctionalMapImpl.create(cache(site, 0).getAdvancedCache());
WriteOnlyMap<Object, Object> wo = WriteOnlyMapImpl.create(fmap);
ReadWriteMap<Object, Object> rw = ReadWriteMapImpl.create(fmap);
Cache<Object, Object> backup = backup(site);
Object[] keys = { new MagicKey("k0", cache(site, 0), cache(site, 1)), new MagicKey("k1", cache(site, 1), cache(site, 0)), new MagicKey("k2", cache(site, 1), cache(site, 2)) };
for (Object key : keys) {
wo.eval(key, "v0", MarshallableFunctions.setValueConsumer()).join();
assertEquals("v0", backup.get(key));
}
for (Object key : keys) {
wo.eval(key, MarshallableFunctions.removeConsumer()).join();
assertEquals(null, backup.get(key));
}
wo.evalMany(map(keys, "v1"), MarshallableFunctions.setValueConsumer()).join();
for (Object key : keys) {
assertEquals("v1", backup.get(key));
}
for (Object key : keys) {
rw.eval(key, view -> view.set(view.get() + "+2")).join();
assertEquals("v1+2", backup.get(key));
}
rw.evalMany(Util.asSet(keys), view -> view.set(view.get() + "+3")).forEach(ret -> assertEquals(null, ret));
for (Object key : keys) {
assertEquals("v1+2+3", backup.get(key));
}
wo.evalMany(Util.asSet(keys), MarshallableFunctions.removeConsumer()).join();
for (Object key : keys) {
assertEquals(null, backup.get(key));
}
rw.evalMany(Util.asSet(keys), view -> view.find().orElse("none")).forEach(ret -> assertEquals("none", ret));
for (Object key : keys) {
assertEquals(null, backup.get(key));
}
if (transactional) {
TransactionManager tm = cache(site, 0).getAdvancedCache().getTransactionManager();
tm.begin();
rw.eval(keys[0], "v4", MarshallableFunctions.setValueReturnPrevOrNull()).join();
// read-only evalMany
rw.evalMany(Util.asSet(keys[1], keys[2]), view -> view.find().orElse("none")).forEach(ret -> assertEquals("none", ret));
tm.commit();
assertEquals("v4", backup.get(keys[0]));
assertEquals(null, backup.get(keys[1]));
assertEquals(null, backup.get(keys[2]));
}
}
Aggregations