Search in sources :

Example 6 with GridInClosure3

use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.

the class CacheMvccTransactionsTest method doImplicitPartsScanTest.

/**
 * @param srvs Number of server nodes.
 * @param clients Number of client nodes.
 * @param cacheBackups Number of cache backups.
 * @param cacheParts Number of cache partitions.
 * @throws Exception If failed.
 */
private void doImplicitPartsScanTest(final int srvs, final int clients, int cacheBackups, int cacheParts) throws Exception {
    final int KEYS_PER_PART = 20;
    final int writers = 4;
    final int readers = 4;
    Map<Integer, List<Integer>> keysByParts = new HashMap<>();
    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();
            for (int i = 0; i < cacheParts; i++) {
                List<Integer> keys = new ArrayList<>();
                keysByParts.put(i, keys);
            }
            Affinity aff = affinity(cache);
            int cntr = 0;
            int key = 0;
            while (cntr < KEYS_PER_PART * cacheParts) {
                int part = aff.partition(key);
                List<Integer> keys = keysByParts.get(part);
                if (keys.size() < KEYS_PER_PART) {
                    keys.add(key);
                    cntr++;
                }
                key++;
            }
            try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                for (List<Integer> keys : keysByParts.values()) for (Integer k : keys) cache.put(k, new MvccTestAccount(0, 1));
                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();
            while (!stop.get()) {
                int part = rnd.nextInt(cacheParts);
                List<Integer> partKeys = keysByParts.get(part);
                TestCache<Integer, MvccTestAccount> cache = randomCache(caches, rnd);
                IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
                Integer k1 = partKeys.get(rnd.nextInt(KEYS_PER_PART));
                Integer k2 = partKeys.get(rnd.nextInt(KEYS_PER_PART));
                while (k1.equals(k2)) k2 = partKeys.get(rnd.nextInt(KEYS_PER_PART));
                if (k1 > k2) {
                    int tmp = k1;
                    k1 = k2;
                    k2 = tmp;
                }
                TreeSet<Integer> keys = new TreeSet<>();
                keys.add(k1);
                keys.add(k2);
                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    Map<Integer, MvccTestAccount> accs = cache.cache.getAll(keys);
                    MvccTestAccount acc1 = accs.get(k1);
                    MvccTestAccount acc2 = accs.get(k2);
                    assertNotNull(acc1);
                    assertNotNull(acc2);
                    cache.cache.put(k1, new MvccTestAccount(acc1.val + 1, acc1.updateCnt + 1));
                    cache.cache.put(k2, new MvccTestAccount(acc2.val - 1, acc2.updateCnt + 1));
                    tx.commit();
                } catch (CacheException ex) {
                    MvccFeatureChecker.assertMvccWriteConflict(ex);
                } finally {
                    cache.readUnlock();
                }
            }
        }
    };
    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();
            while (!stop.get()) {
                int part = rnd.nextInt(cacheParts);
                TestCache<Integer, Integer> cache = randomCache(caches, rnd);
                try {
                    Affinity aff = affinity(cache.cache);
                    ScanQuery<Integer, MvccTestAccount> qry = new ScanQuery<>(part);
                    List<Cache.Entry<Integer, MvccTestAccount>> res = cache.cache.query(qry).getAll();
                    int sum = 0;
                    for (Cache.Entry<Integer, MvccTestAccount> entry : res) {
                        Integer key = entry.getKey();
                        MvccTestAccount acc = entry.getValue();
                        assertEquals(part, aff.partition(key));
                        sum += acc.val;
                    }
                    assertEquals(0, sum);
                } finally {
                    cache.readUnlock();
                }
                if (idx == 0) {
                    cache = randomCache(caches, rnd);
                    try {
                        ScanQuery<Integer, MvccTestAccount> qry = new ScanQuery<>();
                        List<Cache.Entry<Integer, MvccTestAccount>> res = cache.cache.query(qry).getAll();
                        int sum = 0;
                        for (Cache.Entry<Integer, MvccTestAccount> entry : res) {
                            Integer key = entry.getKey();
                            MvccTestAccount acc = entry.getValue();
                            sum += acc.val;
                        }
                        assertEquals(0, sum);
                    } finally {
                        cache.readUnlock();
                    }
                }
            }
        }
    };
    readWriteTest(null, srvs, clients, cacheBackups, cacheParts, writers, readers, SCALED_10SEC_TEST_TIME, null, init, writer, reader);
}
Also used : HashMap(java.util.HashMap) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteTransactions(org.apache.ignite.IgniteTransactions) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) MutableEntry(javax.cache.processor.MutableEntry) GridCacheMapEntry(org.apache.ignite.internal.processors.cache.GridCacheMapEntry) TreeSet(java.util.TreeSet) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) List(java.util.List) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) Affinity(org.apache.ignite.cache.affinity.Affinity) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 7 with GridInClosure3

use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.

the class GridNearTxLocal method checkMissed.

/**
 * @param cacheCtx Cache context.
 * @param topVer Topology version.
 * @param map Return map.
 * @param missedMap Missed keys.
 * @param deserializeBinary Deserialize binary flag.
 * @param skipVals Skip values flag.
 * @param keepCacheObjects Keep cache objects flag.
 * @param skipStore Skip store flag.
 * @param readRepairStrategy Read Repair strategy.
 * @param expiryPlc Expiry policy.
 * @return Loaded key-value pairs.
 */
private <K, V> IgniteInternalFuture<Map<K, V>> checkMissed(final GridCacheContext cacheCtx, final AffinityTopologyVersion topVer, final Map<K, V> map, final Map<KeyCacheObject, GridCacheVersion> missedMap, final boolean deserializeBinary, final boolean skipVals, final boolean keepCacheObjects, final boolean skipStore, final boolean recovery, final ReadRepairStrategy readRepairStrategy, final boolean needVer, final ExpiryPolicy expiryPlc) {
    if (log.isDebugEnabled())
        log.debug("Loading missed values for missed map: " + missedMap);
    final boolean needReadVer = (serializable() && optimistic()) || needVer;
    return new GridEmbeddedFuture<>(new C2<Void, Exception, Map<K, V>>() {

        @Override
        public Map<K, V> apply(Void v, Exception e) {
            if (e != null)
                throw new GridClosureException(e);
            if (isRollbackOnly()) {
                if (timedOut())
                    throw new GridClosureException(new IgniteTxTimeoutCheckedException("Transaction has been timed out: " + GridNearTxLocal.this));
                else
                    throw new GridClosureException(new IgniteTxRollbackCheckedException("Transaction has been rolled back: " + GridNearTxLocal.this));
            }
            return map;
        }
    }, loadMissing(cacheCtx, topVer, !skipStore, false, missedMap.keySet(), skipVals, needReadVer, !deserializeBinary, recovery, readRepairStrategy, expiryPlc, new GridInClosure3<KeyCacheObject, Object, GridCacheVersion>() {

        @Override
        public void apply(KeyCacheObject key, Object val, GridCacheVersion loadVer) {
            CacheObject cacheVal = cacheCtx.toCacheObject(val);
            CacheObject visibleVal = cacheVal;
            IgniteTxKey txKey = cacheCtx.txKey(key);
            IgniteTxEntry txEntry = entry(txKey);
            if (txEntry != null) {
                if (!readCommitted())
                    txEntry.readValue(cacheVal);
                if (!F.isEmpty(txEntry.entryProcessors()))
                    visibleVal = txEntry.applyEntryProcessors(visibleVal);
            }
            assert txEntry != null || readCommitted() || skipVals;
            GridCacheEntryEx e = txEntry == null ? entryEx(cacheCtx, txKey, topVer) : txEntry.cached();
            if (readCommitted() || skipVals) {
                e.touch();
                if (visibleVal != null) {
                    cacheCtx.addResult(map, key, visibleVal, skipVals, keepCacheObjects, deserializeBinary, false, needVer ? loadVer : null, 0, 0, U.deploymentClassLoader(cctx.kernalContext(), deploymentLdrId));
                }
            } else {
                assert txEntry != null;
                txEntry.setAndMarkValid(cacheVal);
                if (needReadVer) {
                    assert loadVer != null;
                    txEntry.entryReadVersion(loadVer);
                }
                if (visibleVal != null) {
                    cacheCtx.addResult(map, key, visibleVal, skipVals, keepCacheObjects, deserializeBinary, false, needVer ? loadVer : null, 0, 0, U.deploymentClassLoader(cctx.kernalContext(), deploymentLdrId));
                }
            }
        }
    }));
}
Also used : GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) IgniteConsistencyViolationException(org.apache.ignite.internal.processors.cache.distributed.near.consistency.IgniteConsistencyViolationException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridEmbeddedFuture(org.apache.ignite.internal.util.future.GridEmbeddedFuture) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 8 with GridInClosure3

use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.

the class CacheMvccTransactionsTest method txReadsSnapshot.

/**
 * @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.
 */
private void txReadsSnapshot(final int srvs, final int clients, int cacheBackups, int cacheParts, ReadMode readMode) throws Exception {
    final int ACCOUNTS = 20;
    final int ACCOUNT_START_VAL = 1000;
    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, MvccTestAccount> accounts = new HashMap<>();
            for (int i = 0; i < ACCOUNTS; i++) accounts.put(i, new MvccTestAccount(ACCOUNT_START_VAL, 1));
            try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                cache.putAll(accounts);
                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, MvccTestAccount> cache = randomCache(caches, rnd);
                try {
                    IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
                    cnt++;
                    Integer id1 = rnd.nextInt(ACCOUNTS);
                    Integer id2 = rnd.nextInt(ACCOUNTS);
                    while (id1.equals(id2)) id2 = rnd.nextInt(ACCOUNTS);
                    if (id1 > id2) {
                        int tmp = id1;
                        id1 = id2;
                        id2 = tmp;
                    }
                    Set<Integer> keys = new HashSet<>();
                    keys.add(id1);
                    keys.add(id2);
                    try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        MvccTestAccount a1;
                        MvccTestAccount a2;
                        Map<Integer, MvccTestAccount> accounts = checkAndGetAll(false, cache.cache, keys, readMode);
                        a1 = accounts.get(id1);
                        a2 = accounts.get(id2);
                        assertNotNull(a1);
                        assertNotNull(a2);
                        cache.cache.put(id1, new MvccTestAccount(a1.val + 1, 1));
                        cache.cache.put(id2, new MvccTestAccount(a2.val - 1, 1));
                        tx.commit();
                    } catch (CacheException ex) {
                        MvccFeatureChecker.assertMvccWriteConflict(ex);
                    }
                } 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();
            int cnt = 0;
            while (!stop.get()) {
                TestCache<Integer, MvccTestAccount> cache = randomCache(caches, rnd);
                IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
                Map<Integer, MvccTestAccount> accounts = new HashMap<>();
                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    int remaining = ACCOUNTS;
                    do {
                        int readCnt = rnd.nextInt(remaining) + 1;
                        Set<Integer> readKeys = new TreeSet<>();
                        for (int i = 0; i < readCnt; i++) readKeys.add(accounts.size() + i);
                        Map<Integer, MvccTestAccount> readRes = checkAndGetAll(false, cache.cache, readKeys, readMode);
                        assertEquals(readCnt, readRes.size());
                        accounts.putAll(readRes);
                        remaining = ACCOUNTS - accounts.size();
                    } while (remaining > 0);
                    validateSum(accounts);
                    tx.commit();
                    cnt++;
                } finally {
                    cache.readUnlock();
                }
            }
            info("Reader finished, txs: " + cnt);
        }

        /**
         * @param accounts Read accounts.
         */
        private void validateSum(Map<Integer, MvccTestAccount> accounts) {
            int sum = 0;
            for (int i = 0; i < ACCOUNTS; i++) {
                MvccTestAccount account = accounts.get(i);
                assertNotNull(account);
                sum += account.val;
            }
            assertEquals(ACCOUNTS * ACCOUNT_START_VAL, sum);
        }
    };
    readWriteTest(null, srvs, clients, cacheBackups, cacheParts, writers, readers, DFLT_TEST_TIME, null, init, writer, reader);
}
Also used : HashMap(java.util.HashMap) CacheException(javax.cache.CacheException) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) TreeSet(java.util.TreeSet) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Ignite(org.apache.ignite.Ignite) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 9 with GridInClosure3

use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.

the class CacheMvccTransactionsTest method operationsSequenceConsistency.

/**
 * @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.
 */
private void operationsSequenceConsistency(final int srvs, final int clients, int cacheBackups, int cacheParts, ReadMode readMode) throws Exception {
    final int writers = 4;
    final int readers = 4;
    final AtomicInteger keyCntr = new AtomicInteger();
    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, Value> cache = randomCache(caches, rnd);
                try {
                    IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
                    Integer key = keyCntr.incrementAndGet();
                    try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        cache.cache.put(key, new Value(idx, cnt++));
                        tx.commit();
                    }
                    if (key > 100_000)
                        break;
                } 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();
            Set keys = new HashSet();
            while (!stop.get()) {
                TestCache<Integer, Value> cache = randomCache(caches, rnd);
                try {
                    Map<Integer, TreeSet<Integer>> vals = new HashMap<>();
                    switch(readMode) {
                        case SCAN:
                            for (Cache.Entry<Integer, Value> e : cache.cache) {
                                Value val = e.getValue();
                                assertNotNull(val);
                                TreeSet<Integer> cntrs = vals.get(val.key);
                                if (cntrs == null)
                                    vals.put(val.key, cntrs = new TreeSet<>());
                                boolean add = cntrs.add(val.cnt);
                                assertTrue(add);
                            }
                            break;
                        case GET:
                            for (int i = keys.size(); i < keyCntr.get(); i++) keys.add(i);
                            Iterable<Map.Entry<Integer, Value>> entries = cache.cache.getAll(keys).entrySet();
                            for (Map.Entry<Integer, Value> e : entries) {
                                Value val = e.getValue();
                                assertNotNull(val);
                                TreeSet<Integer> cntrs = vals.get(val.key);
                                if (cntrs == null)
                                    vals.put(val.key, cntrs = new TreeSet<>());
                                boolean add = cntrs.add(val.cnt);
                                assertTrue(add);
                            }
                            break;
                        default:
                            fail("Unsupported read mode: " + readMode.name() + '.');
                    }
                    for (TreeSet<Integer> readCntrs : vals.values()) {
                        for (int i = 0; i < readCntrs.size(); i++) assertTrue(readCntrs.contains(i));
                    }
                } finally {
                    cache.readUnlock();
                }
            }
        }
    };
    readWriteTest(null, srvs, clients, cacheBackups, cacheParts, writers, readers, SCALED_10SEC_TEST_TIME, null, null, writer, reader);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) IgniteTransactions(org.apache.ignite.IgniteTransactions) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) MutableEntry(javax.cache.processor.MutableEntry) GridCacheMapEntry(org.apache.ignite.internal.processors.cache.GridCacheMapEntry) TreeSet(java.util.TreeSet) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) List(java.util.List) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 10 with GridInClosure3

use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.

the class CacheMvccTransactionsTest method testNodesRestartNoHang.

/**
 * @throws Exception If failed.
 */
@Ignore("https://issues.apache.org/jira/browse/IGNITE-12041")
@Test
public void testNodesRestartNoHang() throws Exception {
    final int srvs = 4;
    final int clients = 4;
    final int writers = 6;
    final int readers = 2;
    final int KEYS = 100_000;
    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();
            Map<Integer, Integer> map = new TreeMap<>();
            Set<Integer> keys = new LinkedHashSet();
            int cnt = 0;
            while (!stop.get()) {
                int keysCnt = rnd.nextInt(32) + 1;
                while (keys.size() < keysCnt) {
                    int key = rnd.nextInt(KEYS);
                    if (keys.add(key))
                        map.put(key, cnt);
                }
                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 (rnd.nextBoolean()) {
                            Map<Integer, Integer> res = checkAndGetAll(false, cache.cache, map.keySet(), rnd.nextBoolean() ? GET : SCAN);
                            assertNotNull(res);
                        }
                        cache.cache.putAll(map);
                        tx.commit();
                    } catch (Exception e) {
                        if (!X.hasCause(e, ClusterTopologyException.class))
                            MvccFeatureChecker.assertMvccWriteConflict(e);
                    }
                } finally {
                    cache.readUnlock();
                    keys.clear();
                    map.clear();
                }
                cnt++;
            }
            info("Writer done, 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()) {
                int keyCnt = rnd.nextInt(64) + 1;
                while (keys.size() < keyCnt) keys.add(rnd.nextInt(KEYS));
                TestCache<Integer, Integer> cache = randomCache(caches, rnd);
                Map<Integer, Integer> map;
                try {
                    map = checkAndGetAll(false, cache.cache, keys, rnd.nextBoolean() ? GET : SCAN);
                    assertNotNull(map);
                } finally {
                    cache.readUnlock();
                }
                keys.clear();
            }
        }
    };
    readWriteTest(RestartMode.RESTART_RND_SRV, srvs, clients, 1, 256, writers, readers, DFLT_TEST_TIME, null, null, writer, reader);
    for (Ignite node : G.allGrids()) checkActiveQueriesCleanup(node);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TreeMap(java.util.TreeMap) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) List(java.util.List) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

GridInClosure3 (org.apache.ignite.internal.util.lang.GridInClosure3)17 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)14 ArrayList (java.util.ArrayList)13 List (java.util.List)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 HashMap (java.util.HashMap)12 CacheException (javax.cache.CacheException)11 Ignite (org.apache.ignite.Ignite)11 IgniteTransactions (org.apache.ignite.IgniteTransactions)10 Transaction (org.apache.ignite.transactions.Transaction)10 Map (java.util.Map)9 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)9 LinkedHashSet (java.util.LinkedHashSet)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 IgniteCache (org.apache.ignite.IgniteCache)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)7 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)7 HashSet (java.util.HashSet)6 LinkedHashMap (java.util.LinkedHashMap)6