@Override void setProperty(CounterConfiguration config, Properties properties) { properties.setProperty(key, String.valueOf(config.type())); } },
@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; } } }
/** * Encodes the {@link Storage} and the {@link CounterType}. * <p> * See the documentation for further details about the encoding. * <p> * * @return the encoded {@link Storage} and the {@link CounterType}. * @see <a href="https://infinispan.org/docs/dev/user_guide/user_guide.html#counter-config-encode">Counter * Configuration Encoding</a> */ public static byte encodeTypeAndStorage(CounterConfiguration configuration) { return (byte) (encodeType(configuration.type()) | encodeStorage(configuration.storage())); }
@Override public WeakCounter getWeakCounter(String name) { WeakCounter c = (WeakCounter) counters.get(name); if (c == null) { CounterConfiguration config = getConfiguration(name); if (config == null || config.type() != CounterType.WEAK) { throw new IllegalStateException(); } c = new TestWeakCounter(name, config, client, notificationManager); counters.put(name, c); } return c; }
@Override public StrongCounter getStrongCounter(String name) { StrongCounter c = (StrongCounter) counters.get(name); if (c == null) { CounterConfiguration config = getConfiguration(name); if (config == null || config.type() == CounterType.WEAK) { throw new IllegalStateException(); } c = new TestStrongCounter(name, config, client, notificationManager); counters.put(name, c); } return c; }
public Properties format(CounterConfiguration configuration) { Properties properties = new Properties(); PropertyKey.TYPE.setProperty(configuration, properties); PropertyKey.STORAGE.setProperty(configuration, properties); PropertyKey.INITIAL_VALUE.setProperty(configuration, properties); switch (configuration.type()) { case UNBOUNDED_STRONG: break; case BOUNDED_STRONG: PropertyKey.UPPER_BOUND.setProperty(configuration, properties); PropertyKey.LOWER_BOUND.setProperty(configuration, properties); break; case WEAK: PropertyKey.CONCURRENCY.setProperty(configuration, properties); break; default: throw new IllegalStateException(); } return properties; }
private void applyCounter(HotRodHeader header, String counterName, BiConsumer<HotRodHeader, StrongCounter> applyStrong, BiConsumer<HotRodHeader, WeakCounter> applyWeak) { EmbeddedCounterManager counterManager = counterManager(header); CounterConfiguration config = counterManager.getConfiguration(counterName); if (config == null) { writeResponse(header, missingCounterResponse(header)); return; } switch (config.type()) { case UNBOUNDED_STRONG: case BOUNDED_STRONG: applyStrong.accept(header, counterManager.getStrongCounter(counterName)); break; case WEAK: applyWeak.accept(header, counterManager.getWeakCounter(counterName)); break; } }
/** * Encodes the configuration. * <p> * See the documentation for further details about the encoding. * <p> * * @see <a href="https://infinispan.org/docs/dev/user_guide/user_guide.html#counter-config-encode">Counter * Configuration Encoding</a> */ public static void encodeConfiguration(CounterConfiguration configuration, Consumer<Byte> byteConsumer, LongConsumer longConsumer, IntConsumer intConsumer) { byteConsumer.accept(encodeTypeAndStorage(configuration)); switch (configuration.type()) { case WEAK: intConsumer.accept(configuration.concurrencyLevel()); break; case BOUNDED_STRONG: longConsumer.accept(configuration.lowerBound()); longConsumer.accept(configuration.upperBound()); break; case UNBOUNDED_STRONG: break; default: throw new IllegalStateException(); } longConsumer.accept(configuration.initialValue()); }
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 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); } }