Search in sources :

Example 1 with Region

use of org.hibernate.cache.spi.Region in project hibernate-orm by hibernate.

the class ConcurrentStatisticsImpl method getSecondLevelCacheStatistics.

/**
	 * Second level cache statistics per region
	 *
	 * @param regionName region name
	 *
	 * @return SecondLevelCacheStatistics
	 */
public ConcurrentSecondLevelCacheStatisticsImpl getSecondLevelCacheStatistics(String regionName) {
    ConcurrentSecondLevelCacheStatisticsImpl stat = secondLevelCacheStatistics.get(regionName);
    if (stat == null) {
        if (sessionFactory == null) {
            return null;
        }
        final EntityRegionAccessStrategy entityRegionAccess = sessionFactory.getCache().getEntityRegionAccess(regionName);
        final CollectionRegionAccessStrategy collectionRegionAccess = sessionFactory.getCache().getCollectionRegionAccess(regionName);
        if (entityRegionAccess == null && collectionRegionAccess == null) {
            final Region region = sessionFactory.getCache().getQueryCache(regionName).getRegion();
            if (region == null) {
                throw new IllegalArgumentException("Could not resolve region name [" + regionName + "]");
            }
            stat = new ConcurrentSecondLevelCacheStatisticsImpl(region, null, null);
        } else {
            final Region region = entityRegionAccess != null ? entityRegionAccess.getRegion() : collectionRegionAccess.getRegion();
            stat = new ConcurrentSecondLevelCacheStatisticsImpl(region, entityRegionAccess, collectionRegionAccess);
        }
        ConcurrentSecondLevelCacheStatisticsImpl previous;
        if ((previous = secondLevelCacheStatistics.putIfAbsent(regionName, stat)) != null) {
            stat = previous;
        }
    }
    return stat;
}
Also used : EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) Region(org.hibernate.cache.spi.Region) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)

Example 2 with Region

use of org.hibernate.cache.spi.Region in project hibernate-orm by hibernate.

the class QueryRegionImplTest method testQueryUpdate.

@Test
public void testQueryUpdate() throws Exception {
    withQueryRegion((sessionFactory, region) -> {
        ExceptionHolder holder = new ExceptionHolder();
        CyclicBarrier barrier = new CyclicBarrier(2);
        withSession(sessionFactory, session -> region.put(session, KEY, VALUE1));
        Thread updater = new Thread() {

            @Override
            public void run() {
                try {
                    withSession(sessionFactory, (session) -> {
                        assertEquals(VALUE1, region.get(session, KEY));
                        region.put(session, KEY, VALUE2);
                        assertEquals(VALUE2, region.get(session, KEY));
                        barrier.await(5, TimeUnit.SECONDS);
                        barrier.await(5, TimeUnit.SECONDS);
                        region.put(session, KEY, VALUE3);
                        assertEquals(VALUE3, region.get(session, KEY));
                        barrier.await(5, TimeUnit.SECONDS);
                        barrier.await(5, TimeUnit.SECONDS);
                    });
                } catch (AssertionFailedError e) {
                    holder.addAssertionFailure(e);
                    barrier.reset();
                } catch (Exception e) {
                    holder.addException(e);
                    barrier.reset();
                }
            }
        };
        Thread reader = new Thread() {

            @Override
            public void run() {
                try {
                    withSession(sessionFactory, (session) -> {
                        assertEquals(VALUE1, region.get(session, KEY));
                        barrier.await(5, TimeUnit.SECONDS);
                        assertEquals(VALUE1, region.get(session, KEY));
                        barrier.await(5, TimeUnit.SECONDS);
                        barrier.await(5, TimeUnit.SECONDS);
                        assertEquals(VALUE1, region.get(session, KEY));
                        barrier.await(5, TimeUnit.SECONDS);
                    });
                } catch (AssertionFailedError e) {
                    holder.addAssertionFailure(e);
                    barrier.reset();
                } catch (Exception e) {
                    holder.addException(e);
                    barrier.reset();
                }
            }
        };
        updater.start();
        reader.start();
        updater.join();
        reader.join();
        holder.checkExceptions();
        assertEquals(VALUE3, 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) AssertionFailedError(junit.framework.AssertionFailedError) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test) AbstractGeneralDataRegionTest(org.hibernate.test.cache.infinispan.AbstractGeneralDataRegionTest)

Example 3 with Region

use of org.hibernate.cache.spi.Region 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 4 with Region

use of org.hibernate.cache.spi.Region 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 5 with Region

use of org.hibernate.cache.spi.Region in project hibernate-orm by hibernate.

the class QueryRegionImplTest method testPutDoesNotBlockGet.

@Test
public void testPutDoesNotBlockGet() throws Exception {
    withQueryRegion((sessionFactory, region) -> {
        withSession(sessionFactory, session -> region.put(session, KEY, VALUE1));
        assertEquals(VALUE1, callWithSession(sessionFactory, session -> region.get(session, KEY)));
        final CountDownLatch readerLatch = 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() {
                try {
                    assertNotEquals(VALUE2, callWithSession(sessionFactory, session -> region.get(session, KEY)));
                } catch (AssertionFailedError e) {
                    holder.addAssertionFailure(e);
                } catch (Exception e) {
                    holder.addException(e);
                } finally {
                    readerLatch.countDown();
                }
            }
        };
        Thread writer = new Thread() {

            @Override
            public void run() {
                try {
                    withSession(sessionFactory, session -> {
                        region.put(session, KEY, VALUE2);
                        writerLatch.await();
                    });
                } catch (Exception e) {
                    holder.addException(e);
                } finally {
                    completionLatch.countDown();
                }
            }
        };
        reader.setDaemon(true);
        writer.setDaemon(true);
        writer.start();
        assertFalse("Writer is blocking", completionLatch.await(100, TimeUnit.MILLISECONDS));
        // Start the reader
        reader.start();
        assertTrue("Reader finished promptly", readerLatch.await(100, TimeUnit.MILLISECONDS));
        writerLatch.countDown();
        assertTrue("Reader finished promptly", completionLatch.await(100, TimeUnit.MILLISECONDS));
        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) CountDownLatch(java.util.concurrent.CountDownLatch) AssertionFailedError(junit.framework.AssertionFailedError) Test(org.junit.Test) AbstractGeneralDataRegionTest(org.hibernate.test.cache.infinispan.AbstractGeneralDataRegionTest)

Aggregations

Region (org.hibernate.cache.spi.Region)5 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 List (java.util.List)4 Properties (java.util.Properties)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 CyclicBarrier (java.util.concurrent.CyclicBarrier)4 TimeUnit (java.util.concurrent.TimeUnit)4 AssertionFailedError (junit.framework.AssertionFailedError)4 Session (org.hibernate.Session)4 SessionFactory (org.hibernate.SessionFactory)4 Transaction (org.hibernate.Transaction)4 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)4 QueryResultsRegionImpl (org.hibernate.cache.infinispan.query.QueryResultsRegionImpl)4 StandardQueryCache (org.hibernate.cache.internal.StandardQueryCache)4 CacheDataDescription (org.hibernate.cache.spi.CacheDataDescription)4 QueryResultsRegion (org.hibernate.cache.spi.QueryResultsRegion)4 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)4 AbstractGeneralDataRegionTest (org.hibernate.test.cache.infinispan.AbstractGeneralDataRegionTest)4