use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class IgniteRoundRobinErrorAfterClientReconnectTest method testClientReconnect.
/**
* @throws Exception If failed.
*/
public void testClientReconnect() throws Exception {
final Ignite cli = grid(CLI_IDX);
final GridFutureAdapter<Boolean> fut = new GridFutureAdapter<>();
cli.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event event) {
try {
cli.compute().apply(new IgniteClosure<String, Void>() {
@Override
public Void apply(String arg) {
return null;
}
}, "Hello!");
fut.onDone(true);
return true;
} catch (Exception e) {
fut.onDone(e);
return false;
}
}
}, EventType.EVT_CLIENT_NODE_RECONNECTED);
stopGrid(SRV_IDX);
startGrid(SRV_IDX);
assert fut.get();
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class GridFutureListenPerformanceTest method main.
/**
* @param args Args.
* @throws InterruptedException If failed.
*/
public static void main(String[] args) throws InterruptedException {
final LongAdder8 cnt = new LongAdder8();
final ConcurrentLinkedDeque8<GridFutureAdapter<Object>> futs = new ConcurrentLinkedDeque8<>();
ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
Thread statThread = new Thread() {
@SuppressWarnings("BusyWait")
@Override
public void run() {
while (!done) {
try {
Thread.sleep(5000);
} catch (InterruptedException ignored) {
return;
}
System.out.println(new Date() + " Notifications per sec: " + (cnt.sumThenReset() / 5));
}
}
};
statThread.setDaemon(true);
statThread.start();
for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
pool.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
Random rnd = new Random();
while (!done) {
for (int j = 0; j < rnd.nextInt(10); j++) {
GridFutureAdapter<Object> fut = new GridFutureAdapter<>();
futs.add(fut);
for (int k = 1; k < rnd.nextInt(3); k++) {
fut.listen(new IgniteInClosure<IgniteInternalFuture<Object>>() {
@Override
public void apply(IgniteInternalFuture<Object> t) {
try {
t.get();
} catch (IgniteCheckedException e) {
e.printStackTrace();
}
cnt.increment();
}
});
}
}
GridFutureAdapter<Object> fut;
while ((fut = futs.poll()) != null) fut.onDone();
}
return null;
}
});
}
Thread.sleep(5 * 60 * 1000);
done = true;
pool.shutdownNow();
pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class GridEventStorageManager method remoteEventsAsync.
/**
* @param p Grid event predicate.
* @param nodes Collection of nodes.
* @param timeout Maximum time to wait for result, if {@code 0}, then wait until result is received.
* @return Collection of events.
*/
public <T extends Event> IgniteInternalFuture<List<T>> remoteEventsAsync(final IgnitePredicate<T> p, final Collection<? extends ClusterNode> nodes, final long timeout) {
assert p != null;
assert nodes != null;
final GridFutureAdapter<List<T>> fut = new GridFutureAdapter<>();
ctx.closure().runLocalSafe(new GPR() {
@Override
public void run() {
try {
fut.onDone(query(p, nodes, timeout));
} catch (IgniteCheckedException e) {
fut.onDone(e);
}
}
}, true);
return fut;
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class GridAffinityProcessor method affinityCache.
/**
* @param cacheName Cache name.
* @param topVer Topology version.
* @return Affinity cache.
* @throws IgniteCheckedException In case of error.
*/
@SuppressWarnings("ErrorNotRethrown")
@Nullable
private AffinityInfo affinityCache(final String cacheName, AffinityTopologyVersion topVer) throws IgniteCheckedException {
AffinityAssignmentKey key = new AffinityAssignmentKey(cacheName, topVer);
IgniteInternalFuture<AffinityInfo> fut = affMap.get(key);
if (fut != null)
return fut.get();
ClusterNode loc = ctx.discovery().localNode();
// Check local node.
Collection<ClusterNode> cacheNodes = ctx.discovery().cacheNodes(cacheName, topVer);
if (cacheNodes.contains(loc)) {
GridCacheAdapter<Object, Object> cache = ctx.cache().internalCache(cacheName);
// Cache is being stopped.
if (cache == null)
return null;
GridCacheContext<Object, Object> cctx = cache.context();
cctx.awaitStarted();
try {
cctx.gate().enter();
} catch (IllegalStateException ignored) {
return null;
}
try {
AffinityAssignment assign0 = cctx.affinity().assignment(topVer);
GridAffinityAssignment assign = assign0 instanceof GridAffinityAssignment ? (GridAffinityAssignment) assign0 : new GridAffinityAssignment(topVer, assign0.assignment(), assign0.idealAssignment());
AffinityInfo info = new AffinityInfo(cctx.config().getAffinity(), cctx.config().getAffinityMapper(), assign, cctx.cacheObjectContext());
IgniteInternalFuture<AffinityInfo> old = affMap.putIfAbsent(key, new GridFinishedFuture<>(info));
if (old != null)
info = old.get();
return info;
} finally {
cctx.gate().leave();
}
}
if (F.isEmpty(cacheNodes))
return null;
GridFutureAdapter<AffinityInfo> fut0 = new GridFutureAdapter<>();
IgniteInternalFuture<AffinityInfo> old = affMap.putIfAbsent(key, fut0);
if (old != null)
return old.get();
int max = ERROR_RETRIES;
int cnt = 0;
Iterator<ClusterNode> it = cacheNodes.iterator();
// We are here because affinity has not been fetched yet, or cache mode is LOCAL.
while (true) {
cnt++;
if (!it.hasNext())
it = cacheNodes.iterator();
// Double check since we deal with dynamic view.
if (!it.hasNext())
// Exception will be caught in this method.
throw new IgniteCheckedException("No cache nodes in topology for cache name: " + cacheName);
ClusterNode n = it.next();
CacheMode mode = ctx.cache().cacheMode(cacheName);
assert mode != null;
// Map all keys to a single node, if the cache mode is LOCAL.
if (mode == LOCAL) {
fut0.onDone(new IgniteCheckedException("Failed to map keys for LOCAL cache."));
// Will throw exception.
fut0.get();
}
try {
// Resolve cache context for remote node.
// Set affinity function before counting down on latch.
fut0.onDone(affinityInfoFromNode(cacheName, topVer, n));
break;
} catch (IgniteCheckedException e) {
if (log.isDebugEnabled())
log.debug("Failed to get affinity from node (will retry) [cache=" + cacheName + ", node=" + U.toShortString(n) + ", msg=" + e.getMessage() + ']');
if (cnt < max) {
U.sleep(ERROR_WAIT);
continue;
}
affMap.remove(cacheName, fut0);
fut0.onDone(new IgniteCheckedException("Failed to get affinity mapping from node: " + n, e));
break;
} catch (RuntimeException | Error e) {
fut0.onDone(new IgniteCheckedException("Failed to get affinity mapping from node: " + n, e));
break;
}
}
return fut0.get();
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class GridDhtPartitionDemander method forceRebalance.
/**
* Force Rebalance.
*/
IgniteInternalFuture<Boolean> forceRebalance() {
GridTimeoutObject obj = lastTimeoutObj.getAndSet(null);
if (obj != null)
cctx.time().removeTimeoutObject(obj);
final GridDhtPartitionsExchangeFuture exchFut = lastExchangeFut;
if (exchFut != null) {
if (log.isDebugEnabled())
log.debug("Forcing rebalance event for future: " + exchFut);
final GridFutureAdapter<Boolean> fut = new GridFutureAdapter<>();
exchFut.listen(new CI1<IgniteInternalFuture<AffinityTopologyVersion>>() {
@Override
public void apply(IgniteInternalFuture<AffinityTopologyVersion> t) {
IgniteInternalFuture<Boolean> fut0 = cctx.shared().exchange().forceRebalance(exchFut);
fut0.listen(new IgniteInClosure<IgniteInternalFuture<Boolean>>() {
@Override
public void apply(IgniteInternalFuture<Boolean> future) {
try {
fut.onDone(future.get());
} catch (Exception e) {
fut.onDone(e);
}
}
});
}
});
return fut;
} else if (log.isDebugEnabled())
log.debug("Ignoring force rebalance request (no topology event happened yet).");
return new GridFinishedFuture<>(true);
}
Aggregations