use of org.apache.ignite.internal.util.worker.GridWorkerFuture in project ignite by apache.
the class GridClosureProcessor method callLocal.
/**
* @param c Closure to execute.
* @param plc Whether to run on system or public pool.
* @param <R> Type of closure return value.
* @return Future.
* @throws IgniteCheckedException Thrown in case of any errors.
*/
public <R> IgniteInternalFuture<R> callLocal(@Nullable final Callable<R> c, byte plc) throws IgniteCheckedException {
if (c == null)
return new GridFinishedFuture<>();
busyLock.readLock();
try {
// Inject only if needed.
if (!(c instanceof GridPlainCallable))
ctx.resource().inject(ctx.deploy().getDeployment(c.getClass().getName()), c.getClass(), c);
final ClassLoader ldr = Thread.currentThread().getContextClassLoader();
final GridWorkerFuture<R> fut = new GridWorkerFuture<>();
GridWorker w = new GridWorker(ctx.igniteInstanceName(), "closure-proc-worker", log) {
@Override
protected void body() {
try {
if (ldr != null)
fut.onDone(U.wrapThreadLoader(ldr, c));
else
fut.onDone(c.call());
} 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 (Error) 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.worker.GridWorkerFuture 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.worker.GridWorkerFuture in project ignite by apache.
the class GridQueryProcessor method rebuildIndexesFromHash.
/**
* @param cacheName Cache name.
* @param desc Type descriptor.
* @return Future that will be completed when rebuilding of all indexes is finished.
*/
private IgniteInternalFuture<Object> rebuildIndexesFromHash(@Nullable final String cacheName, @Nullable final QueryTypeDescriptorImpl desc) {
if (idx == null)
return new GridFinishedFuture<>(new IgniteCheckedException("Indexing is disabled."));
if (desc == null)
return new GridFinishedFuture<>();
final GridWorkerFuture<Object> fut = new GridWorkerFuture<>();
final String schemaName = idx.schema(cacheName);
final String typeName = desc.name();
idx.markForRebuildFromHash(schemaName, typeName);
GridWorker w = new GridWorker(ctx.igniteInstanceName(), "index-rebuild-worker", log) {
@Override
protected void body() {
try {
int cacheId = CU.cacheId(cacheName);
GridCacheContext cctx = ctx.cache().context().cacheContext(cacheId);
idx.rebuildIndexesFromHash(cctx, schemaName, typeName);
fut.onDone();
} catch (Exception e) {
fut.onDone(e);
} catch (Throwable e) {
log.error("Failed to rebuild indexes for type: " + typeName, e);
fut.onDone(e);
throw e;
}
}
};
fut.setWorker(w);
ctx.getExecutorService().execute(w);
return fut;
}
use of org.apache.ignite.internal.util.worker.GridWorkerFuture in project ignite by apache.
the class GridRestProcessor method handleAsync0.
/**
* @param req Request.
* @return Future.
*/
private IgniteInternalFuture<GridRestResponse> handleAsync0(final GridRestRequest req) {
if (!busyLock.tryReadLock())
return new GridFinishedFuture<>(new IgniteCheckedException("Failed to handle request (received request while stopping grid)."));
try {
final GridWorkerFuture<GridRestResponse> fut = new GridWorkerFuture<>();
workersCnt.increment();
GridWorker w = new GridWorker(ctx.igniteInstanceName(), "rest-proc-worker", log) {
@Override
protected void body() {
try {
IgniteInternalFuture<GridRestResponse> res = handleRequest(req);
res.listen(new IgniteInClosure<IgniteInternalFuture<GridRestResponse>>() {
@Override
public void apply(IgniteInternalFuture<GridRestResponse> f) {
try {
fut.onDone(f.get());
} catch (IgniteCheckedException e) {
fut.onDone(e);
}
}
});
} catch (Throwable e) {
if (e instanceof Error)
U.error(log, "Client request execution failed with error.", e);
fut.onDone(U.cast(e));
if (e instanceof Error)
throw e;
} finally {
workersCnt.decrement();
}
}
};
fut.setWorker(w);
try {
ctx.getRestExecutorService().execute(w);
} catch (RejectedExecutionException e) {
U.error(log, "Failed to execute worker due to execution rejection " + "(increase upper bound on REST executor service). " + "Will attempt to process request in the current thread instead.", e);
w.run();
}
return fut;
} finally {
busyLock.readUnlock();
}
}
Aggregations