Search in sources :

Example 56 with Ignite

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

the class IgniteCacheReplicatedQuerySelfTest method testClientOnlyNode.

/**
 * @throws Exception If failed.
 */
public void testClientOnlyNode() throws Exception {
    try {
        Ignite g = startGrid("client");
        IgniteCache<Integer, Integer> c = jcache(g, Integer.class, Integer.class);
        for (int i = 0; i < 10; i++) c.put(i, i);
        // Client cache should be empty.
        assertEquals(0, c.localSize());
        Collection<Cache.Entry<Integer, Integer>> res = c.query(new SqlQuery<Integer, Integer>(Integer.class, "_key >= 5 order by _key")).getAll();
        assertEquals(5, res.size());
        int i = 5;
        for (Cache.Entry<Integer, Integer> e : res) {
            assertEquals(i, e.getKey().intValue());
            assertEquals(i, e.getValue().intValue());
            i++;
        }
    } finally {
        stopGrid("client");
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 57 with Ignite

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

the class ClosureServiceClientsNodesTest method testDefaultClosure.

/**
 * @throws Exception If failed.
 */
public void testDefaultClosure() throws Exception {
    Set<String> srvNames = new HashSet<>(NODES_CNT - 1);
    for (int i = 1; i < NODES_CNT; ++i) srvNames.add(getTestIgniteInstanceName(i));
    for (int i = 0; i < NODES_CNT; i++) {
        log.info("Iteration: " + i);
        Ignite ignite = grid(i);
        Collection<String> res = ignite.compute().broadcast(new IgniteCallable<String>() {

            @IgniteInstanceResource
            Ignite ignite;

            @Override
            public String call() throws Exception {
                assertFalse(ignite.configuration().isClientMode());
                return ignite.name();
            }
        });
        assertEquals(res.size(), NODES_CNT - 1);
        for (String name : res) assertTrue(srvNames.contains(name));
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet)

Example 58 with Ignite

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

the class ClosureServiceClientsNodesTest method testCustomClosure.

/**
 * @throws Exception If failed.
 */
public void testCustomClosure() throws Exception {
    for (int i = 0; i < NODES_CNT; i++) {
        log.info("Iteration: " + i);
        Ignite ignite = grid(i);
        Collection<String> res = ignite.compute(ignite.cluster().forPredicate(F.<ClusterNode>alwaysTrue())).broadcast(new IgniteCallable<String>() {

            @IgniteInstanceResource
            Ignite ignite;

            @Override
            public String call() throws Exception {
                return ignite.name();
            }
        });
        assertEquals(NODES_CNT, res.size());
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Ignite(org.apache.ignite.Ignite)

Example 59 with Ignite

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

the class DynamicColumnsAbstractConcurrentSelfTest method testConcurrentPutRemove.

/**
 * PUT/REMOVE data from cache and add/drop column concurrently.
 *
 * @throws Exception If failed,
 */
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(node, key), val(node, val));
                else
                    cache.remove(key(node, 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(srv1, 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 = ((IgniteEx) 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) 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)

Example 60 with Ignite

use of org.apache.ignite.Ignite 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.
 */
public void testQueryConsistencyMultithreaded() throws Exception {
    // 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, 5000);
    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()) {
                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(5000, res.size());
            }
            return null;
        }
    }, 8);
    Thread.sleep(TEST_DUR);
    stopped.set(true);
    // Make sure nothing hanged.
    fut.get();
    qryFut.get();
}
Also used : SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) 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)

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