Search in sources :

Example 91 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class TxRollbackOnTimeoutTest method testTimeoutRemoval.

/**
 * Tests timeout object cleanup on tx commit.
 *
 * @throws Exception If failed.
 */
public void testTimeoutRemoval() throws Exception {
    IgniteEx client = (IgniteEx) startClient();
    final long TX_TIMEOUT = 250;
    final int modesCnt = 5;
    for (int i = 0; i < modesCnt; i++) testTimeoutRemoval0(grid(0), i, TX_TIMEOUT);
    for (int i = 0; i < modesCnt; i++) testTimeoutRemoval0(client, i, TX_TIMEOUT);
    for (int i = 0; i < modesCnt; i++) testTimeoutRemoval0(grid(0), i, TX_MIN_TIMEOUT);
    for (int i = 0; i < modesCnt; i++) testTimeoutRemoval0(client, i, TX_MIN_TIMEOUT);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    // Repeat with more iterations to make sure everything is cleared.
    for (int i = 0; i < 500; i++) testTimeoutRemoval0(client, rnd.nextInt(modesCnt), TX_MIN_TIMEOUT);
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 92 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class TxDeadlockDetectionNoHangsTest method doTest.

/**
 * @param concurrency Concurrency.
 * @throws IgniteCheckedException If failed.
 */
private void doTest(final TransactionConcurrency concurrency) throws IgniteCheckedException {
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<Long> restartFut = null;
    try {
        restartFut = GridTestUtils.runMultiThreadedAsync(new Runnable() {

            @Override
            public void run() {
                while (!stop.get()) {
                    try {
                        U.sleep(500);
                        startGrid(NODES_CNT);
                        awaitPartitionMapExchange();
                        U.sleep(500);
                        stopGrid(NODES_CNT);
                    } catch (Exception ignored) {
                    // No-op.
                    }
                }
            }
        }, 1, "restart-thread");
        long stopTime = System.currentTimeMillis() + 2 * 60_000L;
        for (int i = 0; System.currentTimeMillis() < stopTime; i++) {
            boolean detectionEnabled = grid(0).context().cache().context().tm().deadlockDetectionEnabled();
            log.info(">>> Iteration " + i + " (detection is " + (detectionEnabled ? "enabled" : "disabled") + ')');
            final AtomicInteger threadCnt = new AtomicInteger();
            IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {

                @Override
                public void run() {
                    int threadNum = threadCnt.getAndIncrement();
                    Ignite ignite = ignite(threadNum % NODES_CNT);
                    IgniteCache<Integer, Integer> cache = ignite.cache(CACHE);
                    try (Transaction tx = ignite.transactions().txStart(concurrency, REPEATABLE_READ, 500, 0)) {
                        ThreadLocalRandom rnd = ThreadLocalRandom.current();
                        for (int i = 0; i < 50; i++) {
                            int key = rnd.nextInt(50);
                            if (log.isDebugEnabled()) {
                                log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode() + ", tx=" + tx + ", key=" + key + ']');
                            }
                            cache.put(key, 0);
                        }
                        tx.commit();
                    } catch (Exception e) {
                        log.info("Ignore error: " + e);
                    }
                }
            }, NODES_CNT * 3, "tx-thread");
            fut.get();
        }
    } finally {
        stop.set(true);
        if (restartFut != null)
            restartFut.get();
        checkDetectionFutures();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteCache(org.apache.ignite.IgniteCache) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 93 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class IgniteCacheClientNodeChangingTopologyTest method multinode.

/**
 * @param atomicityMode Atomicity mode cache.
 * @param testType Test type.
 * @throws Exception If failed.
 */
private void multinode(CacheAtomicityMode atomicityMode, final TestType testType) throws Exception {
    ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setCacheMode(PARTITIONED);
    ccfg.setBackups(1);
    ccfg.setAtomicityMode(atomicityMode);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setRebalanceMode(SYNC);
    final int SRV_CNT = 4;
    for (int i = 0; i < SRV_CNT; i++) startGrid(i);
    final int CLIENT_CNT = 4;
    final List<Ignite> clients = new ArrayList<>();
    client = true;
    for (int i = 0; i < CLIENT_CNT; i++) {
        Ignite ignite = startGrid(SRV_CNT + i);
        assertTrue(ignite.configuration().isClientMode());
        clients.add(ignite);
    }
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicInteger threadIdx = new AtomicInteger(0);
    final int THREADS = CLIENT_CNT * 3;
    final ConcurrentHashSet<Integer> putKeys = new ConcurrentHashSet<>();
    IgniteInternalFuture<?> fut;
    try {
        fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                int clientIdx = threadIdx.getAndIncrement() % CLIENT_CNT;
                Ignite ignite = clients.get(clientIdx);
                assertTrue(ignite.configuration().isClientMode());
                Thread.currentThread().setName("update-thread-" + ignite.name());
                IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
                boolean useTx = testType == TestType.OPTIMISTIC_TX || testType == TestType.OPTIMISTIC_SERIALIZABLE_TX || testType == TestType.PESSIMISTIC_TX;
                if (useTx || testType == TestType.LOCK) {
                    assertEquals(TRANSACTIONAL, cache.getConfiguration(CacheConfiguration.class).getAtomicityMode());
                }
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                int cntr = 0;
                while (!stop.get()) {
                    TreeMap<Integer, Integer> map = new TreeMap<>();
                    for (int i = 0; i < 100; i++) {
                        Integer key = rnd.nextInt(0, 1000);
                        map.put(key, rnd.nextInt());
                    }
                    try {
                        if (testType == TestType.LOCK) {
                            Lock lock = cache.lockAll(map.keySet());
                            lock.lock();
                            lock.unlock();
                        } else {
                            if (useTx) {
                                IgniteTransactions txs = ignite.transactions();
                                TransactionConcurrency concurrency = testType == TestType.PESSIMISTIC_TX ? PESSIMISTIC : OPTIMISTIC;
                                TransactionIsolation isolation = testType == TestType.OPTIMISTIC_SERIALIZABLE_TX ? SERIALIZABLE : REPEATABLE_READ;
                                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                                    cache.putAll(map);
                                    tx.commit();
                                }
                            } else
                                cache.putAll(map);
                            putKeys.addAll(map.keySet());
                        }
                    } catch (CacheException | IgniteException e) {
                        log.info("Operation failed, ignore: " + e);
                    }
                    if (++cntr % 100 == 0)
                        log.info("Iteration: " + cntr);
                    if (updateBarrier != null)
                        updateBarrier.await();
                }
                return null;
            }
        }, THREADS, "update-thread");
        long stopTime = System.currentTimeMillis() + 60_000;
        while (System.currentTimeMillis() < stopTime) {
            boolean restartClient = ThreadLocalRandom.current().nextBoolean();
            Integer idx = null;
            if (restartClient) {
                log.info("Start client node.");
                client = true;
                IgniteEx ignite = startGrid(SRV_CNT + CLIENT_CNT);
                IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
                assertNotNull(cache);
            } else {
                idx = ThreadLocalRandom.current().nextInt(0, SRV_CNT);
                log.info("Stop server node: " + idx);
                stopGrid(idx);
            }
            updateBarrier = new CyclicBarrier(THREADS + 1, new Runnable() {

                @Override
                public void run() {
                    updateBarrier = null;
                }
            });
            try {
                updateBarrier.await(30_000, TimeUnit.MILLISECONDS);
            } catch (TimeoutException ignored) {
                log.error("Failed to wait for update.");
                for (Ignite ignite : G.allGrids()) ((IgniteKernal) ignite).dumpDebugInfo();
                U.dumpThreads(log);
                CyclicBarrier barrier0 = updateBarrier;
                if (barrier0 != null)
                    barrier0.reset();
                fail("Failed to wait for update.");
            }
            U.sleep(500);
            if (restartClient) {
                log.info("Stop client node.");
                stopGrid(SRV_CNT + CLIENT_CNT);
            } else {
                log.info("Start server node: " + idx);
                client = false;
                startGrid(idx);
            }
            updateBarrier = new CyclicBarrier(THREADS + 1, new Runnable() {

                @Override
                public void run() {
                    updateBarrier = null;
                }
            });
            try {
                updateBarrier.await(30_000, TimeUnit.MILLISECONDS);
            } catch (TimeoutException ignored) {
                log.error("Failed to wait for update.");
                for (Ignite ignite : G.allGrids()) ((IgniteKernal) ignite).dumpDebugInfo();
                U.dumpThreads(log);
                CyclicBarrier barrier0 = updateBarrier;
                if (barrier0 != null)
                    barrier0.reset();
                fail("Failed to wait for update.");
            }
            U.sleep(500);
        }
    } finally {
        stop.set(true);
    }
    fut.get(30_000);
    if (testType != TestType.LOCK)
        checkData(null, putKeys, grid(SRV_CNT).cache(DEFAULT_CACHE_NAME), SRV_CNT + CLIENT_CNT);
}
Also used : CacheException(javax.cache.CacheException) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) ArrayList(java.util.ArrayList) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) ConcurrentHashSet(org.eclipse.jetty.util.ConcurrentHashSet) IgniteException(org.apache.ignite.IgniteException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TimeoutException(java.util.concurrent.TimeoutException) IgniteKernal(org.apache.ignite.internal.IgniteKernal) TreeMap(java.util.TreeMap) Lock(java.util.concurrent.locks.Lock) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx)

Example 94 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class IgniteCacheManyClientsTest method manyClientsPutGet.

/**
 * @throws Exception If failed.
 */
private void manyClientsPutGet() throws Throwable {
    client = true;
    final AtomicInteger idx = new AtomicInteger(SRVS);
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicReference<Throwable> err = new AtomicReference<>();
    final int THREADS = 50;
    final CountDownLatch latch = new CountDownLatch(THREADS);
    try {
        IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                boolean counted = false;
                try {
                    int nodeIdx = idx.getAndIncrement();
                    Thread.currentThread().setName("client-thread-node-" + nodeIdx);
                    try (Ignite ignite = startGrid(nodeIdx)) {
                        log.info("Started node: " + ignite.name());
                        assertTrue(ignite.configuration().isClientMode());
                        IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
                        ThreadLocalRandom rnd = ThreadLocalRandom.current();
                        int iter = 0;
                        Integer key = rnd.nextInt(0, 1000);
                        cache.put(key, iter++);
                        assertNotNull(cache.get(key));
                        latch.countDown();
                        counted = true;
                        while (!stop.get() && err.get() == null) {
                            key = rnd.nextInt(0, 1000);
                            cache.put(key, iter++);
                            assertNotNull(cache.get(key));
                            Thread.sleep(1);
                        }
                        log.info("Stopping node: " + ignite.name());
                    }
                    return null;
                } catch (Throwable e) {
                    err.compareAndSet(null, e);
                    log.error("Unexpected error in client thread: " + e, e);
                    throw e;
                } finally {
                    if (!counted)
                        latch.countDown();
                }
            }
        }, THREADS, "client-thread");
        assertTrue(latch.await(getTestTimeout(), TimeUnit.MILLISECONDS));
        log.info("All clients started.");
        Thread.sleep(10_000);
        Throwable err0 = err.get();
        if (err0 != null)
            throw err0;
        boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                try {
                    checkNodes(SRVS + THREADS);
                    return true;
                } catch (AssertionFailedError e) {
                    log.info("Check failed, will retry: " + e);
                }
                return false;
            }
        }, 10_000);
        if (!wait)
            checkNodes(SRVS + THREADS);
        log.info("Stop clients.");
        stop.set(true);
        fut.get();
    } catch (Throwable e) {
        log.error("Unexpected error: " + e, e);
        throw e;
    } finally {
        stop.set(true);
    }
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) AssertionFailedError(junit.framework.AssertionFailedError)

Example 95 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class IgniteCacheMessageRecoveryAbstractTest method testMessageRecovery.

/**
 * @throws Exception If failed.
 */
public void testMessageRecovery() throws Exception {
    final Ignite ignite = grid(0);
    final IgniteCache<Object, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
    Map<Integer, String> map = new HashMap<>();
    for (int i = 0; i < 1000; i++) map.put(i, "0");
    cache.putAll(map);
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Thread.currentThread().setName("update-thread");
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            int iter = 0;
            while (!stop.get()) {
                Map<Integer, String> map = new HashMap<>();
                for (int i = 0; i < 100; i++) map.put(rnd.nextInt(0, 1000), String.valueOf(i));
                cache.putAll(map);
                if (++iter % 100 == 0)
                    log.info("Iteration: " + iter);
            }
            return null;
        }
    });
    try {
        boolean closed = false;
        for (int i = 0; i < 30; i++) {
            Thread.sleep(1000);
            Ignite node0 = ignite(ThreadLocalRandom.current().nextInt(0, GRID_CNT));
            log.info("Close sessions for: " + ignite.name());
            closed |= closeSessions(node0);
        }
        assertTrue(closed);
    } finally {
        stop.set(true);
    }
    fut.get();
}
Also used : HashMap(java.util.HashMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)236 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)73 Ignite (org.apache.ignite.Ignite)65 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 IgniteCache (org.apache.ignite.IgniteCache)44 ArrayList (java.util.ArrayList)34 Test (org.junit.Test)30 IgniteException (org.apache.ignite.IgniteException)27 Transaction (org.apache.ignite.transactions.Transaction)26 CacheException (javax.cache.CacheException)24 HashMap (java.util.HashMap)22 Map (java.util.Map)20 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)17 LongAdder (java.util.concurrent.atomic.LongAdder)15 TreeMap (java.util.TreeMap)14 IgniteTransactions (org.apache.ignite.IgniteTransactions)13 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)13 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 Callable (java.util.concurrent.Callable)11 CountDownLatch (java.util.concurrent.CountDownLatch)11