@Override TestCounter get(CounterManager manager, String name) { return new WeakTestCounter(manager.getWeakCounter(name)); } };
@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(); } }
@Override protected WeakTestCounter createCounter(CounterManager counterManager, String counterName, CounterConfiguration configuration) { counterManager.defineCounter(counterName, configuration); return new WeakTestCounter(counterManager.getWeakCounter(counterName)); }
private long getWeakCounterValue(String name) { return counterManager(0).getWeakCounter(name).getValue(); }
@Override void assertCounterNameAndConfiguration(String counterName, CounterConfiguration configuration) { allCounterManagerSupplier.get().forEach(counterManager -> { WeakCounter counter = counterManager.getWeakCounter(counterName); assertEquals(counterName, counter.getName()); assertEquals(configuration, counter.getConfiguration()); }); } }
@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; } } }
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(); } }
private void addToWeakCounter(String name, long delta) { awaitCounterOperation(counterManager(0).getWeakCounter(name).add(delta)); }
private void resetWeakCounter(String name) { awaitCounterOperation(counterManager(0).getWeakCounter(name).reset()); }
@Override public WeakCounter defineAndCreateCounter(String counterName, long initialValue) { final CounterManager counterManager = counterManagerSupplier.get(); assertTrue(counterManager .defineCounter(counterName, builder(WEAK).initialValue(initialValue).concurrencyLevel(8).build())); return counterManager.getWeakCounter(counterName); }
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; }
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; } }
protected TestCounter createCounter(CounterManager counterManager, String counterName) { counterManager.defineCounter(counterName, CounterConfiguration.builder(CounterType.WEAK).concurrencyLevel(4).build()); return new WeakTestCounter(counterManager.getWeakCounter(counterName)); } }
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); }
@Override public TestCounter getCounter(EmbeddedCacheManager manager, String counterName) { CounterManager counterManager = asCounterManager(manager); counterManager.defineCounter(counterName, CounterConfiguration.builder(CounterType.WEAK).build()); return new WeakTestCounter(asCounterManager(manager).getWeakCounter(counterName)); } };
@Override protected WeakTestCounter createCounter(CounterManager counterManager, String counterName, long initialValue) { counterManager.defineCounter(counterName, CounterConfiguration.builder(CounterType.WEAK).initialValue(initialValue).concurrencyLevel(4).build()); return new WeakTestCounter(counterManager.getWeakCounter(counterName)); } }
public void testSingleConcurrencyLevel() throws ExecutionException, InterruptedException { final CounterManager counterManager = EmbeddedCounterManagerFactory.asCounterManager(manager(0)); final String counterName = "c1-counter"; counterManager.defineCounter(counterName, CounterConfiguration.builder(CounterType.WEAK).concurrencyLevel(1).build()); WeakCounter wc = counterManager.getWeakCounter(counterName); wc.add(2).get(); assertEquals(2, wc.getValue()); }
@Test public void testCounters() { CounterManager counterManager = RemoteCounterManagerFactory.asCounterManager(remoteCacheManager); counterManager.defineCounter("c1", CounterConfiguration.builder(CounterType.BOUNDED_STRONG) .upperBound(10) .initialValue(1) .build()); counterManager.defineCounter("c2", CounterConfiguration.builder(CounterType.WEAK) .initialValue(5) .build()); SyncStrongCounter c1 = counterManager.getStrongCounter("c1").sync(); SyncWeakCounter c2 = counterManager.getWeakCounter("c2").sync(); assertEquals(1, c1.getValue()); assertEquals(5, c2.getValue()); }