use of org.infinispan.counter.api.CounterConfiguration in project infinispan by infinispan.
the class ExpirationStrongCounterTest method testBoundedCounter.
public void testBoundedCounter() {
CounterConfiguration config = CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lifespan(EXPIRATION_MILLISECONDS).upperBound(3).build();
String counterName = "bounded-counter";
CounterManager counterManager = counterManager();
counterManager.defineCounter(counterName, config);
doTest(counterName, 3);
}
use of org.infinispan.counter.api.CounterConfiguration in project infinispan by infinispan.
the class EncodeUtil method decodeConfiguration.
/**
* 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();
}
use of org.infinispan.counter.api.CounterConfiguration in project infinispan by infinispan.
the class CounterResource method backup.
@Override
public CompletionStage<Void> backup() {
return blockingManager.blockingPublisherToVoidStage(Flowable.using(() -> {
mkdirs(root);
return new DataOutputStream(Files.newOutputStream(root.resolve(COUNTERS_FILE)));
}, output -> Flowable.fromIterable(resources).map(counter -> {
CounterConfiguration config = counterManager.getConfiguration(counter);
CounterBackupEntry e = new CounterBackupEntry();
e.name = counter;
e.configuration = config;
e.value = config.type() == CounterType.WEAK ? counterManager.getWeakCounter(counter).getValue() : CompletionStages.join(counterManager.getStrongCounter(counter).getValue());
return e;
}).doOnNext(e -> {
writeMessageStream(e, serCtx, output);
log.debugf("Counter added to backup: %s", e);
}).onErrorResumeNext(RxJavaInterop.cacheExceptionWrapper()), OutputStream::close), "write-counters");
}
use of org.infinispan.counter.api.CounterConfiguration in project infinispan by infinispan.
the class CounterResource method restore.
@Override
public CompletionStage<Void> restore(ZipFile zip) {
return blockingManager.runBlocking(() -> {
Set<String> countersToRestore = resources;
String countersFile = root.resolve(COUNTERS_FILE).toString();
ZipEntry zipEntry = zip.getEntry(countersFile);
if (zipEntry == null) {
if (!countersToRestore.isEmpty())
throw log.unableToFindBackupResource(type.toString(), countersToRestore);
return;
}
try (DataInputStream is = new DataInputStream(zip.getInputStream(zipEntry))) {
while (is.available() > 0) {
CounterBackupEntry entry = readMessageStream(serCtx, CounterBackupEntry.class, is);
if (!countersToRestore.contains(entry.name)) {
log.debugf("Ignoring '%s' counter", entry.name);
continue;
}
CounterConfiguration config = entry.configuration;
counterManager.defineCounter(entry.name, config);
if (config.type() == CounterType.WEAK) {
WeakCounter counter = counterManager.getWeakCounter(entry.name);
counter.add(entry.value - config.initialValue());
} else {
StrongCounter counter = counterManager.getStrongCounter(entry.name);
counter.compareAndSet(config.initialValue(), entry.value);
}
log.debugf("Counter restored: %s", entry);
}
} catch (IOException e) {
throw new CacheException(e);
}
}, "restore-counters");
}
use of org.infinispan.counter.api.CounterConfiguration in project infinispan by infinispan.
the class CounterManagerImplTestStrategy method testGetCounterNames.
@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);
}
}
Aggregations