Search in sources :

Example 86 with Cache

use of javax.cache.Cache in project ignite by apache.

the class IgniteCacheQueryNodeRestartSelfTest method testRestarts.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testRestarts() throws Exception {
    int duration = 60 * 1000;
    int qryThreadNum = 10;
    final int logFreq = 50;
    final IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    grid(0).cluster().baselineAutoAdjustEnabled(false);
    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 = createRestartAction(done, restartCnt);
    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(countRebalances(GRID_CNT, 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) 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) Test(org.junit.Test) GridCacheAbstractSelfTest(org.apache.ignite.internal.processors.cache.GridCacheAbstractSelfTest)

Example 87 with Cache

use of javax.cache.Cache in project ignite by apache.

the class DynamicColumnsAbstractConcurrentSelfTest method testQueryConsistencyMultithreaded.

/**
 * Make sure that contended operations on the same table from different nodes do not hang when we issue both
 * ADD/DROP COLUMN and SELECT statements.
 *
 * @throws Exception If failed.
 */
@Test
public void testQueryConsistencyMultithreaded() throws Exception {
    final int KEY_COUNT = 5000;
    // Start complex topology.
    ignitionStart(serverConfiguration(1));
    ignitionStart(serverConfiguration(2));
    ignitionStart(serverConfiguration(3, true));
    Ignite cli = ignitionStart(clientConfiguration(4));
    createSqlCache(cli);
    run(cli, createSql);
    put(cli, 0, KEY_COUNT);
    final AtomicBoolean stopped = new AtomicBoolean();
    final AtomicInteger dynColCnt = new AtomicInteger();
    final GridConcurrentHashSet<Integer> fields = new GridConcurrentHashSet<>();
    IgniteInternalFuture fut = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!stopped.get()) {
                Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
                IgniteInternalFuture fut;
                int fieldNum = ThreadLocalRandom.current().nextInt(0, dynColCnt.get() + 1);
                boolean removed = fields.remove(fieldNum);
                if (removed)
                    fut = dropCols(node, QueryUtils.DFLT_SCHEMA, "newCol" + fieldNum);
                else {
                    fieldNum = dynColCnt.getAndIncrement();
                    fut = addCols(node, QueryUtils.DFLT_SCHEMA, c("newCol" + fieldNum, Integer.class.getName()));
                }
                try {
                    fut.get();
                    if (!removed)
                        fields.add(fieldNum);
                } catch (SchemaOperationException e) {
                // No-op.
                } catch (Exception e) {
                    fail("Unexpected exception: " + e);
                }
            }
            return null;
        }
    }, 1);
    IgniteInternalFuture qryFut = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!stopped.get()) {
                try {
                    Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
                    IgniteCache<BinaryObject, BinaryObject> cache = node.cache(CACHE_NAME).withKeepBinary();
                    String valTypeName = ((IgniteEx) node).context().query().types(CACHE_NAME).iterator().next().valueTypeName();
                    List<Cache.Entry<BinaryObject, BinaryObject>> res = cache.query(new SqlQuery<BinaryObject, BinaryObject>(valTypeName, "from " + TBL_NAME)).getAll();
                    assertEquals(KEY_COUNT, res.size());
                } catch (Exception e) {
                    // Swallow retry exceptions.
                    if (X.cause(e, QueryRetryException.class) == null)
                        throw e;
                }
            }
            return null;
        }
    }, 8);
    Thread.sleep(TEST_DUR);
    stopped.set(true);
    // Make sure nothing hanged.
    fut.get();
    qryFut.get();
}
Also used : QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BinaryObject(org.apache.ignite.binary.BinaryObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) List(java.util.List) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) Test(org.junit.Test) IgniteClientReconnectAbstractTest(org.apache.ignite.internal.IgniteClientReconnectAbstractTest)

Example 88 with Cache

use of javax.cache.Cache in project ignite by apache.

the class DynamicColumnsAbstractConcurrentSelfTest method testConcurrentPutRemove.

/**
 * PUT/REMOVE data from cache and add/drop column concurrently.
 *
 * @throws Exception If failed,
 */
@Test
public void testConcurrentPutRemove() throws Exception {
    CountDownLatch finishLatch = new CountDownLatch(4);
    // Start several nodes.
    IgniteEx srv1 = ignitionStart(serverConfiguration(1), finishLatch);
    ignitionStart(serverConfiguration(2), finishLatch);
    ignitionStart(serverConfiguration(3), finishLatch);
    ignitionStart(serverConfiguration(4), finishLatch);
    awaitPartitionMapExchange();
    createSqlCache(srv1);
    run(srv1, createSql4Cols);
    // Start data change operations from several threads.
    final AtomicBoolean stopped = new AtomicBoolean();
    IgniteInternalFuture updateFut = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!stopped.get()) {
                Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
                int key = ThreadLocalRandom.current().nextInt(0, LARGE_CACHE_SIZE);
                int val = ThreadLocalRandom.current().nextInt();
                IgniteCache<Object, BinaryObject> cache = node.cache(CACHE_NAME);
                if (ThreadLocalRandom.current().nextBoolean())
                    cache.put(key(key), val(node, val));
                else
                    cache.remove(key(key));
            }
            return null;
        }
    }, 4);
    // Let some to arrive.
    Thread.sleep(500L);
    addCols(srv1, QueryUtils.DFLT_SCHEMA, c("v", Integer.class.getName())).get();
    dropCols(srv1, QueryUtils.DFLT_SCHEMA, "CITY").get();
    // Stop updates once index is ready.
    stopped.set(true);
    updateFut.get();
    finishLatch.await();
    // Make sure new column is there.
    checkTableState(srv1, QueryUtils.DFLT_SCHEMA, TBL_NAME, c("AGE", Integer.class.getName()), c("v", Integer.class.getName()));
    run(srv1, "update person set \"v\" = case when mod(id, 2) <> 0 then substring(name, 7, length(name) - 6) " + "else null end");
    // Get expected values.
    Set<Integer> expKeys = new HashSet<>();
    IgniteCache<Object, BinaryObject> cache = srv1.cache(CACHE_NAME).withKeepBinary();
    for (int i = 0; i < LARGE_CACHE_SIZE; i++) {
        Object key = key(i);
        BinaryObject val = cache.get(key);
        if (val != null) {
            int id = (Integer) key;
            assertEquals(i, id);
            if (id % 2 != 0)
                expKeys.add(i);
        }
    }
    String valTypeName = (srv1).context().query().types(CACHE_NAME).iterator().next().valueTypeName();
    // Validate query result.
    for (Ignite node : Ignition.allGrids()) {
        IgniteCache<Object, BinaryObject> nodeCache = node.cache(CACHE_NAME).withKeepBinary();
        SqlQuery qry = new SqlQuery(valTypeName, "from " + TBL_NAME + " where mod(id, 2) <> 0");
        List<Cache.Entry<Object, BinaryObject>> res = nodeCache.query(qry).getAll();
        assertEquals("Cache size mismatch [exp=" + expKeys.size() + ", actual=" + res.size() + ']', expKeys.size(), res.size());
        for (Cache.Entry<Object, BinaryObject> entry : res) {
            int key = (Integer) entry.getKey();
            int v = entry.getValue().field("v");
            String name = entry.getValue().field("NAME");
            assertTrue("Expected key is not in result set: " + key, expKeys.contains(key));
            assertEquals(Integer.parseInt(name.substring(6)), v);
        }
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) Test(org.junit.Test) IgniteClientReconnectAbstractTest(org.apache.ignite.internal.IgniteClientReconnectAbstractTest)

Example 89 with Cache

use of javax.cache.Cache in project ignite by apache.

the class CacheScanQueryFailoverTest method queryCachesWithFailedPredicates.

/**
 * @param ignite Ignite instance.
 * @param configs Cache configurations.
 */
private void queryCachesWithFailedPredicates(Ignite ignite, CacheConfiguration... configs) {
    if (configs == null)
        return;
    for (CacheConfiguration cfg : configs) {
        IgniteCache cache = ignite.getOrCreateCache(cfg);
        populateCache(ignite, cache.getName());
        // Check that exception propagates to client from filter failure.
        GridTestUtils.assertThrowsAnyCause(log, () -> {
            try (QueryCursor<Cache.Entry<Integer, BinaryObject>> cursor = cache.withKeepBinary().query(new ScanQuery<>(filter))) {
                for (Cache.Entry<Integer, BinaryObject> entry : cursor) log.info("Entry " + entry.toString());
            }
            return null;
        }, Error.class, "Poison pill");
        // Check that exception propagates to client from transformer failure.
        GridTestUtils.assertThrowsAnyCause(log, () -> {
            try (QueryCursor<Cache.Entry<Integer, BinaryObject>> cursor = cache.withKeepBinary().query(new ScanQuery<>(), transformer)) {
                for (Cache.Entry<Integer, BinaryObject> entry : cursor) log.info("Entry " + entry.toString());
            }
            return null;
        }, Error.class, "Poison pill");
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteCache(org.apache.ignite.IgniteCache) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 90 with Cache

use of javax.cache.Cache in project ignite by apache.

the class IndexingSpiQuerySelfTest method testNonBinaryIndexingSpi.

/**
 * @throws Exception If failed.
 */
@Test
public void testNonBinaryIndexingSpi() throws Exception {
    System.setProperty(IgniteSystemProperties.IGNITE_UNWRAP_BINARY_FOR_INDEXING_SPI, "true");
    try {
        indexingSpi = new MyIndexingSpi();
        Ignite ignite = startGrid(0);
        CacheConfiguration<PersonKey, Person> ccfg = cacheConfiguration(DEFAULT_CACHE_NAME);
        IgniteCache<PersonKey, Person> cache = ignite.createCache(ccfg);
        for (int i = 0; i < 10; i++) {
            PersonKey key = new PersonKey(i);
            cache.put(key, new Person("John Doe " + i));
        }
        QueryCursor<Cache.Entry<PersonKey, Person>> cursor = cache.query(new SpiQuery<PersonKey, Person>().setArgs(new PersonKey(2), new PersonKey(5)));
        for (Cache.Entry<PersonKey, Person> entry : cursor) System.out.println(entry);
        cache.remove(new PersonKey(9));
    } finally {
        System.clearProperty(IgniteSystemProperties.IGNITE_UNWRAP_BINARY_FOR_INDEXING_SPI);
    }
}
Also used : SpiQuery(org.apache.ignite.cache.query.SpiQuery) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

Cache (javax.cache.Cache)271 IgniteCache (org.apache.ignite.IgniteCache)157 Test (org.junit.Test)130 Ignite (org.apache.ignite.Ignite)101 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)68 List (java.util.List)62 Map (java.util.Map)56 ScanQuery (org.apache.ignite.cache.query.ScanQuery)54 ArrayList (java.util.ArrayList)51 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)45 QueryCursor (org.apache.ignite.cache.query.QueryCursor)43 HashMap (java.util.HashMap)41 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)41 Collection (java.util.Collection)38 HashSet (java.util.HashSet)38 CacheManager (javax.cache.CacheManager)38 CacheException (javax.cache.CacheException)35 IgniteEx (org.apache.ignite.internal.IgniteEx)35 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)32 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)32