Search in sources :

Example 1 with GridWorkerFuture

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();
    }
}
Also used : TC_NO_FAILOVER(org.apache.ignite.internal.processors.task.GridTaskThreadContextKey.TC_NO_FAILOVER) FAILOVER(org.apache.ignite.compute.ComputeJobResultPolicy.FAILOVER) GridPlainCallable(org.apache.ignite.internal.util.lang.GridPlainCallable) GridWorkerFuture(org.apache.ignite.internal.util.worker.GridWorkerFuture) GridWorker(org.apache.ignite.internal.util.worker.GridWorker) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 2 with GridWorkerFuture

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();
    }
}
Also used : GridWorkerFuture(org.apache.ignite.internal.util.worker.GridWorkerFuture) GridWorker(org.apache.ignite.internal.util.worker.GridWorker) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 3 with GridWorkerFuture

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;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridWorkerFuture(org.apache.ignite.internal.util.worker.GridWorkerFuture) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridWorker(org.apache.ignite.internal.util.worker.GridWorker) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) SQLException(java.sql.SQLException) CacheException(javax.cache.CacheException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException)

Example 4 with GridWorkerFuture

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();
    }
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridWorker(org.apache.ignite.internal.util.worker.GridWorker) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridWorkerFuture(org.apache.ignite.internal.util.worker.GridWorkerFuture)

Aggregations

GridWorker (org.apache.ignite.internal.util.worker.GridWorker)4 GridWorkerFuture (org.apache.ignite.internal.util.worker.GridWorkerFuture)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 SQLException (java.sql.SQLException)1 CacheException (javax.cache.CacheException)1 IgniteException (org.apache.ignite.IgniteException)1 FAILOVER (org.apache.ignite.compute.ComputeJobResultPolicy.FAILOVER)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)1 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)1 SchemaOperationException (org.apache.ignite.internal.processors.query.schema.SchemaOperationException)1 TC_NO_FAILOVER (org.apache.ignite.internal.processors.task.GridTaskThreadContextKey.TC_NO_FAILOVER)1 GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)1 GridClosureException (org.apache.ignite.internal.util.lang.GridClosureException)1 GridPlainCallable (org.apache.ignite.internal.util.lang.GridPlainCallable)1 GridPlainRunnable (org.apache.ignite.internal.util.lang.GridPlainRunnable)1