Search in sources :

Example 96 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class IgniteCacheQueryNodeRestartDistributedJoinSelfTest method restarts.

/**
     * @param broadcastQry If {@code true} tests broadcast query.
     * @throws Exception If failed.
     */
private void restarts(final boolean broadcastQry) throws Exception {
    int duration = 90 * 1000;
    int qryThreadNum = 4;
    // 4 + 2 = 6 nodes
    int restartThreadsNum = 2;
    final int nodeLifeTime = 4000;
    final int logFreq = 100;
    final AtomicIntegerArray locks = new AtomicIntegerArray(totalNodes);
    SqlFieldsQuery qry0;
    if (broadcastQry)
        qry0 = new SqlFieldsQuery(QRY_0_BROADCAST).setDistributedJoins(true).setEnforceJoinOrder(true);
    else
        qry0 = new SqlFieldsQuery(QRY_0).setDistributedJoins(true);
    String plan = queryPlan(grid(0).cache("pu"), qry0);
    X.println("Plan1: " + plan);
    assertEquals(broadcastQry, plan.contains("batched:broadcast"));
    final List<List<?>> pRes = grid(0).cache("pu").query(qry0).getAll();
    Thread.sleep(3000);
    assertEquals(pRes, grid(0).cache("pu").query(qry0).getAll());
    final SqlFieldsQuery qry1;
    if (broadcastQry)
        qry1 = new SqlFieldsQuery(QRY_1_BROADCAST).setDistributedJoins(true).setEnforceJoinOrder(true);
    else
        qry1 = new SqlFieldsQuery(QRY_1).setDistributedJoins(true);
    plan = queryPlan(grid(0).cache("co"), qry1);
    X.println("Plan2: " + plan);
    assertEquals(broadcastQry, plan.contains("batched:broadcast"));
    final List<List<?>> rRes = grid(0).cache("co").query(qry1).getAll();
    assertFalse(pRes.isEmpty());
    assertFalse(rRes.isEmpty());
    final AtomicInteger qryCnt = new AtomicInteger();
    final AtomicBoolean qrysDone = new AtomicBoolean();
    final AtomicBoolean fail = new AtomicBoolean();
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            GridRandom rnd = new GridRandom();
            try {
                while (!qrysDone.get()) {
                    int g;
                    do {
                        g = rnd.nextInt(locks.length());
                        if (fail.get())
                            return;
                    } while (!locks.compareAndSet(g, 0, 1));
                    if (rnd.nextBoolean()) {
                        IgniteCache<?, ?> cache = grid(g).cache("pu");
                        SqlFieldsQuery qry;
                        if (broadcastQry)
                            qry = new SqlFieldsQuery(QRY_0_BROADCAST).setDistributedJoins(true).setEnforceJoinOrder(true);
                        else
                            qry = new SqlFieldsQuery(QRY_0).setDistributedJoins(true);
                        boolean smallPageSize = rnd.nextBoolean();
                        qry.setPageSize(smallPageSize ? 30 : 1000);
                        try {
                            assertEquals(pRes, cache.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.");
                            }
                        }
                    } else {
                        IgniteCache<?, ?> cache = grid(g).cache("co");
                        assertEquals(rRes, cache.query(qry1).getAll());
                    }
                    locks.set(g, 0);
                    int c = qryCnt.incrementAndGet();
                    if (c % logFreq == 0)
                        info("Executed queries: " + c);
                }
            } catch (Throwable e) {
                e.printStackTrace();
                error("Got exception: " + e.getMessage());
                fail.set(true);
            }
        }
    }, 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 {
            try {
                GridRandom rnd = new GridRandom();
                while (!restartsDone.get()) {
                    int g;
                    do {
                        g = rnd.nextInt(locks.length());
                        if (fail.get())
                            return null;
                    } 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;
            } catch (Throwable e) {
                e.printStackTrace();
                return true;
            }
        }
    }, restartThreadsNum, "restart-thread");
    Thread.sleep(duration);
    info("Stopping...");
    restartsDone.set(true);
    qrysDone.set(true);
    fut2.get();
    fut1.get();
    if (fail.get())
        fail("See message above");
    info("Stopped.");
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) CacheException(javax.cache.CacheException) IgniteCache(org.apache.ignite.IgniteCache) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) CAX(org.apache.ignite.internal.util.typedef.CAX)

Example 97 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class IgniteCacheQueryNodeRestartSelfTest method testRestarts.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
@SuppressWarnings({ "TooBroadScope" })
public void testRestarts() throws Exception {
    int duration = 60 * 1000;
    int qryThreadNum = 10;
    final long nodeLifeTime = 2 * 1000;
    final int logFreq = 50;
    final IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    assert cache != null;
    for (int i = 0; i < KEY_CNT; i++) cache.put(i, i);
    assertEquals(KEY_CNT, cache.size());
    final AtomicInteger qryCnt = new AtomicInteger();
    final AtomicBoolean done = new AtomicBoolean();
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (!done.get()) {
                Collection<Cache.Entry<Integer, Integer>> res = cache.query(new SqlQuery<Integer, Integer>(Integer.class, "true")).getAll();
                Set<Integer> keys = new HashSet<>();
                for (Cache.Entry<Integer, Integer> entry : res) keys.add(entry.getKey());
                if (KEY_CNT > keys.size()) {
                    for (int i = 0; i < KEY_CNT; i++) {
                        if (!keys.contains(i))
                            assertEquals(Integer.valueOf(i), cache.get(i));
                    }
                    fail("res size: " + res.size());
                }
                assertEquals(KEY_CNT, keys.size());
                int c = qryCnt.incrementAndGet();
                if (c % logFreq == 0)
                    info("Executed queries: " + c);
            }
        }
    }, qryThreadNum, "query-thread");
    final AtomicInteger restartCnt = new AtomicInteger();
    CollectingEventListener lsnr = new CollectingEventListener();
    for (int i = 0; i < GRID_CNT; i++) grid(i).events().localListen(lsnr, EventType.EVT_CACHE_REBALANCE_STOPPED);
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Object>() {

        @SuppressWarnings({ "BusyWait" })
        @Override
        public Object call() throws Exception {
            while (!done.get()) {
                int idx = GRID_CNT;
                startGrid(idx);
                Thread.sleep(nodeLifeTime);
                stopGrid(idx);
                int c = restartCnt.incrementAndGet();
                if (c % logFreq == 0)
                    info("Node restarts: " + c);
            }
            return true;
        }
    }, 1, "restart-thread");
    Thread.sleep(duration);
    info("Stopping..");
    done.set(true);
    fut2.get();
    info("Restarts stopped.");
    fut1.get();
    info("Queries stopped.");
    info("Awaiting rebalance events [restartCnt=" + restartCnt.get() + ']');
    boolean success = lsnr.awaitEvents(GRID_CNT * 2 * restartCnt.get(), 15000);
    for (int i = 0; i < GRID_CNT; i++) grid(i).events().stopLocalListen(lsnr, EventType.EVT_CACHE_REBALANCE_STOPPED);
    assert success;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) CAX(org.apache.ignite.internal.util.typedef.CAX) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 98 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class IgniteCacheQueryNodeRestartSelfTest2 method testRestarts.

/**
     * @throws Exception If failed.
     */
public void testRestarts() throws Exception {
    int duration = 90 * 1000;
    int qryThreadNum = 4;
    // 4 + 2 = 6 nodes
    int restartThreadsNum = 2;
    final int nodeLifeTime = 2 * 1000;
    final int logFreq = 10;
    startGridsMultiThreaded(GRID_CNT);
    final AtomicIntegerArray locks = new AtomicIntegerArray(GRID_CNT);
    fillCaches();
    final List<List<?>> pRes = grid(0).cache("pu").query(new SqlFieldsQuery(PARTITIONED_QRY)).getAll();
    Thread.sleep(3000);
    assertEquals(pRes, grid(0).cache("pu").query(new SqlFieldsQuery(PARTITIONED_QRY)).getAll());
    final List<List<?>> rRes = grid(0).cache("co").query(new SqlFieldsQuery(REPLICATED_QRY)).getAll();
    assertFalse(pRes.isEmpty());
    assertFalse(rRes.isEmpty());
    final AtomicInteger qryCnt = new AtomicInteger();
    final AtomicBoolean qrysDone = new AtomicBoolean();
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            GridRandom rnd = new GridRandom();
            while (!qrysDone.get()) {
                int g;
                do {
                    g = rnd.nextInt(locks.length());
                } while (!locks.compareAndSet(g, 0, 1));
                try {
                    if (rnd.nextBoolean()) {
                        // Partitioned query.
                        IgniteCache<?, ?> cache = grid(g).cache("pu");
                        SqlFieldsQuery qry = new SqlFieldsQuery(PARTITIONED_QRY);
                        boolean smallPageSize = rnd.nextBoolean();
                        if (smallPageSize)
                            qry.setPageSize(3);
                        try {
                            assertEquals(pRes, cache.query(qry).getAll());
                        } catch (CacheException e) {
                            // Interruptions are expected here.
                            if (e.getCause() instanceof IgniteInterruptedCheckedException)
                                continue;
                            if (e.getCause() instanceof QueryCancelledException)
                                fail("Retry is expected");
                            if (!smallPageSize)
                                e.printStackTrace();
                            assertTrue("On large page size must retry.", smallPageSize);
                            boolean failedOnRemoteFetch = false;
                            boolean failedOnInterruption = false;
                            for (Throwable th = e; th != null; th = th.getCause()) {
                                if (th instanceof InterruptedException) {
                                    failedOnInterruption = true;
                                    break;
                                }
                                if (!(th instanceof CacheException))
                                    continue;
                                if (th.getMessage() != null && th.getMessage().startsWith("Failed to fetch data from node:")) {
                                    failedOnRemoteFetch = true;
                                    break;
                                }
                            }
                            // Interruptions are expected here.
                            if (failedOnInterruption)
                                continue;
                            if (!failedOnRemoteFetch) {
                                e.printStackTrace();
                                fail("Must fail inside of GridResultPage.fetchNextPage or subclass.");
                            }
                        }
                    } else {
                        // Replicated query.
                        IgniteCache<?, ?> cache = grid(g).cache("co");
                        assertEquals(rRes, cache.query(new SqlFieldsQuery(REPLICATED_QRY)).getAll());
                    }
                } finally {
                    // Clearing lock in final handler to avoid endless loop if exception is thrown.
                    locks.set(g, 0);
                    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));
                try {
                    log.info("Stop node: " + g);
                    stopGrid(g);
                    Thread.sleep(rnd.nextInt(nodeLifeTime));
                    log.info("Start node: " + g);
                    startGrid(g);
                    Thread.sleep(rnd.nextInt(nodeLifeTime));
                } finally {
                    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);
    // Query thread can stuck in next page waiting loop because all nodes are left.
    try {
        fut1.get(5_000);
    } catch (IgniteFutureTimeoutCheckedException ignored) {
        fut1.cancel();
    }
    info("Queries stopped.");
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) CacheException(javax.cache.CacheException) IgniteCache(org.apache.ignite.IgniteCache) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) CacheException(javax.cache.CacheException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) List(java.util.List) CAX(org.apache.ignite.internal.util.typedef.CAX) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException)

Example 99 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class IgniteCrossCachesJoinsQueryTest method initCachesData.

/**
     *
     */
private void initCachesData() {
    IgniteCache accCache = ignite(0).cache(ACC_CACHE_NAME);
    for (Account account : data.accounts) accCache.put(account.key(useCollocatedData()), account);
    IgniteCache personCache = ignite(0).cache(PERSON_CACHE_NAME);
    for (Person person : data.persons) personCache.put(person.key(useCollocatedData()), person);
    IgniteCache orgCache = ignite(0).cache(ORG_CACHE_NAME);
    for (Organization org : data.orgs) orgCache.put(org.id, org);
}
Also used : IgniteCache(org.apache.ignite.IgniteCache)

Example 100 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class IgniteClientReconnectCacheTest method testReconnectClusterRestartMultinode.

/**
     * @throws Exception If failed.
     */
public void testReconnectClusterRestartMultinode() throws Exception {
    clientMode = true;
    final int CLIENTS = 5;
    CountDownLatch disconnectLatch = new CountDownLatch(CLIENTS);
    CountDownLatch reconnectLatch = new CountDownLatch(CLIENTS);
    List<IgniteCache> caches = new ArrayList<>();
    for (int i = 0; i < CLIENTS; i++) {
        Ignite client = startGrid(SRV_CNT + i);
        addListener(client, disconnectLatch, reconnectLatch);
        IgniteCache cache = client.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
        assertNotNull(cache);
        caches.add(cache);
    }
    for (int i = 0; i < SRV_CNT; i++) stopGrid(i);
    assertTrue(disconnectLatch.await(30_000, MILLISECONDS));
    log.info("Restart servers.");
    clientMode = false;
    startGridsMultiThreaded(0, SRV_CNT);
    assertTrue(reconnectLatch.await(30_000, MILLISECONDS));
    for (final IgniteCache clientCache : caches) {
        GridTestUtils.assertThrows(log, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                return clientCache.get(1);
            }
        }, IllegalStateException.class, null);
    }
    for (int i = 0; i < SRV_CNT + CLIENTS; i++) {
        Ignite ignite = grid(i);
        ClusterGroup grp = ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME);
        assertEquals(0, grp.nodes().size());
        grp = ignite.cluster().forClientNodes(DEFAULT_CACHE_NAME);
        assertEquals(0, grp.nodes().size());
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) CacheException(javax.cache.CacheException)

Aggregations

IgniteCache (org.apache.ignite.IgniteCache)298 Ignite (org.apache.ignite.Ignite)161 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)104 Cache (javax.cache.Cache)72 Transaction (org.apache.ignite.transactions.Transaction)71 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)51 IgniteException (org.apache.ignite.IgniteException)50 ArrayList (java.util.ArrayList)47 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)40 CacheException (javax.cache.CacheException)37 Map (java.util.Map)34 List (java.util.List)33 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)33 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)31 HashMap (java.util.HashMap)27 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)26 CyclicBarrier (java.util.concurrent.CyclicBarrier)25 IgniteKernal (org.apache.ignite.internal.IgniteKernal)24 Random (java.util.Random)23 IgniteTransactions (org.apache.ignite.IgniteTransactions)22