@Override protected StrongTestCounter createCounter(CounterManager counterManager, String counterName, CounterConfiguration configuration) { counterManager.defineCounter(counterName, configuration); return new StrongTestCounter(counterManager.getStrongCounter(counterName)); }
@Override public void start(StartContext context) { EmbeddedCacheManager container = this.dependencies.getCacheContainer(); CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(container); //define counter counterManager.defineCounter(counterConfigurationName, counterConfiguration); //but also instantiate the counter as well switch (counterConfiguration.type()) { case BOUNDED_STRONG: case UNBOUNDED_STRONG: { counterManager.getStrongCounter(counterConfigurationName); break; } case WEAK: { counterManager.getWeakCounter(counterConfigurationName); break; } default: { log.warn("Unknown counter type " + counterConfiguration.type() + " did not get instantiated"); break; } } }
@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 WeakTestCounter createCounter(CounterManager counterManager, String counterName, CounterConfiguration configuration) { counterManager.defineCounter(counterName, configuration); return new WeakTestCounter(counterManager.getWeakCounter(counterName)); }
private long getValue(CounterManager counterManager) { switch (type) { case WEAK: return counterManager.getWeakCounter(name).getValue(); case BOUNDED_STRONG: case UNBOUNDED_STRONG: return counterManager.getStrongCounter(name).sync().getValue(); } throw new IllegalStateException(); } }
/** * 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; }
@Override public void testGetCounterNames(Method method) { //we need to cleanup other tests counters from the caches. clearCaches(); final Random random = generateRandom(); final String counterNamePrefix = method.getName(); final CounterManager counterManager = getTestedCounterManager(); final int numCounters = random.nextInt(10) + 1; final List<CounterConfiguration> configList = new ArrayList<>(numCounters); final Set<String> counterSet = new HashSet<>(); //adds some randomness to the test by adding 1 to 10 counters for (int i = 0; i < numCounters; ++i) { CounterConfiguration config = builder(CounterType.valueOf(random.nextInt(3))).initialValue(random.nextLong()) .build(); assertTrue(counterManager.defineCounter(counterNamePrefix + i, config)); configList.add(config); counterSet.add(counterNamePrefix + i); } Set<String> counterNames = new HashSet<>(counterManager.getCounterNames()); assertEquals(counterSet, counterNames); for (int i = 0; i < numCounters; ++i) { final String counterName = counterNamePrefix + i; assertTrue(counterNames.contains(counterName)); CounterConfiguration config = configList.get(i); CounterConfiguration storedConfig = config.type() == CounterType.WEAK ? counterManager.getWeakCounter(counterName).getConfiguration() : counterManager.getStrongCounter(counterName).getConfiguration(); assertEquals(config, storedConfig); } }
private void defineCounter(CounterManager counterManager, String counterName, int initialValue) { counterManager.defineCounter( counterName, CounterConfiguration.builder( CounterType.UNBOUNDED_STRONG ) .initialValue( initialValue ) .storage( Storage.PERSISTENT ) .build() ); } }
private Handle<Listener> createListener(byte version, String counterName, ByRef<Boolean> status) { CounterConfiguration configuration = counterManager.getConfiguration(counterName); if (configuration == null) { status.set(false); return null; } Handle<Listener> handle; if (configuration.type() == CounterType.WEAK) { handle = counterManager.getWeakCounter(counterName).addListener(new Listener(counterName, version)); } else { handle = counterManager.getStrongCounter(counterName).addListener(new Listener(counterName, version)); } status.set(true); return handle; }
@Override TestCounter get(CounterManager manager, String name) { return new StrongTestCounter(manager.getStrongCounter(name)); } },
@Override public void testRemove(Method method) { //we need to cleanup other tests counters from the caches because of cache.size() clearCaches(); final Random random = generateRandom(); final String counterName = method.getName(); final CounterManager counterManager = getTestedCounterManager(); CounterConfiguration config = builder(CounterType.UNBOUNDED_STRONG).initialValue(random.nextLong()).build(); assertTrue(counterManager.defineCounter(counterName, config)); awaitCounterOperation(counterManager.getStrongCounter(counterName).addAndGet(10)); EmbeddedCacheManager cacheManager = cacheManagerSupplier.get(); Cache<?, ?> cache = cacheManager.getCache(CounterModuleLifecycle.COUNTER_CACHE_NAME); assertEquals(1, cache.size()); counterManager.remove(counterName); assertEquals(0, cache.size()); }
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 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 TestCounter get(CounterManager manager, String name) { return new WeakTestCounter(manager.getWeakCounter(name)); } };
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)); } } }
@Override public void testUndefinedCounter() { CounterManager counterManager = getTestedCounterManager(); assertFalse(counterManager.isDefined("not-defined-counter")); assertEquals(null, counterManager.getConfiguration("not-defined-counter")); }
@Override public void testRemove(Method method) { final String counterName = method.getName(); final long initialValue = 5; T counter = defineAndCreateCounter(counterName, initialValue); add(counter, 100, 105); assertCounterValue(counter, 105); remove(counter); assertCounterValue(counter, initialValue); add(counter, -100, -95); assertCounterValue(counter, -95); counterManagerSupplier.get().remove(counterName); assertCounterValue(counter, initialValue); }
private static WeakCounterImpl getCounter(EmbeddedCacheManager manager, String counterName) { CounterManager counterManager = asCounterManager(manager); counterManager .defineCounter(counterName, CounterConfiguration.builder(CounterType.WEAK).concurrencyLevel(128).build()); return (WeakCounterImpl) counterManager.getWeakCounter(counterName); }
private void incrementCounter(CounterManager counterManager) { switch (type) { case UNBOUNDED_STRONG: case BOUNDED_STRONG: counterManager.getStrongCounter(name).sync().incrementAndGet(); break; case WEAK: counterManager.getWeakCounter(name).sync().increment(); break; } }
/** * 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; }