Search in sources :

Example 1 with WeakCounter

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());
}
Also used : WeakCounter(org.infinispan.counter.api.WeakCounter) CounterManager(org.infinispan.counter.api.CounterManager)

Example 2 with WeakCounter

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;
}
Also used : WeakCounter(org.infinispan.counter.api.WeakCounter) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration)

Example 3 with WeakCounter

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());
    });
}
Also used : WeakCounter(org.infinispan.counter.api.WeakCounter)

Example 4 with WeakCounter

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");
}
Also used : CacheException(org.infinispan.commons.CacheException) ZipEntry(java.util.zip.ZipEntry) WeakCounter(org.infinispan.counter.api.WeakCounter) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration) StrongCounter(org.infinispan.counter.api.StrongCounter)

Aggregations

WeakCounter (org.infinispan.counter.api.WeakCounter)4 CounterConfiguration (org.infinispan.counter.api.CounterConfiguration)2 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 ZipEntry (java.util.zip.ZipEntry)1 CacheException (org.infinispan.commons.CacheException)1 CounterManager (org.infinispan.counter.api.CounterManager)1 StrongCounter (org.infinispan.counter.api.StrongCounter)1