Search in sources :

Example 86 with MagicKey

use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.

the class StreamDistPartitionHandlingTest method testUsingIteratorButPartitionOccursBeforeRetrievingRemoteValues.

public void testUsingIteratorButPartitionOccursBeforeRetrievingRemoteValues() throws Exception {
    Cache<MagicKey, String> cache0 = cache(0);
    // Make sure we have 1 entry in each - since onEach will then be invoked once on each node
    cache0.put(new MagicKey(cache(1), cache(2)), "not-local");
    cache0.put(new MagicKey(cache(0), cache(1)), "local");
    CheckPoint iteratorCP = new CheckPoint();
    // We let the completeable future be returned - but don't let it process the values yet
    iteratorCP.triggerForever(Mocks.BEFORE_RELEASE);
    // This must be before the stream is generated or else it won't see the update
    registerBlockingRpcManagerOnInitialPublisherCommand(iteratorCP, cache0, testExecutor());
    try (CloseableIterator<?> iterator = Closeables.iterator(cache0.entrySet().stream())) {
        CheckPoint partitionCP = new CheckPoint();
        // Now we replace the notifier so we know when the notifier was told of the partition change so we know
        // our iterator should have been notified
        registerBlockingCacheNotifierOnDegradedMode(partitionCP, cache0);
        // We don't want to block the notifier
        partitionCP.triggerForever(Mocks.BEFORE_RELEASE);
        partitionCP.triggerForever(Mocks.AFTER_RELEASE);
        // Now split the cluster
        splitCluster(new int[] { 0, 1 }, new int[] { 2, 3 });
        // Wait until we have been notified before letting remote responses to arrive
        partitionCP.awaitStrict(Mocks.AFTER_INVOCATION, 10, TimeUnit.SECONDS);
        // Afterwards let all the responses come in
        iteratorCP.triggerForever(Mocks.AFTER_RELEASE);
        try {
            while (iterator.hasNext()) {
                iterator.next();
            }
            fail("Expected AvailabilityException");
        } catch (AvailabilityException e) {
        // Should go here
        }
    }
}
Also used : MagicKey(org.infinispan.distribution.MagicKey) CheckPoint(org.infinispan.test.fwk.CheckPoint)

Example 87 with MagicKey

use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.

the class StreamReplPartitionHandlingTest method testRetrievalWhenPartitionIsDegradedButLocal.

@Override
public void testRetrievalWhenPartitionIsDegradedButLocal() {
    Cache<MagicKey, String> cache0 = cache(0);
    cache0.put(new MagicKey(cache(1), cache(2)), "not-local");
    cache0.put(new MagicKey(cache(0), cache(1)), "local");
    splitCluster(new int[] { 0, 1 }, new int[] { 2, 3 });
    partition(0).assertDegradedMode();
    try (CloseableIterator<Map.Entry<MagicKey, String>> iterator = Closeables.iterator(cache0.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).entrySet().stream())) {
        assertNotNull(iterator.next());
        assertNotNull(iterator.next());
        assertFalse(iterator.hasNext());
    }
}
Also used : MagicKey(org.infinispan.distribution.MagicKey)

Example 88 with MagicKey

use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.

the class ClusterListenerReplTest method testPrimaryOwnerGoesDownBeforeBackupRaisesEvent.

public void testPrimaryOwnerGoesDownBeforeBackupRaisesEvent() throws InterruptedException, TimeoutException, ExecutionException {
    final Cache<Object, String> cache0 = cache(0, CACHE_NAME);
    Cache<Object, String> cache1 = cache(1, CACHE_NAME);
    Cache<Object, String> cache2 = cache(2, CACHE_NAME);
    ClusterListener clusterListener = new ClusterListener();
    cache0.addListener(clusterListener);
    // Now we want to block the outgoing put to the backup owner
    ControlledRpcManager controlledRpcManager = ControlledRpcManager.replaceRpcManager(cache1);
    final MagicKey key = new MagicKey(cache1, cache2);
    Future<String> future = fork(() -> cache0.put(key, FIRST_VALUE));
    // Wait until the primary owner has sent the put command successfully to the backups
    ControlledRpcManager.BlockedRequest<?> blockedPut = controlledRpcManager.expectCommand(PutKeyValueCommand.class);
    // And discard the request
    blockedPut.skipSend();
    // Kill the cache now
    TestingUtil.killCacheManagers(cache1.getCacheManager());
    // This should return null normally, but when it was retried it returns its own value
    String returnValue = future.get(10, TimeUnit.SECONDS);
    assertTrue(returnValue == null || returnValue.equals(FIRST_VALUE));
    // We should have received an event that was marked as retried
    assertTrue(clusterListener.events.size() >= 1);
    // Because a rebalance has 4 phases, the command may be retried 4 times
    assertTrue(clusterListener.events.size() <= 4);
    for (CacheEntryEvent<Object, String> event : clusterListener.events) {
        checkEvent(event, key, true, true);
    }
}
Also used : ControlledRpcManager(org.infinispan.util.ControlledRpcManager) MagicKey(org.infinispan.distribution.MagicKey)

Example 89 with MagicKey

use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.

the class ClusterListenerReplTest method testPrimaryOwnerGoesDownAfterBackupRaisesEvent.

public void testPrimaryOwnerGoesDownAfterBackupRaisesEvent() throws InterruptedException, TimeoutException, ExecutionException, BrokenBarrierException {
    final Cache<Object, String> cache0 = cache(0, CACHE_NAME);
    Cache<Object, String> cache1 = cache(1, CACHE_NAME);
    Cache<Object, String> cache2 = cache(2, CACHE_NAME);
    ClusterListener clusterListener = new ClusterListener();
    cache0.addListener(clusterListener);
    CyclicBarrier barrier = new CyclicBarrier(3);
    BlockingInterceptor<?> blockingInterceptor0 = new BlockingInterceptor<>(barrier, PutKeyValueCommand.class, true, false);
    extractInterceptorChain(cache0).addInterceptorBefore(blockingInterceptor0, EntryWrappingInterceptor.class);
    BlockingInterceptor<?> blockingInterceptor2 = new BlockingInterceptor<>(barrier, PutKeyValueCommand.class, true, false);
    extractInterceptorChain(cache2).addInterceptorBefore(blockingInterceptor2, EntryWrappingInterceptor.class);
    // this is a replicated cache, all other nodes are backup owners
    final MagicKey key = new MagicKey(cache1);
    Future<String> future = fork(() -> cache0.put(key, FIRST_VALUE));
    // Wait until the primary owner has sent the put command successfully to both backups
    barrier.await(10, TimeUnit.SECONDS);
    // Remove the interceptor so the next command can proceed properly
    extractInterceptorChain(cache0).removeInterceptor(BlockingInterceptor.class);
    extractInterceptorChain(cache2).removeInterceptor(BlockingInterceptor.class);
    blockingInterceptor0.suspend(true);
    blockingInterceptor2.suspend(true);
    // Kill the cache now - note this will automatically unblock the fork thread
    TestingUtil.killCacheManagers(cache1.getCacheManager());
    // Unblock the command
    barrier.await(10, TimeUnit.SECONDS);
    // This should return null normally, but since it was retried it returns it's own value :(
    // Maybe some day this can work properly
    String returnValue = future.get(10, TimeUnit.SECONDS);
    assertEquals(FIRST_VALUE, returnValue);
    // We should have received an event that was marked as retried
    assertTrue(clusterListener.events.size() >= 2);
    // Because a rebalance has 4 phases, the command may be retried 4 times
    assertTrue(clusterListener.events.size() <= 4);
    // First create should not be retried since it was sent before node failure.
    checkEvent(clusterListener.events.get(0), key, true, false);
    // Events from retried commands are a MODIFY since CREATE was already done
    for (int i = 1; i < clusterListener.events.size(); i++) {
        CacheEntryEvent<Object, String> event = clusterListener.events.get(i);
        checkEvent(event, key, false, true);
    }
    checkEvent(clusterListener.events.get(1), key, false, true);
}
Also used : BlockingInterceptor(org.infinispan.distribution.BlockingInterceptor) MagicKey(org.infinispan.distribution.MagicKey) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 90 with MagicKey

use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.

the class ClusteredListenerWithDslFilterTest method testEventFilterAndConverter.

public void testEventFilterAndConverter() {
    QueryFactory qf = Search.getQueryFactory(cache(0));
    Query<Object[]> query = qf.create("SELECT name, age FROM " + Person.class.getName() + " WHERE age <= 31");
    EntryListener listener = new EntryListener();
    for (int i = 0; i < 5; ++i) {
        Person value = new Person();
        value.setName("John");
        value.setAge(i + 30);
        Cache<Object, Person> cache = cache(i % NUM_NODES);
        Object key = new MagicKey(cache);
        cache.put(key, value);
    }
    // we want our cluster listener to be notified only if the entity matches our query
    cache(0).addListener(listener, Search.makeFilter(query), null);
    for (int i = 0; i < 10; ++i) {
        Person value = new Person();
        value.setName("John");
        value.setAge(i + 25);
        Cache<Object, Person> cache = cache(i % NUM_NODES);
        Object key = new MagicKey(cache);
        cache.put(key, value);
    }
    assertEquals(9, listener.results.size());
    for (ObjectFilter.FilterResult r : listener.results) {
        assertTrue((Integer) r.getProjection()[1] <= 31);
    }
    cache(0).removeListener(listener);
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) ObjectFilter(org.infinispan.objectfilter.ObjectFilter) Person(org.infinispan.query.test.Person) MagicKey(org.infinispan.distribution.MagicKey)

Aggregations

MagicKey (org.infinispan.distribution.MagicKey)185 Test (org.testng.annotations.Test)74 TransactionManager (javax.transaction.TransactionManager)23 CheckPoint (org.infinispan.test.fwk.CheckPoint)20 HashMap (java.util.HashMap)16 Map (java.util.Map)16 Transaction (javax.transaction.Transaction)16 Cache (org.infinispan.Cache)16 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)15 CountDownLatch (java.util.concurrent.CountDownLatch)10 CacheMode (org.infinispan.configuration.cache.CacheMode)10 CacheEntry (org.infinispan.container.entries.CacheEntry)10 Address (org.infinispan.remoting.transport.Address)10 StateSequencer (org.infinispan.test.concurrent.StateSequencer)9 AssertJUnit.assertEquals (org.testng.AssertJUnit.assertEquals)9 ArrayList (java.util.ArrayList)8 CyclicBarrier (java.util.concurrent.CyclicBarrier)8 DistributionManager (org.infinispan.distribution.DistributionManager)8 MultipleCacheManagersTest (org.infinispan.test.MultipleCacheManagersTest)8 RollbackException (javax.transaction.RollbackException)7