Search in sources :

Example 26 with AdvancedCache

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

the class AbstractGeneralDataRegionTest method testEvictAll.

/**
	 * Test method for {@link QueryResultsRegion#evictAll()}.
	 * <p/>
	 * FIXME add testing of the "immediately without regard for transaction isolation" bit in the
	 * CollectionRegionAccessStrategy API.
	 */
public void testEvictAll() throws Exception {
    withSessionFactoriesAndRegions(2, (sessionFactories, regions) -> {
        GeneralDataRegion localRegion = regions.get(0);
        GeneralDataRegion remoteRegion = regions.get(1);
        AdvancedCache localCache = ((BaseGeneralDataRegion) localRegion).getCache();
        AdvancedCache remoteCache = ((BaseGeneralDataRegion) remoteRegion).getCache();
        SharedSessionContractImplementor localSession = (SharedSessionContractImplementor) sessionFactories.get(0).openSession();
        SharedSessionContractImplementor remoteSession = (SharedSessionContractImplementor) sessionFactories.get(1).openSession();
        try {
            Set localKeys = localCache.keySet();
            assertEquals("No valid children in " + localKeys, 0, localKeys.size());
            Set remoteKeys = remoteCache.keySet();
            assertEquals("No valid children in " + remoteKeys, 0, remoteKeys.size());
            assertNull("local is clean", localRegion.get(null, KEY));
            assertNull("remote is clean", remoteRegion.get(null, KEY));
            localRegion.put(localSession, KEY, VALUE1);
            assertEquals(VALUE1, localRegion.get(null, KEY));
            remoteRegion.put(remoteSession, KEY, VALUE1);
            assertEquals(VALUE1, remoteRegion.get(null, KEY));
            localRegion.evictAll();
            // This should re-establish the region root node in the optimistic case
            assertNull(localRegion.get(null, KEY));
            localKeys = localCache.keySet();
            assertEquals("No valid children in " + localKeys, 0, localKeys.size());
            // Re-establishing the region root on the local node doesn't
            // propagate it to other nodes. Do a get on the remote node to re-establish
            // This only adds a node in the case of optimistic locking
            assertEquals(null, remoteRegion.get(null, KEY));
            remoteKeys = remoteCache.keySet();
            assertEquals("No valid children in " + remoteKeys, 0, remoteKeys.size());
            assertEquals("local is clean", null, localRegion.get(null, KEY));
            assertEquals("remote is clean", null, remoteRegion.get(null, KEY));
        } finally {
            localSession.close();
            remoteSession.close();
        }
    });
}
Also used : BaseGeneralDataRegion(org.hibernate.cache.infinispan.impl.BaseGeneralDataRegion) GeneralDataRegion(org.hibernate.cache.spi.GeneralDataRegion) Set(java.util.Set) BaseGeneralDataRegion(org.hibernate.cache.infinispan.impl.BaseGeneralDataRegion) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) AdvancedCache(org.infinispan.AdvancedCache)

Example 27 with AdvancedCache

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

the class VersionedCallInterceptor method visitSizeCommand.

@Override
public Object visitSizeCommand(InvocationContext ctx, SizeCommand command) throws Throwable {
    Set<Flag> flags = command.getFlags();
    int size = 0;
    AdvancedCache decoratedCache = cache.getAdvancedCache();
    if (flags != null) {
        decoratedCache = decoratedCache.withFlags(flags.toArray(new Flag[flags.size()]));
    }
    // In non-transactional caches we don't care about context
    CloseableIterable<CacheEntry<Object, Void>> iterable = decoratedCache.filterEntries(VersionedEntry.EXCLUDE_EMPTY_EXTRACT_VALUE).converter(NullValueConverter.getInstance());
    try {
        for (CacheEntry<Object, Void> entry : iterable) {
            if (size++ == Integer.MAX_VALUE) {
                return Integer.MAX_VALUE;
            }
        }
    } finally {
        iterable.close();
    }
    return size;
}
Also used : AdvancedCache(org.infinispan.AdvancedCache) CacheEntry(org.infinispan.container.entries.CacheEntry) Flag(org.infinispan.context.Flag)

Example 28 with AdvancedCache

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

the class BaseTransactionalDataRegion method removeEntries.

private void removeEntries(boolean inTransaction, KeyValueFilter filter) {
    // If the transaction is required, we simply need it -> will create our own
    boolean startedTx = false;
    if (!inTransaction && requiresTransaction) {
        try {
            tm.begin();
            startedTx = true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    // We can never use cache.clear() since tombstones must be kept.
    try {
        AdvancedCache localCache = Caches.localCache(cache);
        CloseableIterator<CacheEntry> it = Caches.entrySet(localCache, Tombstone.EXCLUDE_TOMBSTONES).iterator();
        long now = nextTimestamp();
        try {
            while (it.hasNext()) {
                // Cannot use it.next(); it.remove() due to ISPN-5653
                CacheEntry entry = it.next();
                switch(strategy) {
                    case TOMBSTONES:
                        localCache.remove(entry.getKey(), entry.getValue());
                        break;
                    case VERSIONED_ENTRIES:
                        localCache.put(entry.getKey(), new VersionedEntry(null, null, now), tombstoneExpiration, TimeUnit.MILLISECONDS);
                        break;
                }
            }
        } finally {
            it.close();
        }
    } finally {
        if (startedTx) {
            try {
                tm.commit();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : VersionedEntry(org.hibernate.cache.infinispan.util.VersionedEntry) AdvancedCache(org.infinispan.AdvancedCache) CacheEntry(org.infinispan.container.entries.CacheEntry)

Example 29 with AdvancedCache

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

the class InfinispanRegionFactoryTestCase method testBuildDiffCacheNameTimestampsRegion.

@Test
public void testBuildDiffCacheNameTimestampsRegion() {
    final String timestamps = "org.hibernate.cache.spi.UpdateTimestampsCache";
    final String unrecommendedTimestamps = "unrecommended-timestamps";
    Properties p = createProperties();
    p.setProperty(TIMESTAMPS_CACHE_RESOURCE_PROP, unrecommendedTimestamps);
    TestInfinispanRegionFactory factory = createRegionFactory(p, (f, m) -> {
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.clustering().stateTransfer().fetchInMemoryState(true);
        builder.clustering().cacheMode(CacheMode.REPL_SYNC);
        m.defineConfiguration(unrecommendedTimestamps, builder.build());
    });
    try {
        assertEquals(unrecommendedTimestamps, factory.getBaseConfiguration(DataType.TIMESTAMPS));
        TimestampsRegionImpl region = (TimestampsRegionImpl) factory.buildTimestampsRegion(timestamps, p);
        AdvancedCache cache = region.getCache();
        Configuration cacheCfg = cache.getCacheConfiguration();
        assertEquals(EvictionStrategy.NONE, cacheCfg.eviction().strategy());
        assertEquals(CacheMode.REPL_SYNC, cacheCfg.clustering().cacheMode());
        assertFalse(cacheCfg.storeAsBinary().enabled());
        assertFalse(cacheCfg.jmxStatistics().enabled());
    } finally {
        factory.stop();
    }
}
Also used : 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) Configuration(org.infinispan.configuration.cache.Configuration) AdvancedCache(org.infinispan.AdvancedCache) Properties(java.util.Properties) Test(org.junit.Test)

Example 30 with AdvancedCache

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

the class InfinispanRegionFactoryTestCase method testBuildEntityCollectionRegionsPersonPlusEntityCollectionOverrides.

@Test
public void testBuildEntityCollectionRegionsPersonPlusEntityCollectionOverrides() {
    final String person = "com.acme.Person";
    final String address = "com.acme.Address";
    final String car = "com.acme.Car";
    final String addresses = "com.acme.Person.addresses";
    final String parts = "com.acme.Car.parts";
    Properties p = createProperties();
    // First option, cache defined for entity and overrides for generic entity data type and entity itself.
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.cfg", "person-cache");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.eviction.strategy", "LRU");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.eviction.max_entries", "5000");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.expiration.wake_up_interval", "2000");
    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", "LIRS");
    p.setProperty("hibernate.cache.infinispan.entity.expiration.wake_up_interval", "3000");
    p.setProperty("hibernate.cache.infinispan.entity.eviction.max_entries", "20000");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.addresses.cfg", "addresses-cache");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.addresses.eviction.strategy", "LIRS");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.addresses.eviction.max_entries", "5500");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.addresses.expiration.wake_up_interval", "2500");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.addresses.expiration.lifespan", "65000");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.addresses.expiration.max_idle", "35000");
    p.setProperty("hibernate.cache.infinispan.collection.cfg", "mycollection-cache");
    p.setProperty("hibernate.cache.infinispan.collection.eviction.strategy", "LRU");
    p.setProperty("hibernate.cache.infinispan.collection.expiration.wake_up_interval", "3500");
    p.setProperty("hibernate.cache.infinispan.collection.eviction.max_entries", "25000");
    TestInfinispanRegionFactory factory = createRegionFactory(p);
    try {
        EmbeddedCacheManager manager = factory.getCacheManager();
        assertFalse(manager.getCacheManagerConfiguration().globalJmxStatistics().enabled());
        assertNotNull(factory.getBaseConfiguration(person));
        assertFalse(isDefinedCache(factory, person));
        assertNotNull(factory.getBaseConfiguration(addresses));
        assertFalse(isDefinedCache(factory, addresses));
        assertNull(factory.getBaseConfiguration(address));
        assertNull(factory.getBaseConfiguration(parts));
        AdvancedCache cache;
        EntityRegionImpl region = (EntityRegionImpl) factory.buildEntityRegion(person, p, MUTABLE_NON_VERSIONED);
        assertTrue(isDefinedCache(factory, person));
        cache = region.getCache();
        Configuration cacheCfg = cache.getCacheConfiguration();
        assertEquals(EvictionStrategy.LRU, cacheCfg.eviction().strategy());
        assertEquals(2000, cacheCfg.expiration().wakeUpInterval());
        assertEquals(5000, cacheCfg.eviction().maxEntries());
        assertEquals(60000, cacheCfg.expiration().lifespan());
        assertEquals(30000, cacheCfg.expiration().maxIdle());
        assertFalse(cacheCfg.jmxStatistics().enabled());
        region = (EntityRegionImpl) factory.buildEntityRegion(address, p, MUTABLE_NON_VERSIONED);
        assertTrue(isDefinedCache(factory, person));
        cache = region.getCache();
        cacheCfg = cache.getCacheConfiguration();
        assertEquals(EvictionStrategy.LIRS, cacheCfg.eviction().strategy());
        assertEquals(3000, cacheCfg.expiration().wakeUpInterval());
        assertEquals(20000, cacheCfg.eviction().maxEntries());
        assertFalse(cacheCfg.jmxStatistics().enabled());
        region = (EntityRegionImpl) factory.buildEntityRegion(car, p, MUTABLE_NON_VERSIONED);
        assertTrue(isDefinedCache(factory, person));
        cache = region.getCache();
        cacheCfg = cache.getCacheConfiguration();
        assertEquals(EvictionStrategy.LIRS, cacheCfg.eviction().strategy());
        assertEquals(3000, cacheCfg.expiration().wakeUpInterval());
        assertEquals(20000, cacheCfg.eviction().maxEntries());
        assertFalse(cacheCfg.jmxStatistics().enabled());
        CollectionRegionImpl collectionRegion = (CollectionRegionImpl) factory.buildCollectionRegion(addresses, p, MUTABLE_NON_VERSIONED);
        assertTrue(isDefinedCache(factory, person));
        cache = collectionRegion.getCache();
        cacheCfg = cache.getCacheConfiguration();
        assertEquals(EvictionStrategy.LIRS, cacheCfg.eviction().strategy());
        assertEquals(2500, cacheCfg.expiration().wakeUpInterval());
        assertEquals(5500, cacheCfg.eviction().maxEntries());
        assertEquals(65000, cacheCfg.expiration().lifespan());
        assertEquals(35000, cacheCfg.expiration().maxIdle());
        assertFalse(cacheCfg.jmxStatistics().enabled());
        collectionRegion = (CollectionRegionImpl) factory.buildCollectionRegion(parts, p, MUTABLE_NON_VERSIONED);
        assertTrue(isDefinedCache(factory, addresses));
        cache = collectionRegion.getCache();
        cacheCfg = cache.getCacheConfiguration();
        assertEquals(EvictionStrategy.LRU, cacheCfg.eviction().strategy());
        assertEquals(3500, cacheCfg.expiration().wakeUpInterval());
        assertEquals(25000, cacheCfg.eviction().maxEntries());
        assertFalse(cacheCfg.jmxStatistics().enabled());
        collectionRegion = (CollectionRegionImpl) factory.buildCollectionRegion(parts, p, MUTABLE_NON_VERSIONED);
        assertTrue(isDefinedCache(factory, addresses));
        cache = collectionRegion.getCache();
        cacheCfg = cache.getCacheConfiguration();
        assertEquals(EvictionStrategy.LRU, cacheCfg.eviction().strategy());
        assertEquals(3500, cacheCfg.expiration().wakeUpInterval());
        assertEquals(25000, cacheCfg.eviction().maxEntries());
        assertFalse(cacheCfg.jmxStatistics().enabled());
    } finally {
        factory.stop();
    }
}
Also used : EntityRegionImpl(org.hibernate.cache.infinispan.entity.EntityRegionImpl) Configuration(org.infinispan.configuration.cache.Configuration) 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

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