Search in sources :

Example 11 with MagicKey

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

the class DistributedStreamIteratorRepeatableReadTxTest method testCacheCollectionsIncludesEntriesNotYetCommitted.

/**
 * See ISPN-12731 Cache collections ignore values added in transaction
 */
public void testCacheCollectionsIncludesEntriesNotYetCommitted() throws Exception {
    Cache<Object, String> cache = cache(0, CACHE_NAME);
    TransactionManager tm = tm(cache);
    tm.begin();
    try {
        Map<Object, String> inserted = new LinkedHashMap<>();
        for (int i = 0; i < 3; ++i) {
            Object key = new MagicKey(cache(i, CACHE_NAME));
            cache.put(key, key.toString());
            inserted.put(key, key.toString());
        }
        // cache collections use streams internally
        Set<Object> expectedKeys = inserted.keySet();
        Set<Object> keySetResults = new HashSet<>(cache.keySet());
        assertEquals(expectedKeys, keySetResults);
        Set<String> expectedValues = new HashSet<>(inserted.values());
        Set<String> valuesResults = new HashSet<>(cache.values());
        assertEquals(expectedValues, valuesResults);
        Set<Map.Entry<Object, String>> expectedEntries = inserted.entrySet();
        Set<Map.Entry<Object, String>> entrySetResults = new HashSet<>(cache.entrySet());
        assertEquals(expectedEntries, entrySetResults);
    } finally {
        tm.rollback();
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) CacheEntry(org.infinispan.container.entries.CacheEntry) TransactionManager(javax.transaction.TransactionManager) MagicKey(org.infinispan.distribution.MagicKey) HashSet(java.util.HashSet)

Example 12 with MagicKey

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

the class DistL1WriteSkewTest method testL1ValueGetCanExpire.

@Test
public void testL1ValueGetCanExpire() {
    Cache<Object, Object> cache0 = cache(0);
    Cache<Object, Object> cache1 = cache(1);
    Cache<Object, Object> cache2 = cache(2);
    MagicKey hello = new MagicKey("hello", cache0, cache1);
    DistributionTestHelper.assertIsNotInL1(cache2, hello);
    // Auto-commit is true
    cache0.put(hello, "world 1");
    assertEquals("world 1", cache2.get(hello));
    DistributionTestHelper.assertIsInL1(cache2, hello);
}
Also used : MagicKey(org.infinispan.distribution.MagicKey) Test(org.testng.annotations.Test)

Example 13 with MagicKey

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

the class DistL1WriteSkewTest method testL1ValuePutCanExpire.

@Test
public void testL1ValuePutCanExpire() {
    Cache<Object, Object> cache0 = cache(0);
    Cache<Object, Object> cache1 = cache(1);
    Cache<Object, Object> cache2 = cache(2);
    MagicKey hello = new MagicKey("hello", cache0, cache1);
    DistributionTestHelper.assertIsNotInL1(cache2, hello);
    // Auto-commit is true
    cache2.put(hello, "world 1");
    DistributionTestHelper.assertIsInL1(cache2, hello);
}
Also used : MagicKey(org.infinispan.distribution.MagicKey) Test(org.testng.annotations.Test)

Example 14 with MagicKey

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

the class DistWriteSkewTest method testResendPrepare.

public void testResendPrepare() throws Exception {
    Cache<Object, Object> cache0 = cache(0);
    Cache<Object, Object> cache1 = cache(1);
    Cache<Object, Object> cache2 = cache(2);
    Cache<Object, Object> cache3 = cache(3);
    MagicKey hello = new MagicKey("hello", cache(2));
    // Auto-commit is true
    cache0.put(hello, "world");
    // create a write skew
    tm(2).begin();
    assertEquals("world", cache2.get(hello));
    Transaction t = tm(2).suspend();
    // Implicit tx.  Prepare should be retried.
    cache(0).put(hello, "world 2");
    assertEquals("world 2", cache0.get(hello));
    assertEquals("world 2", cache1.get(hello));
    assertEquals("world 2", cache2.get(hello));
    assertEquals("world 2", cache3.get(hello));
    tm(2).resume(t);
    cache2.put(hello, "world 3");
    try {
        tm(2).commit();
        fail("This transaction should roll back");
    } catch (RollbackException expected) {
    // expected
    }
    assertEquals("world 2", cache0.get(hello));
    assertEquals("world 2", cache1.get(hello));
    assertEquals("world 2", cache2.get(hello));
    assertEquals("world 2", cache3.get(hello));
}
Also used : Transaction(javax.transaction.Transaction) RollbackException(javax.transaction.RollbackException) MagicKey(org.infinispan.distribution.MagicKey)

Example 15 with MagicKey

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

the class DistWriteSkewTest method testWriteSkewMultiEntries.

public void testWriteSkewMultiEntries() throws Exception {
    Cache<Object, Object> cache0 = cache(0);
    Cache<Object, Object> cache1 = cache(1);
    Cache<Object, Object> cache2 = cache(2);
    Cache<Object, Object> cache3 = cache(3);
    MagicKey hello = new MagicKey("hello", cache(2));
    MagicKey hello2 = new MagicKey("hello2", cache(3));
    MagicKey hello3 = new MagicKey("hello3", cache(0));
    tm(1).begin();
    cache1.put(hello, "world 1");
    cache1.put(hello2, "world 1");
    cache1.put(hello3, "world 1");
    tm(1).commit();
    tm(1).begin();
    cache1.put(hello2, "world 2");
    cache1.put(hello3, "world 2");
    assertEquals("world 1", cache1.get(hello));
    assertEquals("world 2", cache1.get(hello2));
    assertEquals("world 2", cache1.get(hello3));
    Transaction t = tm(1).suspend();
    // Induce a write skew
    // Auto-commit is true
    cache3.put(hello, "world 3");
    for (Cache<Object, Object> c : caches()) {
        assertEquals("world 3", c.get(hello));
        assertEquals("world 1", c.get(hello2));
        assertEquals("world 1", c.get(hello3));
    }
    tm(1).resume(t);
    cache1.put(hello, "world 2");
    try {
        tm(1).commit();
        fail("Transaction should roll back");
    } catch (RollbackException re) {
    // expected
    }
    for (Cache<Object, Object> c : caches()) {
        assertEquals("world 3", c.get(hello));
        assertEquals("world 1", c.get(hello2));
        assertEquals("world 1", c.get(hello3));
    }
}
Also used : Transaction(javax.transaction.Transaction) RollbackException(javax.transaction.RollbackException) 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