Search in sources :

Example 31 with MagicKey

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

the class ConditionalOperationPrimaryOwnerFailTest method testEntryNotWrapped.

public void testEntryNotWrapped() throws Throwable {
    assertClusterSize("Wrong cluster size!", 3);
    final Object key = new MagicKey(cache(0), cache(1));
    final Cache<Object, Object> futureBackupOwnerCache = cache(2);
    cache(0).put(key, INITIAL_VALUE);
    final PerCacheInboundInvocationHandler spyHandler = spyInvocationHandler(futureBackupOwnerCache);
    final EntryFactory spyEntryFactory = spyEntryFactory(futureBackupOwnerCache);
    // it blocks the StateResponseCommand.class
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    doAnswer(invocation -> {
        CacheRpcCommand command = (CacheRpcCommand) invocation.getArguments()[0];
        if (command instanceof StateResponseCommand) {
            log.debugf("Blocking command %s", command);
            latch2.countDown();
            latch1.await();
        }
        return invocation.callRealMethod();
    }).when(spyHandler).handle(any(CacheRpcCommand.class), any(Reply.class), any(DeliverOrder.class));
    doAnswer(invocation -> {
        InvocationContext context = (InvocationContext) invocation.getArguments()[0];
        log.debugf("wrapEntryForWriting invoked with %s", context);
        CompletionStage<Void> stage = (CompletionStage<Void>) invocation.callRealMethod();
        CompletionStages.join(stage);
        assertNull(context.lookupEntry(key), "Entry should not be wrapped!");
        return stage;
    }).when(spyEntryFactory).wrapEntryForWriting(any(InvocationContext.class), any(), anyInt(), anyBoolean(), anyBoolean(), any());
    Future<?> killMemberResult = fork(() -> killMember(1));
    // await until the key is received from state transfer (the command is blocked now...)
    latch2.await(30, TimeUnit.SECONDS);
    futureBackupOwnerCache.put(key, FINAL_VALUE);
    latch1.countDown();
    killMemberResult.get(30, TimeUnit.SECONDS);
}
Also used : EntryFactory(org.infinispan.container.impl.EntryFactory) PerCacheInboundInvocationHandler(org.infinispan.remoting.inboundhandler.PerCacheInboundInvocationHandler) DeliverOrder(org.infinispan.remoting.inboundhandler.DeliverOrder) CountDownLatch(java.util.concurrent.CountDownLatch) CacheRpcCommand(org.infinispan.commands.remote.CacheRpcCommand) StateResponseCommand(org.infinispan.commands.statetransfer.StateResponseCommand) Reply(org.infinispan.remoting.inboundhandler.Reply) InvocationContext(org.infinispan.context.InvocationContext) MagicKey(org.infinispan.distribution.MagicKey) CompletionStage(java.util.concurrent.CompletionStage)

Example 32 with MagicKey

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

the class AbstractRepeatableReadIsolationTest method doIsolationTest.

private void doIsolationTest(boolean executeOnOwner, boolean initialized, Operation operation) throws Exception {
    final Cache<Object, Object> ownerCache = cache(0);
    final Object key = new MagicKey("shared", ownerCache);
    final Cache<Object, Object> cache = executeOnOwner ? cache(0) : cache(1);
    final TransactionManager tm = executeOnOwner ? tm(0) : tm(1);
    assertValueInAllCaches(key, null);
    final Object initValue = initialized ? INITIAL_VALUE : null;
    if (initialized) {
        ownerCache.put(key, initValue);
        assertValueInAllCaches(key, initValue);
    }
    tm.begin();
    assertEquals("Wrong first get.", initValue, cache.get(key));
    Transaction tx = tm.suspend();
    ownerCache.put(key, OTHER_VALUE);
    assertValueInAllCaches(key, OTHER_VALUE);
    Object finalValueExpected = null;
    boolean commitFails = false;
    tm.resume(tx);
    assertEquals("Wrong second get.", initValue, cache.get(key));
    switch(operation) {
        case PUT:
            finalValueExpected = FINAL_VALUE;
            commitFails = lockingMode == LockingMode.OPTIMISTIC;
            assertEquals("Wrong put return value.", initValue, cache.put(key, FINAL_VALUE));
            assertEquals("Wrong final get.", FINAL_VALUE, cache.get(key));
            break;
        case REMOVE:
            finalValueExpected = null;
            commitFails = lockingMode == LockingMode.OPTIMISTIC;
            assertEquals("Wrong remove return value.", initValue, cache.remove(key));
            assertEquals("Wrong final get.", null, cache.get(key));
            break;
        case REPLACE:
            finalValueExpected = initialized ? FINAL_VALUE : OTHER_VALUE;
            commitFails = lockingMode == LockingMode.OPTIMISTIC && initialized;
            assertEquals("Wrong replace return value.", initValue, cache.replace(key, FINAL_VALUE));
            assertEquals("Wrong final get.", initialized ? FINAL_VALUE : null, cache.get(key));
            break;
        case CONDITIONAL_PUT:
            finalValueExpected = initialized ? OTHER_VALUE : FINAL_VALUE;
            commitFails = lockingMode == LockingMode.OPTIMISTIC && !initialized;
            assertEquals("Wrong put return value.", initialized ? initValue : null, cache.putIfAbsent(key, FINAL_VALUE));
            assertEquals("Wrong final get.", initialized ? initValue : FINAL_VALUE, cache.get(key));
            break;
        case CONDITIONAL_REMOVE:
            finalValueExpected = initialized ? null : OTHER_VALUE;
            commitFails = lockingMode == LockingMode.OPTIMISTIC && initialized;
            assertEquals("Wrong remove return value.", initialized, cache.remove(key, INITIAL_VALUE));
            assertEquals("Wrong final get.", null, cache.get(key));
            break;
        case CONDITIONAL_REPLACE:
            finalValueExpected = initialized ? FINAL_VALUE : OTHER_VALUE;
            commitFails = lockingMode == LockingMode.OPTIMISTIC && initialized;
            assertEquals("Wrong replace return value.", initialized, cache.replace(key, INITIAL_VALUE, FINAL_VALUE));
            assertEquals("Wrong final get.", initialized ? FINAL_VALUE : null, cache.get(key));
            break;
        default:
            fail("Unknown operation " + operation);
            break;
    }
    if (commitFails) {
        Exceptions.expectException(RollbackException.class, tm::commit);
    } else {
        tm.commit();
    }
    assertValueInAllCaches(key, lockingMode == LockingMode.PESSIMISTIC ? finalValueExpected : OTHER_VALUE);
    assertNoTransactions();
}
Also used : Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) MagicKey(org.infinispan.distribution.MagicKey)

Example 33 with MagicKey

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

the class AbstractRepeatableReadIsolationTest method doIsolationTestAfterRemove.

private void doIsolationTestAfterRemove(boolean executeOnOwner, Operation operation) throws Exception {
    final Cache<Object, Object> ownerCache = cache(0);
    final Object key = new MagicKey("shared", ownerCache);
    final Cache<Object, Object> cache = executeOnOwner ? cache(0) : cache(1);
    final TransactionManager tm = executeOnOwner ? tm(0) : tm(1);
    assertValueInAllCaches(key, null);
    final Object initValue = INITIAL_VALUE;
    ownerCache.put(key, initValue);
    assertValueInAllCaches(key, initValue);
    tm.begin();
    assertEquals("Wrong first get.", initValue, cache.get(key));
    Transaction tx = tm.suspend();
    ownerCache.put(key, OTHER_VALUE);
    assertValueInAllCaches(key, OTHER_VALUE);
    Object finalValueExpected = null;
    boolean commitFails = lockingMode == LockingMode.OPTIMISTIC;
    tm.resume(tx);
    assertEquals("Wrong second get.", initValue, cache.get(key));
    assertEquals("Wrong value after remove.", initValue, cache.remove(key));
    switch(operation) {
        case PUT:
            finalValueExpected = FINAL_VALUE;
            assertEquals("Wrong put return value.", null, cache.put(key, FINAL_VALUE));
            assertEquals("Wrong final get.", FINAL_VALUE, cache.get(key));
            break;
        case REMOVE:
            finalValueExpected = null;
            assertEquals("Wrong remove return value.", null, cache.remove(key));
            assertEquals("Wrong final get.", null, cache.get(key));
            break;
        case REPLACE:
            finalValueExpected = null;
            assertEquals("Wrong replace return value.", null, cache.replace(key, FINAL_VALUE));
            assertEquals("Wrong final get.", null, cache.get(key));
            break;
        case CONDITIONAL_PUT:
            finalValueExpected = FINAL_VALUE;
            assertEquals("Wrong put return value.", null, cache.putIfAbsent(key, FINAL_VALUE));
            assertEquals("Wrong final get.", FINAL_VALUE, cache.get(key));
            break;
        case CONDITIONAL_REMOVE:
            finalValueExpected = null;
            assertEquals("Wrong remove return value.", false, cache.remove(key, INITIAL_VALUE));
            assertEquals("Wrong final get.", null, cache.get(key));
            break;
        case CONDITIONAL_REPLACE:
            finalValueExpected = null;
            assertEquals("Wrong replace return value.", false, cache.replace(key, INITIAL_VALUE, FINAL_VALUE));
            assertEquals("Wrong final get.", null, cache.get(key));
            break;
        default:
            fail("Unknown operation " + operation);
            break;
    }
    if (commitFails) {
        Exceptions.expectException(RollbackException.class, tm::commit);
    } else {
        tm.commit();
    }
    assertValueInAllCaches(key, lockingMode == LockingMode.PESSIMISTIC ? finalValueExpected : OTHER_VALUE);
    assertNoTransactions();
}
Also used : Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) MagicKey(org.infinispan.distribution.MagicKey)

Example 34 with MagicKey

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

the class FlagsEnabledTest method testWithFlagsSemantics.

public void testWithFlagsSemantics() {
    final AdvancedCache<MagicKey, String> cache1 = advancedCache(0, cacheName);
    final AdvancedCache<MagicKey, String> cache2 = advancedCache(1, cacheName);
    assertNotSame("CacheStores", getCacheStore(cache1), getCacheStore(cache2));
    assertLoadsAndReset(cache1, 0, cache2, 0);
    final AdvancedCache<MagicKey, String> cache1LocalOnly = cache1.withFlags(CACHE_MODE_LOCAL);
    MagicKey localKey = new MagicKey("local", cache1);
    cache1LocalOnly.put(localKey, "value1");
    assertLoadsAndReset(cache1, 1, cache2, 0);
    cache2.withFlags(CACHE_MODE_LOCAL).put(localKey, "value2");
    assertLoadsAndReset(cache1, 0, cache2, 1);
    assertCacheValue(cache1, localKey, "value1");
    assertLoadsAndReset(cache1, 0, cache2, 0);
    assertCacheValue(cache2, localKey, "value2");
    assertLoadsAndReset(cache1, 0, cache2, 0);
    MagicKey nonLocalKey = new MagicKey("nonLocal", cache2);
    cache1.put(nonLocalKey, "value");
    // Write skew check needs the previous version on the originator AND on the primary owner
    int cache1Loads = isTxCache() ? 1 : 0;
    assertLoadsAndReset(cache1, cache1Loads, cache2, 1);
    assertCacheValue(cache2, nonLocalKey, "value");
    assertLoadsAndReset(cache1, 0, cache2, 0);
    final AdvancedCache<MagicKey, String> cache1SkipRemoteAndStores = cache1LocalOnly.withFlags(SKIP_CACHE_LOAD);
    MagicKey localKey2 = new MagicKey("local2", cache1);
    cache1SkipRemoteAndStores.put(localKey2, "value");
    // CACHE_MODE_LOCAL operation is not replicated with the PrepareCommand and WSC is not executed,
    // but the entry is committed on the origin
    assertLoadsAndReset(cache1, 0, cache2, 0);
    assertCacheValue(cache1, localKey2, "value");
    // localKey2 isn't in memory, looks into store
    assertCacheValue(cache2, localKey2, null);
    assertLoadsAndReset(cache1, 0, cache2, 1);
    assertCacheValue(cache2, localKey2, null);
    assertLoadsAndReset(cache1, 0, cache2, 1);
    assertCacheValue(cache2.withFlags(SKIP_CACHE_LOAD), localKey2, null);
    assertLoadsAndReset(cache1, 0, cache2, 0);
    // Options on cache1SkipRemoteAndStores did NOT affect this cache
    MagicKey localKey3 = new MagicKey("local3", cache1);
    assertCacheValue(cache1LocalOnly, localKey3, null);
    assertLoadsAndReset(cache1, 1, cache2, 0);
}
Also used : MagicKey(org.infinispan.distribution.MagicKey)

Example 35 with MagicKey

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

the class NonTxFlagsEnabledTest method testCacheLocalInNonOwner.

public void testCacheLocalInNonOwner() {
    EmbeddedCacheManager cm = addClusterEnabledCacheManager(TestDataSCI.INSTANCE);
    cm.createCache(cacheName, getConfigurationBuilder().build());
    waitForClusterToForm(cacheName);
    final AdvancedCache<Object, String> cache1 = advancedCache(0, cacheName);
    final AdvancedCache<Object, String> cache2 = advancedCache(1, cacheName);
    final AdvancedCache<Object, String> cache3 = advancedCache(2, cacheName);
    final Object key = new MagicKey("k-no", cache1);
    cache3.withFlags(CACHE_MODE_LOCAL).put(key, "value");
    assertCacheValue(cache3, key, "value");
    assertCacheValue(cache1, key, null);
    assertCacheValue(cache2, key, null);
}
Also used : EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) 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