Search in sources :

Example 21 with CounterConfiguration

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

Example 22 with CounterConfiguration

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

Example 23 with CounterConfiguration

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");
}
Also used : DataInputStream(java.io.DataInputStream) ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext) COUNTERS(org.infinispan.server.core.BackupManager.Resources.Type.COUNTERS) CounterManager(org.infinispan.counter.api.CounterManager) CompletionStages(org.infinispan.util.concurrent.CompletionStages) ProtoStreamTypeIds(org.infinispan.commons.marshall.ProtoStreamTypeIds) CounterType(org.infinispan.counter.api.CounterType) DataOutputStream(java.io.DataOutputStream) StrongCounter(org.infinispan.counter.api.StrongCounter) ZipFile(java.util.zip.ZipFile) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) ProtoField(org.infinispan.protostream.annotations.ProtoField) OutputStream(java.io.OutputStream) CacheException(org.infinispan.commons.CacheException) Flowable(io.reactivex.rxjava3.core.Flowable) Files(java.nio.file.Files) RxJavaInterop(org.infinispan.commons.reactive.RxJavaInterop) Set(java.util.Set) IOException(java.io.IOException) BackupManager(org.infinispan.server.core.BackupManager) CompletionStage(java.util.concurrent.CompletionStage) WeakCounter(org.infinispan.counter.api.WeakCounter) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration) ProtoTypeId(org.infinispan.protostream.annotations.ProtoTypeId) BlockingManager(org.infinispan.util.concurrent.BlockingManager) DataOutputStream(java.io.DataOutputStream) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration)

Example 24 with CounterConfiguration

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");
}
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)

Example 25 with CounterConfiguration

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);
    }
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) CounterManager(org.infinispan.counter.api.CounterManager) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration) HashSet(java.util.HashSet)

Aggregations

CounterConfiguration (org.infinispan.counter.api.CounterConfiguration)30 CounterManager (org.infinispan.counter.api.CounterManager)8 Random (java.util.Random)7 AbstractCounterConfiguration (org.infinispan.counter.configuration.AbstractCounterConfiguration)7 StrongCounter (org.infinispan.counter.api.StrongCounter)4 RestResponse (org.infinispan.client.rest.RestResponse)3 CounterType (org.infinispan.counter.api.CounterType)3 WeakCounter (org.infinispan.counter.api.WeakCounter)3 DataInputStream (java.io.DataInputStream)2 IOException (java.io.IOException)2 ZipEntry (java.util.zip.ZipEntry)2 RestCounterClient (org.infinispan.client.rest.RestCounterClient)2 CacheException (org.infinispan.commons.CacheException)2 BlockingManager (org.infinispan.util.concurrent.BlockingManager)2 Flowable (io.reactivex.rxjava3.core.Flowable)1 DataOutputStream (java.io.DataOutputStream)1 OutputStream (java.io.OutputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1