Search in sources :

Example 26 with CAX

use of org.apache.ignite.internal.util.typedef.CAX in project ignite by apache.

the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedSqlFieldsQuery.

/**
 * SqlFieldsQuery paging mechanics stress test
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testMultiThreadedSqlFieldsQuery() throws Throwable {
    int threadCnt = 16;
    // set resultSet size bigger than page size
    final int keyCnt = 1100;
    final int logMod = 5000;
    final Ignite g = grid(0);
    // Put test values into cache.
    final IgniteCache<Integer, TestValue> c = cache(Integer.class, TestValue.class);
    for (int i = 0; i < keyCnt; i++) c.put(i, new TestValue(i));
    final AtomicInteger cnt = new AtomicInteger();
    final AtomicBoolean done = new AtomicBoolean();
    IgniteInternalFuture<?> fut = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            int iter = 0;
            while (!done.get() && !Thread.currentThread().isInterrupted()) {
                iter++;
                List<List<?>> entries = c.query(new SqlFieldsQuery("SELECT * from TestValue").setPageSize(100)).getAll();
                assert entries != null;
                assertEquals("Entries count is not as expected on iteration: " + iter, keyCnt, entries.size());
                if (cnt.incrementAndGet() % logMod == 0) {
                    GridCacheQueryManager<Object, Object> qryMgr = ((IgniteKernal) g).internalCache(DEFAULT_CACHE_NAME).context().queries();
                    assert qryMgr != null;
                    qryMgr.printMemoryStats();
                }
            }
        }
    }, threadCnt);
    Thread.sleep(DURATION);
    done.set(true);
    fut.get();
}
Also used : SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheQueryManager(org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager) Ignite(org.apache.ignite.Ignite) List(java.util.List) CAX(org.apache.ignite.internal.util.typedef.CAX) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Test(org.junit.Test)

Example 27 with CAX

use of org.apache.ignite.internal.util.typedef.CAX in project ignite by apache.

the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedSwapUnswapLongString.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testMultiThreadedSwapUnswapLongString() throws Exception {
    int threadCnt = 50;
    final int keyCnt = 2000;
    final int valCnt = 10000;
    final Ignite g = grid(0);
    // Put test values into cache.
    final IgniteCache<Integer, Object> c = cache(Integer.class, Object.class);
    assertEquals(0, g.cache(DEFAULT_CACHE_NAME).size());
    assertEquals(0, c.query(new SqlQuery(Object.class, "1 = 1")).getAll().size());
    Random rnd = new Random();
    for (int i = 0; i < keyCnt; i += 1 + rnd.nextInt(3)) {
        c.put(i, rnd.nextBoolean() ? (long) rnd.nextInt(valCnt) : String.valueOf(rnd.nextInt(valCnt)));
        if (evictsEnabled() && rnd.nextBoolean())
            c.localEvict(Arrays.asList(i));
    }
    final AtomicBoolean done = new AtomicBoolean();
    IgniteInternalFuture<?> fut = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            Random rnd = new Random();
            while (!done.get()) {
                int key = rnd.nextInt(keyCnt);
                switch(rnd.nextInt(5)) {
                    case 0:
                        c.put(key, rnd.nextBoolean() ? (long) rnd.nextInt(valCnt) : String.valueOf(rnd.nextInt(valCnt)));
                        break;
                    case 1:
                        if (evictsEnabled())
                            c.localEvict(Arrays.asList(key));
                        break;
                    case 2:
                        c.remove(key);
                        break;
                    case 3:
                        c.get(key);
                        break;
                    case 4:
                        int from = rnd.nextInt(valCnt);
                        c.query(new SqlQuery(Object.class, "_val between ? and ?").setArgs(from, from + 250)).getAll();
                }
            }
        }
    }, threadCnt);
    Thread.sleep(DURATION);
    done.set(true);
    fut.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Random(java.util.Random) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Test(org.junit.Test)

Example 28 with CAX

use of org.apache.ignite.internal.util.typedef.CAX in project ignite by apache.

the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedSwapUnswapObject.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testMultiThreadedSwapUnswapObject() throws Exception {
    int threadCnt = 50;
    final int keyCnt = 4000;
    final int valCnt = 10000;
    final Ignite g = grid(0);
    // Put test values into cache.
    final IgniteCache<Integer, TestValue> c = cache(Integer.class, TestValue.class);
    assertEquals(0, g.cache(DEFAULT_CACHE_NAME).localSize());
    assertEquals(0, c.query(new SqlQuery(TestValue.class, "1 = 1")).getAll().size());
    Random rnd = new Random();
    for (int i = 0; i < keyCnt; i += 1 + rnd.nextInt(3)) {
        c.put(i, new TestValue(rnd.nextInt(valCnt)));
        if (evictsEnabled() && rnd.nextBoolean())
            c.localEvict(Arrays.asList(i));
    }
    final AtomicBoolean done = new AtomicBoolean();
    IgniteInternalFuture<?> fut = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            Random rnd = new Random();
            while (!done.get()) {
                int key = rnd.nextInt(keyCnt);
                switch(rnd.nextInt(5)) {
                    case 0:
                        c.put(key, new TestValue(rnd.nextInt(valCnt)));
                        break;
                    case 1:
                        if (evictsEnabled())
                            c.localEvict(Arrays.asList(key));
                        break;
                    case 2:
                        c.remove(key);
                        break;
                    case 3:
                        c.get(key);
                        break;
                    case 4:
                        int from = rnd.nextInt(valCnt);
                        c.query(new SqlQuery(TestValue.class, "TestValue.val between ? and ?").setArgs(from, from + 250)).getAll();
                }
            }
        }
    }, threadCnt);
    Thread.sleep(DURATION);
    done.set(true);
    fut.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Random(java.util.Random) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Test(org.junit.Test)

Example 29 with CAX

use of org.apache.ignite.internal.util.typedef.CAX in project ignite by apache.

the class IgniteCacheClientQueryReplicatedNodeRestartSelfTest method testRestarts.

/**
 * @throws Exception If failed.
 */
@Test
public void testRestarts() throws Exception {
    int duration = 90 * 1000;
    int qryThreadNum = 5;
    // 2 of 4 data nodes
    int restartThreadsNum = 2;
    final int nodeLifeTime = 2 * 1000;
    final int logFreq = 10;
    startGridsMultiThreaded(GRID_CNT);
    // The last is client only.
    final AtomicIntegerArray locks = new AtomicIntegerArray(GRID_CNT - 1);
    fillCaches();
    final List<List<?>> pRes = grid(0).cache("pu").query(new SqlFieldsQuery(QRY)).getAll();
    Thread.sleep(3000);
    assertEquals(pRes, grid(0).cache("pu").query(new SqlFieldsQuery(QRY)).getAll());
    assertFalse(pRes.isEmpty());
    final AtomicInteger qryCnt = new AtomicInteger();
    final AtomicBoolean qrysDone = new AtomicBoolean();
    final List<Integer> cacheSize = new ArrayList<>(4);
    for (int i = 0; i < GRID_CNT - 1; i++) {
        int j = 0;
        for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
            IgniteCache<?, ?> cache = grid(i).cache(cacheName);
            assertClient(cache, false);
            if (i == 0)
                cacheSize.add(cache.size());
            else
                assertEquals(cacheSize.get(j++).intValue(), cache.size());
        }
    }
    int j = 0;
    for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
        IgniteCache<?, ?> cache = grid(GRID_CNT - 1).cache(cacheName);
        assertClient(cache, true);
        assertEquals(cacheSize.get(j++).intValue(), cache.size());
    }
    final IgniteCache<?, ?> clientCache = grid(GRID_CNT - 1).cache("pu");
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            GridRandom rnd = new GridRandom();
            while (!qrysDone.get()) {
                SqlFieldsQuery qry = new SqlFieldsQuery(QRY);
                boolean smallPageSize = rnd.nextBoolean();
                if (smallPageSize)
                    qry.setPageSize(3);
                List<List<?>> res;
                try {
                    res = clientCache.query(qry).getAll();
                } catch (CacheException e) {
                    assertTrue("On large page size must retry.", smallPageSize);
                    boolean failedOnRemoteFetch = false;
                    for (Throwable th = e; th != null; th = th.getCause()) {
                        if (!(th instanceof CacheException))
                            continue;
                        if (th.getMessage() != null && th.getMessage().startsWith("Failed to fetch data from node:")) {
                            failedOnRemoteFetch = true;
                            break;
                        }
                    }
                    if (!failedOnRemoteFetch) {
                        e.printStackTrace();
                        fail("Must fail inside of GridResultPage.fetchNextPage or subclass.");
                    }
                    res = FAKE;
                }
                if (res != FAKE && !res.equals(pRes)) {
                    int j = 0;
                    // Check for data loss.
                    for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
                        assertEquals(cacheName, cacheSize.get(j++).intValue(), grid(GRID_CNT - 1).cache(cacheName).size());
                    }
                    // Fail with nice message.
                    assertEquals(pRes, res);
                }
                int c = qryCnt.incrementAndGet();
                if (c % logFreq == 0)
                    info("Executed queries: " + c);
            }
        }
    }, qryThreadNum, "query-thread");
    final AtomicInteger restartCnt = new AtomicInteger();
    final AtomicBoolean restartsDone = new AtomicBoolean();
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Object>() {

        @SuppressWarnings({ "BusyWait" })
        @Override
        public Object call() throws Exception {
            GridRandom rnd = new GridRandom();
            while (!restartsDone.get()) {
                int g;
                do {
                    g = rnd.nextInt(locks.length());
                } while (!locks.compareAndSet(g, 0, -1));
                log.info("Stop node: " + g);
                stopGrid(g);
                Thread.sleep(rnd.nextInt(nodeLifeTime));
                log.info("Start node: " + g);
                startGrid(g);
                Thread.sleep(rnd.nextInt(nodeLifeTime));
                locks.set(g, 0);
                int c = restartCnt.incrementAndGet();
                if (c % logFreq == 0)
                    info("Node restarts: " + c);
            }
            return true;
        }
    }, restartThreadsNum, "restart-thread");
    Thread.sleep(duration);
    info("Stopping..");
    restartsDone.set(true);
    fut2.get();
    info("Restarts stopped.");
    qrysDone.set(true);
    fut1.get();
    info("Queries stopped.");
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CacheException(javax.cache.CacheException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CAX(org.apache.ignite.internal.util.typedef.CAX) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

CAX (org.apache.ignite.internal.util.typedef.CAX)29 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)20 Test (org.junit.Test)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Ignite (org.apache.ignite.Ignite)14 IgniteCache (org.apache.ignite.IgniteCache)11 SqlQuery (org.apache.ignite.cache.query.SqlQuery)8 AbstractIndexingCommonTest (org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest)8 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)8 Random (java.util.Random)7 List (java.util.List)6 Collection (java.util.Collection)5 Cache (javax.cache.Cache)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)4 GridCacheQueryManager (org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager)4 ArrayList (java.util.ArrayList)3 AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)3 CacheException (javax.cache.CacheException)3