Search in sources :

Example 1 with ReadWriteMap

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));
}
Also used : CyclicBarrier(java.util.concurrent.CyclicBarrier) EntryWrappingInterceptor(org.infinispan.interceptors.impl.EntryWrappingInterceptor) BeforeClass(org.testng.annotations.BeforeClass) ReadWriteMapImpl(org.infinispan.functional.impl.ReadWriteMapImpl) Test(org.testng.annotations.Test) Collectors(java.util.stream.Collectors) FunctionalMapImpl(org.infinispan.functional.impl.FunctionalMapImpl) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Future(java.util.concurrent.Future) AdvancedCache(org.infinispan.AdvancedCache) ReadWriteMap(org.infinispan.functional.FunctionalMap.ReadWriteMap) Assert(org.testng.Assert) ReadWriteKeyCommand(org.infinispan.commands.functional.ReadWriteKeyCommand) BlockingInterceptor(org.infinispan.distribution.BlockingInterceptor) Collections(java.util.Collections) BlockingInterceptor(org.infinispan.distribution.BlockingInterceptor) AdvancedCache(org.infinispan.AdvancedCache) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 2 with ReadWriteMap

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]));
    }
}
Also used : MagicKey(org.infinispan.distribution.MagicKey) Factory(org.testng.annotations.Factory) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) Util(org.infinispan.commons.util.Util) ReadWriteMapImpl(org.infinispan.functional.impl.ReadWriteMapImpl) HashMap(java.util.HashMap) LockingMode(org.infinispan.transaction.LockingMode) Test(org.testng.annotations.Test) MarshallableFunctions(org.infinispan.marshall.core.MarshallableFunctions) Cache(org.infinispan.Cache) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) FunctionalMapImpl(org.infinispan.functional.impl.FunctionalMapImpl) CacheMode(org.infinispan.configuration.cache.CacheMode) Stream(java.util.stream.Stream) ReadWriteMap(org.infinispan.functional.FunctionalMap.ReadWriteMap) AssertJUnit.assertNull(org.testng.AssertJUnit.assertNull) Map(java.util.Map) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) TransactionManager(javax.transaction.TransactionManager) WriteOnlyMap(org.infinispan.functional.FunctionalMap.WriteOnlyMap) WriteOnlyMapImpl(org.infinispan.functional.impl.WriteOnlyMapImpl) TransactionManager(javax.transaction.TransactionManager) MagicKey(org.infinispan.distribution.MagicKey)

Aggregations

Collectors (java.util.stream.Collectors)2 ReadWriteMap (org.infinispan.functional.FunctionalMap.ReadWriteMap)2 FunctionalMapImpl (org.infinispan.functional.impl.FunctionalMapImpl)2 ReadWriteMapImpl (org.infinispan.functional.impl.ReadWriteMapImpl)2 Test (org.testng.annotations.Test)2 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1 Stream (java.util.stream.Stream)1 TransactionManager (javax.transaction.TransactionManager)1 AdvancedCache (org.infinispan.AdvancedCache)1 Cache (org.infinispan.Cache)1 ReadWriteKeyCommand (org.infinispan.commands.functional.ReadWriteKeyCommand)1 Util (org.infinispan.commons.util.Util)1 CacheMode (org.infinispan.configuration.cache.CacheMode)1