Search in sources :

Example 1 with ConfigurationBuilder

use of org.infinispan.configuration.cache.ConfigurationBuilder 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 2 with ConfigurationBuilder

use of org.infinispan.configuration.cache.ConfigurationBuilder in project hibernate-orm by hibernate.

the class InfinispanRegionFactory method getOrCreateConfig.

private ConfigurationBuilder getOrCreateConfig(int prefixLoc, String key, int suffixLoc) {
    final String name = key.substring(prefixLoc + PREFIX.length(), suffixLoc);
    ConfigurationBuilder builder = configOverrides.get(name);
    if (builder == null) {
        builder = new ConfigurationBuilder();
        configOverrides.put(name, builder);
    }
    return builder;
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder)

Example 3 with ConfigurationBuilder

use of org.infinispan.configuration.cache.ConfigurationBuilder 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)

Example 4 with ConfigurationBuilder

use of org.infinispan.configuration.cache.ConfigurationBuilder in project hibernate-orm by hibernate.

the class InfinispanRegionFactoryTestCase method testTimestampValidation.

@Test(expected = CacheException.class)
public void testTimestampValidation() {
    final String timestamps = "org.hibernate.cache.spi.UpdateTimestampsCache";
    Properties p = createProperties();
    InputStream configStream = FileLookupFactory.newInstance().lookupFile(InfinispanRegionFactory.DEF_INFINISPAN_CONFIG_RESOURCE, getClass().getClassLoader());
    ConfigurationBuilderHolder cbh = new ParserRegistry().parse(configStream);
    DefaultCacheManager manager = new DefaultCacheManager(cbh, true);
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.clustering().cacheMode(CacheMode.INVALIDATION_SYNC);
    manager.defineConfiguration(DEF_TIMESTAMPS_RESOURCE, builder.build());
    try {
        InfinispanRegionFactory factory = createRegionFactory(manager, p, null);
        factory.start(CacheTestUtil.sfOptionsForStart(), p);
        TimestampsRegionImpl region = (TimestampsRegionImpl) factory.buildTimestampsRegion(timestamps, p);
        fail("Should have failed saying that invalidation is not allowed for timestamp caches.");
    } finally {
        TestingUtil.killCacheManagers(manager);
    }
}
Also used : TimestampsRegionImpl(org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) ClusteringConfigurationBuilder(org.infinispan.configuration.cache.ClusteringConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) ConfigurationBuilderHolder(org.infinispan.configuration.parsing.ConfigurationBuilderHolder) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) InputStream(java.io.InputStream) ParserRegistry(org.infinispan.configuration.parsing.ParserRegistry) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with ConfigurationBuilder

use of org.infinispan.configuration.cache.ConfigurationBuilder in project hibernate-orm by hibernate.

the class PutFromLoadValidatorUnitTest method testGetForNullReleasePuts.

@Test
@TestForIssue(jiraKey = "HHH-9928")
public void testGetForNullReleasePuts() {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.simpleCache(true).expiration().maxIdle(500);
    Configuration ppCfg = cb.build();
    InfinispanRegionFactory regionFactory = mock(InfinispanRegionFactory.class);
    doReturn(ppCfg).when(regionFactory).getPendingPutsCacheConfiguration();
    doAnswer(invocation -> TIME_SERVICE.wallClockTime()).when(regionFactory).nextTimestamp();
    PutFromLoadValidator testee = new PutFromLoadValidator(cache, regionFactory, cm);
    for (int i = 0; i < 100; ++i) {
        try {
            withTx(tm, () -> {
                SharedSessionContractImplementor session = mock(SharedSessionContractImplementor.class);
                testee.registerPendingPut(session, KEY1, 0);
                return null;
            });
            TIME_SERVICE.advance(10);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    String ppName = cm.getCache().getName() + "-" + InfinispanRegionFactory.DEF_PENDING_PUTS_RESOURCE;
    Map ppCache = cm.getCache(ppName, false);
    assertNotNull(ppCache);
    Object pendingPutMap = ppCache.get(KEY1);
    assertNotNull(pendingPutMap);
    int size;
    try {
        Method sizeMethod = pendingPutMap.getClass().getMethod("size");
        sizeMethod.setAccessible(true);
        size = (Integer) sizeMethod.invoke(pendingPutMap);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    // some of the pending puts need to be expired by now
    assertTrue(size < 100);
    // but some are still registered
    assertTrue(size > 0);
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) Configuration(org.infinispan.configuration.cache.Configuration) PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Method(java.lang.reflect.Method) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException) Exceptions.expectException(org.infinispan.test.Exceptions.expectException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)53 Configuration (org.infinispan.configuration.cache.Configuration)24 Test (org.junit.Test)19 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)14 DefaultCacheManager (org.infinispan.manager.DefaultCacheManager)12 ClusteringConfigurationBuilder (org.infinispan.configuration.cache.ClusteringConfigurationBuilder)7 GlobalConfiguration (org.infinispan.configuration.global.GlobalConfiguration)7 Properties (java.util.Properties)6 Map (java.util.Map)5 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)5 TimestampsRegionImpl (org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl)5 BeforeClass (org.junit.BeforeClass)5 AdvancedCache (org.infinispan.AdvancedCache)4 BackupConfigurationBuilder (org.infinispan.configuration.cache.BackupConfigurationBuilder)4 CacheMode (org.infinispan.configuration.cache.CacheMode)4 SitesConfigurationBuilder (org.infinispan.configuration.cache.SitesConfigurationBuilder)3 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)3 HashMap (java.util.HashMap)2 Consumer (java.util.function.Consumer)2 CacheProducer (org.commonjava.indy.subsys.infinispan.CacheProducer)2