Search in sources :

Example 16 with CounterConfiguration

use of org.infinispan.counter.api.CounterConfiguration in project infinispan by infinispan.

the class ConfigurationTest method testInvalidEqualsUpperAndLowerBoundInManager.

public void testInvalidEqualsUpperAndLowerBoundInManager() {
    TestingUtil.withCacheManager(DefaultCacheManager::new, cacheManager -> {
        CounterManager manager = asCounterManager(cacheManager);
        CounterConfiguration cfg = CounterConfiguration.builder(CounterType.BOUNDED_STRONG).initialValue(10).lowerBound(10).upperBound(10).build();
        expectException(CounterConfigurationException.class, () -> manager.defineCounter("invalid", cfg));
    });
}
Also used : DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) CounterManager(org.infinispan.counter.api.CounterManager) EmbeddedCounterManagerFactory.asCounterManager(org.infinispan.counter.EmbeddedCounterManagerFactory.asCounterManager) StrongCounterConfiguration(org.infinispan.counter.configuration.StrongCounterConfiguration) AbstractCounterConfiguration(org.infinispan.counter.configuration.AbstractCounterConfiguration) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration) WeakCounterConfiguration(org.infinispan.counter.configuration.WeakCounterConfiguration)

Example 17 with CounterConfiguration

use of org.infinispan.counter.api.CounterConfiguration in project infinispan by infinispan.

the class CounterConfigurationManager method start.

/**
 * It checks for existing defined configurations in {@link CounterConfigurationStorage} and in the {@link Cache}.
 * <p>
 * If any is found, it starts the counter's {@link Cache}.
 */
public void start() {
    this.stateCache = cacheManager.<ScopedState, CounterConfiguration>getCache(GlobalConfigurationManager.CONFIG_STATE_CACHE_NAME).getAdvancedCache();
    listener = new CounterConfigurationListener();
    Map<String, CounterConfiguration> persisted = storage.loadAll();
    persisted.forEach((name, cfg) -> stateCache.putIfAbsent(stateKey(name), cfg));
    counterCacheStarted.set(false);
    stateCache.addListener(listener, new ScopeFilter(COUNTER_SCOPE), null);
    if (!persisted.isEmpty() || stateCache.keySet().stream().anyMatch(scopedState -> COUNTER_SCOPE.equals(scopedState.getScope()))) {
        // we have counter defined
        startCounterCache();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) GlobalConfigurationManager(org.infinispan.globalstate.GlobalConfigurationManager) Cache(org.infinispan.Cache) ScopeFilter(org.infinispan.globalstate.ScopeFilter) AdvancedCache(org.infinispan.AdvancedCache) ScopedState(org.infinispan.globalstate.ScopedState) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) Map(java.util.Map) CacheEntryModified(org.infinispan.notifications.cachelistener.annotation.CacheEntryModified) Listener(org.infinispan.notifications.Listener) AbstractCounterConfiguration(org.infinispan.counter.configuration.AbstractCounterConfiguration) ConvertUtil.parsedConfigToConfig(org.infinispan.counter.configuration.ConvertUtil.parsedConfigToConfig) Collection(java.util.Collection) Utils.validateStrongCounterBounds(org.infinispan.counter.impl.Utils.validateStrongCounterBounds) CounterModuleLifecycle(org.infinispan.counter.impl.CounterModuleLifecycle) Collectors(java.util.stream.Collectors) CacheCollectors(org.infinispan.stream.CacheCollectors) Objects(java.util.Objects) CounterManagerConfiguration(org.infinispan.counter.configuration.CounterManagerConfiguration) ComponentStatus(org.infinispan.lifecycle.ComponentStatus) CacheEntryCreated(org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated) Event(org.infinispan.notifications.cachelistener.event.Event) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration) CONTAINER(org.infinispan.counter.logging.Log.CONTAINER) BlockingManager(org.infinispan.util.concurrent.BlockingManager) Collections(java.util.Collections) ScopeFilter(org.infinispan.globalstate.ScopeFilter) ScopedState(org.infinispan.globalstate.ScopedState) AbstractCounterConfiguration(org.infinispan.counter.configuration.AbstractCounterConfiguration) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration)

Example 18 with CounterConfiguration

use of org.infinispan.counter.api.CounterConfiguration in project infinispan by infinispan.

the class EmbeddedCounterManager method removeCounter.

private void removeCounter(String counterName, boolean keepConfig) {
    CounterConfiguration configuration = getConfiguration(counterName);
    if (configuration == null) {
        // counter not defined (cluster-wide). do nothing :)
        return;
    }
    counters.compute(counterName, (name, counter) -> {
        removeCounter(name, counter, configuration);
        if (!keepConfig)
            awaitCounterOperation(configurationManager.removeConfiguration(name));
        return null;
    });
}
Also used : CounterConfiguration(org.infinispan.counter.api.CounterConfiguration)

Example 19 with CounterConfiguration

use of org.infinispan.counter.api.CounterConfiguration in project infinispan by infinispan.

the class EmbeddedCounterManager method createCounter.

private Object createCounter(String counterName) {
    CounterConfiguration configuration = getConfiguration(counterName);
    if (configuration == null) {
        throw CONTAINER.undefinedCounter(counterName);
    }
    switch(configuration.type()) {
        case WEAK:
            // topology listener is used to compute the keys where this node is the primary owner
            // adds are made on these keys to avoid contention and improve performance
            notificationManager.registerTopologyListener(cache());
            // the weak counter keeps a local value and, on each event, the local value is updated (reads are always local)
            notificationManager.registerCounterValueListener(cache());
            return createWeakCounter(counterName, configuration);
        case BOUNDED_STRONG:
            return createBoundedStrongCounter(counterName, configuration);
        case UNBOUNDED_STRONG:
            return createUnboundedStrongCounter(counterName, configuration);
        default:
            throw new IllegalStateException("[should never happen] unknown counter type: " + configuration.type());
    }
}
Also used : CounterConfiguration(org.infinispan.counter.api.CounterConfiguration)

Example 20 with CounterConfiguration

use of org.infinispan.counter.api.CounterConfiguration in project infinispan by infinispan.

the class ExpirationStrongCounterTest method testBoundedCounterNeverReached.

public void testBoundedCounterNeverReached() {
    CounterConfiguration config = CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lifespan(EXPIRATION_MILLISECONDS).upperBound(30).build();
    String counterName = "bounded-counter-2";
    CounterManager counterManager = counterManager();
    counterManager.defineCounter(counterName, config);
    doTest(counterName, 30);
}
Also used : CounterManager(org.infinispan.counter.api.CounterManager) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration)

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