Search in sources :

Example 11 with AdvancedCache

use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.

the class InvalidationTest method testConcurrentRemoveAndPutFromLoad.

@Test
@TestForIssue(jiraKey = "HHH-9868")
public void testConcurrentRemoveAndPutFromLoad() throws Exception {
    final Item item = new Item("chris", "Chris's Item");
    withTxSession(s -> {
        s.persist(item);
    });
    Phaser deletePhaser = new Phaser(2);
    Phaser getPhaser = new Phaser(2);
    HookInterceptor hook = new HookInterceptor();
    AdvancedCache pendingPutsCache = getPendingPutsCache(Item.class);
    pendingPutsCache.addInterceptor(hook, 0);
    AtomicBoolean getThreadBlockedInDB = new AtomicBoolean(false);
    Thread deleteThread = new Thread(() -> {
        try {
            withTxSession(s -> {
                Item loadedItem = s.get(Item.class, item.getId());
                assertNotNull(loadedItem);
                arriveAndAwait(deletePhaser, 2000);
                arriveAndAwait(deletePhaser, 2000);
                log.trace("Item loaded");
                s.delete(loadedItem);
                s.flush();
                log.trace("Item deleted");
                arriveAndAwait(deletePhaser, 2000);
                arriveAndAwait(deletePhaser, 4000);
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }, "delete-thread");
    Thread getThread = new Thread(() -> {
        try {
            withTxSession(s -> {
                Item loadedItem = s.get(Item.class, item.getId());
                if (getThreadBlockedInDB.get()) {
                    assertNull(loadedItem);
                } else {
                    assertNotNull(loadedItem);
                }
            });
        } catch (PessimisticLockException e) {
            // (delete-thread has ITEMS table write-locked and we try to acquire read-lock)
            try {
                arriveAndAwait(getPhaser, 2000);
                arriveAndAwait(getPhaser, 2000);
            } catch (Exception e1) {
                throw new RuntimeException(e1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }, "get-thread");
    deleteThread.start();
    // deleteThread loads the entity
    arriveAndAwait(deletePhaser, 2000);
    withTx(() -> {
        sessionFactory().getCache().evictEntity(Item.class, item.getId());
        assertFalse(sessionFactory().getCache().containsEntity(Item.class, item.getId()));
        return null;
    });
    arriveAndAwait(deletePhaser, 2000);
    // delete thread invalidates PFER
    arriveAndAwait(deletePhaser, 2000);
    // get thread gets the entity from DB
    hook.block(getPhaser, getThread);
    getThread.start();
    try {
        arriveAndAwait(getPhaser, 2000);
    } catch (TimeoutException e) {
        getThreadBlockedInDB.set(true);
    }
    arriveAndAwait(deletePhaser, 2000);
    // delete thread finishes the remove from DB and cache
    deleteThread.join();
    hook.unblock();
    arriveAndAwait(getPhaser, 2000);
    // get thread puts the entry into cache
    getThread.join();
    assertNoInvalidators(pendingPutsCache);
    withTxSession(s -> {
        Item loadedItem = s.get(Item.class, item.getId());
        assertNull(loadedItem);
    });
}
Also used : Item(org.hibernate.test.cache.infinispan.functional.entities.Item) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AdvancedCache(org.infinispan.AdvancedCache) Phaser(java.util.concurrent.Phaser) TimeoutException(java.util.concurrent.TimeoutException) PessimisticLockException(org.hibernate.PessimisticLockException) PessimisticLockException(org.hibernate.PessimisticLockException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 12 with AdvancedCache

use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.

the class QueryRegionImplTest method testGetDoesNotBlockPut.

@Test
public void testGetDoesNotBlockPut() throws Exception {
    withQueryRegion((sessionFactory, region) -> {
        withSession(sessionFactory, session -> region.put(session, KEY, VALUE1));
        assertEquals(VALUE1, callWithSession(sessionFactory, session -> region.get(session, KEY)));
        final AdvancedCache cache = ((QueryResultsRegionImpl) region).getCache();
        final CountDownLatch blockerLatch = new CountDownLatch(1);
        final CountDownLatch writerLatch = new CountDownLatch(1);
        final CountDownLatch completionLatch = new CountDownLatch(1);
        final ExceptionHolder holder = new ExceptionHolder();
        Thread reader = new Thread() {

            @Override
            public void run() {
                GetBlocker blocker = new GetBlocker(blockerLatch, KEY);
                try {
                    cache.addListener(blocker);
                    withSession(sessionFactory, session -> region.get(session, KEY));
                } catch (Exception e) {
                    holder.addException(e);
                } finally {
                    cache.removeListener(blocker);
                }
            }
        };
        Thread writer = new Thread() {

            @Override
            public void run() {
                try {
                    writerLatch.await();
                    withSession(sessionFactory, session -> region.put(session, KEY, VALUE2));
                } catch (Exception e) {
                    holder.addException(e);
                } finally {
                    completionLatch.countDown();
                }
            }
        };
        reader.setDaemon(true);
        writer.setDaemon(true);
        boolean unblocked = false;
        try {
            reader.start();
            writer.start();
            assertFalse("Reader is blocking", completionLatch.await(100, TimeUnit.MILLISECONDS));
            // Start the writer
            writerLatch.countDown();
            assertTrue("Writer finished promptly", completionLatch.await(100, TimeUnit.MILLISECONDS));
            blockerLatch.countDown();
            unblocked = true;
            if (IsolationLevel.REPEATABLE_READ.equals(cache.getCacheConfiguration().locking().isolationLevel())) {
                assertEquals(VALUE1, callWithSession(sessionFactory, session -> region.get(session, KEY)));
            } else {
                assertEquals(VALUE2, callWithSession(sessionFactory, session -> region.get(session, KEY)));
            }
            holder.checkExceptions();
        } finally {
            if (!unblocked) {
                blockerLatch.countDown();
            }
        }
    });
}
Also used : InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) QueryResultsRegionImpl(org.hibernate.cache.infinispan.query.QueryResultsRegionImpl) Logger(org.jboss.logging.Logger) CacheEntryModifiedEvent(org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent) CacheEntryVisitedEvent(org.infinispan.notifications.cachelistener.event.CacheEntryVisitedEvent) Session(org.hibernate.Session) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) QueryResultsRegion(org.hibernate.cache.spi.QueryResultsRegion) ArrayList(java.util.ArrayList) Transaction(org.hibernate.Transaction) TestForIssue(org.hibernate.testing.TestForIssue) CacheEntryVisited(org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited) AdvancedCache(org.infinispan.AdvancedCache) CacheEntryModified(org.infinispan.notifications.cachelistener.annotation.CacheEntryModified) CyclicBarrier(java.util.concurrent.CyclicBarrier) Properties(java.util.Properties) Listener(org.infinispan.notifications.Listener) SessionFactory(org.hibernate.SessionFactory) CacheTestUtil(org.hibernate.test.cache.infinispan.util.CacheTestUtil) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AssertionFailedError(junit.framework.AssertionFailedError) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) StandardQueryCache(org.hibernate.cache.internal.StandardQueryCache) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CacheDataDescription(org.hibernate.cache.spi.CacheDataDescription) IsolationLevel(org.infinispan.util.concurrent.IsolationLevel) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Region(org.hibernate.cache.spi.Region) AbstractGeneralDataRegionTest(org.hibernate.test.cache.infinispan.AbstractGeneralDataRegionTest) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) QueryResultsRegionImpl(org.hibernate.cache.infinispan.query.QueryResultsRegionImpl) AdvancedCache(org.infinispan.AdvancedCache) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) AbstractGeneralDataRegionTest(org.hibernate.test.cache.infinispan.AbstractGeneralDataRegionTest)

Example 13 with AdvancedCache

use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.

the class QueryRegionImplTest method testPutDuringPut.

@Test
@TestForIssue(jiraKey = "HHH-7898")
public void testPutDuringPut() throws Exception {
    withQueryRegion((sessionFactory, region) -> {
        withSession(sessionFactory, session -> region.put(session, KEY, VALUE1));
        assertEquals(VALUE1, callWithSession(sessionFactory, session -> region.get(session, KEY)));
        final AdvancedCache cache = ((QueryResultsRegionImpl) region).getCache();
        CountDownLatch blockerLatch = new CountDownLatch(1);
        CountDownLatch triggerLatch = new CountDownLatch(1);
        ExceptionHolder holder = new ExceptionHolder();
        Thread blocking = new Thread() {

            @Override
            public void run() {
                PutBlocker blocker = null;
                try {
                    blocker = new PutBlocker(blockerLatch, triggerLatch, KEY);
                    cache.addListener(blocker);
                    withSession(sessionFactory, session -> region.put(session, KEY, VALUE2));
                } catch (Exception e) {
                    holder.addException(e);
                } finally {
                    if (blocker != null) {
                        cache.removeListener(blocker);
                    }
                    if (triggerLatch.getCount() > 0) {
                        triggerLatch.countDown();
                    }
                }
            }
        };
        Thread blocked = new Thread() {

            @Override
            public void run() {
                try {
                    triggerLatch.await();
                    // this should silently fail
                    withSession(sessionFactory, session -> region.put(session, KEY, VALUE3));
                } catch (Exception e) {
                    holder.addException(e);
                }
            }
        };
        blocking.setName("blocking-thread");
        blocking.start();
        blocked.setName("blocked-thread");
        blocked.start();
        blocked.join();
        blockerLatch.countDown();
        blocking.join();
        holder.checkExceptions();
        assertEquals(VALUE2, callWithSession(sessionFactory, session -> region.get(session, KEY)));
    });
}
Also used : InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) QueryResultsRegionImpl(org.hibernate.cache.infinispan.query.QueryResultsRegionImpl) Logger(org.jboss.logging.Logger) CacheEntryModifiedEvent(org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent) CacheEntryVisitedEvent(org.infinispan.notifications.cachelistener.event.CacheEntryVisitedEvent) Session(org.hibernate.Session) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) QueryResultsRegion(org.hibernate.cache.spi.QueryResultsRegion) ArrayList(java.util.ArrayList) Transaction(org.hibernate.Transaction) TestForIssue(org.hibernate.testing.TestForIssue) CacheEntryVisited(org.infinispan.notifications.cachelistener.annotation.CacheEntryVisited) AdvancedCache(org.infinispan.AdvancedCache) CacheEntryModified(org.infinispan.notifications.cachelistener.annotation.CacheEntryModified) CyclicBarrier(java.util.concurrent.CyclicBarrier) Properties(java.util.Properties) Listener(org.infinispan.notifications.Listener) SessionFactory(org.hibernate.SessionFactory) CacheTestUtil(org.hibernate.test.cache.infinispan.util.CacheTestUtil) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AssertionFailedError(junit.framework.AssertionFailedError) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) StandardQueryCache(org.hibernate.cache.internal.StandardQueryCache) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CacheDataDescription(org.hibernate.cache.spi.CacheDataDescription) IsolationLevel(org.infinispan.util.concurrent.IsolationLevel) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Region(org.hibernate.cache.spi.Region) AbstractGeneralDataRegionTest(org.hibernate.test.cache.infinispan.AbstractGeneralDataRegionTest) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) QueryResultsRegionImpl(org.hibernate.cache.infinispan.query.QueryResultsRegionImpl) AdvancedCache(org.infinispan.AdvancedCache) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) AbstractGeneralDataRegionTest(org.hibernate.test.cache.infinispan.AbstractGeneralDataRegionTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 14 with AdvancedCache

use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.

the class InfinispanRegionFactory method getCache.

protected AdvancedCache getCache(String regionName, DataType type, CacheDataDescription metadata) {
    if (!manager.cacheExists(regionName)) {
        String templateCacheName = baseConfigurations.get(regionName);
        Configuration configuration = null;
        ConfigurationBuilder builder = new ConfigurationBuilder();
        if (templateCacheName != null) {
            configuration = manager.getCacheConfiguration(templateCacheName);
            if (configuration == null) {
                log.customConfigForRegionNotFound(templateCacheName, regionName, type.key);
            } else {
                log.debugf("Region '%s' will use cache template '%s'", regionName, templateCacheName);
                builder.read(configuration);
                configureTransactionManager(builder);
            // do not apply data type overrides to regions that set special cache configuration
            }
        }
        if (configuration == null) {
            configuration = dataTypeConfigurations.get(type);
            if (configuration == null) {
                throw new IllegalStateException("Configuration not defined for type " + type.key);
            }
            builder.read(configuration);
        // overrides for data types are already applied, but we should check custom ones
        }
        ConfigurationBuilder override = configOverrides.get(regionName);
        if (override != null) {
            log.debugf("Region '%s' has additional configuration set through properties.", regionName);
            builder.read(override.build(false));
        }
        if (getCacheKeysFactory() instanceof SimpleCacheKeysFactory) {
            // the keys may not define hashCode/equals correctly (e.g. arrays)
            if (metadata != null && metadata.getKeyType() != null) {
                builder.dataContainer().keyEquivalence(new TypeEquivalance(metadata.getKeyType()));
            }
        }
        if (globalStats != null) {
            builder.jmxStatistics().enabled(globalStats).available(globalStats);
        }
        configuration = builder.build();
        type.validate(configuration);
        manager.defineConfiguration(regionName, configuration);
    }
    final AdvancedCache cache = manager.getCache(regionName).getAdvancedCache();
    // TODO: not sure if this is needed in recent Infinispan
    if (!cache.getStatus().allowInvocations()) {
        cache.start();
    }
    return createCacheWrapper(cache);
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) TransactionConfiguration(org.infinispan.configuration.cache.TransactionConfiguration) Configuration(org.infinispan.configuration.cache.Configuration) SimpleCacheKeysFactory(org.hibernate.cache.internal.SimpleCacheKeysFactory) AdvancedCache(org.infinispan.AdvancedCache)

Example 15 with AdvancedCache

use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.

the class InfinispanRegionFactory method buildTimestampsRegion.

@Override
public TimestampsRegion buildTimestampsRegion(String regionName, Map<String, Object> configValues) {
    if (log.isDebugEnabled()) {
        log.debug("Building timestamps cache region [" + regionName + "]");
    }
    final AdvancedCache cache = getCache(regionName, DataType.TIMESTAMPS, null);
    final TimestampsRegionImpl region = createTimestampsRegion(cache, regionName);
    startRegion(region);
    return region;
}
Also used : TimestampsRegionImpl(org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl) ClusteredTimestampsRegionImpl(org.hibernate.cache.infinispan.timestamp.ClusteredTimestampsRegionImpl) AdvancedCache(org.infinispan.AdvancedCache)

Aggregations

AdvancedCache (org.infinispan.AdvancedCache)32 Test (org.junit.Test)20 Properties (java.util.Properties)12 EntityRegionImpl (org.hibernate.cache.infinispan.entity.EntityRegionImpl)10 Configuration (org.infinispan.configuration.cache.Configuration)9 QueryResultsRegionImpl (org.hibernate.cache.infinispan.query.QueryResultsRegionImpl)7 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)7 TestForIssue (org.hibernate.testing.TestForIssue)7 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)6 CollectionRegionImpl (org.hibernate.cache.infinispan.collection.CollectionRegionImpl)5 TimestampsRegionImpl (org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl)5 List (java.util.List)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Session (org.hibernate.Session)4 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)4 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 TimeUnit (java.util.concurrent.TimeUnit)3