case 304: counterProcessor.createCounter(getHeader(), auth.getSubject(hr2x_operation), hr2x_counterName, hr2x_counterConfiguration.build()); case 305: hr2x_counterConfiguration = CounterConfiguration.builder(EncodeUtil.decodeType(hr2x_counterFlags)).storage(EncodeUtil.decodeStorage(hr2x_counterFlags)); hr2x_counterConfiguration.concurrencyLevel(hr2x_vInt); if (buf.readerIndex() == pos) return false; hr2x_initialValue = hr2x_long; hr2x_counterConfiguration = hr2x_counterConfiguration.initialValue(hr2x_initialValue); hr2x_counterConfiguration.lowerBound(hr2x_lowerBound).upperBound(hr2x_upperBound);
.upperBound(10) .initialValue(1) .build()); .initialValue(2) .build()); .initialValue(3) .build());
@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); } }
/** * 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; }
/** * Decodes a {@link CounterConfiguration} encoded by {@link #encodeConfiguration(CounterConfiguration, Consumer, * LongConsumer, IntConsumer)}. * * @return the decoded {@link CounterConfiguration}. * @see #encodeConfiguration(CounterConfiguration, Consumer, LongConsumer, IntConsumer) */ public static CounterConfiguration decodeConfiguration(Supplier<Byte> byteSupplier, LongSupplier longSupplier, IntSupplier intSupplier) { byte flags = byteSupplier.get(); CounterType type = decodeType(flags); CounterConfiguration.Builder builder = CounterConfiguration.builder(type); builder.storage(decodeStorage(flags)); switch (type) { case WEAK: builder.concurrencyLevel(intSupplier.getAsInt()); break; case BOUNDED_STRONG: builder.lowerBound(longSupplier.getAsLong()); builder.upperBound(longSupplier.getAsLong()); break; case UNBOUNDED_STRONG: default: break; } builder.initialValue(longSupplier.getAsLong()); return builder.build(); }
/** * 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; }
/** * Implementation of abstract method processModelNode * */ @Override void processModelNode(OperationContext context, ModelNode counter, CounterConfiguration.Builder builder) throws OperationFailedException { super.processModelNode(context, counter, builder); ModelNode upperBoundModel = counter.get(ModelKeys.UPPER_BOUND); ModelNode lowerBoundModel = counter.get(ModelKeys.LOWER_BOUND); if (lowerBoundModel.isDefined()) { Long lowerBound = StrongCounterResource.LOWER_BOUND.resolveModelAttribute(context, counter).asLong(); builder.lowerBound(lowerBound); } if (upperBoundModel.isDefined()) { Long upperBound = StrongCounterResource.UPPER_BOUND.resolveModelAttribute(context, counter).asLong(); builder.upperBound(upperBound); } } }
@Override public Collection<ServiceController<?>> installRuntimeServices(OperationContext context, ModelNode operation, ModelNode containerModel, ModelNode cacheModel) throws OperationFailedException { String counterName = getCounterName(operation); String containerName = getContainerName(operation); String counterType = getCounterType(operation); Builder b = getBuilder(containerModel, counterType); processModelNode(context, containerModel, b); String name = CounterResource.COUNTER_NAME.resolveModelAttribute(context, containerModel).asString(); if (!counterName.equals(name)) { throw new OperationFailedException("Counter node name and node's name attribute should be the same"); } // wire counter service for this counter Collection<ServiceController<?>> controllers = new ArrayList<>(1); ServiceController<?> service = this.installCounterService(context.getServiceTarget(), containerName, counterName, b.build()); controllers.add(service); return controllers; }
@Override public void testListenerWithBounds(Method method) throws InterruptedException { final String counterName = method.getName(); final CounterManager counterManager = counterManagerSupplier.get(); assertTrue(counterManager .defineCounter(counterName, builder(BOUNDED_STRONG).initialValue(0).lowerBound(0).upperBound(20).build())); StrongCounter counter = counterManager.getStrongCounter(counterName); Handle<EventLogger> handle = counter.addListener(new EventLogger()); add(counter, 1, 1); expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.addAndGet(20).get()); assertCounterValue(counter, 20); reset(counter); expectException(CounterOutOfBoundsException.class, () -> add(counter, -1, 0)); assertCounterValue(counter, 0); assertNextValidEvent(handle, 0, 1); assertNextEvent(handle, 1, CounterState.VALID, 20, CounterState.UPPER_BOUND_REACHED); assertNextEvent(handle, 20, CounterState.UPPER_BOUND_REACHED, 0, CounterState.VALID); assertNextEvent(handle, 0, CounterState.VALID, 0, CounterState.LOWER_BOUND_REACHED); assertNoEvents(handle); handle.remove(); }
@Override public void testBoundaries(Method method) { final String counterName = method.getName(); final CounterManager counterManager = counterManagerSupplier.get(); assertTrue(counterManager .defineCounter(counterName, builder(BOUNDED_STRONG).initialValue(1).lowerBound(0).upperBound(20).build())); StrongCounter counter = counterManager.getStrongCounter(counterName); assertCounterValue(counter, 1); expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.addAndGet(-10).get()); expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.addAndGet(30).get()); assertCounterValue(counter, 20); expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.compareAndSet(20, -1).get()); assertCounterValue(counter, 20); expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.compareAndSet(20, 21).get()); }
@Override public CounterConfiguration readObject(ObjectInput input) throws IOException, ClassNotFoundException { byte flags = input.readByte(); CounterType type = decodeType(flags); Builder builder = builder(type); builder.storage(decodeStorage(flags)); builder.initialValue(input.readLong()); switch (type) { case BOUNDED_STRONG: builder.lowerBound(input.readLong()); builder.upperBound(input.readLong()); break; case WEAK: builder.concurrencyLevel(readUnsignedInt(input)); break; default: } return builder.build(); } }
@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 void testCompareAndSwapBounds(Method method) { CounterManager counterManager = counterManager(0); counterManager.defineCounter(method.getName(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(-2).upperBound(2).build()); SyncStrongCounter counter = counterManager.getStrongCounter(method.getName()).sync(); assertEquals(0, counter.compareAndSwap(0, 2)); assertEquals(2, counter.getValue()); assertOutOfBoundCompareAndSwap(counter, 2, 3); counter.reset(); assertEquals(0, counter.compareAndSwap(0, -2)); assertEquals(-2, counter.getValue()); assertOutOfBoundCompareAndSwap(counter, -2, -3); counter.reset(); assertEquals(0, counter.compareAndSwap(1, 3)); assertEquals(0, counter.compareAndSwap(1, -3)); }
public void testCompareAndSetBounds(Method method) { CounterManager counterManager = counterManager(0); counterManager.defineCounter(method.getName(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(-2).upperBound(2).build()); SyncStrongCounter counter = counterManager.getStrongCounter(method.getName()).sync(); assertTrue(counter.compareAndSet(0, 2)); assertEquals(2, counter.getValue()); assertOutOfBoundCompareAndSet(counter, 2, 3); counter.reset(); assertTrue(counter.compareAndSet(0, -2)); assertEquals(-2, counter.getValue()); assertOutOfBoundCompareAndSet(counter, -2, -3); counter.reset(); assertFalse(counter.compareAndSet(1, 3)); assertFalse(counter.compareAndSet(1, -3)); }
@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()); }
public void testSimpleThreshold(Method method) { CounterManager counterManager = counterManager(0); counterManager.defineCounter(method.getName(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(-1).upperBound(1).build()); StrongTestCounter counter = new StrongTestCounter(counterManager.getStrongCounter(method.getName())); addAndAssertResult(counter, 1, 1); assertOutOfBoundsAdd(counter, 1, 1); addAndAssertResult(counter, -1, 0); addAndAssertResult(counter, -1, -1); assertOutOfBoundsAdd(counter, -1, -1); counter.reset(); assertOutOfBoundsAdd(counter, 2, 1); assertOutOfBoundsAdd(counter, -3, -1); }
private static CounterConfiguration fromProperties(Properties properties) { return PropertyFormatter.getInstance().from(properties).build(); }
@Override void define(CounterManager manager, String name) { manager.defineCounter(name, builder(CounterType.UNBOUNDED_STRONG).initialValue(10).build()); }
private void define(CounterManager manager) { //lower bound is ignored if the type is not bounded. manager.defineCounter(name, CounterConfiguration.builder(type).lowerBound(0).storage(storage).build()); }
@Override List<CounterConfiguration> configurationsToTest() { return singletonList(builder(WEAK).initialValue(1).concurrencyLevel(2).build()); }