Search in sources :

Example 6 with MagicKey

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

the class DistributedStreamRehashTest method testNodeFailureDuringProcessingForCollect.

public void testNodeFailureDuringProcessingForCollect() throws InterruptedException, TimeoutException, ExecutionException {
    // Insert a key into each cache - so we always have values to find on each node
    for (Cache<MagicKey, Object> cache : this.<MagicKey, Object>caches(CACHE_NAME)) {
        MagicKey key = new MagicKey(cache);
        cache.put(key, key.toString());
    }
    Cache<MagicKey, Object> originator = cache(0, CACHE_NAME);
    // We stop the #1 node which equates to entries store in segment 2
    Cache<MagicKey, Object> nodeToBlockBeforeProcessing = cache(1, CACHE_NAME);
    Cache<MagicKey, Object> nodeToStop = cache(3, CACHE_NAME);
    CheckPoint checkPoint = new CheckPoint();
    // Always let it process the publisher
    checkPoint.triggerForever(Mocks.BEFORE_RELEASE);
    // Block on the publisher
    InternalDataContainer internalDataContainer = TestingUtil.extractComponent(nodeToBlockBeforeProcessing, InternalDataContainer.class);
    InternalDataContainer spy = spy(internalDataContainer);
    doAnswer(invocation -> {
        Publisher result = (Publisher) invocation.callRealMethod();
        return Mocks.blockingPublisher(result, checkPoint);
    // Cache2 owns segment 1 primary and 2 as backup
    }).when(spy).publisher(eq(1));
    TestingUtil.replaceComponent(nodeToBlockBeforeProcessing, InternalDataContainer.class, spy, true);
    Future<List<Map.Entry<MagicKey, Object>>> future = fork(() -> originator.entrySet().stream().collect(() -> Collectors.toList()));
    // Make sure we are up to sync
    checkPoint.awaitStrict(Mocks.AFTER_INVOCATION, 10, TimeUnit.SECONDS);
    // Note that segment 2 doesn't map to the node1 anymore
    consistentHashFactory.setOwnerIndexes(new int[][] { { 0, 1 }, { 0, 2 }, { 2, 1 }, { 1, 0 } });
    // Have to remove the cache manager so the cluster formation can work properly
    cacheManagers.remove(cacheManagers.size() - 1);
    nodeToStop.getCacheManager().stop();
    TestingUtil.blockUntilViewsReceived((int) TimeUnit.SECONDS.toMillis(10), false, caches(CACHE_NAME));
    TestingUtil.waitForNoRebalance(caches(CACHE_NAME));
    // Now let the stream be processed
    checkPoint.triggerForever(Mocks.AFTER_RELEASE);
    List<Map.Entry<MagicKey, Object>> list = future.get(10, TimeUnit.SECONDS);
    assertEquals(cacheManagers.size() + 1, list.size());
}
Also used : List(java.util.List) Publisher(org.reactivestreams.Publisher) InternalDataContainer(org.infinispan.container.impl.InternalDataContainer) MagicKey(org.infinispan.distribution.MagicKey) Map(java.util.Map) CheckPoint(org.infinispan.test.fwk.CheckPoint)

Example 7 with MagicKey

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

the class BaseStreamIteratorWithLoaderTest method insertDefaultValues.

private Map<Object, String> insertDefaultValues(boolean includeLoaderEntry) {
    Cache<Object, String> cache0 = cache(0);
    Map<Object, String> originalValues = new HashMap<>();
    Object loaderKey;
    if (cacheMode.needsStateTransfer()) {
        Cache<Object, String> cache1 = cache(1);
        Cache<Object, String> cache2 = cache(2);
        originalValues.put(new MagicKey(cache0), "cache0");
        originalValues.put(new MagicKey(cache1), "cache1");
        originalValues.put(new MagicKey(cache2), "cache2");
        loaderKey = new MagicKey(cache2);
    } else {
        originalValues.put(1, "value0");
        originalValues.put(2, "value1");
        originalValues.put(3, "value2");
        loaderKey = 4;
    }
    cache0.putAll(originalValues);
    DummyInMemoryStore store = TestingUtil.getFirstStore(cache0);
    TestObjectStreamMarshaller sm = new TestObjectStreamMarshaller(sci);
    try {
        String loaderValue = "loader-value";
        store.write(MarshalledEntryUtil.create(loaderKey, loaderValue, sm));
        if (includeLoaderEntry) {
            originalValues.put(loaderKey, loaderValue);
        }
    } finally {
        sm.stop();
    }
    return originalValues;
}
Also used : DummyInMemoryStore(org.infinispan.persistence.dummy.DummyInMemoryStore) HashMap(java.util.HashMap) TestObjectStreamMarshaller(org.infinispan.marshall.TestObjectStreamMarshaller) MagicKey(org.infinispan.distribution.MagicKey)

Example 8 with MagicKey

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

the class DistributedWriteBehindStreamIteratorTest method testBackupSegmentsOptimizationWithWriteBehindStore.

@Test(dataProvider = "rehashAware")
public void testBackupSegmentsOptimizationWithWriteBehindStore(boolean rehashAware) {
    Cache<Object, String> cache1 = cache(1, CACHE_NAME);
    RpcManager rpcManager = Mocks.replaceComponentWithSpy(cache1, RpcManager.class);
    for (Cache<Object, String> cache : this.<Object, String>caches(CACHE_NAME)) {
        MagicKey key = new MagicKey(cache);
        cache.put(key, key.toString());
    }
    // Remember that segment ownership is as {0, 1}, {1, 2}, {2, 1}
    CacheStream<Map.Entry<Object, String>> stream = cache1.entrySet().stream();
    if (!rehashAware)
        stream = stream.disableRehashAware();
    int invocationCount;
    if (cacheMode.isReplicated()) {
        Map<Object, String> entries = mapFromIterator(stream.iterator());
        assertEquals(caches(CACHE_NAME).size(), entries.size());
        invocationCount = cacheManagers.size() - 1;
    } else {
        // Distributed cache
        // Cache1 owns 1 (primary) and 2 (backup)
        // When it is a write behind shared store it will have to go remote otherwise will stay local
        Map<Object, String> entries = mapFromIterator(stream.filterKeySegments(IntSets.immutableSet(2)).iterator());
        assertEquals(1, entries.size());
        invocationCount = 1;
    }
    // on the primary owner, so we could miss updates
    if (asyncStore && sharedStore) {
        verify(rpcManager, times(invocationCount)).invokeCommand(any(Address.class), any(InitialPublisherCommand.class), any(), any());
    } else {
        verify(rpcManager, never()).invokeCommand(any(Address.class), any(InitialPublisherCommand.class), any(), any());
    }
}
Also used : RpcManager(org.infinispan.remoting.rpc.RpcManager) Address(org.infinispan.remoting.transport.Address) MagicKey(org.infinispan.distribution.MagicKey) InitialPublisherCommand(org.infinispan.reactive.publisher.impl.commands.batch.InitialPublisherCommand) Test(org.testng.annotations.Test)

Example 9 with MagicKey

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

the class TxDuringStateTransferTest method performTest.

private void performTest(Operation operation) throws Exception {
    assertClusterSize("Wrong number of caches.", 4);
    final Object key = new MagicKey(cache(0), cache(1), cache(2));
    // init
    operation.init(cache(0), key);
    final EmbeddedTransactionManager transactionManager = (EmbeddedTransactionManager) tm(0);
    transactionManager.begin();
    operation.perform(cache(0), key);
    final EmbeddedTransaction transaction = transactionManager.getTransaction();
    transaction.runPrepare();
    assertEquals("Wrong transaction status before killing backup owner.", Status.STATUS_PREPARED, transaction.getStatus());
    // now, we kill cache(1). the transaction is prepared in cache(1) and it should be forward to cache(3)
    killMember(1);
    assertEquals("Wrong transaction status after killing backup owner.", Status.STATUS_PREPARED, transaction.getStatus());
    transaction.runCommit(false);
    for (Cache<Object, Object> cache : caches()) {
        // all the caches are owner
        operation.check(cache, key, valueOf(address(cache)));
    }
}
Also used : EmbeddedTransaction(org.infinispan.transaction.tm.EmbeddedTransaction) EmbeddedTransactionManager(org.infinispan.transaction.tm.EmbeddedTransactionManager) MagicKey(org.infinispan.distribution.MagicKey)

Example 10 with MagicKey

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

the class TxReplayTest method testReplay.

public void testReplay() throws Exception {
    assertClusterSize("Wrong cluster size", 3);
    final Object key = new MagicKey(cache(0), cache(1));
    final Cache<Object, Object> newBackupOwnerCache = cache(2);
    final TxCommandInterceptor interceptor = TxCommandInterceptor.inject(newBackupOwnerCache);
    EmbeddedTransactionManager transactionManager = (EmbeddedTransactionManager) tm(0);
    transactionManager.begin();
    cache(0).put(key, VALUE);
    final EmbeddedTransaction transaction = transactionManager.getTransaction();
    transaction.runPrepare();
    assertEquals("Wrong transaction status before killing backup owner.", Status.STATUS_PREPARED, transaction.getStatus());
    // now, we kill cache(1). the transaction is prepared in cache(1) and it should be forward to cache(2)
    killMember(1);
    checkIfTransactionExists(newBackupOwnerCache);
    assertEquals("Wrong transaction status after killing backup owner.", Status.STATUS_PREPARED, transaction.getStatus());
    transaction.runCommit(false);
    assertNoTransactions();
    assertEquals("Wrong number of prepares!", 1, interceptor.numberPrepares.get());
    assertEquals("Wrong number of commits!", 1, interceptor.numberCommits.get());
    assertEquals("Wrong number of rollbacks!", 0, interceptor.numberRollbacks.get());
    checkKeyInDataContainer(key);
}
Also used : EmbeddedTransaction(org.infinispan.transaction.tm.EmbeddedTransaction) EmbeddedTransactionManager(org.infinispan.transaction.tm.EmbeddedTransactionManager) 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