Search in sources :

Example 1 with CounterManager

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

the class RemoveCounterTest method assertCounterRemove.

private void assertCounterRemove(String name, TestCounter counter, Factory factory) {
    CounterManager manager = counterManager(0);
    counter.remove();
    assertTrue(cache(0, CounterModuleLifecycle.COUNTER_CACHE_NAME).isEmpty());
    TestCounter anotherCounter = factory.get(manager, name);
    assertTrue(counter.isSame(anotherCounter));
}
Also used : TestCounter(org.infinispan.counter.util.TestCounter) WeakTestCounter(org.infinispan.counter.util.WeakTestCounter) StrongTestCounter(org.infinispan.counter.util.StrongTestCounter) CounterManager(org.infinispan.counter.api.CounterManager)

Example 2 with CounterManager

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

the class RemoveCounterTest method testCounterRemove.

private void testCounterRemove(Factory factory, String counterName) {
    CounterManager manager = counterManager(0);
    factory.define(manager, counterName);
    TestCounter counter = factory.get(manager, counterName);
    assertEquals(10, counter.getValue());
    assertTrue(counter.isSame(factory.get(manager, counterName)));
    assertCounterRemove(counterName, counter, factory);
    counter.increment();
    assertEquals(11, counter.getValue());
    assertCounterRemove(counterName, counter, factory);
    counter.decrement();
    assertEquals(9, counter.getValue());
    assertCounterRemove(counterName, counter, factory);
    counter.reset();
    assertEquals(10, counter.getValue());
}
Also used : TestCounter(org.infinispan.counter.util.TestCounter) WeakTestCounter(org.infinispan.counter.util.WeakTestCounter) StrongTestCounter(org.infinispan.counter.util.StrongTestCounter) CounterManager(org.infinispan.counter.api.CounterManager)

Example 3 with CounterManager

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

the class RemoveCounterTest method testCounterManagerRemoveNonExisting.

private void testCounterManagerRemoveNonExisting(Factory factory, String counterName) {
    // similar to testCounterManagerRemove but the remove() will be invoked in the CounterManager where the counter instance doesn't exist,
    CounterManager manager = counterManager(0);
    factory.define(manager, counterName);
    TestCounter counter = factory.get(manager, counterName);
    assertEquals(10, counter.getValue());
    assertTrue(counter.isSame(factory.get(manager, counterName)));
    counter = assertCounterManagerRemove(counterName, counter, factory, 1);
    counter.increment();
    assertEquals(11, counter.getValue());
    counter = assertCounterManagerRemove(counterName, counter, factory, 1);
    counter.decrement();
    assertEquals(9, counter.getValue());
    counter = assertCounterManagerRemove(counterName, counter, factory, 1);
    counter.reset();
    assertEquals(10, counter.getValue());
}
Also used : TestCounter(org.infinispan.counter.util.TestCounter) WeakTestCounter(org.infinispan.counter.util.WeakTestCounter) StrongTestCounter(org.infinispan.counter.util.StrongTestCounter) CounterManager(org.infinispan.counter.api.CounterManager)

Example 4 with CounterManager

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

the class RestartCounterTest method testRuntimeCounters.

public void testRuntimeCounters() {
    final CounterManager counterManager = counterManager(0);
    // while the retries during state transfer aren't fixed, we need to wait for the cache to start everywhere
    waitForClusterToForm(CounterModuleLifecycle.COUNTER_CACHE_NAME);
    incrementAll(defaultCounters, counterManager);
    incrementAll(defaultCounters, counterManager);
    otherCounters.forEach(counterDefinition -> counterDefinition.define(counterManager));
    incrementAll(otherCounters, counterManager);
    assertCounterValue(defaultCounters, counterManager, 2, 2);
    assertCounterValue(otherCounters, counterManager, 1, 1);
    shutdownAndRestart();
    // recreate the counter manager.
    final CounterManager counterManager2 = counterManager(0);
    Collection<CounterDefinition> othersPersisted = otherCounters.stream().filter(counterDefinition -> counterDefinition.storage == Storage.PERSISTENT).collect(Collectors.toList());
    Collection<CounterDefinition> otherVolatile = otherCounters.stream().filter(counterDefinition -> counterDefinition.storage == Storage.VOLATILE).collect(Collectors.toList());
    assertDefined(defaultCounters);
    assertDefined(othersPersisted);
    assertNotDefined(otherVolatile);
    assertCounterValue(defaultCounters, counterManager2, 0, 2);
    assertCounterValue(othersPersisted, counterManager2, -1, /*doesn't mather*/
    1);
    incrementAll(defaultCounters, counterManager2);
    incrementAll(othersPersisted, counterManager2);
    assertCounterValue(defaultCounters, counterManager2, 1, 3);
    assertCounterValue(othersPersisted, counterManager2, -1, /*doesn't mather*/
    2);
}
Also used : CleanupAfterMethod(org.infinispan.test.fwk.CleanupAfterMethod) Collection(java.util.Collection) Util(org.infinispan.commons.util.Util) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) AssertJUnit.assertFalse(org.testng.AssertJUnit.assertFalse) CounterManager(org.infinispan.counter.api.CounterManager) Test(org.testng.annotations.Test) Storage(org.infinispan.counter.api.Storage) CounterModuleLifecycle(org.infinispan.counter.impl.CounterModuleLifecycle) AfterMethod(org.testng.annotations.AfterMethod) AssertJUnit.assertTrue(org.testng.AssertJUnit.assertTrue) Collectors(java.util.stream.Collectors) CounterType(org.infinispan.counter.api.CounterType) CounterManagerConfigurationBuilder(org.infinispan.counter.configuration.CounterManagerConfigurationBuilder) ArrayList(java.util.ArrayList) CommonsTestingUtil.tmpDirectory(org.infinispan.commons.test.CommonsTestingUtil.tmpDirectory) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) Paths(java.nio.file.Paths) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration) BaseCounterTest(org.infinispan.counter.impl.BaseCounterTest) CounterManager(org.infinispan.counter.api.CounterManager)

Example 5 with CounterManager

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

the class StrongCounterTest method testCompareAndSwapConcurrent.

public void testCompareAndSwapConcurrent(Method method) throws ExecutionException, InterruptedException, TimeoutException {
    // local mode will have 8 concurrent thread, cluster mode will have 8 concurrent threads (4 nodes, 2 threads per node)
    final int numThreadsPerNode = clusterSize() == 1 ? 8 : 2;
    final int totalThreads = clusterSize() * numThreadsPerNode;
    final List<Future<Boolean>> workers = new ArrayList<>(totalThreads);
    final String counterName = method.getName();
    final CyclicBarrier barrier = new CyclicBarrier(totalThreads);
    final AtomicIntegerArray retValues = new AtomicIntegerArray(totalThreads);
    final long maxIterations = 100;
    for (int i = 0; i < totalThreads; ++i) {
        final int threadIndex = i;
        final int cmIndex = i % clusterSize();
        workers.add(fork(() -> {
            long iteration = 0;
            final long initialValue = 0;
            long previousValue = initialValue;
            CounterManager manager = counterManager(cmIndex);
            StrongTestCounter counter = createCounter(manager, counterName, initialValue);
            while (iteration < maxIterations) {
                assertEquals(previousValue, counter.getValue());
                long update = previousValue + 1;
                barrier.await();
                // all threads calling compareAndSet at the same time, only one should succeed
                long ret = counter.compareAndSwap(previousValue, update);
                boolean success = ret == previousValue;
                previousValue = success ? update : ret;
                retValues.set(threadIndex, success ? 1 : 0);
                barrier.await();
                assertUnique(retValues, iteration);
                ++iteration;
            }
            return true;
        }));
    }
    for (Future<?> w : workers) {
        w.get(1, TimeUnit.MINUTES);
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) ArrayList(java.util.ArrayList) StrongTestCounter(org.infinispan.counter.util.StrongTestCounter) Future(java.util.concurrent.Future) CounterManager(org.infinispan.counter.api.CounterManager) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Aggregations

CounterManager (org.infinispan.counter.api.CounterManager)35 StrongTestCounter (org.infinispan.counter.util.StrongTestCounter)9 CounterConfiguration (org.infinispan.counter.api.CounterConfiguration)8 ArrayList (java.util.ArrayList)5 TestCounter (org.infinispan.counter.util.TestCounter)5 WeakTestCounter (org.infinispan.counter.util.WeakTestCounter)5 StrongCounter (org.infinispan.counter.api.StrongCounter)4 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 Future (java.util.concurrent.Future)3 EmbeddedCounterManagerFactory.asCounterManager (org.infinispan.counter.EmbeddedCounterManagerFactory.asCounterManager)3 SyncStrongCounter (org.infinispan.counter.api.SyncStrongCounter)3 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)3 HashSet (java.util.HashSet)2 Random (java.util.Random)2 AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)2 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)2 CounterManagerConfigurationBuilder (org.infinispan.counter.configuration.CounterManagerConfigurationBuilder)2 DefaultCacheManager (org.infinispan.manager.DefaultCacheManager)2 Test (org.testng.annotations.Test)2 Paths (java.nio.file.Paths)1