Search in sources :

Example 91 with MagicKey

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

the class InfinispanClusteredConsumerTest method consumerReceivedPostEntryCreatedEventNotifications.

@Test
public void consumerReceivedPostEntryCreatedEventNotifications() throws Exception {
    MagicKey key = new MagicKey(defaultCache(1));
    mockResultCreatedEvents.expectedMessageCount(1);
    mockResultCreatedEvents.message(0).outHeader(InfinispanConstants.EVENT_TYPE).isEqualTo("CACHE_ENTRY_CREATED");
    mockResultCreatedEvents.message(0).outHeader(InfinispanConstants.IS_PRE).isEqualTo(false);
    mockResultCreatedEvents.message(0).outHeader(InfinispanConstants.CACHE_NAME).isNotNull();
    mockResultCreatedEvents.message(0).outHeader(InfinispanConstants.KEY).isEqualTo(key.toString());
    defaultCache(1).put(key, "value");
    mockResultCreatedEvents.assertIsSatisfied();
}
Also used : MagicKey(org.infinispan.distribution.MagicKey) Test(org.junit.Test)

Example 92 with MagicKey

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

the class InfinispanClusteredConsumerTest method consumerReceivedExpirationEventNotifications.

@Test
public void consumerReceivedExpirationEventNotifications() throws Exception {
    MagicKey key = new MagicKey(defaultCache(1));
    mockResultCreatedEvents.expectedMessageCount(1);
    mockResultExpiredEvents.expectedMessageCount(1);
    mockResultCreatedEvents.message(0).outHeader(InfinispanConstants.EVENT_TYPE).isEqualTo("CACHE_ENTRY_CREATED");
    mockResultCreatedEvents.message(0).outHeader(InfinispanConstants.IS_PRE).isEqualTo(false);
    mockResultCreatedEvents.message(0).outHeader(InfinispanConstants.CACHE_NAME).isNotNull();
    mockResultCreatedEvents.message(0).outHeader(InfinispanConstants.KEY).isEqualTo(key.toString());
    mockResultExpiredEvents.message(0).outHeader(InfinispanConstants.EVENT_TYPE).isEqualTo("CACHE_ENTRY_EXPIRED");
    mockResultExpiredEvents.message(0).outHeader(InfinispanConstants.IS_PRE).isEqualTo(false);
    mockResultExpiredEvents.message(0).outHeader(InfinispanConstants.CACHE_NAME).isNotNull();
    mockResultExpiredEvents.message(0).outHeader(InfinispanConstants.KEY).isEqualTo(key.toString());
    injectTimeService();
    defaultCache(1).put(key, "value", 1000, TimeUnit.MILLISECONDS);
    ts0.advance(1001);
    ts1.advance(1001);
    assertNull(defaultCache(1).get(key));
    mockResultCreatedEvents.assertIsSatisfied();
    mockResultExpiredEvents.assertIsSatisfied(WAIT_TIMEOUT);
}
Also used : MagicKey(org.infinispan.distribution.MagicKey) Test(org.junit.Test)

Example 93 with MagicKey

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

the class VersionedDistStateTransferTest method testStateTransfer.

public void testStateTransfer() throws Exception {
    Cache<Object, Object> cache0 = cache(0);
    Cache<Object, Object> cache3 = cache(3);
    int NUM_KEYS = 20;
    MagicKey[] keys = new MagicKey[NUM_KEYS];
    String[] values = new String[NUM_KEYS];
    for (int i = 0; i < NUM_KEYS; i++) {
        // Put the entries on the cache that will be killed
        keys[i] = new MagicKey("key" + i, cache3);
        values[i] = "v" + i;
        cache0.put(keys[i], values[i]);
    }
    // no state transfer per se, but we check that the initial data is ok
    checkStateTransfer(keys, values);
    Transaction[] txs = new Transaction[NUM_KEYS];
    for (int i = 0; i < NUM_KEYS; i++) {
        int cacheIndex = i % 3;
        tm(cacheIndex).begin();
        {
            assertEquals(values[i], cache(cacheIndex).get(keys[i]));
        }
        txs[i] = tm(cacheIndex).suspend();
    }
    log.debugf("Starting joiner");
    addClusterEnabledCacheManager(TestDataSCI.INSTANCE, builder);
    Cache<Object, Object> cache4 = cache(4);
    log.debugf("Joiner started, checking transferred data");
    checkStateTransfer(keys, values);
    log.debugf("Stopping cache %s", cache3);
    manager(3).stop();
    // Eliminate the dead cache from the caches collection, cache4 now becomes cache(3)
    cacheManagers.remove(3);
    TestingUtil.waitForNoRebalance(caches());
    log.debugf("Leaver stopped, checking transferred data");
    checkStateTransfer(keys, values);
    // Cause a write skew
    for (int i = 0; i < NUM_KEYS; i++) {
        cache4.put(keys[i], "new " + values[i]);
    }
    for (int i = 0; i < NUM_KEYS; i++) {
        int cacheIndex = i % 3;
        log.tracef("Expecting a write skew failure for key %s on cache %s", keys[i], cache(cacheIndex));
        tm(cacheIndex).resume(txs[i]);
        cache(cacheIndex).put(keys[i], "new new " + values[i]);
        try {
            tm(cacheIndex).commit();
            fail("The write skew check should have failed!");
        } catch (RollbackException expected) {
        // Expected
        }
    }
    for (int cacheIndex = 0; cacheIndex < 4; cacheIndex++) {
        for (int i = 0; i < NUM_KEYS; i++) {
            assertEquals("Wrong value found on cache " + cache(cacheIndex), "new " + values[i], cache(cacheIndex).get(keys[i]));
        }
    }
}
Also used : Transaction(javax.transaction.Transaction) RollbackException(javax.transaction.RollbackException) MagicKey(org.infinispan.distribution.MagicKey)

Example 94 with MagicKey

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

the class AbstractClusteredWriteSkewTest method doTestIgnoreReturnValueAndRead.

protected void doTestIgnoreReturnValueAndRead(boolean executeOnPrimaryOwner) throws Exception {
    final Object key = new MagicKey("ignore-previous-value", cache(0));
    final AdvancedCache<Object, Object> c = executeOnPrimaryOwner ? advancedCache(0) : advancedCache(1);
    final TransactionManager tm = executeOnPrimaryOwner ? tm(0) : tm(1);
    for (Cache cache : caches()) {
        assertNull("wrong initial value for " + address(cache) + ".", cache.get(key));
    }
    c.put("k", "init");
    tm.begin();
    c.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put("k", "v1");
    assertEquals("v1", c.put("k", "v2"));
    Transaction tx = tm.suspend();
    assertEquals("init", c.put("k", "other"));
    tm.resume(tx);
    tm.commit();
}
Also used : Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) MagicKey(org.infinispan.distribution.MagicKey) Cache(org.infinispan.Cache) AdvancedCache(org.infinispan.AdvancedCache)

Example 95 with MagicKey

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

the class AbstractClusteredWriteSkewTest method doIgnoreReturnValueTest.

private void doIgnoreReturnValueTest(boolean executeOnPrimaryOwner, Operation operation, boolean initKey) throws Exception {
    final Object key = new MagicKey("ignore-return-value", cache(0));
    final AdvancedCache<Object, Object> c = executeOnPrimaryOwner ? advancedCache(0) : advancedCache(1);
    final TransactionManager tm = executeOnPrimaryOwner ? tm(0) : tm(1);
    for (Cache cache : caches()) {
        assertNull("wrong initial value for " + address(cache) + ".", cache.get(key));
    }
    log.debugf("Initialize the key? %s", initKey);
    if (initKey) {
        cache(0).put(key, "init");
    }
    Object finalValue = null;
    boolean rollbackExpected = false;
    log.debugf("Start the transaction and perform a %s operation", operation);
    tm.begin();
    switch(operation) {
        case PUT:
            finalValue = "v1";
            rollbackExpected = false;
            c.withFlags(Flag.IGNORE_RETURN_VALUES).put(key, "v1");
            break;
        case REMOVE:
            finalValue = null;
            rollbackExpected = false;
            c.withFlags(Flag.IGNORE_RETURN_VALUES).remove(key);
            break;
        case REPLACE:
            finalValue = "v2";
            rollbackExpected = initKey;
            c.withFlags(Flag.IGNORE_RETURN_VALUES).replace(key, "v1");
            break;
        case CONDITIONAL_PUT:
            finalValue = "v2";
            rollbackExpected = !initKey;
            c.withFlags(Flag.IGNORE_RETURN_VALUES).putIfAbsent(key, "v1");
            break;
        case CONDITIONAL_REMOVE:
            finalValue = "v2";
            rollbackExpected = initKey;
            c.withFlags(Flag.IGNORE_RETURN_VALUES).remove(key, "init");
            break;
        case CONDITIONAL_REPLACE:
            finalValue = "v2";
            rollbackExpected = initKey;
            c.withFlags(Flag.IGNORE_RETURN_VALUES).replace(key, "init", "v1");
            break;
        default:
            tm.rollback();
            fail("Unknown operation " + operation);
    }
    Transaction tx = tm.suspend();
    log.debugf("Suspend the transaction and update the key");
    c.put(key, "v2");
    log.debugf("Checking if all the keys has the same value");
    for (Cache cache : caches()) {
        assertEquals("wrong intermediate value for " + address(cache) + ".", "v2", cache.get(key));
    }
    log.debugf("It is going to try to commit the suspended transaction");
    try {
        tm.resume(tx);
        tm.commit();
        if (rollbackExpected) {
            fail("Rollback expected!");
        }
    } catch (RollbackException | HeuristicRollbackException e) {
        if (!rollbackExpected) {
            fail("Rollback *not* expected!");
        }
    // no-op
    }
    log.debugf("So far so good. Check the key final value");
    assertNoTransactions();
    for (Cache cache : caches()) {
        assertEquals("wrong final value for " + address(cache) + ".", finalValue, cache.get(key));
    }
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) MagicKey(org.infinispan.distribution.MagicKey) Cache(org.infinispan.Cache) AdvancedCache(org.infinispan.AdvancedCache)

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