Search in sources :

Example 41 with Ignite

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

the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedSwapUnswapLong.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TooBroadScope" })
public void testMultiThreadedSwapUnswapLong() 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, Long> c = cache(Integer.class, Long.class);
    assertEquals(0, g.cache(DEFAULT_CACHE_NAME).localSize());
    assertEquals(0, c.query(new SqlQuery(Long.class, "1 = 1")).getAll().size());
    Random rnd = new Random();
    for (int i = 0; i < keyCnt; i += 1 + rnd.nextInt(3)) {
        c.put(i, (long) 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, (long) 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(Long.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)

Example 42 with Ignite

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

the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedSwapUnswapString.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TooBroadScope" })
public void testMultiThreadedSwapUnswapString() 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, String> c = cache(Integer.class, String.class);
    assertEquals(0, g.cache(DEFAULT_CACHE_NAME).localSize());
    assertEquals(0, c.query(new SqlQuery(String.class, "1 = 1")).getAll().size());
    Random rnd = new Random();
    for (int i = 0; i < keyCnt; i += 1 + rnd.nextInt(3)) {
        c.put(i, 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()) {
                switch(rnd.nextInt(5)) {
                    case 0:
                        c.put(rnd.nextInt(keyCnt), String.valueOf(rnd.nextInt(valCnt)));
                        break;
                    case 1:
                        if (evictsEnabled())
                            c.localEvict(Arrays.asList(rnd.nextInt(keyCnt)));
                        break;
                    case 2:
                        c.remove(rnd.nextInt(keyCnt));
                        break;
                    case 3:
                        c.get(rnd.nextInt(keyCnt));
                        break;
                    case 4:
                        int from = rnd.nextInt(valCnt);
                        c.query(new SqlQuery(String.class, "_val between ? and ?").setArgs(String.valueOf(from), String.valueOf(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)

Example 43 with Ignite

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

the class IgniteClientReconnectCacheQueriesFailoverTest method testReconnectCacheQueries.

/**
 * @throws Exception If failed.
 */
public void testReconnectCacheQueries() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCache<Integer, Person> cache = client.cache(DEFAULT_CACHE_NAME);
    assertNotNull(cache);
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            SqlQuery<Integer, Person> sqlQry = new SqlQuery<>(Person.class, "where id > 1");
            try {
                assertEquals(9999, cache.query(sqlQry).getAll().size());
            } catch (CacheException e) {
                if (e.getCause() instanceof IgniteClientDisconnectedException)
                    throw e;
                else
                    log.info("Ignore error: " + e);
            }
            try {
                SqlFieldsQuery fieldsQry = new SqlFieldsQuery("select avg(p.id) from Person p");
                List<List<?>> res = cache.query(fieldsQry).getAll();
                assertEquals(1, res.size());
                Integer avg = (Integer) res.get(0).get(0);
                assertEquals(5_000, avg.intValue());
            } catch (CacheException e) {
                if (e.getCause() instanceof IgniteClientDisconnectedException)
                    throw e;
                else
                    log.info("Ignore error: " + e);
            }
            return null;
        }
    });
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) CacheException(javax.cache.CacheException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Ignite(org.apache.ignite.Ignite) List(java.util.List)

Example 44 with Ignite

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

the class IgniteClientReconnectCacheQueriesFailoverTest method testReconnectScanQuery.

/**
 * @throws Exception If failed.
 */
public void testReconnectScanQuery() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCache<Integer, Person> cache = client.cache(DEFAULT_CACHE_NAME);
    assertNotNull(cache);
    final Affinity<Integer> aff = client.affinity(DEFAULT_CACHE_NAME);
    final Map<Integer, Integer> partMap = new HashMap<>();
    for (int i = 0; i < aff.partitions(); i++) partMap.put(i, 0);
    for (int i = 0; i <= 10_000; i++) {
        Integer part = aff.partition(i);
        Integer size = partMap.get(part);
        partMap.put(part, size + 1);
    }
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ScanQuery<Integer, Person> qry = new ScanQuery<>(new IgniteBiPredicate<Integer, Person>() {

                @Override
                public boolean apply(Integer key, Person val) {
                    return val.getId() % 2 == 1;
                }
            });
            assertEquals(5000, cache.query(qry).getAll().size());
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            Integer part = rnd.nextInt(0, aff.partitions());
            qry = new ScanQuery<>(part);
            assertEquals((int) partMap.get(part), cache.query(qry).getAll().size());
            return null;
        }
    });
}
Also used : HashMap(java.util.HashMap) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) CacheException(javax.cache.CacheException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 45 with Ignite

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

the class IgniteClientReconnectQueriesTest method testReconnectQueryInProgress.

/**
 * @throws Exception If failed.
 */
public void testReconnectQueryInProgress() throws Exception {
    Ignite cln = grid(serverCount());
    assertTrue(cln.cluster().localNode().isClient());
    final Ignite srv = clientRouter(cln);
    final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE);
    clnCache.put(1, new Person(1, "name1", "surname1"));
    clnCache.put(2, new Person(2, "name2", "surname2"));
    clnCache.put(3, new Person(3, "name3", "surname3"));
    blockMessage(GridQueryNextPageResponse.class);
    final SqlQuery<Integer, Person> qry = new SqlQuery<>(Person.class, "_key <> 0");
    qry.setPageSize(1);
    final QueryCursor<Cache.Entry<Integer, Person>> cur1 = clnCache.query(qry);
    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                cur1.getAll();
            } catch (CacheException e) {
                checkAndWait(e);
                return true;
            }
            return false;
        }
    });
    // Check that client waiting operation.
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            return fut.get(200);
        }
    }, IgniteFutureTimeoutCheckedException.class, null);
    assertNotDone(fut);
    unblockMessage();
    reconnectClientNode(cln, srv, null);
    assertTrue((Boolean) fut.get(2, SECONDS));
    QueryCursor<Cache.Entry<Integer, Person>> cur2 = clnCache.query(qry);
    assertEquals(3, cur2.getAll().size());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) CacheException(javax.cache.CacheException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Ignite(org.apache.ignite.Ignite)

Aggregations

Ignite (org.apache.ignite.Ignite)2007 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)358 CountDownLatch (java.util.concurrent.CountDownLatch)238 IgniteCache (org.apache.ignite.IgniteCache)234 IgniteException (org.apache.ignite.IgniteException)216 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)215 Transaction (org.apache.ignite.transactions.Transaction)194 ArrayList (java.util.ArrayList)177 ClusterNode (org.apache.ignite.cluster.ClusterNode)152 UUID (java.util.UUID)137 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)135 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)128 CacheException (javax.cache.CacheException)112 Event (org.apache.ignite.events.Event)112 HashMap (java.util.HashMap)105 List (java.util.List)89 IgniteEx (org.apache.ignite.internal.IgniteEx)85 Map (java.util.Map)84 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)81 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)78