use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.
the class CacheMvccAbstractTest method putAllGetAll.
/**
* @param restartMode Restart mode.
* @param srvs Number of server nodes.
* @param clients Number of client nodes.
* @param cacheBackups Number of cache backups.
* @param cacheParts Number of cache partitions.
* @param readMode Read mode.
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
protected void putAllGetAll(RestartMode restartMode, final int srvs, final int clients, int cacheBackups, int cacheParts, @Nullable IgniteInClosure<CacheConfiguration> cfgC, ReadMode readMode, WriteMode writeMode) throws Exception {
final int RANGE = 20;
final int writers = 4;
final int readers = 4;
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
int min = idx * RANGE;
int max = min + RANGE;
info("Thread range [min=" + min + ", max=" + max + ']');
// Sorted map for put to avoid deadlocks.
Map<Integer, Integer> map = new TreeMap<>();
// Unordered key sequence.
Set<Integer> keys = new LinkedHashSet<>();
int v = idx * 1_000_000;
boolean first = true;
while (!stop.get()) {
while (keys.size() < RANGE) {
int key = rnd.nextInt(min, max);
if (keys.add(key))
map.put(key, v);
}
TestCache<Integer, Integer> cache = randomCache(caches, rnd);
try {
IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
if (!first && rnd.nextBoolean()) {
Map<Integer, Integer> res = readAllByMode(cache.cache, keys, readMode, INTEGER_CODEC);
for (Integer k : keys) assertEquals("res=" + res, v - 1, (Object) res.get(k));
}
writeAllByMode(cache.cache, map, writeMode, INTEGER_CODEC);
tx.commit();
v++;
first = false;
}
if (rnd.nextBoolean()) {
Map<Integer, Integer> res = readAllByMode(cache.cache, keys, readMode, INTEGER_CODEC);
for (Integer k : keys) assertEquals("key=" + k, v - 1, (Object) res.get(k));
}
} catch (Exception e) {
handleTxException(e);
} finally {
cache.readUnlock();
keys.clear();
map.clear();
}
}
info("Writer done, updates: " + v);
}
};
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
Set<Integer> keys = new LinkedHashSet<>();
Map<Integer, Integer> readVals = new HashMap<>();
while (!stop.get()) {
int range = rnd.nextInt(0, writers);
int min = range * RANGE;
int max = min + RANGE;
keys.clear();
while (keys.size() < RANGE) keys.add(rnd.nextInt(min, max));
TestCache<Integer, Integer> cache = randomCache(caches, rnd);
Map<Integer, Integer> map;
try {
map = readAllByMode(cache.cache, keys, readMode, INTEGER_CODEC);
} catch (Exception e) {
handleTxException(e);
continue;
} finally {
cache.readUnlock();
}
assertTrue("Invalid map size: " + map.size() + ", map=" + map, map.isEmpty() || map.size() == RANGE);
Integer val0 = null;
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
Integer val = e.getValue();
assertNotNull(val);
if (val0 == null) {
Integer readVal = readVals.get(range);
if (readVal != null)
assertTrue("readVal=" + readVal + ", val=" + val + ", map=" + map, readVal <= val);
readVals.put(range, val);
val0 = val;
} else {
if (!F.eq(val0, val)) {
assertEquals("Unexpected value [range=" + range + ", key=" + e.getKey() + ']' + ", map=" + map, val0, val);
}
}
}
}
}
};
readWriteTest(restartMode, srvs, clients, cacheBackups, cacheParts, writers, readers, DFLT_TEST_TIME, cfgC, null, writer, reader);
for (Ignite node : G.allGrids()) checkActiveQueriesCleanup(node);
}
use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.
the class CacheMvccAbstractTest method updateNObjectsTest.
/**
* @param N Number of object to update in single transaction.
* @param srvs Number of server nodes.
* @param clients Number of client nodes.
* @param cacheBackups Number of cache backups.
* @param cacheParts Number of cache partitions.
* @param time Test time.
* @param readMode Read mode.
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
protected void updateNObjectsTest(final int N, final int srvs, final int clients, int cacheBackups, int cacheParts, long time, @Nullable IgniteInClosure<CacheConfiguration> cfgC, ReadMode readMode, WriteMode writeMode, RestartMode restartMode) throws Exception {
final int TOTAL = 20;
assert N <= TOTAL;
info("updateNObjectsTest [n=" + N + ", total=" + TOTAL + ']');
final int writers = 4;
final int readers = 4;
final IgniteInClosure<IgniteCache<Object, Object>> init = new IgniteInClosure<IgniteCache<Object, Object>>() {
@Override
public void apply(IgniteCache<Object, Object> cache) {
final IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
Map<Integer, Integer> vals = new LinkedHashMap<>();
for (int i = 0; i < TOTAL; i++) vals.put(i, N);
try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
writeAllByMode(cache, vals, writeMode, INTEGER_CODEC);
tx.commit();
}
}
};
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
int cnt = 0;
while (!stop.get()) {
TestCache<Integer, Integer> cache = randomCache(caches, rnd);
IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
TreeSet<Integer> keys = new TreeSet<>();
while (keys.size() < N) keys.add(rnd.nextInt(TOTAL));
try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
tx.timeout(TX_TIMEOUT);
Map<Integer, Integer> curVals = readAllByMode(cache.cache, keys, readMode, INTEGER_CODEC);
assertEquals(N, curVals.size());
Map<Integer, Integer> newVals = new TreeMap<>();
for (Map.Entry<Integer, Integer> e : curVals.entrySet()) newVals.put(e.getKey(), e.getValue() + 1);
writeAllByMode(cache.cache, newVals, writeMode, INTEGER_CODEC);
tx.commit();
} catch (Exception e) {
handleTxException(e);
} finally {
cache.readUnlock();
}
cnt++;
}
info("Writer finished, updates: " + cnt);
}
};
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
Set<Integer> keys = new LinkedHashSet<>();
while (!stop.get()) {
while (keys.size() < TOTAL) keys.add(rnd.nextInt(TOTAL));
TestCache<Integer, Integer> cache = randomCache(caches, rnd);
Map<Integer, Integer> vals = null;
try {
vals = readAllByMode(cache.cache, keys, readMode, INTEGER_CODEC);
} catch (Exception e) {
handleTxException(e);
} finally {
cache.readUnlock();
}
assertEquals("vals=" + vals, TOTAL, vals.size());
int sum = 0;
for (int i = 0; i < TOTAL; i++) {
Integer val = vals.get(i);
assertNotNull(val);
sum += val;
}
assertEquals(0, sum % N);
}
if (idx == 0) {
TestCache<Integer, Integer> cache = randomCache(caches, rnd);
Map<Integer, Integer> vals;
try {
vals = readAllByMode(cache.cache, keys, readMode, INTEGER_CODEC);
} finally {
cache.readUnlock();
}
int sum = 0;
for (int i = 0; i < TOTAL; i++) {
Integer val = vals.get(i);
info("Value [id=" + i + ", val=" + val + ']');
sum += val;
}
info("Sum [sum=" + sum + ", mod=" + sum % N + ']');
}
}
};
readWriteTest(restartMode, srvs, clients, cacheBackups, cacheParts, writers, readers, time, cfgC, init, writer, reader);
}
use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.
the class CacheMvccSqlQueriesAbstractTest method countTransactional.
/**
* @param singleNode {@code True} for test with single node.
* @throws Exception If failed.
*/
private void countTransactional(boolean singleNode) throws Exception {
final int writers = 4;
final int readers = 4;
final int THREAD_KEY_RANGE = 100;
final int VAL_RANGE = 10;
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
int min = idx * THREAD_KEY_RANGE;
int max = min + THREAD_KEY_RANGE;
info("Thread range [min=" + min + ", max=" + max + ']');
int cnt = 0;
Set<Integer> keys = new LinkedHashSet<>();
while (!stop.get()) {
TestCache<Integer, MvccTestSqlIndexValue> cache = randomCache(caches, rnd);
try {
// Add or remove 10 keys.
if (!keys.isEmpty() && (keys.size() == THREAD_KEY_RANGE || rnd.nextInt(3) == 0)) {
Set<Integer> rmvKeys = new HashSet<>();
for (Integer key : keys) {
rmvKeys.add(key);
if (rmvKeys.size() == 10)
break;
}
assertEquals(10, rmvKeys.size());
cache.cache.removeAll(rmvKeys);
keys.removeAll(rmvKeys);
} else {
TreeMap<Integer, MvccTestSqlIndexValue> map = new TreeMap<>();
while (map.size() != 10) {
Integer key = rnd.nextInt(min, max);
if (keys.add(key))
map.put(key, new MvccTestSqlIndexValue(rnd.nextInt(VAL_RANGE)));
}
assertEquals(10, map.size());
cache.cache.putAll(map);
}
} finally {
cache.readUnlock();
}
}
info("Writer finished, updates: " + cnt);
}
};
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
List<SqlFieldsQuery> qrys = new ArrayList<>();
qrys.add(new SqlFieldsQuery("select count(*) from MvccTestSqlIndexValue"));
qrys.add(new SqlFieldsQuery("select count(*) from MvccTestSqlIndexValue where idxVal1 >= 0 and idxVal1 <= " + VAL_RANGE));
while (!stop.get()) {
TestCache<Integer, MvccTestSqlIndexValue> cache = randomCache(caches, rnd);
try {
for (SqlFieldsQuery qry : qrys) {
List<List<?>> res = cache.cache.query(qry).getAll();
assertEquals(1, res.size());
Long cnt = (Long) res.get(0).get(0);
assertTrue(cnt % 10 == 0);
}
} finally {
cache.readUnlock();
}
}
}
};
int srvs;
int clients;
if (singleNode) {
srvs = 1;
clients = 0;
} else {
srvs = 4;
clients = 2;
}
readWriteTest(null, srvs, clients, 0, DFLT_PARTITION_COUNT, writers, readers, DFLT_TEST_TIME, new InitIndexing(Integer.class, MvccTestSqlIndexValue.class), null, writer, reader);
}
use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.
the class CacheMvccSqlQueriesAbstractTest method testJoinTransactional_DistributedJoins_ClientServer2.
/**
* @throws Exception If failed.
*/
@Test
public void testJoinTransactional_DistributedJoins_ClientServer2() throws Exception {
final int KEYS = 100;
final int writers = 1;
final int readers = 4;
final int CHILDREN_CNT = 10;
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
int cnt = 0;
while (!stop.get()) {
TestCache<Object, Object> cache = randomCache(caches, rnd);
IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
try {
try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
Integer key = rnd.nextInt(KEYS);
JoinTestParentKey parentKey = new JoinTestParentKey(key);
JoinTestParent parent = (JoinTestParent) cache.cache.get(parentKey);
if (parent == null) {
for (int i = 0; i < CHILDREN_CNT; i++) cache.cache.put(new JoinTestChildKey(key * 10_000 + i), new JoinTestChild(key));
cache.cache.put(parentKey, new JoinTestParent(key));
} else {
for (int i = 0; i < CHILDREN_CNT; i++) cache.cache.remove(new JoinTestChildKey(key * 10_000 + i));
cache.cache.remove(parentKey);
}
tx.commit();
}
cnt++;
} finally {
cache.readUnlock();
}
}
info("Writer finished, updates: " + cnt);
}
};
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
SqlFieldsQuery qry = new SqlFieldsQuery("select c.parentId, p.id from " + "JoinTestChild c left outer join JoinTestParent p on (c.parentId = p.id) where p.id=?").setDistributedJoins(true);
int cnt = 0;
while (!stop.get()) {
TestCache<Object, Object> cache = randomCache(caches, rnd);
qry.setArgs(rnd.nextInt(KEYS));
try {
List<List<?>> res = cache.cache.query(qry).getAll();
if (!res.isEmpty())
assertEquals(CHILDREN_CNT, res.size());
cnt++;
} finally {
cache.readUnlock();
}
}
info("Reader finished, read count: " + cnt);
}
};
readWriteTest(null, 4, 2, 0, DFLT_PARTITION_COUNT, writers, readers, DFLT_TEST_TIME, new InitIndexing(JoinTestParentKey.class, JoinTestParent.class, JoinTestChildKey.class, JoinTestChild.class), null, writer, reader);
}
use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.
the class CacheMvccSqlQueriesAbstractTest method maxMinTransactional.
/**
* @param singleNode {@code True} for test with single node.
* @throws Exception If failed.
*/
private void maxMinTransactional(boolean singleNode) throws Exception {
final int writers = 1;
final int readers = 1;
final int THREAD_OPS = 10;
final int OP_RANGE = 10;
final int THREAD_KEY_RANGE = OP_RANGE * THREAD_OPS;
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
int min = idx * THREAD_KEY_RANGE;
info("Thread range [start=" + min + ']');
int cnt = 0;
boolean add = true;
int op = 0;
while (!stop.get()) {
TestCache<Integer, MvccTestSqlIndexValue> cache = randomCache(caches, rnd);
try {
int startKey = min + op * OP_RANGE;
if (add) {
Map<Integer, MvccTestSqlIndexValue> vals = new HashMap<>();
for (int i = 0; i < 10; i++) {
Integer key = startKey + i + 1;
vals.put(key, new MvccTestSqlIndexValue(key));
}
cache.cache.putAll(vals);
// info("put " + vals.keySet());
} else {
Set<Integer> rmvKeys = new HashSet<>();
for (int i = 0; i < 10; i++) rmvKeys.add(startKey + i + 1);
cache.cache.removeAll(rmvKeys);
// info("remove " + rmvKeys);
}
if (++op == THREAD_OPS) {
add = !add;
op = 0;
}
} finally {
cache.readUnlock();
}
}
info("Writer finished, updates: " + cnt);
}
};
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
List<SqlFieldsQuery> maxQrys = new ArrayList<>();
List<SqlFieldsQuery> minQrys = new ArrayList<>();
maxQrys.add(new SqlFieldsQuery("select max(idxVal1) from MvccTestSqlIndexValue"));
maxQrys.add(new SqlFieldsQuery("select max(idxVal1) from MvccTestSqlIndexValue where idxVal1 >= 0"));
minQrys.add(new SqlFieldsQuery("select min(idxVal1) from MvccTestSqlIndexValue"));
minQrys.add(new SqlFieldsQuery("select min(idxVal1) from MvccTestSqlIndexValue where idxVal1 >= 0"));
while (!stop.get()) {
TestCache<Integer, MvccTestSqlIndexValue> cache = randomCache(caches, rnd);
try {
for (SqlFieldsQuery qry : maxQrys) {
List<List<?>> res = cache.cache.query(qry).getAll();
assertEquals(1, res.size());
Integer m = (Integer) res.get(0).get(0);
assertTrue(m == null || m % 10 == 0);
}
for (SqlFieldsQuery qry : minQrys) {
List<List<?>> res = cache.cache.query(qry).getAll();
assertEquals(1, res.size());
Integer m = (Integer) res.get(0).get(0);
assertTrue(m == null || m % 10 == 1);
}
} finally {
cache.readUnlock();
}
}
}
};
int srvs;
int clients;
if (singleNode) {
srvs = 1;
clients = 0;
} else {
srvs = 4;
clients = 2;
}
readWriteTest(null, srvs, clients, 0, DFLT_PARTITION_COUNT, writers, readers, DFLT_TEST_TIME, new InitIndexing(Integer.class, MvccTestSqlIndexValue.class), null, writer, reader);
}
Aggregations