Search in sources :

Example 1 with CheckPoint

use of org.infinispan.test.fwk.CheckPoint in project infinispan by infinispan.

the class LockingTest method injectBlockingCommandInterceptor.

private CheckPoint injectBlockingCommandInterceptor(String cacheName) {
    final CheckPoint checkPoint = new CheckPoint();
    TestingUtil.extractInterceptorChain(cache(cacheName)).addInterceptorBefore(new BaseCustomAsyncInterceptor() {

        private final AtomicBoolean first = new AtomicBoolean(false);

        @Override
        public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) {
            if (first.compareAndSet(false, true)) {
                checkPoint.trigger("before-block");
                return asyncInvokeNext(ctx, command, checkPoint.future("block", 30, TimeUnit.SECONDS, testExecutor()));
            }
            return invokeNext(ctx, command);
        }
    }, CallInterceptor.class);
    return checkPoint;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InvocationContext(org.infinispan.context.InvocationContext) CheckPoint(org.infinispan.test.fwk.CheckPoint) BaseCustomAsyncInterceptor(org.infinispan.interceptors.BaseCustomAsyncInterceptor) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand)

Example 2 with CheckPoint

use of org.infinispan.test.fwk.CheckPoint in project infinispan by infinispan.

the class TxInvalidationLockingTest method testOptimisticPrepareAcquiresGlobalLock.

public void testOptimisticPrepareAcquiresGlobalLock() throws Exception {
    CheckPoint checkPoint = new CheckPoint();
    Future<Void> tx2Future;
    Cache<Object, Object> cache1 = cache(0, OPTIMISTIC_CACHE);
    tm(cache1).begin();
    EmbeddedTransaction tx1 = null;
    try {
        Object initialValue = cache1.put(KEY, VALUE1);
        assertNull(initialValue);
        tx1 = (EmbeddedTransaction) tm(cache1).getTransaction();
        tx1.runPrepare();
        tx2Future = fork(() -> {
            AdvancedCache<Object, Object> cache2 = advancedCache(1, OPTIMISTIC_CACHE);
            tm(cache2).begin();
            try {
                assertNull(cache2.get(KEY));
                checkPoint.trigger("tx2_read");
                cache2.put(KEY, VALUE2);
            } finally {
                tm(cache2).commit();
            }
        });
        checkPoint.awaitStrict("tx2_read", 10, TimeUnit.SECONDS);
        Thread.sleep(10);
        assertFalse(tx2Future.isDone());
    } finally {
        if (tx1 != null) {
            tx1.runCommit(false);
        }
    }
    // No WriteSkewException
    tx2Future.get(30, TimeUnit.SECONDS);
    assertEquals(VALUE2, cache1.get(KEY));
}
Also used : EmbeddedTransaction(org.infinispan.transaction.tm.EmbeddedTransaction) AdvancedCache(org.infinispan.AdvancedCache) CheckPoint(org.infinispan.test.fwk.CheckPoint)

Example 3 with CheckPoint

use of org.infinispan.test.fwk.CheckPoint in project infinispan by infinispan.

the class DistributedStreamIteratorTest method verifyNodeLeavesAfterSendingBackSomeData.

/**
 * This test is to verify proper behavior when a node dies after sending a batch to the requestor
 */
@Test
public void verifyNodeLeavesAfterSendingBackSomeData() throws TimeoutException, InterruptedException, ExecutionException {
    Cache<Object, String> cache0 = cache(0, CACHE_NAME);
    Cache<Object, String> cache1 = cache(1, CACHE_NAME);
    Map<Object, String> values = new HashMap<>();
    int chunkSize = cache0.getCacheConfiguration().clustering().stateTransfer().chunkSize();
    // Now insert 2 more values than the chunk size into the node we will kill
    for (int i = 0; i < chunkSize + 2; ++i) {
        MagicKey key = new MagicKey(cache1);
        cache1.put(key, key.toString());
        values.put(key, key.toString());
    }
    CheckPoint checkPoint = new CheckPoint();
    // Let the first request come through fine
    checkPoint.trigger(Mocks.BEFORE_RELEASE);
    waitUntilSendingResponse(cache1, checkPoint);
    final BlockingQueue<Map.Entry<Object, String>> returnQueue = new LinkedBlockingQueue<>();
    Future<Void> future = fork(() -> {
        Iterator<Map.Entry<Object, String>> iter = cache0.entrySet().stream().iterator();
        while (iter.hasNext()) {
            Map.Entry<Object, String> entry = iter.next();
            returnQueue.add(entry);
        }
        return null;
    });
    // Now wait for them to send back first results
    checkPoint.awaitStrict(Mocks.AFTER_INVOCATION, 10, TimeUnit.SECONDS);
    checkPoint.trigger(Mocks.AFTER_RELEASE);
    // We should get a value now, note all values are currently residing on cache1 as primary
    Map.Entry<Object, String> value = returnQueue.poll(10, TimeUnit.SECONDS);
    // Now kill the cache - we should recover
    killMember(1, CACHE_NAME);
    future.get(10, TimeUnit.SECONDS);
    for (Map.Entry<Object, String> entry : values.entrySet()) {
        assertTrue("Entry wasn't found:" + entry, returnQueue.contains(entry) || entry.equals(value));
    }
}
Also used : HashMap(java.util.HashMap) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CheckPoint(org.infinispan.test.fwk.CheckPoint) MagicKey(org.infinispan.distribution.MagicKey) Map(java.util.Map) HashMap(java.util.HashMap) CheckPoint(org.infinispan.test.fwk.CheckPoint) Test(org.testng.annotations.Test)

Example 4 with CheckPoint

use of org.infinispan.test.fwk.CheckPoint in project infinispan by infinispan.

the class DistributedStreamIteratorTest method testIterationDuringInitialTransfer.

@Test
public void testIterationDuringInitialTransfer() throws Exception {
    Map<Object, String> values = putValueInEachCache(3);
    // Go back to 2 caches, because we assign all 3 segments to the first 3 nodes
    // And we need the joiner to request some state in order to block it
    killMember(2, CACHE_NAME);
    Cache<Object, String> cache0 = cache(0, CACHE_NAME);
    CheckPoint checkPoint = new CheckPoint();
    checkPoint.triggerForever(Mocks.AFTER_RELEASE);
    blockStateTransfer(cache0, checkPoint);
    EmbeddedCacheManager joinerManager = addClusterEnabledCacheManager(sci, new ConfigurationBuilder(), new TransportFlags().withFD(true));
    ConfigurationBuilder builderNoAwaitInitialTransfer = new ConfigurationBuilder();
    builderNoAwaitInitialTransfer.read(builderUsed.build());
    builderNoAwaitInitialTransfer.clustering().stateTransfer().awaitInitialTransfer(false);
    joinerManager.defineConfiguration(CACHE_NAME, builderNoAwaitInitialTransfer.build());
    Cache<String, String> joinerCache = joinerManager.getCache(CACHE_NAME, true);
    // Not required, but it should make the logs clearer
    checkPoint.awaitStrict(Mocks.BEFORE_INVOCATION, 10, TimeUnit.SECONDS);
    Set<String> iteratorValues = new HashSet<>();
    try {
        Iterator<String> iter = joinerCache.entrySet().stream().map(Map.Entry::getValue).iterator();
        while (iter.hasNext()) {
            String value = iter.next();
            iteratorValues.add(value);
        }
    } finally {
        checkPoint.triggerForever(Mocks.BEFORE_RELEASE);
    }
    for (Map.Entry<Object, String> entry : values.entrySet()) {
        assertTrue("Entry wasn't found:" + entry, iteratorValues.contains(entry.getValue()));
    }
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) TransportFlags(org.infinispan.test.fwk.TransportFlags) Map(java.util.Map) HashMap(java.util.HashMap) CheckPoint(org.infinispan.test.fwk.CheckPoint) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 5 with CheckPoint

use of org.infinispan.test.fwk.CheckPoint in project infinispan by infinispan.

the class DistributedStreamIteratorTest method waitUntilProcessingResults.

@Test
public void waitUntilProcessingResults() throws TimeoutException, InterruptedException, ExecutionException {
    Cache<Object, String> cache0 = cache(0, CACHE_NAME);
    Cache<Object, String> cache1 = cache(1, CACHE_NAME);
    Map<Object, String> values = new HashMap<>();
    for (int i = 0; i < 9; ++i) {
        MagicKey key = new MagicKey(cache1);
        cache1.put(key, key.toString());
        values.put(key, key.toString());
    }
    CheckPoint checkPoint = new CheckPoint();
    checkPoint.triggerForever(Mocks.AFTER_RELEASE);
    ClusterPublisherManager<Object, String> spy = Mocks.replaceComponentWithSpy(cache0, ClusterPublisherManager.class);
    doAnswer(invocation -> {
        SegmentPublisherSupplier<?> result = (SegmentPublisherSupplier<?>) invocation.callRealMethod();
        return Mocks.blockingPublisher(result, checkPoint);
    }).when(spy).entryPublisher(any(), any(), any(), anyLong(), any(), anyInt(), any());
    final BlockingQueue<Map.Entry<Object, String>> returnQueue = new LinkedBlockingQueue<>();
    Future<Void> future = fork(() -> {
        Iterator<Map.Entry<Object, String>> iter = cache0.entrySet().stream().iterator();
        while (iter.hasNext()) {
            Map.Entry<Object, String> entry = iter.next();
            returnQueue.add(entry);
        }
        return null;
    });
    // Now wait for them to send back first results but don't let them process
    checkPoint.awaitStrict(Mocks.BEFORE_INVOCATION, 10, TimeUnit.SECONDS);
    // Now let them process the results
    checkPoint.triggerForever(Mocks.BEFORE_RELEASE);
    // Now kill the cache - we should recover and get appropriate values
    killMember(1, CACHE_NAME);
    future.get(10, TimeUnit.SECONDS);
    KeyPartitioner keyPartitioner = TestingUtil.extractComponent(cache0, KeyPartitioner.class);
    Map<Integer, Set<Map.Entry<Object, String>>> expected = generateEntriesPerSegment(keyPartitioner, values.entrySet());
    Map<Integer, Set<Map.Entry<Object, String>>> answer = generateEntriesPerSegment(keyPartitioner, returnQueue);
    for (Map.Entry<Integer, Set<Map.Entry<Object, String>>> entry : expected.entrySet()) {
        Integer segment = entry.getKey();
        Set<Map.Entry<Object, String>> answerForSegment = answer.get(segment);
        if (answerForSegment != null) {
            for (Map.Entry<Object, String> exp : entry.getValue()) {
                if (!answerForSegment.contains(exp)) {
                    log.errorf("Segment %d, missing %s", segment, exp);
                }
            }
            for (Map.Entry<Object, String> ans : answerForSegment) {
                if (!entry.getValue().contains(ans)) {
                    log.errorf("Segment %d, extra %s", segment, ans);
                }
            }
            assertEquals(entry.getValue().size(), answerForSegment.size());
        }
        assertEquals("Segment " + segment + " had a mismatch", entry.getValue(), answerForSegment);
    }
}
Also used : SegmentPublisherSupplier(org.infinispan.reactive.publisher.impl.SegmentPublisherSupplier) Set(java.util.Set) IntSet(org.infinispan.commons.util.IntSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) CheckPoint(org.infinispan.test.fwk.CheckPoint) CheckPoint(org.infinispan.test.fwk.CheckPoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MagicKey(org.infinispan.distribution.MagicKey) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Aggregations

CheckPoint (org.infinispan.test.fwk.CheckPoint)64 Test (org.testng.annotations.Test)32 MagicKey (org.infinispan.distribution.MagicKey)21 HashMap (java.util.HashMap)18 Map (java.util.Map)17 Address (org.infinispan.remoting.transport.Address)15 Cache (org.infinispan.Cache)11 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)11 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)10 CacheEntry (org.infinispan.container.entries.CacheEntry)9 PersistenceManager (org.infinispan.persistence.manager.PersistenceManager)9 ImmortalCacheEntry (org.infinispan.container.entries.ImmortalCacheEntry)8 TimeUnit (java.util.concurrent.TimeUnit)6 CacheMode (org.infinispan.configuration.cache.CacheMode)6 TestingUtil (org.infinispan.test.TestingUtil)6 HashSet (java.util.HashSet)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 Future (java.util.concurrent.Future)5 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)5 TimeoutException (java.util.concurrent.TimeoutException)5