@Override protected ModelNode invoke(CounterManager counterManager, ModelNode operation) { final String counterName = counterName(operation); final String counterType = counterType(operation); if (counterManager.isDefined(counterName)) { boolean isStrongCounter = ModelKeys.STRONG_COUNTER.equals(counterType); if (isStrongCounter) { StrongCounter strongCounter = counterManager.getStrongCounter(counterName); strongCounter.sync().incrementAndGet(); } else { WeakCounter weakCounter = counterManager.getWeakCounter(counterName); weakCounter.sync().increment(); } } return new ModelNode(); } }
@Override protected ModelNode invoke(CounterManager counterManager, ModelNode operation) { final String counterName = counterName(operation); final String counterType = counterType(operation); if (counterManager.isDefined(counterName)) { boolean isStrongCounter = ModelKeys.STRONG_COUNTER.equals(counterType); if (isStrongCounter) { StrongCounter strongCounter = counterManager.getStrongCounter(counterName); strongCounter.sync().decrementAndGet(); } else { WeakCounter weakCounter = counterManager.getWeakCounter(counterName); weakCounter.sync().decrement(); } } return new ModelNode(); } }
@Override protected ModelNode invoke(CounterManager counterManager, ModelNode operation) { final String counterName = counterName(operation); final String counterType = counterType(operation); if (counterManager.isDefined(counterName)) { boolean isStrongCounter = ModelKeys.STRONG_COUNTER.equals(counterType); if (isStrongCounter) { StrongCounter strongCounter = counterManager.getStrongCounter(counterName); strongCounter.sync().reset(); } else { WeakCounter weakCounter = counterManager.getWeakCounter(counterName); weakCounter.sync().reset(); } } return new ModelNode(); } }
public CounterHandler(RemoteCacheManager cacheManager, Sequence sequence) { CounterManager counterManager = RemoteCounterManagerFactory.asCounterManager( cacheManager ); counterName = sequence.getExportIdentifier(); if ( !counterManager.isDefined( counterName ) ) { defineCounter( counterManager, counterName, sequence.getInitialValue() ); } counter = counterManager.getStrongCounter( counterName ); }
private void assertDefined(Collection<CounterDefinition> counters) { for (int i = 0; i < CLUSTER_SIZE; ++i) { CounterManager counterManager = counterManager(i); for (CounterDefinition definition : counters) { assertTrue("Configuration of " + definition.name + " is missing on manager " + i, counterManager.isDefined(definition.name)); } } }
private void assertNotDefined(Collection<CounterDefinition> counters) { for (int i = 0; i < CLUSTER_SIZE; ++i) { CounterManager counterManager = counterManager(i); for (CounterDefinition definition : counters) { assertFalse("Configuration of " + definition.name + " is defined on manager " + i, counterManager.isDefined(definition.name)); } } }
private static EmbeddedCacheManager buildCacheManager(GlobalConfigurationBuilder builder) { DefaultCacheManager cacheManager = new DefaultCacheManager(builder.build()); //result doesn't matter. isDefined will wait until the caches are started to avoid starting and killing //caches too fast asCounterManager(cacheManager).isDefined("some-counter"); return cacheManager; }
private void doCreationTest(String name, CounterConfiguration config) { List<CounterManager> remoteCounterManagers = allRemoteCounterManagerSupplier.get(); assertTrue(remoteCounterManagers.get(0).defineCounter(name, config)); remoteCounterManagers.forEach(cm -> assertFalse(cm.defineCounter(name, builder(CounterType.WEAK).build()))); remoteCounterManagers.forEach(cm -> assertTrue(cm.isDefined(name))); remoteCounterManagers.forEach(cm -> assertEquals(config, cm.getConfiguration(name))); //test single embedded counter manager to check if everything is correctly stored EmbeddedCacheManager cacheManager = cacheManagerSupplier.get(); CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(cacheManager); assertTrue(counterManager.isDefined(name)); assertEquals(config, counterManager.getConfiguration(name)); }
@Override public void testUndefinedCounter() { CounterManager counterManager = getTestedCounterManager(); assertFalse(counterManager.isDefined("not-defined-counter")); assertEquals(null, counterManager.getConfiguration("not-defined-counter")); }
/** * Create a counter if one is not defined already, otherwise return the existing one. * * @param counterName unique name for the counter * @param initialValue initial value for the counter * @return a {@link StrongCounter} */ protected StrongCounter getCounterOrCreateIt(String counterName, int initialValue) { CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager( cacheManager ); if ( !counterManager.isDefined( counterName ) ) { LOG.tracef( "Counter %s is not defined, creating it", counterName ); // global configuration is mandatory in order to define // a new clustered counter with persistent storage validateGlobalConfiguration(); counterManager.defineCounter( counterName, CounterConfiguration.builder( CounterType.UNBOUNDED_STRONG ) .initialValue( initialValue ) .storage( Storage.PERSISTENT ) .build() ); } StrongCounter strongCounter = counterManager.getStrongCounter( counterName ); return strongCounter; }
/** * Create a counter if one is not defined already, otherwise return the existing one. * * @param counterName unique name for the counter * @param initialValue initial value for the counter * @return a {@link StrongCounter} */ protected StrongCounter getCounterOrCreateIt(String counterName, int initialValue) { CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager( cacheManager ); if ( !counterManager.isDefined( counterName ) ) { LOG.tracef( "Counter %s is not defined, creating it", counterName ); // global configuration is mandatory in order to define // a new clustered counter with persistent storage validateGlobalConfiguration(); counterManager.defineCounter( counterName, CounterConfiguration.builder( CounterType.UNBOUNDED_STRONG ) .initialValue( initialValue ) .storage( Storage.PERSISTENT ) .build() ); } StrongCounter strongCounter = counterManager.getStrongCounter( counterName ); return strongCounter; }
public void testCounters() { final GlobalConfigurationBuilder builder = defaultGlobalConfigurationBuilder(true); CounterManagerConfigurationBuilder counterBuilder = builder.addModule(CounterManagerConfigurationBuilder.class); counterBuilder.addStrongCounter().name("unbounded-strong-1").initialValue(1).storage(Storage.VOLATILE) .addStrongCounter().name("lower-bounded-strong-2").initialValue(2).lowerBound(-10) .storage(Storage.PERSISTENT) .addStrongCounter().name("upper-bounded-strong-3").initialValue(3).upperBound(10) .storage(Storage.VOLATILE) .addStrongCounter().name("bounded-strong-4").initialValue(4).lowerBound(-20).upperBound(20) .storage(Storage.PERSISTENT) .addWeakCounter().name("weak-5").initialValue(5).concurrencyLevel(10).storage(Storage.VOLATILE); GlobalConfiguration config = builder.build(); CounterManagerConfiguration counterConfig = config.module(CounterManagerConfiguration.class); assertUnboundedStrongCounter(counterConfig); assertBoundedStrongCounter(counterConfig, "lower-bounded-strong-2", 2, -10, Long.MAX_VALUE, Storage.PERSISTENT); assertBoundedStrongCounter(counterConfig, "upper-bounded-strong-3", 3, Long.MIN_VALUE, 10, Storage.VOLATILE); assertBoundedStrongCounter(counterConfig, "bounded-strong-4", 4, -20, 20, Storage.PERSISTENT); assertWeakCounter(counterConfig); TestingUtil.withCacheManager(() -> new DefaultCacheManager(builder.build()), cacheManager -> { CounterManager manager = asCounterManager(cacheManager); AssertJUnit.assertTrue(manager.isDefined("unbounded-strong-1")); AssertJUnit.assertTrue(manager.isDefined("lower-bounded-strong-2")); AssertJUnit.assertTrue(manager.isDefined("upper-bounded-strong-3")); AssertJUnit.assertTrue(manager.isDefined("bounded-strong-4")); AssertJUnit.assertTrue(manager.isDefined("weak-5")); AssertJUnit.assertFalse(manager.isDefined("not-defined-counter")); }); }