use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class GridEventStorageManager method waitForEvent.
/**
*
* @param timeout Timeout.
* @param c Optional continuation.
* @param p Optional predicate.
* @param types Event types to wait for.
* @return Event.
* @throws IgniteCheckedException Thrown in case of any errors.
*/
public Event waitForEvent(long timeout, @Nullable Runnable c, @Nullable final IgnitePredicate<? super Event> p, int... types) throws IgniteCheckedException {
assert timeout >= 0;
final GridFutureAdapter<Event> fut = new GridFutureAdapter<>();
addLocalEventListener(new GridLocalEventListener() {
@Override
public void onEvent(Event evt) {
if (p == null || p.apply(evt)) {
fut.onDone(evt);
removeLocalEventListener(this);
}
}
}, types);
try {
if (c != null)
c.run();
} catch (Exception e) {
throw new IgniteCheckedException(e);
}
return fut.get(timeout);
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class CacheAffinitySharedManager method initCoordinatorCaches.
/**
* @param fut Exchange future.
* @throws IgniteCheckedException If failed.
* @return Future completed when caches initialization is done.
*/
private IgniteInternalFuture<?> initCoordinatorCaches(final GridDhtPartitionsExchangeFuture fut) throws IgniteCheckedException {
final List<IgniteInternalFuture<AffinityTopologyVersion>> futs = new ArrayList<>();
forAllRegisteredCaches(new IgniteInClosureX<DynamicCacheDescriptor>() {
@Override
public void applyx(DynamicCacheDescriptor desc) throws IgniteCheckedException {
CacheHolder cache = caches.get(desc.cacheId());
if (cache != null) {
if (cache.client())
cache.affinity().calculate(fut.topologyVersion(), fut.discoveryEvent(), fut.discoCache());
return;
}
final Integer cacheId = desc.cacheId();
GridCacheContext cacheCtx = cctx.cacheContext(cacheId);
if (cacheCtx == null) {
cctx.io().addHandler(desc.cacheId(), GridDhtAffinityAssignmentResponse.class, new IgniteBiInClosure<UUID, GridDhtAffinityAssignmentResponse>() {
@Override
public void apply(UUID nodeId, GridDhtAffinityAssignmentResponse res) {
processAffinityAssignmentResponse(nodeId, res);
}
});
cache = CacheHolder2.create(cctx, desc, fut, null);
final GridAffinityAssignmentCache aff = cache.affinity();
List<GridDhtPartitionsExchangeFuture> exchFuts = cctx.exchange().exchangeFutures();
int idx = exchFuts.indexOf(fut);
assert idx >= 0 && idx < exchFuts.size() - 1 : "Invalid exchange futures state [cur=" + idx + ", total=" + exchFuts.size() + ']';
final GridDhtPartitionsExchangeFuture prev = exchFuts.get(idx + 1);
if (log.isDebugEnabled()) {
log.debug("Need initialize affinity on coordinator [" + "cache=" + desc.cacheConfiguration().getName() + "prevAff=" + prev.topologyVersion() + ']');
}
assert prev.topologyVersion().compareTo(fut.topologyVersion()) < 0 : prev;
GridDhtAssignmentFetchFuture fetchFut = new GridDhtAssignmentFetchFuture(cctx, desc, prev.topologyVersion(), prev.discoCache());
fetchFut.init();
final GridFutureAdapter<AffinityTopologyVersion> affFut = new GridFutureAdapter<>();
fetchFut.listen(new IgniteInClosureX<IgniteInternalFuture<GridDhtAffinityAssignmentResponse>>() {
@Override
public void applyx(IgniteInternalFuture<GridDhtAffinityAssignmentResponse> fetchFut) throws IgniteCheckedException {
fetchAffinity(prev, aff, (GridDhtAssignmentFetchFuture) fetchFut);
aff.calculate(fut.topologyVersion(), fut.discoveryEvent(), fut.discoCache());
affFut.onDone(fut.topologyVersion());
}
});
futs.add(affFut);
} else
cache = new CacheHolder1(cacheCtx, null);
CacheHolder old = caches.put(cache.cacheId(), cache);
assert old == null : old;
}
});
if (!futs.isEmpty()) {
GridCompoundFuture<AffinityTopologyVersion, ?> affFut = new GridCompoundFuture<>();
for (IgniteInternalFuture<AffinityTopologyVersion> f : futs) affFut.add(f);
affFut.markInitialized();
return affFut;
}
return null;
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class GridCachePartitionExchangeManager method forceRebalance.
/**
* Forces preload exchange.
*
* @param exchFut Exchange future.
*/
public IgniteInternalFuture<Boolean> forceRebalance(GridDhtPartitionsExchangeFuture exchFut) {
GridFutureAdapter<Boolean> fut = new GridFutureAdapter<>();
exchWorker.addExchangeFuture(new GridDhtPartitionsExchangeFuture(cctx, exchFut.discoveryEvent(), exchFut.exchangeId(), fut));
return fut;
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class IgfsIpcHandler method handleAsync.
/**
* {@inheritDoc}
*/
@Override
public IgniteInternalFuture<IgfsMessage> handleAsync(final IgfsClientSession ses, final IgfsMessage msg, final DataInput in) {
try {
// Even if will be closed right after this call, response write error will be ignored.
if (stopping)
return null;
final IgfsIpcCommand cmd = msg.command();
IgniteInternalFuture<IgfsMessage> fut;
switch(cmd) {
// Execute not-blocking command synchronously in worker thread.
case WRITE_BLOCK:
case MAKE_DIRECTORIES:
case LIST_FILES:
case LIST_PATHS:
{
fut = executeSynchronously(ses, cmd, msg, in);
break;
}
// Execute command asynchronously in pool.
default:
{
try {
final GridFutureAdapter<IgfsMessage> fut0 = new GridFutureAdapter<>();
pool.execute(new Runnable() {
@Override
public void run() {
try {
fut0.onDone(execute(ses, cmd, msg, in));
} catch (Exception e) {
fut0.onDone(e);
}
}
});
fut = fut0;
} catch (RejectedExecutionException ignored) {
fut = executeSynchronously(ses, cmd, msg, in);
}
}
}
// Pack result object into response format.
return fut;
} catch (Exception e) {
return new GridFinishedFuture<>(e);
}
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class GridClusterStateProcessor method changeGlobalState0.
/**
*/
private IgniteInternalFuture<?> changeGlobalState0(final boolean activate, BaselineTopology blt, boolean forceChangeBaselineTopology) {
if (ctx.isDaemon() || ctx.clientNode()) {
GridFutureAdapter<Void> fut = new GridFutureAdapter<>();
sendComputeChangeGlobalState(activate, blt, forceChangeBaselineTopology, fut);
return fut;
}
if (cacheProc.transactions().tx() != null || sharedCtx.lockedTopologyVersion(null) != null) {
return new GridFinishedFuture<>(new IgniteCheckedException("Failed to " + prettyStr(activate) + " cluster (must invoke the method outside of an active transaction)."));
}
DiscoveryDataClusterState curState = globalState;
if (!curState.transition() && curState.active() == activate && BaselineTopology.equals(curState.baselineTopology(), blt))
return new GridFinishedFuture<>();
GridChangeGlobalStateFuture startedFut = null;
GridChangeGlobalStateFuture fut = stateChangeFut.get();
while (fut == null || fut.isDone()) {
fut = new GridChangeGlobalStateFuture(UUID.randomUUID(), activate, ctx);
if (stateChangeFut.compareAndSet(null, fut)) {
startedFut = fut;
break;
} else
fut = stateChangeFut.get();
}
if (startedFut == null) {
if (fut.activate != activate) {
return new GridFinishedFuture<>(new IgniteCheckedException("Failed to " + prettyStr(activate) + ", because another state change operation is currently in progress: " + prettyStr(fut.activate)));
} else
return fut;
}
List<StoredCacheData> storedCfgs = null;
if (activate && CU.isPersistenceEnabled(ctx.config())) {
try {
Map<String, StoredCacheData> cfgs = ctx.cache().context().pageStore().readCacheConfigurations();
if (!F.isEmpty(cfgs))
storedCfgs = new ArrayList<>(cfgs.values());
} catch (IgniteCheckedException e) {
U.error(log, "Failed to read stored cache configurations: " + e, e);
startedFut.onDone(e);
return startedFut;
}
}
ChangeGlobalStateMessage msg = new ChangeGlobalStateMessage(startedFut.requestId, ctx.localNodeId(), storedCfgs, activate, blt, forceChangeBaselineTopology, System.currentTimeMillis());
try {
if (log.isInfoEnabled())
U.log(log, "Sending " + prettyStr(activate) + " request with BaselineTopology " + blt);
ctx.discovery().sendCustomEvent(msg);
if (ctx.isStopping())
startedFut.onDone(new IgniteCheckedException("Failed to execute " + prettyStr(activate) + " request, " + "node is stopping."));
} catch (IgniteCheckedException e) {
U.error(log, "Failed to send global state change request: " + activate, e);
startedFut.onDone(e);
}
return wrapStateChangeFuture(startedFut, msg);
}
Aggregations