Search in sources :

Example 1 with GridCacheMvccManager

use of org.apache.ignite.internal.processors.cache.GridCacheMvccManager in project ignite by apache.

the class CacheGetReadFromBackupFailoverTest method testFailover.

/**
 * @throws Exception If failed.
 */
@Test
public void testFailover() throws Exception {
    Ignite ignite = ignite(0);
    ignite.cluster().active(true);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    try (IgniteDataStreamer<Object, Object> stmr = ignite.dataStreamer(TX_CACHE)) {
        for (int i = 0; i < KEYS_CNT; i++) stmr.addData(i, rnd.nextLong());
    }
    try (IgniteDataStreamer<Object, Object> stmr = ignite.dataStreamer(ATOMIC_CACHE)) {
        for (int i = 0; i < KEYS_CNT; i++) stmr.addData(i, rnd.nextLong());
    }
    AtomicInteger idx = new AtomicInteger(-1);
    AtomicInteger successGet = new AtomicInteger();
    IgniteInternalFuture fut = GridTestUtils.runAsync(() -> {
        ThreadLocalRandom rnd0 = ThreadLocalRandom.current();
        while (!stop.get()) {
            Ignite ig = null;
            while (ig == null) {
                int n = rnd0.nextInt(gridCount());
                if (idx.get() != n) {
                    try {
                        ig = ignite(n);
                    } catch (IgniteIllegalStateException e) {
                    // No-op.
                    }
                }
            }
            try {
                if (rnd.nextBoolean()) {
                    ig.cache(TX_CACHE).get(rnd0.nextLong(KEYS_CNT));
                    ig.cache(ATOMIC_CACHE).get(rnd0.nextLong(KEYS_CNT));
                } else {
                    ig.cache(TX_CACHE).getAll(rnd.longs(16, 0, KEYS_CNT).boxed().collect(Collectors.toSet()));
                    ig.cache(ATOMIC_CACHE).getAll(rnd.longs(16, 0, KEYS_CNT).boxed().collect(Collectors.toSet()));
                }
                successGet.incrementAndGet();
            } catch (CacheException e) {
                if (!X.hasCause(e, NodeStoppingException.class))
                    throw e;
            }
        }
    }, "load-thread");
    long startTime = System.currentTimeMillis();
    while (System.currentTimeMillis() - startTime < 30 * 1000L) {
        int idx0 = idx.get();
        if (idx0 >= 0)
            startGrid(idx0);
        U.sleep(500);
        int next = rnd.nextInt(gridCount());
        idx.set(next);
        stopGrid(next);
        U.sleep(500);
    }
    stop.set(true);
    while (true) {
        try {
            fut.get(10_000);
            break;
        } catch (IgniteFutureTimeoutCheckedException e) {
            for (Ignite i : G.allGrids()) {
                IgniteEx ex = (IgniteEx) i;
                log.info(">>>> " + ex.context().localNodeId());
                GridCacheMvccManager mvcc = ex.context().cache().context().mvcc();
                for (GridCacheFuture<?> fut0 : mvcc.activeFutures()) {
                    log.info("activeFut - " + fut0);
                }
                for (GridCacheFuture<?> fut0 : mvcc.atomicFutures()) {
                    log.info("atomicFut - " + fut0);
                }
            }
        }
    }
    Assert.assertTrue(String.valueOf(successGet.get()), successGet.get() > 50);
    Throwable e = err.get();
    if (e != null) {
        log.error("Test failed", e);
        fail("Test failed");
    }
}
Also used : GridCacheMvccManager(org.apache.ignite.internal.processors.cache.GridCacheMvccManager) CacheException(javax.cache.CacheException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteIllegalStateException(org.apache.ignite.IgniteIllegalStateException) GridCacheFuture(org.apache.ignite.internal.processors.cache.GridCacheFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CacheException (javax.cache.CacheException)1 Ignite (org.apache.ignite.Ignite)1 IgniteIllegalStateException (org.apache.ignite.IgniteIllegalStateException)1 IgniteEx (org.apache.ignite.internal.IgniteEx)1 IgniteFutureTimeoutCheckedException (org.apache.ignite.internal.IgniteFutureTimeoutCheckedException)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 GridCacheFuture (org.apache.ignite.internal.processors.cache.GridCacheFuture)1 GridCacheMvccManager (org.apache.ignite.internal.processors.cache.GridCacheMvccManager)1 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)1 Test (org.junit.Test)1