use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class GridNearTxLocal method localCacheLoadMissing.
/**
* @param cacheCtx Cache context.
* @param readThrough Read through flag.
* @param async if {@code True}, then loading will happen in a separate thread.
* @param keys Keys.
* @param skipVals Skip values flag.
* @param needVer If {@code true} version is required for loaded values.
* @param c Closure to be applied for loaded values.
* @param expiryPlc Expiry policy.
* @return Future with {@code True} value if loading took place.
*/
private IgniteInternalFuture<Void> localCacheLoadMissing(final GridCacheContext cacheCtx, final AffinityTopologyVersion topVer, final boolean readThrough, boolean async, final Collection<KeyCacheObject> keys, boolean skipVals, boolean needVer, boolean keepBinary, boolean recovery, final ExpiryPolicy expiryPlc, final GridInClosure3<KeyCacheObject, Object, GridCacheVersion> c) {
assert cacheCtx.isLocal() : cacheCtx.name();
if (!readThrough || !cacheCtx.readThrough()) {
for (KeyCacheObject key : keys) c.apply(key, null, SER_READ_EMPTY_ENTRY_VER);
return new GridFinishedFuture<>();
}
try {
IgniteCacheExpiryPolicy expiryPlc0 = optimistic() ? accessPolicy(cacheCtx, keys) : cacheCtx.cache().expiryPolicy(expiryPlc);
Map<KeyCacheObject, GridCacheVersion> misses = null;
for (KeyCacheObject key : keys) {
while (true) {
IgniteTxEntry txEntry = entry(cacheCtx.txKey(key));
GridCacheEntryEx entry = txEntry == null ? cacheCtx.cache().entryEx(key) : txEntry.cached();
if (entry == null)
continue;
try {
EntryGetResult res = entry.innerGetVersioned(null, this, /*update-metrics*/
!skipVals, /*event*/
!skipVals, CU.subjectId(this, cctx), null, resolveTaskName(), expiryPlc0, txEntry == null ? keepBinary : txEntry.keepBinary(), null);
if (res == null) {
if (misses == null)
misses = new LinkedHashMap<>();
misses.put(key, entry.version());
} else
c.apply(key, skipVals ? true : res.value(), res.version());
break;
} catch (GridCacheEntryRemovedException ignore) {
if (log.isDebugEnabled())
log.debug("Got removed entry, will retry: " + key);
if (txEntry != null)
txEntry.cached(cacheCtx.cache().entryEx(key, topologyVersion()));
}
}
}
if (misses != null) {
final Map<KeyCacheObject, GridCacheVersion> misses0 = misses;
cacheCtx.store().loadAll(this, misses.keySet(), new CI2<KeyCacheObject, Object>() {
@Override
public void apply(KeyCacheObject key, Object val) {
GridCacheVersion ver = misses0.remove(key);
assert ver != null : key;
if (val != null) {
CacheObject cacheVal = cacheCtx.toCacheObject(val);
while (true) {
GridCacheEntryEx entry = cacheCtx.cache().entryEx(key, topVer);
try {
cacheCtx.shared().database().ensureFreeSpace(cacheCtx.memoryPolicy());
EntryGetResult verVal = entry.versionedValue(cacheVal, ver, null, null, null);
if (log.isDebugEnabled()) {
log.debug("Set value loaded from store into entry [" + "oldVer=" + ver + ", newVer=" + verVal.version() + ", entry=" + entry + ']');
}
ver = verVal.version();
break;
} catch (GridCacheEntryRemovedException ignore) {
if (log.isDebugEnabled())
log.debug("Got removed entry, (will retry): " + entry);
} catch (IgniteCheckedException e) {
// Wrap errors (will be unwrapped).
throw new GridClosureException(e);
}
}
} else
ver = SER_READ_EMPTY_ENTRY_VER;
c.apply(key, val, ver);
}
});
for (KeyCacheObject key : misses0.keySet()) c.apply(key, null, SER_READ_EMPTY_ENTRY_VER);
}
return new GridFinishedFuture<>();
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(e);
}
}
use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class GridNearTxLocal method commitAsyncLocal.
/**
* Commits local part of colocated transaction.
*
* @return Commit future.
*/
public IgniteInternalFuture<IgniteInternalTx> commitAsyncLocal() {
if (log.isDebugEnabled())
log.debug("Committing colocated tx locally: " + this);
IgniteInternalFuture<?> prep = prepFut;
// Do not create finish future if there are no remote nodes.
if (F.isEmpty(dhtMap) && F.isEmpty(nearMap)) {
if (prep != null)
return (IgniteInternalFuture<IgniteInternalTx>) prep;
return new GridFinishedFuture<IgniteInternalTx>(this);
}
final GridDhtTxFinishFuture fut = new GridDhtTxFinishFuture<>(cctx, this, true);
cctx.mvcc().addFuture(fut, fut.futureId());
if (prep == null || prep.isDone()) {
assert prep != null || optimistic();
IgniteCheckedException err = null;
try {
if (prep != null)
// Check for errors of a parent future.
prep.get();
} catch (IgniteCheckedException e) {
err = e;
U.error(log, "Failed to prepare transaction: " + this, e);
}
if (err != null)
fut.rollbackOnError(err);
else
fut.finish(true);
} else
prep.listen(new CI1<IgniteInternalFuture<?>>() {
@Override
public void apply(IgniteInternalFuture<?> f) {
IgniteCheckedException err = null;
try {
// Check for errors of a parent future.
f.get();
} catch (IgniteCheckedException e) {
err = e;
U.error(log, "Failed to prepare transaction: " + this, e);
}
if (err != null)
fut.rollbackOnError(err);
else
fut.finish(true);
}
});
return fut;
}
use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class GridClosureProcessor method runLocal.
/**
* @param c Closure to execute.
* @param plc Whether to run on system or public pool.
* @return Future.
* @throws IgniteCheckedException Thrown in case of any errors.
*/
public IgniteInternalFuture<?> runLocal(@Nullable final Runnable c, byte plc) throws IgniteCheckedException {
if (c == null)
return new GridFinishedFuture();
busyLock.readLock();
try {
// Inject only if needed.
if (!(c instanceof GridPlainRunnable))
ctx.resource().inject(ctx.deploy().getDeployment(c.getClass().getName()), c.getClass(), c);
final ClassLoader ldr = Thread.currentThread().getContextClassLoader();
final GridWorkerFuture fut = new GridWorkerFuture();
GridWorker w = new GridWorker(ctx.igniteInstanceName(), "closure-proc-worker", log) {
@Override
protected void body() {
try {
if (ldr != null)
U.wrapThreadLoader(ldr, c);
else
c.run();
fut.onDone();
} catch (Throwable e) {
if (e instanceof Error)
U.error(log, "Closure execution failed with error.", e);
fut.onDone(U.cast(e));
if (e instanceof Error)
throw e;
}
}
};
fut.setWorker(w);
try {
pools.poolForPolicy(plc).execute(w);
} catch (RejectedExecutionException e) {
U.error(log, "Failed to execute worker due to execution rejection " + "(increase upper bound on executor service) [policy=" + plc + ']', e);
w.run();
}
return fut;
} finally {
busyLock.readUnlock();
}
}
use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class HadoopJobTracker method submit.
/**
* Submits execution of Hadoop job to grid.
*
* @param jobId Job ID.
* @param info Job info.
* @return Job completion future.
*/
@SuppressWarnings("unchecked")
public IgniteInternalFuture<HadoopJobId> submit(HadoopJobId jobId, HadoopJobInfo info) {
if (!busyLock.tryReadLock()) {
return new GridFinishedFuture<>(new IgniteCheckedException("Failed to execute map-reduce job " + "(grid is stopping): " + info));
}
try {
long jobPrepare = U.currentTimeMillis();
if (jobs.containsKey(jobId) || jobMetaCache().containsKey(jobId))
throw new IgniteCheckedException("Failed to submit job. Job with the same ID already exists: " + jobId);
HadoopJobEx job = job(jobId, info);
HadoopMapReducePlan mrPlan = mrPlanner.preparePlan(job, ctx.nodes(), null);
logPlan(info, mrPlan);
HadoopJobMetadata meta = new HadoopJobMetadata(ctx.localNodeId(), jobId, info);
meta.mapReducePlan(mrPlan);
meta.pendingSplits(allSplits(mrPlan));
meta.pendingReducers(allReducers(mrPlan));
GridFutureAdapter<HadoopJobId> completeFut = new GridFutureAdapter<>();
GridFutureAdapter<HadoopJobId> old = activeFinishFuts.put(jobId, completeFut);
assert old == null : "Duplicate completion future [jobId=" + jobId + ", old=" + old + ']';
if (log.isDebugEnabled())
log.debug("Submitting job metadata [jobId=" + jobId + ", meta=" + meta + ']');
long jobStart = U.currentTimeMillis();
HadoopPerformanceCounter perfCntr = HadoopPerformanceCounter.getCounter(meta.counters(), ctx.localNodeId());
perfCntr.clientSubmissionEvents(info);
perfCntr.onJobPrepare(jobPrepare);
perfCntr.onJobStart(jobStart);
if (jobMetaCache().getAndPutIfAbsent(jobId, meta) != null)
throw new IgniteCheckedException("Failed to submit job. Job with the same ID already exists: " + jobId);
return completeFut;
} catch (IgniteCheckedException e) {
U.error(log, "Failed to submit job: " + jobId, e);
return new GridFinishedFuture<>(e);
} finally {
busyLock.readUnlock();
}
}
use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class HadoopIgfsInProc method readData.
/** {@inheritDoc} */
@Override
public IgniteInternalFuture<byte[]> readData(HadoopIgfsStreamDelegate delegate, long pos, int len, @Nullable byte[] outBuf, int outOff, int outLen) {
IgfsInputStream stream = delegate.target();
try {
byte[] res = null;
if (outBuf != null) {
int outTailLen = outBuf.length - outOff;
if (len <= outTailLen)
stream.readFully(pos, outBuf, outOff, len);
else {
stream.readFully(pos, outBuf, outOff, outTailLen);
int remainderLen = len - outTailLen;
res = new byte[remainderLen];
stream.readFully(pos, res, 0, remainderLen);
}
} else {
res = new byte[len];
stream.readFully(pos, res, 0, len);
}
return new GridFinishedFuture<>(res);
} catch (IllegalStateException | IOException e) {
HadoopIgfsStreamEventListener lsnr = lsnrs.get(delegate);
if (lsnr != null)
lsnr.onError(e.getMessage());
return new GridFinishedFuture<>(e);
}
}
Aggregations