use of org.infinispan.counter.api.WeakCounter in project infinispan by infinispan.
the class WeakCounterTest method testSingleConcurrencyLevel.
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());
}
use of org.infinispan.counter.api.WeakCounter in project infinispan by infinispan.
the class TestCounterManager method getWeakCounter.
@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;
}
use of org.infinispan.counter.api.WeakCounter in project infinispan by infinispan.
the class WeakCounterImplTestStrategy method assertCounterNameAndConfiguration.
@Override
void assertCounterNameAndConfiguration(String counterName, CounterConfiguration configuration) {
allCounterManagerSupplier.get().forEach(counterManager -> {
WeakCounter counter = counterManager.getWeakCounter(counterName);
assertEquals(counterName, counter.getName());
assertEquals(configuration, counter.getConfiguration());
});
}
use of org.infinispan.counter.api.WeakCounter 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");
}
Aggregations