Search in sources :

Example 26 with IgniteUuid

use of org.apache.ignite.lang.IgniteUuid in project ignite by apache.

the class GridTaskCommandHandler method handleAsyncUnsafe.

/**
     * @param req Request.
     * @return Future.
     * @throws IgniteCheckedException On any handling exception.
     */
private IgniteInternalFuture<GridRestResponse> handleAsyncUnsafe(final GridRestRequest req) throws IgniteCheckedException {
    assert req instanceof GridRestTaskRequest : "Invalid command for topology handler: " + req;
    assert SUPPORTED_COMMANDS.contains(req.command());
    if (log.isDebugEnabled())
        log.debug("Handling task REST request: " + req);
    GridRestTaskRequest req0 = (GridRestTaskRequest) req;
    final GridFutureAdapter<GridRestResponse> fut = new GridFutureAdapter<>();
    final GridRestResponse res = new GridRestResponse();
    final GridClientTaskResultBean taskRestRes = new GridClientTaskResultBean();
    // Set ID placeholder for the case it wouldn't be available due to remote execution.
    taskRestRes.setId('~' + ctx.localNodeId().toString());
    final boolean locExec = req0.destinationId() == null || req0.destinationId().equals(ctx.localNodeId()) || ctx.discovery().node(req0.destinationId()) == null;
    switch(req.command()) {
        case EXE:
            {
                final boolean async = req0.async();
                final String name = req0.taskName();
                if (F.isEmpty(name))
                    throw new IgniteCheckedException(missingParameter("name"));
                final List<Object> params = req0.params();
                long timeout = req0.timeout();
                final UUID clientId = req.clientId();
                final IgniteInternalFuture<Object> taskFut;
                if (locExec) {
                    ctx.task().setThreadContextIfNotNull(TC_SUBJ_ID, clientId);
                    ctx.task().setThreadContext(TC_TIMEOUT, timeout);
                    Object arg = !F.isEmpty(params) ? params.size() == 1 ? params.get(0) : params.toArray() : null;
                    taskFut = ctx.task().execute(name, arg);
                } else {
                    // Using predicate instead of node intentionally
                    // in order to provide user well-structured EmptyProjectionException.
                    ClusterGroup prj = ctx.grid().cluster().forPredicate(F.nodeForNodeId(req.destinationId()));
                    ctx.task().setThreadContext(TC_NO_FAILOVER, true);
                    taskFut = ctx.closure().callAsync(BALANCE, new ExeCallable(name, params, timeout, clientId), prj.nodes());
                }
                if (async) {
                    if (locExec) {
                        IgniteUuid tid = ((ComputeTaskInternalFuture) taskFut).getTaskSession().getId();
                        taskDescs.put(tid, new TaskDescriptor(false, null, null));
                        taskRestRes.setId(tid.toString() + '~' + ctx.localNodeId().toString());
                        res.setResponse(taskRestRes);
                    } else
                        res.setError("Asynchronous task execution is not supported for routing request.");
                    fut.onDone(res);
                }
                taskFut.listen(new IgniteInClosure<IgniteInternalFuture<Object>>() {

                    @Override
                    public void apply(IgniteInternalFuture<Object> taskFut) {
                        try {
                            TaskDescriptor desc;
                            try {
                                desc = new TaskDescriptor(true, taskFut.get(), null);
                            } catch (IgniteCheckedException e) {
                                if (e.hasCause(ClusterTopologyCheckedException.class, ClusterGroupEmptyCheckedException.class))
                                    U.warn(log, "Failed to execute task due to topology issues (are all mapped " + "nodes alive?) [name=" + name + ", clientId=" + req.clientId() + ", err=" + e + ']');
                                else {
                                    if (!X.hasCause(e, VisorClusterGroupEmptyException.class))
                                        U.error(log, "Failed to execute task [name=" + name + ", clientId=" + req.clientId() + ']', e);
                                }
                                desc = new TaskDescriptor(true, null, e);
                            }
                            if (async && locExec) {
                                assert taskFut instanceof ComputeTaskInternalFuture;
                                IgniteUuid tid = ((ComputeTaskInternalFuture) taskFut).getTaskSession().getId();
                                taskDescs.put(tid, desc);
                            }
                            if (!async) {
                                if (desc.error() == null) {
                                    try {
                                        taskRestRes.setFinished(true);
                                        taskRestRes.setResult(desc.result());
                                        res.setResponse(taskRestRes);
                                        fut.onDone(res);
                                    } catch (IgniteException e) {
                                        fut.onDone(new IgniteCheckedException("Failed to marshal task result: " + desc.result(), e));
                                    }
                                } else
                                    fut.onDone(desc.error());
                            }
                        } finally {
                            if (!async && !fut.isDone())
                                fut.onDone(new IgniteCheckedException("Failed to execute task (see server logs for details)."));
                        }
                    }
                });
                break;
            }
        case RESULT:
            {
                String id = req0.taskId();
                if (F.isEmpty(id))
                    throw new IgniteCheckedException(missingParameter("id"));
                StringTokenizer st = new StringTokenizer(id, "~");
                if (st.countTokens() != 2)
                    throw new IgniteCheckedException("Failed to parse id parameter: " + id);
                String tidParam = st.nextToken();
                String resHolderIdParam = st.nextToken();
                taskRestRes.setId(id);
                try {
                    IgniteUuid tid = !F.isEmpty(tidParam) ? IgniteUuid.fromString(tidParam) : null;
                    UUID resHolderId = !F.isEmpty(resHolderIdParam) ? UUID.fromString(resHolderIdParam) : null;
                    if (tid == null || resHolderId == null)
                        throw new IgniteCheckedException("Failed to parse id parameter: " + id);
                    if (ctx.localNodeId().equals(resHolderId)) {
                        TaskDescriptor desc = taskDescs.get(tid);
                        if (desc == null)
                            throw new IgniteCheckedException("Task with provided id has never been started on provided node" + " [taskId=" + tidParam + ", taskResHolderId=" + resHolderIdParam + ']');
                        taskRestRes.setFinished(desc.finished());
                        if (desc.error() != null)
                            throw new IgniteCheckedException(desc.error().getMessage());
                        taskRestRes.setResult(desc.result());
                        res.setResponse(taskRestRes);
                    } else {
                        IgniteBiTuple<String, GridTaskResultResponse> t = requestTaskResult(resHolderId, tid);
                        if (t.get1() != null)
                            throw new IgniteCheckedException(t.get1());
                        GridTaskResultResponse taskRes = t.get2();
                        assert taskRes != null;
                        if (!taskRes.found())
                            throw new IgniteCheckedException("Task with provided id has never been started on provided node " + "[taskId=" + tidParam + ", taskResHolderId=" + resHolderIdParam + ']');
                        taskRestRes.setFinished(taskRes.finished());
                        if (taskRes.error() != null)
                            throw new IgniteCheckedException(taskRes.error());
                        taskRestRes.setResult(taskRes.result());
                        res.setResponse(taskRestRes);
                    }
                } catch (IllegalArgumentException e) {
                    String msg = "Failed to parse parameters [taskId=" + tidParam + ", taskResHolderId=" + resHolderIdParam + ", err=" + e.getMessage() + ']';
                    if (log.isDebugEnabled())
                        log.debug(msg);
                    throw new IgniteCheckedException(msg, e);
                }
                fut.onDone(res);
                break;
            }
        case NOOP:
            {
                fut.onDone(new GridRestResponse());
                break;
            }
        default:
            assert false : "Invalid command for task handler: " + req;
    }
    if (log.isDebugEnabled())
        log.debug("Handled task REST request [res=" + res + ", req=" + req + ']');
    return fut;
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridClientTaskResultBean(org.apache.ignite.internal.processors.rest.client.message.GridClientTaskResultBean) GridRestTaskRequest(org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteException(org.apache.ignite.IgniteException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) List(java.util.List) UUID(java.util.UUID) ComputeTaskInternalFuture(org.apache.ignite.internal.ComputeTaskInternalFuture) StringTokenizer(java.util.StringTokenizer) ClusterGroupEmptyCheckedException(org.apache.ignite.internal.cluster.ClusterGroupEmptyCheckedException) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 27 with IgniteUuid

use of org.apache.ignite.lang.IgniteUuid in project ignite by apache.

the class GridTaskWorker method processMappedJobs.

/**
     * @param jobs Map of jobs.
     * @throws IgniteCheckedException Thrown in case of any error.
     */
private void processMappedJobs(Map<? extends ComputeJob, ClusterNode> jobs) throws IgniteCheckedException {
    if (F.isEmpty(jobs))
        return;
    Collection<GridJobResultImpl> jobResList = new ArrayList<>(jobs.size());
    Collection<ComputeJobSibling> sibs = new ArrayList<>(jobs.size());
    // Map jobs to nodes for computation.
    for (Map.Entry<? extends ComputeJob, ClusterNode> mappedJob : jobs.entrySet()) {
        ComputeJob job = mappedJob.getKey();
        ClusterNode node = mappedJob.getValue();
        if (job == null)
            throw new IgniteCheckedException("Job can not be null [mappedJob=" + mappedJob + ", ses=" + ses + ']');
        if (node == null)
            throw new IgniteCheckedException("Node can not be null [mappedJob=" + mappedJob + ", ses=" + ses + ']');
        IgniteUuid jobId = IgniteUuid.fromUuid(ctx.localNodeId());
        GridJobSiblingImpl sib = new GridJobSiblingImpl(ses.getId(), jobId, node.id(), ctx);
        jobResList.add(new GridJobResultImpl(job, jobId, node, sib));
        // Do not add siblings if result cache is disabled.
        if (resCache)
            sibs.add(sib);
        recordJobEvent(EVT_JOB_MAPPED, jobId, node, "Job got mapped.");
    }
    synchronized (mux) {
        if (state != State.WAITING)
            throw new IgniteCheckedException("Task is not in waiting state [state=" + state + ", ses=" + ses + ']');
        // Do not add siblings if result cache is disabled.
        if (resCache)
            ses.addJobSiblings(sibs);
        if (jobRes == null)
            jobRes = new HashMap<>();
        // getting results while still sending out references.
        for (GridJobResultImpl res : jobResList) {
            if (jobRes.put(res.getJobContext().getJobId(), res) != null)
                throw new IgniteCheckedException("Duplicate job ID for remote job found: " + res.getJobContext().getJobId());
            res.setOccupied(true);
            if (resCache && jobRes.size() > ctx.discovery().size() && jobRes.size() % SPLIT_WARN_THRESHOLD == 0)
                LT.warn(log, "Number of jobs in task is too large for task: " + ses.getTaskName() + ". Consider reducing number of jobs or disabling job result cache with " + "@ComputeTaskNoResultCache annotation.");
        }
    }
    // Set mapped flag.
    ses.onMapped();
    // Send out all remote mappedJobs.
    for (GridJobResultImpl res : jobResList) {
        evtLsnr.onJobSend(this, res.getSibling());
        try {
            sendRequest(res);
        } finally {
            // Open job for processing results.
            synchronized (mux) {
                res.setOccupied(false);
            }
        }
    }
    processDelayedResponses();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GridJobResultImpl(org.apache.ignite.internal.GridJobResultImpl) ComputeJob(org.apache.ignite.compute.ComputeJob) GridJobSiblingImpl(org.apache.ignite.internal.GridJobSiblingImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteUuid(org.apache.ignite.lang.IgniteUuid) ComputeJobSibling(org.apache.ignite.compute.ComputeJobSibling) Map(java.util.Map) HashMap(java.util.HashMap)

Example 28 with IgniteUuid

use of org.apache.ignite.lang.IgniteUuid in project ignite by apache.

the class VisorCacheConfigurationCollectorJob method run.

/** {@inheritDoc} */
@Override
protected Map<IgniteUuid, VisorCacheConfiguration> run(VisorCacheConfigurationCollectorTaskArg arg) {
    Collection<IgniteCacheProxy<?, ?>> caches = ignite.context().cache().jcaches();
    Collection<IgniteUuid> depIds = arg.getDeploymentIds();
    boolean all = depIds == null || depIds.isEmpty();
    Map<IgniteUuid, VisorCacheConfiguration> res = U.newHashMap(caches.size());
    for (IgniteCacheProxy<?, ?> cache : caches) {
        IgniteUuid deploymentId = cache.context().dynamicDeploymentId();
        if (all || depIds.contains(deploymentId))
            res.put(deploymentId, config(cache.getConfiguration(CacheConfiguration.class)));
    }
    return res;
}
Also used : IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 29 with IgniteUuid

use of org.apache.ignite.lang.IgniteUuid in project ignite by apache.

the class GridTimeoutProcessorSelfTest method testTimeouts.

/**
     * Tests timeouts.
     *
     * @throws Exception If test failed.
     */
public void testTimeouts() throws Exception {
    int max = 100;
    final CountDownLatch latch = new CountDownLatch(max);
    final Collection<GridTimeoutObject> timeObjs = new ConcurrentLinkedQueue<>();
    for (int i = 0; i < max; i++) {
        final int idx = i;
        ctx.timeout().addTimeoutObject(new GridTimeoutObject() {

            /** Timeout ID. */
            private final IgniteUuid id = IgniteUuid.randomUuid();

            /** End time. */
            private final long endTime = System.currentTimeMillis() + RAND.nextInt(1000);

            /** {@inheritDoc} */
            @Override
            public IgniteUuid timeoutId() {
                return id;
            }

            /** {@inheritDoc} */
            @Override
            public long endTime() {
                return endTime;
            }

            /** {@inheritDoc} */
            @Override
            public void onTimeout() {
                info("Received timeout callback: " + this);
                long now = System.currentTimeMillis();
                if (now < endTime) {
                    fail("Timeout event happened prematurely [endTime=" + endTime + ", now=" + now + ", obj=" + this + ']');
                }
                synchronized (timeObjs) {
                    timeObjs.add(this);
                }
                latch.countDown();
            }

            /** {@inheritDoc} */
            @Override
            public String toString() {
                return "Timeout test object [idx=" + idx + ", endTime=" + endTime + ", id=" + id + ']';
            }
        });
    }
    latch.await();
    assert timeObjs.size() == max;
    // Ensure proper timeout sequence.
    long endTime = 0;
    for (GridTimeoutObject obj : timeObjs) {
        assert endTime <= obj.endTime();
        endTime = obj.endTime();
    }
}
Also used : IgniteUuid(org.apache.ignite.lang.IgniteUuid) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 30 with IgniteUuid

use of org.apache.ignite.lang.IgniteUuid in project ignite by apache.

the class GridTimeoutProcessorSelfTest method testTimeoutSameEndTime.

/**
     * @throws Exception If test failed.
     */
public void testTimeoutSameEndTime() throws Exception {
    final CountDownLatch latch = new CountDownLatch(2);
    final long endTime0 = System.currentTimeMillis() + 1000;
    ctx.timeout().addTimeoutObject(new GridTimeoutObject() {

        /** Timeout ID. */
        private final IgniteUuid id = IgniteUuid.randomUuid();

        /** End time. */
        private final long endTime = endTime0;

        /** {@inheritDoc} */
        @Override
        public IgniteUuid timeoutId() {
            return id;
        }

        /** {@inheritDoc} */
        @Override
        public long endTime() {
            return endTime;
        }

        /** {@inheritDoc} */
        @Override
        public void onTimeout() {
            info("Received timeout callback: " + this);
            latch.countDown();
        }

        /** {@inheritDoc} */
        @Override
        public String toString() {
            return "Timeout test object [endTime=" + endTime + ", id=" + id + ']';
        }
    });
    ctx.timeout().addTimeoutObject(new GridTimeoutObject() {

        /** Timeout ID. */
        private final IgniteUuid id = IgniteUuid.randomUuid();

        /** End time. */
        private final long endTime = endTime0;

        /** {@inheritDoc} */
        @Override
        public IgniteUuid timeoutId() {
            return id;
        }

        /** {@inheritDoc} */
        @Override
        public long endTime() {
            return endTime;
        }

        /** {@inheritDoc} */
        @Override
        public void onTimeout() {
            info("Received timeout callback: " + this);
            latch.countDown();
        }

        /** {@inheritDoc} */
        @Override
        public String toString() {
            return "Timeout test object [endTime=" + endTime + ", id=" + id + ']';
        }
    });
    assert latch.await(3000, MILLISECONDS);
}
Also used : IgniteUuid(org.apache.ignite.lang.IgniteUuid) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

IgniteUuid (org.apache.ignite.lang.IgniteUuid)98 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)22 UUID (java.util.UUID)19 IgniteException (org.apache.ignite.IgniteException)17 HashMap (java.util.HashMap)13 Map (java.util.Map)11 IgfsPath (org.apache.ignite.igfs.IgfsPath)11 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)10 ArrayList (java.util.ArrayList)9 ClusterNode (org.apache.ignite.cluster.ClusterNode)9 Nullable (org.jetbrains.annotations.Nullable)9 IgfsOutputStream (org.apache.ignite.igfs.IgfsOutputStream)7 TreeSet (java.util.TreeSet)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 IgfsException (org.apache.ignite.igfs.IgfsException)6 IgfsPathNotFoundException (org.apache.ignite.igfs.IgfsPathNotFoundException)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Event (org.apache.ignite.events.Event)5 TaskEvent (org.apache.ignite.events.TaskEvent)5 IgfsPathIsDirectoryException (org.apache.ignite.igfs.IgfsPathIsDirectoryException)5