Search in sources :

Example 26 with IgniteFuture

use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.

the class CachePutEventListenerErrorSelfTest method doTest.

/**
     * @param cacheMode Cache mode.
     * @param atomicityMode Atomicity mode.
     * @throws Exception If failed.
     */
private void doTest(CacheMode cacheMode, CacheAtomicityMode atomicityMode) throws Exception {
    Ignite ignite = grid("client");
    try {
        CacheConfiguration<Integer, Integer> cfg = defaultCacheConfiguration();
        cfg.setName("cache");
        cfg.setCacheMode(cacheMode);
        cfg.setAtomicityMode(atomicityMode);
        IgniteCache<Integer, Integer> cache = ignite.createCache(cfg);
        IgniteFuture f = cache.putAsync(0, 0);
        try {
            f.get(2000);
            assert false : "Exception was not thrown";
        } catch (CacheException e) {
            info("Caught expected exception: " + e);
        }
    } finally {
        ignite.destroyCache("cache");
    }
}
Also used : CacheException(javax.cache.CacheException) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite)

Example 27 with IgniteFuture

use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.

the class CacheRebalancingSelfTest method testRebalanceFuture.

/**
     * @throws Exception If failed.
     */
public void testRebalanceFuture() throws Exception {
    IgniteEx ig0 = startGrid(0);
    startGrid(1);
    IgniteCache<Object, Object> cache = ig0.cache(DEFAULT_CACHE_NAME);
    IgniteFuture fut1 = cache.rebalance();
    fut1.get();
    startGrid(2);
    IgniteFuture fut2 = cache.rebalance();
    assert internalFuture(fut2) != internalFuture(fut1);
    fut2.get();
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteFuture(org.apache.ignite.lang.IgniteFuture)

Example 28 with IgniteFuture

use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.

the class GridCachePartitionedExplicitLockNodeFailureSelfTest method testLockFromNearOrBackup.

/** @throws Exception If check failed. */
@SuppressWarnings("ErrorNotRethrown")
public void testLockFromNearOrBackup() throws Exception {
    startGrids(GRID_CNT);
    int idx = 0;
    info("Grid will be stopped: " + idx);
    Integer key = 0;
    while (grid(idx).affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key).id().equals(grid(0).localNode().id())) key++;
    ClusterNode node = grid(idx).affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key);
    info("Primary node for key [id=" + node.id() + ", order=" + node.order() + ", key=" + key + ']');
    IgniteCache<Integer, String> cache = jcache(idx);
    cache.put(key, "val");
    Lock lock = cache.lock(key);
    assert lock.tryLock();
    for (int checkIdx = 1; checkIdx < GRID_CNT; checkIdx++) {
        info("Check grid index: " + checkIdx);
        IgniteCache<Integer, String> checkCache = jcache(checkIdx);
        assert !checkCache.lock(key).tryLock();
    }
    Collection<IgniteFuture<?>> futs = new LinkedList<>();
    for (int i = 1; i < GRID_CNT; i++) {
        futs.add(waitForLocalEvent(grid(i).events(), new P1<Event>() {

            @Override
            public boolean apply(Event e) {
                info("Received grid event: " + e);
                return true;
            }
        }, EVT_NODE_LEFT));
    }
    stopGrid(idx);
    for (IgniteFuture<?> fut : futs) fut.get();
    for (int i = 0; i < 3; i++) {
        try {
            for (int checkIdx = 1; checkIdx < GRID_CNT; checkIdx++) {
                info("Check grid index: " + checkIdx);
                IgniteCache<Integer, String> checkCache = jcache(checkIdx);
                assert !checkCache.isLocalLocked(key, false);
            }
        } catch (AssertionError e) {
            if (i == 2)
                throw e;
            U.warn(log, "Check failed (will retry in 1000 ms): " + e.getMessage());
            U.sleep(1000);
            continue;
        }
        // Check passed on all grids.
        break;
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) P1(org.apache.ignite.internal.util.typedef.P1) IgniteFuture(org.apache.ignite.lang.IgniteFuture) LinkedList(java.util.LinkedList) Lock(java.util.concurrent.locks.Lock) Event(org.apache.ignite.events.Event)

Example 29 with IgniteFuture

use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.

the class GridScheduleSelfTest method testScheduleRunnable.

/**
     * @throws Exception If failed.
     */
public void testScheduleRunnable() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    SchedulerFuture<?> fut = null;
    // 1 minute frequency.
    long freq = 60;
    // 2 seconds delay.
    long delay = 2;
    try {
        // Execute 2 times after 2 seconds delay every minute.
        fut = grid(0).scheduler().scheduleLocal(new Runnable() {

            @Override
            public void run() {
                latch.countDown();
                info(">>> EXECUTING SCHEDULED RUNNABLE! <<<");
            }
        }, "{2, 2} * * * * *");
        assert !fut.isDone();
        assert !fut.isCancelled();
        assert fut.last() == null;
        final AtomicInteger notifyCnt = new AtomicInteger();
        fut.listen(new CI1<IgniteFuture<?>>() {

            @Override
            public void apply(IgniteFuture<?> e) {
                notifyCnt.incrementAndGet();
            }
        });
        final SchedulerFuture<?> fut0 = fut;
        //noinspection ThrowableNotThrown
        assertThrows(log, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                fut0.listenAsync(new IgniteInClosure<IgniteFuture<?>>() {

                    @Override
                    public void apply(IgniteFuture<?> fut) {
                    // No-op
                    }
                }, null);
                return null;
            }
        }, NullPointerException.class, null);
        fut.listenAsync(new IgniteInClosure<IgniteFuture<?>>() {

            @Override
            public void apply(IgniteFuture<?> fut) {
                assertEquals(Thread.currentThread().getName(), CUSTOM_THREAD_NAME);
                notifyCnt.incrementAndGet();
            }
        }, exec);
        //noinspection ThrowableNotThrown
        assertThrows(log, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                fut0.chainAsync(new IgniteClosure<IgniteFuture<?>, String>() {

                    @Override
                    public String apply(IgniteFuture<?> fut) {
                        return null;
                    }
                }, null);
                return null;
            }
        }, NullPointerException.class, null);
        IgniteFuture<String> chained1 = fut.chainAsync(new IgniteClosure<IgniteFuture<?>, String>() {

            @Override
            public String apply(IgniteFuture<?> fut) {
                assertEquals(Thread.currentThread().getName(), CUSTOM_THREAD_NAME);
                fut.get();
                return "done-custom";
            }
        }, exec);
        long timeTillRun = freq + delay;
        info("Going to wait for the first run: " + timeTillRun);
        latch.await(timeTillRun, SECONDS);
        assertEquals(0, latch.getCount());
        assert !fut.isDone();
        assert !fut.isCancelled();
        assert fut.last() == null;
        assertFalse(chained1.isDone());
        info("Going to wait for 2nd run: " + timeTillRun);
        // Wait until scheduling will be finished.
        Thread.sleep(timeTillRun * 1000);
        assert fut.isDone();
        assert notifyCnt.get() == 2 * 2;
        assert !fut.isCancelled();
        assert fut.last() == null;
        assertEquals("done-custom", chained1.get());
        assertTrue(chained1.isDone());
    } finally {
        assert fut != null;
        fut.cancel();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteFuture(org.apache.ignite.lang.IgniteFuture) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteException(org.apache.ignite.IgniteException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure)

Example 30 with IgniteFuture

use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.

the class IgniteGetFromComputeBenchmark method test.

/** {@inheritDoc} */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    IgniteFuture fut = invokeFut.get();
    if (fut == null || fut.isDone()) {
        Set<Integer> keys = new TreeSet<>();
        for (int i = 0; i < 3; i++) keys.add(nextRandom(args.range()));
        asyncCache.invokeAll(keys, new SlowEntryProcessor(0));
        invokeFut.set(asyncCache.future());
    }
    int key = nextRandom(args.range());
    compute.affinityCall(CACHE_NAME, key, new GetClosure(key));
    return true;
}
Also used : TreeSet(java.util.TreeSet) IgniteFuture(org.apache.ignite.lang.IgniteFuture)

Aggregations

IgniteFuture (org.apache.ignite.lang.IgniteFuture)65 Ignite (org.apache.ignite.Ignite)31 ArrayList (java.util.ArrayList)23 IgniteException (org.apache.ignite.IgniteException)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 CountDownLatch (java.util.concurrent.CountDownLatch)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 CacheException (javax.cache.CacheException)10 IgniteCompute (org.apache.ignite.IgniteCompute)8 CI1 (org.apache.ignite.internal.util.typedef.CI1)7 UUID (java.util.UUID)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 List (java.util.List)5 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)5 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)5 Collection (java.util.Collection)4 LinkedList (java.util.LinkedList)4 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)4 HashMap (java.util.HashMap)3