Search in sources :

Example 1 with QueryResultsRegionImpl

use of org.hibernate.cache.infinispan.query.QueryResultsRegionImpl 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 2 with QueryResultsRegionImpl

use of org.hibernate.cache.infinispan.query.QueryResultsRegionImpl 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 3 with QueryResultsRegionImpl

use of org.hibernate.cache.infinispan.query.QueryResultsRegionImpl in project hibernate-orm by hibernate.

the class InfinispanRegionFactory method buildQueryResultsRegion.

@Override
public QueryResultsRegion buildQueryResultsRegion(String regionName, Map<String, Object> configValues) {
    if (log.isDebugEnabled()) {
        log.debug("Building query results cache region [" + regionName + "]");
    }
    final AdvancedCache cache = getCache(regionName, DataType.QUERY, null);
    final QueryResultsRegionImpl region = new QueryResultsRegionImpl(cache, regionName, transactionManager, this);
    startRegion(region);
    return region;
}
Also used : QueryResultsRegionImpl(org.hibernate.cache.infinispan.query.QueryResultsRegionImpl) AdvancedCache(org.infinispan.AdvancedCache)

Example 4 with QueryResultsRegionImpl

use of org.hibernate.cache.infinispan.query.QueryResultsRegionImpl in project hibernate-orm by hibernate.

the class InfinispanRegionFactoryTestCase method testBuildQueryRegionWithCustomRegionName.

@Test
public void testBuildQueryRegionWithCustomRegionName() {
    final String queryRegionName = "myquery";
    Properties p = createProperties();
    p.setProperty("hibernate.cache.infinispan.myquery.cfg", "timestamps-none-eviction");
    p.setProperty("hibernate.cache.infinispan.myquery.eviction.strategy", "LIRS");
    p.setProperty("hibernate.cache.infinispan.myquery.expiration.wake_up_interval", "2222");
    p.setProperty("hibernate.cache.infinispan.myquery.eviction.max_entries", "11111");
    TestInfinispanRegionFactory factory = createRegionFactory(p);
    try {
        assertTrue(isDefinedCache(factory, "local-query"));
        QueryResultsRegionImpl region = (QueryResultsRegionImpl) factory.buildQueryResultsRegion(queryRegionName, p);
        assertNotNull(factory.getBaseConfiguration(queryRegionName));
        assertTrue(isDefinedCache(factory, queryRegionName));
        AdvancedCache cache = region.getCache();
        Configuration cacheCfg = cache.getCacheConfiguration();
        assertEquals(EvictionStrategy.LIRS, cacheCfg.eviction().strategy());
        assertEquals(2222, cacheCfg.expiration().wakeUpInterval());
        assertEquals(11111, cacheCfg.eviction().maxEntries());
    } finally {
        factory.stop();
    }
}
Also used : QueryResultsRegionImpl(org.hibernate.cache.infinispan.query.QueryResultsRegionImpl) Configuration(org.infinispan.configuration.cache.Configuration) AdvancedCache(org.infinispan.AdvancedCache) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with QueryResultsRegionImpl

use of org.hibernate.cache.infinispan.query.QueryResultsRegionImpl in project hibernate-orm by hibernate.

the class InfinispanRegionFactoryTestCase method testEnableStatistics.

@Test
public void testEnableStatistics() {
    Properties p = createProperties();
    p.setProperty("hibernate.cache.infinispan.statistics", "true");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.expiration.lifespan", "60000");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.expiration.max_idle", "30000");
    p.setProperty("hibernate.cache.infinispan.entity.cfg", "myentity-cache");
    p.setProperty("hibernate.cache.infinispan.entity.eviction.strategy", "FIFO");
    p.setProperty("hibernate.cache.infinispan.entity.expiration.wake_up_interval", "3000");
    p.setProperty("hibernate.cache.infinispan.entity.eviction.max_entries", "10000");
    InfinispanRegionFactory factory = createRegionFactory(p);
    try {
        EmbeddedCacheManager manager = factory.getCacheManager();
        assertTrue(manager.getCacheManagerConfiguration().globalJmxStatistics().enabled());
        EntityRegionImpl region = (EntityRegionImpl) factory.buildEntityRegion("com.acme.Address", p, MUTABLE_NON_VERSIONED);
        AdvancedCache cache = region.getCache();
        assertTrue(cache.getCacheConfiguration().jmxStatistics().enabled());
        region = (EntityRegionImpl) factory.buildEntityRegion("com.acme.Person", p, MUTABLE_NON_VERSIONED);
        cache = region.getCache();
        assertTrue(cache.getCacheConfiguration().jmxStatistics().enabled());
        final String query = "org.hibernate.cache.internal.StandardQueryCache";
        QueryResultsRegionImpl queryRegion = (QueryResultsRegionImpl) factory.buildQueryResultsRegion(query, p);
        cache = queryRegion.getCache();
        assertTrue(cache.getCacheConfiguration().jmxStatistics().enabled());
        final String timestamps = "org.hibernate.cache.spi.UpdateTimestampsCache";
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.clustering().stateTransfer().fetchInMemoryState(true);
        manager.defineConfiguration("timestamps", builder.build());
        TimestampsRegionImpl timestampsRegion = (TimestampsRegionImpl) factory.buildTimestampsRegion(timestamps, p);
        cache = timestampsRegion.getCache();
        assertTrue(cache.getCacheConfiguration().jmxStatistics().enabled());
        CollectionRegionImpl collectionRegion = (CollectionRegionImpl) factory.buildCollectionRegion("com.acme.Person.addresses", p, MUTABLE_NON_VERSIONED);
        cache = collectionRegion.getCache();
        assertTrue(cache.getCacheConfiguration().jmxStatistics().enabled());
    } finally {
        factory.stop();
    }
}
Also used : QueryResultsRegionImpl(org.hibernate.cache.infinispan.query.QueryResultsRegionImpl) TimestampsRegionImpl(org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) ClusteringConfigurationBuilder(org.infinispan.configuration.cache.ClusteringConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) EntityRegionImpl(org.hibernate.cache.infinispan.entity.EntityRegionImpl) CollectionRegionImpl(org.hibernate.cache.infinispan.collection.CollectionRegionImpl) AdvancedCache(org.infinispan.AdvancedCache) Properties(java.util.Properties) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) Test(org.junit.Test)

Aggregations

QueryResultsRegionImpl (org.hibernate.cache.infinispan.query.QueryResultsRegionImpl)7 AdvancedCache (org.infinispan.AdvancedCache)7 Properties (java.util.Properties)6 Test (org.junit.Test)6 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)5 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 TimeUnit (java.util.concurrent.TimeUnit)2 AssertionFailedError (junit.framework.AssertionFailedError)2 Session (org.hibernate.Session)2 SessionFactory (org.hibernate.SessionFactory)2 Transaction (org.hibernate.Transaction)2 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)2 CollectionRegionImpl (org.hibernate.cache.infinispan.collection.CollectionRegionImpl)2 EntityRegionImpl (org.hibernate.cache.infinispan.entity.EntityRegionImpl)2 TimestampsRegionImpl (org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl)2 StandardQueryCache (org.hibernate.cache.internal.StandardQueryCache)2