Search in sources :

Example 26 with CounterConfiguration

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

the class CounterManagerImplTestStrategy method testUpperBoundedStrongCounter.

@Override
public void testUpperBoundedStrongCounter(Method method) {
    final Random random = generateRandom();
    final String counterName = method.getName();
    CounterConfiguration config = builder(CounterType.BOUNDED_STRONG).initialValue(5).upperBound(15).storage(random.nextBoolean() ? Storage.VOLATILE : Storage.PERSISTENT).build();
    doCreationTest(counterName, config);
}
Also used : Random(java.util.Random) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration)

Example 27 with CounterConfiguration

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

the class CounterResourceTest method testWeakCounterLifecycle.

@Test
public void testWeakCounterLifecycle() {
    CounterConfiguration counterConfig = CounterConfiguration.builder(CounterType.WEAK).initialValue(5).storage(Storage.VOLATILE).concurrencyLevel(6).build();
    createCounter("sample-counter", counterConfig);
    RestCounterClient counterClient = client.counter("sample-counter");
    RestResponse response = join(counterClient.configuration(APPLICATION_JSON_TYPE));
    Json jsonNode = Json.read(response.getBody());
    Json config = jsonNode.at("weak-counter");
    assertEquals(config.at("initial-value").asInteger(), 5);
    assertEquals(config.at("storage").asString(), "VOLATILE");
    assertEquals(config.at("concurrency-level").asInteger(), 6);
    response = join(counterClient.delete());
    assertThat(response).isOk();
    response = join(counterClient.configuration());
    assertThat(response).isNotFound();
}
Also used : RestCounterClient(org.infinispan.client.rest.RestCounterClient) RestResponse(org.infinispan.client.rest.RestResponse) Json(org.infinispan.commons.dataconversion.internal.Json) AbstractCounterConfiguration(org.infinispan.counter.configuration.AbstractCounterConfiguration) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration) Test(org.testng.annotations.Test)

Example 28 with CounterConfiguration

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

the class CounterRequestProcessor method applyCounter.

private void applyCounter(HotRodHeader header, String counterName, BiConsumer<HotRodHeader, StrongCounter> applyStrong, BiConsumer<HotRodHeader, WeakCounter> applyWeak) {
    EmbeddedCounterManager counterManager = counterManager(header);
    CounterConfiguration config = counterManager.getConfiguration(counterName);
    if (config == null) {
        writeResponse(header, missingCounterResponse(header));
        return;
    }
    switch(config.type()) {
        case UNBOUNDED_STRONG:
        case BOUNDED_STRONG:
            applyStrong.accept(header, counterManager.getStrongCounter(counterName));
            break;
        case WEAK:
            applyWeak.accept(header, counterManager.getWeakCounter(counterName));
            break;
    }
}
Also used : EmbeddedCounterManager(org.infinispan.counter.impl.manager.EmbeddedCounterManager) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration)

Example 29 with CounterConfiguration

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

the class ClientNotificationManager method createListener.

private Handle<Listener> createListener(byte version, String counterName, ByRef<Boolean> status) {
    CounterConfiguration configuration = counterManager.getConfiguration(counterName);
    if (configuration == null) {
        status.set(false);
        return null;
    }
    Handle<Listener> handle;
    if (configuration.type() == CounterType.WEAK) {
        handle = counterManager.getWeakCounter(counterName).addListener(new Listener(counterName, version));
    } else {
        handle = counterManager.getStrongCounter(counterName).addListener(new Listener(counterName, version));
    }
    status.set(true);
    return handle;
}
Also used : CounterListener(org.infinispan.counter.api.CounterListener) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration)

Example 30 with CounterConfiguration

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

the class CounterResource method createCounter.

private CompletionStage<RestResponse> createCounter(RestRequest request) throws RestResponseException {
    NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder();
    String counterName = request.variables().get("counterName");
    String contents = request.contents().asString();
    if (contents == null || contents.length() == 0) {
        responseBuilder.status(HttpResponseStatus.BAD_REQUEST);
        responseBuilder.entity("Configuration not provided");
        return completedFuture(responseBuilder.build());
    }
    CounterConfiguration configuration = createCounterConfiguration(contents);
    if (configuration == null) {
        responseBuilder.status(HttpResponseStatus.BAD_REQUEST).entity("Invalid configuration");
        return completedFuture(responseBuilder.build());
    }
    return invocationHelper.getCounterManager().defineCounterAsync(counterName, configuration).thenApply(r -> responseBuilder.build());
}
Also used : CounterManagerConfigurationBuilder(org.infinispan.counter.configuration.CounterManagerConfigurationBuilder) NettyRestResponse(org.infinispan.rest.NettyRestResponse) AbstractCounterConfiguration(org.infinispan.counter.configuration.AbstractCounterConfiguration) 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