Search in sources :

Example 86 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatePartition.

/**
     * @param atomicityMode Cache atomicity mode.
     * @throws Exception If failed.
     */
private void concurrentUpdatePartition(CacheAtomicityMode atomicityMode) throws Exception {
    Ignite srv = startGrid(0);
    client = true;
    Ignite client = startGrid(1);
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setAtomicityMode(atomicityMode);
    IgniteCache clientCache = client.createCache(ccfg);
    final AtomicInteger evtCnt = new AtomicInteger();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
            for (CacheEntryEvent evt : evts) {
                assertNotNull(evt.getKey());
                assertNotNull(evt.getValue());
                evtCnt.incrementAndGet();
            }
        }
    });
    clientCache.query(qry);
    Affinity<Integer> aff = srv.affinity(DEFAULT_CACHE_NAME);
    final List<Integer> keys = new ArrayList<>();
    final int KEYS = 10;
    for (int i = 0; i < 100_000; i++) {
        if (aff.partition(i) == 0) {
            keys.add(i);
            if (keys.size() == KEYS)
                break;
        }
    }
    assertEquals(KEYS, keys.size());
    final int THREADS = 10;
    final int UPDATES = 1000;
    final IgniteCache<Object, Object> srvCache = srv.cache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 15; i++) {
        log.info("Iteration: " + i);
        GridTestUtils.runMultiThreaded(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < UPDATES; i++) srvCache.put(keys.get(rnd.nextInt(KEYS)), i);
                return null;
            }
        }, THREADS, "update");
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                log.info("Events: " + evtCnt.get());
                return evtCnt.get() >= THREADS * UPDATES;
            }
        }, 5000);
        assertEquals(THREADS * UPDATES, evtCnt.get());
        evtCnt.set(0);
    }
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 87 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class GridCancelledJobsMetricsSelfTest method testCancelledJobs.

/**
 * @throws Exception If failed.
 */
public void testCancelledJobs() throws Exception {
    final Ignite ignite = G.ignite(getTestIgniteInstanceName());
    Collection<ComputeTaskFuture<?>> futs = new ArrayList<>();
    for (int i = 1; i <= 10; i++) futs.add(ignite.compute().executeAsync(CancelledTask.class, null));
    // Wait to be sure that metrics were updated.
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return ignite.cluster().localNode().metrics().getTotalCancelledJobs() > 0;
        }
    }, 5000);
    colSpi.externalCollision();
    for (ComputeTaskFuture<?> fut : futs) {
        try {
            fut.get();
            assert false : "Job was not interrupted.";
        } catch (IgniteException e) {
            if (e.hasCause(InterruptedException.class))
                throw new IgniteCheckedException("Test run has been interrupted.", e);
            info("Caught expected exception: " + e.getMessage());
        }
    }
    // Job was cancelled and now we need to calculate metrics.
    int totalCancelledJobs = ignite.cluster().localNode().metrics().getTotalCancelledJobs();
    assert totalCancelledJobs == 10 : "Metrics were not updated. Expected 10 got " + totalCancelledJobs;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteException(org.apache.ignite.IgniteException) ComputeTaskFuture(org.apache.ignite.compute.ComputeTaskFuture) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite)

Example 88 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class GridEventStorageCheckAllEventsSelfTest method startTimestamp.

/**
 * Returns timestamp at the method call moment, but sleeps before return,
 * to allow pass {@link IgniteUtils#currentTimeMillis()}.
 *
 * @return Call timestamp.
 * @throws Exception If failed.
 */
private long startTimestamp() throws Exception {
    final long tstamp = U.currentTimeMillis();
    Thread.sleep(20);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return U.currentTimeMillis() > tstamp;
        }
    }, 5000);
    assert U.currentTimeMillis() > tstamp;
    return U.currentTimeMillis();
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate)

Example 89 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class GridProjectionForCachesSelfTest method testProjectionForDefaultCache.

/**
 * @throws Exception If failed.
 */
public void testProjectionForDefaultCache() throws Exception {
    final ClusterGroup prj = ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME);
    assertNotNull(prj);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return prj.nodes().size() == 3;
        }
    }, 5000);
    assertEquals(3, prj.nodes().size());
    assertTrue(prj.nodes().contains(grid(0).localNode()));
    assertFalse(prj.nodes().contains(grid(1).localNode()));
    assertTrue(prj.nodes().contains(grid(2).localNode()));
    assertTrue(prj.nodes().contains(grid(3).localNode()));
    assertFalse(prj.nodes().contains(grid(4).localNode()));
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) ClusterGroup(org.apache.ignite.cluster.ClusterGroup)

Example 90 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class IgniteClientReconnectStreamerTest method testStreamerReconnect.

/**
 * @throws Exception If failed.
 */
public void testStreamerReconnect() throws Exception {
    final Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    Ignite srv = clientRouter(client);
    final IgniteCache<Object, Object> srvCache = srv.cache(CACHE_NAME);
    IgniteDataStreamer<Integer, Integer> streamer = client.dataStreamer(CACHE_NAME);
    for (int i = 0; i < 50; i++) streamer.addData(i, i);
    streamer.flush();
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return srvCache.localSize() == 50;
        }
    }, 2000L);
    assertEquals(50, srvCache.localSize());
    reconnectClientNode(client, srv, new Runnable() {

        @Override
        public void run() {
            try {
                client.dataStreamer(CACHE_NAME);
                fail();
            } catch (IgniteClientDisconnectedException e) {
                assertNotNull(e.reconnectFuture());
            }
        }
    });
    checkStreamerClosed(streamer);
    streamer = client.dataStreamer(CACHE_NAME);
    for (int i = 50; i < 100; i++) streamer.addData(i, i);
    streamer.flush();
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return srvCache.localSize() == 100;
        }
    }, 2000L);
    assertEquals(100, srvCache.localSize());
    streamer.close();
    streamer.future().get(2, TimeUnit.SECONDS);
    srvCache.removeAll();
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) Ignite(org.apache.ignite.Ignite)

Aggregations

GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)175 Ignite (org.apache.ignite.Ignite)81 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)42 ClusterNode (org.apache.ignite.cluster.ClusterNode)32 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)27 IgniteException (org.apache.ignite.IgniteException)22 CountDownLatch (java.util.concurrent.CountDownLatch)20 IgniteKernal (org.apache.ignite.internal.IgniteKernal)19 ArrayList (java.util.ArrayList)18 IgniteCache (org.apache.ignite.IgniteCache)16 Map (java.util.Map)15 Transaction (org.apache.ignite.transactions.Transaction)15 IgniteEx (org.apache.ignite.internal.IgniteEx)14 Duration (javax.cache.expiry.Duration)13 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)13 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)12 HashMap (java.util.HashMap)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 TouchedExpiryPolicy (javax.cache.expiry.TouchedExpiryPolicy)11 Event (org.apache.ignite.events.Event)11