Search in sources :

Example 1 with GET

use of org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.GET in project ignite by apache.

the class MvccRepeatableReadOperationsTest method testReplaceConsistency.

/**
 * Check getAndPut/getAndRemove operations consistency.
 *
 * @throws IgniteCheckedException If failed.
 */
@Test
public void testReplaceConsistency() throws IgniteCheckedException {
    Ignite node1 = grid(0);
    TestCache<Integer, MvccTestAccount> cache1 = new TestCache<>(node1.cache(DEFAULT_CACHE_NAME));
    final Set<Integer> existedKeys = new HashSet<>(3);
    final Set<Integer> nonExistedKeys = new HashSet<>(3);
    final Set<Integer> allKeys = generateKeySet(grid(0).cache(DEFAULT_CACHE_NAME), existedKeys, nonExistedKeys);
    final Map<Integer, MvccTestAccount> initialMap = existedKeys.stream().collect(Collectors.toMap(k -> k, k -> new MvccTestAccount(k, 1)));
    Map<Integer, MvccTestAccount> updateMap = existedKeys.stream().collect(Collectors.toMap(k -> k, k -> new MvccTestAccount(k, 3)));
    cache1.cache.putAll(initialMap);
    IgniteTransactions txs = node1.transactions();
    try (Transaction tx = txs.txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.REPEATABLE_READ)) {
        for (Integer key : allKeys) {
            MvccTestAccount newVal = new MvccTestAccount(key, 2);
            if (existedKeys.contains(key)) {
                assertTrue(cache1.cache.replace(key, new MvccTestAccount(key, 1), newVal));
                assertEquals(newVal, cache1.cache.getAndReplace(key, new MvccTestAccount(key, 3)));
            } else {
                assertFalse(cache1.cache.replace(key, new MvccTestAccount(key, 1), newVal));
                assertNull(cache1.cache.getAndReplace(key, new MvccTestAccount(key, 3)));
            }
        }
        assertEquals(updateMap, getEntries(cache1, allKeys, SQL));
        assertEquals(updateMap, getEntries(cache1, allKeys, GET));
        tx.commit();
    }
    assertEquals(updateMap, getEntries(cache1, allKeys, SQL));
    assertEquals(updateMap, getEntries(cache1, allKeys, GET));
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) GET(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.GET) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) SQL(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL) HashSet(java.util.HashSet) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) Map(java.util.Map) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with GET

use of org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.GET in project ignite by apache.

the class MvccRepeatableReadOperationsTest method testGetAndUpdateOperations.

/**
 * Check getAndPut/getAndRemove operations consistency.
 *
 * @throws IgniteCheckedException If failed.
 */
@Test
public void testGetAndUpdateOperations() throws IgniteCheckedException {
    Ignite node1 = grid(0);
    TestCache<Integer, MvccTestAccount> cache1 = new TestCache<>(node1.cache(DEFAULT_CACHE_NAME));
    final Set<Integer> keysForUpdate = new HashSet<>(3);
    final Set<Integer> keysForRemove = new HashSet<>(3);
    final Set<Integer> allKeys = generateKeySet(grid(0).cache(DEFAULT_CACHE_NAME), keysForUpdate, keysForRemove);
    final Map<Integer, MvccTestAccount> initialMap = keysForRemove.stream().collect(Collectors.toMap(k -> k, k -> new MvccTestAccount(k, 1)));
    Map<Integer, MvccTestAccount> updateMap = keysForUpdate.stream().collect(Collectors.toMap(k -> k, k -> new MvccTestAccount(k, 3)));
    cache1.cache.putAll(initialMap);
    IgniteTransactions txs = node1.transactions();
    try (Transaction tx = txs.txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.REPEATABLE_READ)) {
        for (Integer key : keysForUpdate) {
            MvccTestAccount newVal1 = new MvccTestAccount(key, 1);
            // Check create.
            assertNull(cache1.cache.getAndPut(key, newVal1));
            MvccTestAccount newVal2 = new MvccTestAccount(key, 2);
            // Check update.
            assertEquals(newVal1, cache1.cache.getAndPut(key, newVal2));
        }
        for (Integer key : keysForRemove) {
            // Check remove existed.
            assertEquals(initialMap.get(key), cache1.cache.getAndRemove(key));
            // Check remove non-existed.
            assertNull(cache1.cache.getAndRemove(key));
        }
        for (Integer key : allKeys) {
            MvccTestAccount oldVal = new MvccTestAccount(key, 2);
            MvccTestAccount newVal = new MvccTestAccount(key, 3);
            if (keysForRemove.contains(key))
                // Omit update 'null'.
                assertNull(cache1.cache.getAndReplace(key, newVal));
            else
                // Check updated.
                assertEquals(oldVal, cache1.cache.getAndReplace(key, newVal));
        }
        assertEquals(updateMap, getEntries(cache1, allKeys, SQL));
        assertEquals(updateMap, getEntries(cache1, allKeys, GET));
        tx.commit();
    }
    assertEquals(updateMap, getEntries(cache1, allKeys, SQL));
    assertEquals(updateMap, getEntries(cache1, allKeys, GET));
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) GET(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.GET) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) SQL(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL) HashSet(java.util.HashSet) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) Map(java.util.Map) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with GET

use of org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.GET in project ignite by apache.

the class MvccRepeatableReadOperationsTest method testPutIfAbsentConsistency.

/**
 * Check getAndPut/getAndRemove operations consistency.
 *
 * @throws IgniteCheckedException If failed.
 */
@Test
public void testPutIfAbsentConsistency() throws IgniteCheckedException {
    Ignite node1 = grid(0);
    TestCache<Integer, MvccTestAccount> cache1 = new TestCache<>(node1.cache(DEFAULT_CACHE_NAME));
    final Set<Integer> keysForCreate = new HashSet<>(3);
    final Set<Integer> keysForUpdate = new HashSet<>(3);
    final Set<Integer> allKeys = generateKeySet(grid(0).cache(DEFAULT_CACHE_NAME), keysForCreate, keysForUpdate);
    final Map<Integer, MvccTestAccount> initialMap = keysForUpdate.stream().collect(Collectors.toMap(k -> k, k -> new MvccTestAccount(k, 1)));
    Map<Integer, MvccTestAccount> updatedMap = allKeys.stream().collect(Collectors.toMap(k -> k, k -> new MvccTestAccount(k, 1)));
    cache1.cache.putAll(initialMap);
    IgniteTransactions txs = node1.transactions();
    try (Transaction tx = txs.txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.REPEATABLE_READ)) {
        for (Integer key : keysForUpdate) // Check update.
        assertFalse(cache1.cache.putIfAbsent(key, new MvccTestAccount(key, 2)));
        for (Integer key : keysForCreate) // Check create.
        assertTrue(cache1.cache.putIfAbsent(key, new MvccTestAccount(key, 1)));
        assertEquals(updatedMap, getEntries(cache1, allKeys, SQL));
        tx.commit();
    }
    assertEquals(updatedMap, getEntries(cache1, allKeys, SQL));
    assertEquals(updatedMap, getEntries(cache1, allKeys, GET));
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) GET(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.GET) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) SQL(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL) HashSet(java.util.HashSet) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) Map(java.util.Map) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with GET

use of org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.GET in project ignite by apache.

the class MvccRepeatableReadBulkOpsTest method checkOperationsConsistency.

/**
 * Checks SQL and CacheAPI operation see consistent results before and after update.
 *
 * @throws Exception If failed.
 */
private void checkOperationsConsistency(WriteMode writeMode, boolean requestFromClient) throws Exception {
    Ignite node = grid(requestFromClient ? nodesCount() - 1 : 0);
    TestCache<Integer, MvccTestAccount> cache = new TestCache<>(node.cache(DEFAULT_CACHE_NAME));
    final Set<Integer> keysForUpdate = new HashSet<>(3);
    final Set<Integer> keysForRemove = new HashSet<>(3);
    final Set<Integer> allKeys = generateKeySet(grid(0).cache(DEFAULT_CACHE_NAME), keysForUpdate, keysForRemove);
    try {
        int updCnt = 1;
        final Map<Integer, MvccTestAccount> initialVals = allKeys.stream().collect(Collectors.toMap(k -> k, k -> new MvccTestAccount(k, 1)));
        updateEntries(cache, initialVals, writeMode);
        assertEquals(initialVals.size(), cache.cache.size());
        IgniteTransactions txs = node.transactions();
        Map<Integer, MvccTestAccount> updatedVals = null;
        try (Transaction tx = txs.txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.REPEATABLE_READ)) {
            Map<Integer, MvccTestAccount> vals1 = getEntries(cache, allKeys, GET);
            Map<Integer, MvccTestAccount> vals2 = getEntries(cache, allKeys, SQL);
            Map<Integer, MvccTestAccount> vals3 = getEntries(cache, allKeys, ReadMode.INVOKE);
            assertEquals(initialVals, vals1);
            assertEquals(initialVals, vals2);
            assertEquals(initialVals, vals3);
            assertEquals(initialVals.size(), cache.cache.size());
            for (ReadMode readMode : new ReadMode[] { GET, SQL, INVOKE }) {
                int updCnt0 = ++updCnt;
                updatedVals = allKeys.stream().collect(Collectors.toMap(Function.identity(), k -> new MvccTestAccount(k, updCnt0)));
                updateEntries(cache, updatedVals, writeMode);
                assertEquals(allKeys.size(), cache.cache.size());
                removeEntries(cache, keysForRemove, writeMode);
                for (Integer key : keysForRemove) updatedVals.remove(key);
                assertEquals(String.valueOf(readMode), updatedVals, getEntries(cache, allKeys, readMode));
            }
            tx.commit();
        }
        try (Transaction tx = txs.txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.REPEATABLE_READ)) {
            assertEquals(updatedVals, getEntries(cache, allKeys, GET));
            assertEquals(updatedVals, getEntries(cache, allKeys, SQL));
            assertEquals(updatedVals, getEntries(cache, allKeys, INVOKE));
            tx.commit();
        }
        assertEquals(updatedVals.size(), cache.cache.size());
    } finally {
        cache.cache.removeAll(keysForUpdate);
    }
    assertEquals(0, cache.cache.size());
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) PUT(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.WriteMode.PUT) GET(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.GET) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) Transaction(org.apache.ignite.transactions.Transaction) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Function(java.util.function.Function) EntryProcessorException(javax.cache.processor.EntryProcessorException) SQL(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MutableEntry(javax.cache.processor.MutableEntry) Map(java.util.Map) LinkedHashSet(java.util.LinkedHashSet) F(org.apache.ignite.internal.util.typedef.F) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Set(java.util.Set) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) INVOKE(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.INVOKE) IgniteTransactions(org.apache.ignite.IgniteTransactions) DML(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.WriteMode.DML) CacheMode(org.apache.ignite.cache.CacheMode) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Ignite (org.apache.ignite.Ignite)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteTransactions (org.apache.ignite.IgniteTransactions)4 CacheEntryProcessor (org.apache.ignite.cache.CacheEntryProcessor)4 GET (org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.GET)4 SQL (org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL)4 Transaction (org.apache.ignite.transactions.Transaction)4 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)4 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1