Search in sources :

Example 6 with GridWorker

use of org.apache.ignite.internal.util.worker.GridWorker in project ignite by apache.

the class HadoopExecutorService method startThread.

/**
 * @param task Task.
 */
private void startThread(final Callable<?> task) {
    String workerName;
    if (task instanceof HadoopRunnableTask) {
        final HadoopTaskInfo i = ((HadoopRunnableTask) task).taskInfo();
        workerName = "Hadoop-task-" + i.jobId() + "-" + i.type() + "-" + i.taskNumber() + "-" + i.attempt();
    } else
        workerName = task.toString();
    GridWorker w = new GridWorker(igniteInstanceName, workerName, log, lsnr) {

        @Override
        protected void body() {
            try {
                task.call();
            } catch (Exception e) {
                log.error("Failed to execute task: " + task, e);
            }
        }
    };
    workers.add(w);
    if (shutdown)
        w.cancel();
    new IgniteThread(w).start();
}
Also used : HadoopTaskInfo(org.apache.ignite.internal.processors.hadoop.HadoopTaskInfo) IgniteThread(org.apache.ignite.thread.IgniteThread) GridWorker(org.apache.ignite.internal.util.worker.GridWorker)

Example 7 with GridWorker

use of org.apache.ignite.internal.util.worker.GridWorker 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)

Example 8 with GridWorker

use of org.apache.ignite.internal.util.worker.GridWorker 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<>();
    idx.markForRebuildFromHash(cacheName);
    GridWorker w = new GridWorker(ctx.igniteInstanceName(), "index-rebuild-worker", log) {

        @Override
        protected void body() {
            try {
                idx.rebuildIndexesFromHash(cacheName);
                fut.onDone();
            } catch (Exception e) {
                fut.onDone(e);
            } catch (Throwable e) {
                U.error(log, "Failed to rebuild indexes for type [cache=" + cacheName + ", name=" + desc.name() + ']', e);
                fut.onDone(e);
                throw e;
            }
        }
    };
    fut.setWorker(w);
    ctx.getExecutorService().execute(w);
    return fut;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) 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) SQLException(java.sql.SQLException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) IgniteException(org.apache.ignite.IgniteException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) CacheException(javax.cache.CacheException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException)

Example 9 with GridWorker

use of org.apache.ignite.internal.util.worker.GridWorker 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 10 with GridWorker

use of org.apache.ignite.internal.util.worker.GridWorker 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)

Aggregations

GridWorker (org.apache.ignite.internal.util.worker.GridWorker)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteThread (org.apache.ignite.thread.IgniteThread)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 GridWorkerFuture (org.apache.ignite.internal.util.worker.GridWorkerFuture)4 IgniteException (org.apache.ignite.IgniteException)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CacheException (javax.cache.CacheException)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 FAILOVER (org.apache.ignite.compute.ComputeJobResultPolicy.FAILOVER)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)1