Search in sources :

Example 31 with IgniteFuture

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

the class IgniteFutureImplTest method testListeners.

/**
 * @throws Exception If failed.
 */
public void testListeners() throws Exception {
    GridFutureAdapter<String> fut0 = new GridFutureAdapter<>();
    IgniteFutureImpl<String> fut = createFuture(fut0);
    final AtomicInteger lsnr1Cnt = new AtomicInteger();
    IgniteInClosure<? super IgniteFuture<String>> lsnr1 = new CI1<IgniteFuture<String>>() {

        @Override
        public void apply(IgniteFuture<String> fut) {
            assertEquals("test", fut.get());
            lsnr1Cnt.incrementAndGet();
        }
    };
    final AtomicInteger lsnr2Cnt = new AtomicInteger();
    IgniteInClosure<? super IgniteFuture<String>> lsnr2 = new CI1<IgniteFuture<String>>() {

        @Override
        public void apply(IgniteFuture<String> fut) {
            assertEquals("test", fut.get());
            lsnr2Cnt.incrementAndGet();
        }
    };
    assertFalse(fut.isDone());
    fut.listen(lsnr1);
    fut.listen(lsnr2);
    U.sleep(100);
    assertEquals(0, lsnr1Cnt.get());
    assertEquals(0, lsnr2Cnt.get());
    fut0.onDone("test");
    assertEquals(1, lsnr1Cnt.get());
    assertEquals(1, lsnr2Cnt.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFuture(org.apache.ignite.lang.IgniteFuture) CI1(org.apache.ignite.internal.util.typedef.CI1)

Example 32 with IgniteFuture

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

the class IgniteFutureImplTest method checkChainedOnError.

/**
 * @param err Exception.
 * @throws Exception If failed.
 */
private void checkChainedOnError(final Exception err) throws Exception {
    GridFutureAdapter<String> fut0 = new GridFutureAdapter<>();
    IgniteFutureImpl<String> fut = createFuture(fut0);
    // Chain callback will be invoked in specific executor.
    IgniteFuture<Integer> chained1 = fut.chainAsync(new C1<IgniteFuture<String>, Integer>() {

        @Override
        public Integer apply(IgniteFuture<String> fut) {
            assertEquals(CUSTOM_THREAD_NAME, Thread.currentThread().getName());
            try {
                fut.get();
                fail();
            } catch (IgniteException | CacheException e) {
                assertExpectedException(e, err);
                throw e;
            }
            return -1;
        }
    }, customExec);
    assertFalse(chained1.isDone());
    assertFalse(fut.isDone());
    final CountDownLatch latch = new CountDownLatch(1);
    chained1.listen(new CI1<IgniteFuture<Integer>>() {

        @Override
        public void apply(IgniteFuture<Integer> fut) {
            try {
                assertEquals(CUSTOM_THREAD_NAME, Thread.currentThread().getName());
                fut.get();
                fail();
            } catch (IgniteException | CacheException e) {
                assertExpectedException(e, err);
            } finally {
                latch.countDown();
            }
        }
    });
    fut0.onDone(err);
    assertExceptionThrown(err, chained1);
    assertExceptionThrown(err, fut);
    assertTrue(chained1.isDone());
    assertTrue(fut.isDone());
    assert latch.await(100, TimeUnit.MILLISECONDS);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFuture(org.apache.ignite.lang.IgniteFuture) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 33 with IgniteFuture

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

the class IgniteCacheConfigVariationsFullApiTest method testInvokeException.

/**
 * @throws Exception If failed.
 */
public void testInvokeException() throws Exception {
    final IgniteCache cache = jcache();
    final IgniteFuture fut = cache.invokeAsync("key2", ERR_PROCESSOR);
    assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            fut.chain(new IgniteClosure<IgniteFuture, Object>() {

                @Override
                public Object apply(IgniteFuture o) {
                    return o.get();
                }
            });
            fut.get();
            return null;
        }
    }, EntryProcessorException.class, null);
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteCache(org.apache.ignite.IgniteCache) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException)

Example 34 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 35 with IgniteFuture

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

the class GridClusterStateProcessor method changeGlobalState.

/**
     *
     */
public IgniteInternalFuture<?> changeGlobalState(final boolean activate) {
    if (cacheProc.transactions().tx() != null || sharedCtx.lockedTopologyVersion(null) != null)
        throw new IgniteException("Cannot " + prettyStr(activate) + " cluster, because cache locked on transaction.");
    if ((globalState == ACTIVE && activate) || (this.globalState == INACTIVE && !activate))
        return new GridFinishedFuture<>();
    final UUID requestId = UUID.randomUUID();
    final GridChangeGlobalStateFuture cgsFut = new GridChangeGlobalStateFuture(requestId, activate, ctx);
    if (!cgsLocFut.compareAndSet(null, cgsFut)) {
        GridChangeGlobalStateFuture locF = cgsLocFut.get();
        if (locF.activate == activate)
            return locF;
        else
            return new GridFinishedFuture<>(new IgniteException("fail " + prettyStr(activate) + ", because now in progress" + prettyStr(locF.activate)));
    }
    try {
        if (ctx.clientNode()) {
            AffinityTopologyVersion topVer = ctx.discovery().topologyVersionEx();
            IgniteCompute comp = ((ClusterGroupAdapter) ctx.cluster().get().forServers()).compute().withAsync();
            if (log.isInfoEnabled())
                log.info("Send " + prettyStr(activate) + " request from client node [id=" + ctx.localNodeId() + " topVer=" + topVer + " ]");
            comp.run(new ClientChangeGlobalStateComputeRequest(activate));
            comp.future().listen(new CI1<IgniteFuture>() {

                @Override
                public void apply(IgniteFuture fut) {
                    try {
                        fut.get();
                        cgsFut.onDone();
                    } catch (Exception e) {
                        cgsFut.onDone(e);
                    }
                }
            });
        } else {
            List<DynamicCacheChangeRequest> reqs = new ArrayList<>();
            DynamicCacheChangeRequest changeGlobalStateReq = new DynamicCacheChangeRequest(requestId, activate ? ACTIVE : INACTIVE, ctx.localNodeId());
            reqs.add(changeGlobalStateReq);
            reqs.addAll(activate ? cacheProc.startAllCachesRequests() : cacheProc.stopAllCachesRequests());
            ChangeGlobalStateMessage changeGlobalStateMsg = new ChangeGlobalStateMessage(requestId, ctx.localNodeId(), activate, new DynamicCacheChangeBatch(reqs));
            try {
                ctx.discovery().sendCustomEvent(changeGlobalStateMsg);
                if (ctx.isStopping())
                    cgsFut.onDone(new IgniteCheckedException("Failed to execute " + prettyStr(activate) + " request, " + "node is stopping."));
            } catch (IgniteCheckedException e) {
                log.error("Fail create or send change global state request." + cgsFut, e);
                cgsFut.onDone(e);
            }
        }
    } catch (IgniteCheckedException e) {
        log.error("Fail create or send change global state request." + cgsFut, e);
        cgsFut.onDone(e);
    }
    return cgsFut;
}
Also used : DynamicCacheChangeRequest(org.apache.ignite.internal.processors.cache.DynamicCacheChangeRequest) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ArrayList(java.util.ArrayList) ChangeGlobalStateMessage(org.apache.ignite.internal.processors.cache.ChangeGlobalStateMessage) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) DynamicCacheChangeBatch(org.apache.ignite.internal.processors.cache.DynamicCacheChangeBatch) UUID(java.util.UUID) IgniteCompute(org.apache.ignite.IgniteCompute)

Aggregations

IgniteFuture (org.apache.ignite.lang.IgniteFuture)76 Ignite (org.apache.ignite.Ignite)36 ArrayList (java.util.ArrayList)28 IgniteException (org.apache.ignite.IgniteException)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 CountDownLatch (java.util.concurrent.CountDownLatch)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 CacheException (javax.cache.CacheException)11 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 IgniteCompute (org.apache.ignite.IgniteCompute)9 CI1 (org.apache.ignite.internal.util.typedef.CI1)9 List (java.util.List)7 ClusterNode (org.apache.ignite.cluster.ClusterNode)7 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)7 UUID (java.util.UUID)6 Collection (java.util.Collection)5 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)5 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)5 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4