new ConfigurationBuilder().persistence(); persistConfig.passivation(false); final SingleFileStoreConfigurationBuilder fileStore = fileStore.jmxStatistics().disable(); final Configuration config = persistConfig.addStore(fileStore).build(); config.compatibility().enabled(); manager.getManager().defineConfiguration(namedCache, config); final Cache<String, String> cache = manager.getManager().getCache(namedCache); treeCache = f.createTreeCache(cache); cache.start();
@Override public void accept(EmbeddedCacheManager manager) { manager.removeListener(this); manager.stop(); InfinispanLogger.ROOT_LOGGER.debugf("%s cache container stopped", this.name); }
@Override public String locate(String sessionId) { DistributionManager dist = this.cache.getAdvancedCache().getDistributionManager(); Address address = (dist != null) && !this.cache.getCacheConfiguration().clustering().cacheMode().isScattered() ? dist.getCacheTopology().getDistribution(new Key<>(sessionId)).primary() : this.cache.getCacheManager().getAddress(); Node node = (address != null) ? this.factory.createNode(address) : null; Map.Entry<String, Void> entry = (node != null) ? this.registry.getEntry(node) : null; if (entry == null) { entry = this.registry.getEntry(this.registry.getGroup().getLocalMember()); } return (entry != null) ? entry.getKey() : null; } }
@Override public EmbeddedCacheManager get() { GlobalConfiguration config = this.configuration.get(); String defaultCacheName = config.defaultCacheName().orElse(null); // We need to create a dummy default configuration if cache has a default cache Configuration defaultConfiguration = (defaultCacheName != null) ? new ConfigurationBuilder().build() : null; EmbeddedCacheManager manager = new DefaultCacheManager(config, defaultConfiguration, false); // Undefine the default cache, if we defined one if (defaultCacheName != null) { manager.undefineConfiguration(defaultCacheName); } // Override GlobalConfigurationManager with a local implementation @SuppressWarnings("deprecation") BasicComponentRegistry registry = manager.getGlobalComponentRegistry().getComponent(BasicComponentRegistry.class); registry.replaceComponent(GlobalConfigurationManager.class.getName(), new LocalGlobalConfigurationManager(), false); registry.rewire(); manager.start(); manager.addListener(this); InfinispanLogger.ROOT_LOGGER.debugf("%s cache container started", this.name); return manager; }
@TopologyChanged public void topologyChanged(TopologyChangedEvent<?, ?> event) { if (event.isPre()) return; int viewId = event.getCache().getCacheManager().getTransport().getViewId(); if (!this.listeners.isEmpty()) { Address localAddress = event.getCache().getCacheManager().getAddress(); Membership previousMembership = new CacheMembership(localAddress, event.getWriteConsistentHashAtStart(), this); Membership membership = new CacheMembership(localAddress, event.getWriteConsistentHashAtEnd(), this); Boolean status = this.views.get(viewId); boolean merged = (status != null) ? status.booleanValue() : false; for (Map.Entry<GroupListener, ExecutorService> entry : this.listeners.entrySet()) { GroupListener listener = entry.getKey(); ExecutorService executor = entry.getValue(); try { executor.submit(() -> { try { listener.membershipChanged(previousMembership, membership, merged); } catch (Throwable e) { ClusteringServerLogger.ROOT_LOGGER.warn(e.getLocalizedMessage(), e); } }); } catch (RejectedExecutionException e) { // Listener was unregistered } } } // Purge obsolete views this.views.headMap(viewId).clear(); }
@Override public void call() { ConfigurationBuilder builder = new ConfigurationBuilder(); CacheMode cacheMode = CacheMode.DIST_ASYNC; // DIST_ASYNC isn't default so it should stay applied builder.clustering().cacheMode(cacheMode); String distCacheName = "dist-cache"; cm.defineConfiguration(distCacheName, builder.build()); String templateName = "template"; cm.defineConfiguration(templateName, new ConfigurationBuilder().template(true).build()); Cache cache = cm.getCache(distCacheName, templateName); assertEquals(cacheMode, cache.getCacheConfiguration().clustering().cacheMode()); } });
public void testConfigurationOverride() throws Exception { ConfigurationBuilder defaultCfgBuilder = new ConfigurationBuilder(); defaultCfgBuilder.memory().size(200).storageType(StorageType.BINARY); cm = TestCacheManagerFactory.createCacheManager(defaultCfgBuilder); final ConfigurationBuilder cacheCfgBuilder = new ConfigurationBuilder().read(defaultCfgBuilder.build()); cm.defineConfiguration("my-cache", cacheCfgBuilder.build()); Cache<?, ?> cache = cm.getCache("my-cache"); assertEquals(200, cache.getCacheConfiguration().memory().size()); assertEquals(StorageType.BINARY, cache.getCacheConfiguration().memory().storageType()); }
@Override public void call() { List<AsyncInterceptor> interceptors = cacheManager.getCache("interceptors").getAdvancedCache().getAsyncInterceptorChain() .getInterceptors(); assertEquals(FooInterceptor.class, interceptors.get(interceptors.size() - 2).getClass()); } });
@Override public void call() { ConfigurationBuilder customConfiguration = TestCacheManagerFactory.getDefaultCacheConfiguration(true); customConfiguration.transaction().transactionManagerLookup(new TxManagerLookupA()); Configuration definedConfiguration = cm.defineConfiguration("aCache", customConfiguration.build()); // verify the setting was not lost: assertTrue(definedConfiguration.transaction().transactionManagerLookup() instanceof TxManagerLookupA); // verify it's actually being used: TransactionManager activeTransactionManager = cm.getCache("aCache").getAdvancedCache().getTransactionManager(); assertNotNull(activeTransactionManager); assertTrue(activeTransactionManager instanceof TmA); } });
public void testStatistics() { Configuration cfg = new ConfigurationBuilder().simpleCache(true).jmxStatistics().enabled(true).build(); String name = "statsCache"; cacheManager.defineConfiguration(name, cfg); Cache<Object, Object> cache = cacheManager.getCache(name); assertEquals(0L, cache.getAdvancedCache().getStats().getStores()); cache.put("key", "value"); assertEquals(1L, cache.getAdvancedCache().getStats().getStores()); } }
@Override public void call() { // DIST_ASYNC isn't default so it should stay applied CacheMode cacheMode = CacheMode.DIST_ASYNC; String templateName = "dist-cache-template"; cm.defineConfiguration(templateName, new ConfigurationBuilder().clustering().cacheMode(cacheMode).template(true).build()); CacheMode overrideCacheMode = CacheMode.REPL_ASYNC; Configuration overrideConfiguration = new ConfigurationBuilder().clustering().cacheMode(overrideCacheMode).build(); String ourCacheName = "my-cache"; cm.defineConfiguration(ourCacheName, templateName, overrideConfiguration); Cache cache = cm.getCache(ourCacheName); // We expect the override to take precedence assertEquals(overrideCacheMode, cache.getCacheConfiguration().clustering().cacheMode()); } });
@Test public void testDependencyOnStoppedCaches() { cacheManager.defineConfiguration("A", new ConfigurationBuilder().build()); cacheManager.defineConfiguration("B", new ConfigurationBuilder().build()); Cache<?, ?> cacheA = cacheManager.getCache("A"); Cache<?, ?> cacheB = cacheManager.getCache("B"); cacheA.stop(); cacheManager.addCacheDependency("A", "B"); CacheEventListener listener = new CacheEventListener(); cacheManager.addListener(listener); cacheManager.stop(); assertAllTerminated(cacheA, cacheB); assertEquals(Arrays.asList("B", GlobalConfigurationManager.CONFIG_STATE_CACHE_NAME, DEFAULT_CACHE_NAME), listener.stopOrder); }
@Override public void call() { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.clustering().cacheMode(CacheMode.DIST_SYNC).l1().disable(); cm.defineConfiguration("testConfigCache", cb.build()); Cache<Object, Object> cache = cm.getCache("testConfigCache"); assert !cache.getCacheConfiguration().clustering().l1().enabled(); } });
@Test public void testRestoreTransactionalAtomicMap(final Method m) throws Exception { cacheManager.defineConfiguration(m.getName(), configureCacheLoader(null, false).build()); Cache<String, Object> cache = cacheManager.getCache(m.getName()); TransactionManager tm = cache.getAdvancedCache().getTransactionManager(); tm.begin(); final AtomicMap<String, String> map = AtomicMapLookup.getAtomicMap(cache, m.getName()); map.put("a", "b"); tm.commit(); //evict from memory cache.evict(m.getName()); // now re-retrieve the map and make sure we see the diffs assertEquals("b", AtomicMapLookup.getAtomicMap(cache, m.getName()).get("a")); }
public void testOverrideWithStore() { final ConfigurationBuilder builder1 = new ConfigurationBuilder(); builder1.persistence().addStore(DummyInMemoryStoreConfigurationBuilder.class); cm = TestCacheManagerFactory.createCacheManager(builder1); ConfigurationBuilder builder2 = new ConfigurationBuilder(); builder2.read(cm.getDefaultCacheConfiguration()); builder2.memory().size(1000); Configuration configuration = cm.defineConfiguration("named", builder2.build()); assertEquals(1, configuration.persistence().stores().size()); }
protected void createCacheManagers() throws Throwable { cm1 = addClusterEnabledCacheManager(); cfg = new ConfigurationBuilder(); cfg.clustering().cacheMode(CacheMode.REPL_SYNC) .stateTransfer().fetchInMemoryState(false); cm1.defineConfiguration("cache1", cfg.build()); cm1.defineConfiguration("cache2", cfg.build()); }
protected void checkConsistencyAcrossCluster(String cacheName, Configuration configuration) { // Initialize the partitioner to ensure we can compare config equality configuration.clustering().hash().keyPartitioner().init(configuration.clustering().hash()); for (EmbeddedCacheManager m : cacheManagers) { Configuration actualConfiguration = m.getCacheConfiguration(cacheName); assertNotNull("Cache " + cacheName + " missing from " + m, actualConfiguration); assertEquals(configuration, actualConfiguration); Cache<Object, Object> cache = m.getCache(cacheName); assertEquals(cacheManagers.size(), cache.getAdvancedCache().getRpcManager().getMembers().size()); } }
@Test public void testIndexingOnCacheItself() throws CyclicDependencyException { EmbeddedCacheManager cacheManager = createClusteredCacheManager(); cacheManager.defineConfiguration("single", getIndexedConfigWithCustomCaches("single", "single", "single").build()); startAndIndexData("single", cacheManager); cacheManager.stop(); assertEquals(cacheManager.getStatus(), ComponentStatus.TERMINATED); }
public void testConfigOverride() { ConfigurationBuilder configuration = getDefaultStandaloneCacheConfig(true); configuration.transaction().useSynchronization(true); cacheManager.defineConfiguration("newCache", configuration.build()); assertTrue(cacheManager.getCache("newCache").getCacheConfiguration().transaction().useSynchronization()); } }
@Test public void testInternalDataContainer() { DefaultSegmentedDataContainer container = new DefaultSegmentedDataContainer((Supplier<ConcurrentMap>) ConcurrentHashMap::new, 1); ConfigurationBuilder configuration = new ConfigurationBuilder(); configuration.dataContainer().dataContainer(container); EmbeddedCacheManager cm = TestCacheManagerFactory.createCacheManager(configuration); try { AdvancedCache<Object, Object> cache = cm.getCache().getAdvancedCache(); assertEquals(container, cache.getDataContainer()); } finally { TestingUtil.killCacheManagers(cm); } }