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));
}
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));
}
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));
}
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());
}
Aggregations