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());
}
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);
}
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);
}
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;
}
}
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;
}
Aggregations