Search in sources :

Example 1 with GridClientTaskResultBean

use of org.apache.ignite.internal.processors.rest.client.message.GridClientTaskResultBean in project ignite by apache.

the class RestBinaryProtocolSelfTest method testExecute.

/**
 * @throws Exception If failed.
 */
public void testExecute() throws Exception {
    GridClientTaskResultBean res = client.execute(TestTask.class.getName(), Arrays.asList("executing", 3, "test", 5, "task"));
    assertTrue(res.isFinished());
    assertEquals(new Integer(25), res.getResult());
}
Also used : GridClientTaskResultBean(org.apache.ignite.internal.processors.rest.client.message.GridClientTaskResultBean)

Example 2 with GridClientTaskResultBean

use of org.apache.ignite.internal.processors.rest.client.message.GridClientTaskResultBean in project ignite by apache.

the class GridClientNioTcpConnection method execute.

/**
 * {@inheritDoc}
 */
@Override
public <R> GridClientFutureAdapter<R> execute(String taskName, Object arg, UUID destNodeId, final boolean keepBinaries) throws GridClientConnectionResetException, GridClientClosedException {
    GridClientTaskRequest msg = new GridClientTaskRequest();
    msg.taskName(taskName);
    msg.argument(arg);
    msg.keepBinaries(keepBinaries);
    return this.<GridClientTaskResultBean>makeRequest(msg, destNodeId).chain(new GridClientFutureCallback<GridClientTaskResultBean, R>() {

        @Override
        public R onComplete(GridClientFuture<GridClientTaskResultBean> fut) throws GridClientException {
            return fut.get().getResult();
        }
    });
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridClientTaskResultBean(org.apache.ignite.internal.processors.rest.client.message.GridClientTaskResultBean) MARSHALLER(org.apache.ignite.internal.util.nio.GridNioSessionMetaKey.MARSHALLER) GridClientTaskRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientTaskRequest)

Example 3 with GridClientTaskResultBean

use of org.apache.ignite.internal.processors.rest.client.message.GridClientTaskResultBean 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 4 with GridClientTaskResultBean

use of org.apache.ignite.internal.processors.rest.client.message.GridClientTaskResultBean in project ignite by apache.

the class GridRestProcessor method interceptResponse.

/**
 * Applies {@link ConnectorMessageInterceptor} from
 * {@link ConnectorConfiguration#getMessageInterceptor()}
 * to all user objects in the response.
 *
 * @param res Response.
 * @param req Request.
 */
private void interceptResponse(GridRestResponse res, GridRestRequest req) {
    ConnectorMessageInterceptor interceptor = config().getMessageInterceptor();
    if (interceptor != null && res.getResponse() != null) {
        switch(req.command()) {
            case CACHE_CONTAINS_KEYS:
            case CACHE_CONTAINS_KEY:
            case CACHE_GET:
            case CACHE_GET_ALL:
            case CACHE_PUT:
            case CACHE_ADD:
            case CACHE_PUT_ALL:
            case CACHE_REMOVE:
            case CACHE_REMOVE_ALL:
            case CACHE_CLEAR:
            case CACHE_REPLACE:
            case ATOMIC_INCREMENT:
            case ATOMIC_DECREMENT:
            case CACHE_CAS:
            case CACHE_APPEND:
            case CACHE_PREPEND:
                res.setResponse(interceptSendObject(res.getResponse(), interceptor));
                break;
            case EXE:
                if (res.getResponse() instanceof GridClientTaskResultBean) {
                    GridClientTaskResultBean taskRes = (GridClientTaskResultBean) res.getResponse();
                    taskRes.setResult(interceptor.onSend(taskRes.getResult()));
                }
                break;
            default:
                break;
        }
    }
}
Also used : GridClientTaskResultBean(org.apache.ignite.internal.processors.rest.client.message.GridClientTaskResultBean) ConnectorMessageInterceptor(org.apache.ignite.configuration.ConnectorMessageInterceptor)

Aggregations

GridClientTaskResultBean (org.apache.ignite.internal.processors.rest.client.message.GridClientTaskResultBean)4 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 UUID (java.util.UUID)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)1 ConnectorMessageInterceptor (org.apache.ignite.configuration.ConnectorMessageInterceptor)1 ComputeTaskInternalFuture (org.apache.ignite.internal.ComputeTaskInternalFuture)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 GridClientException (org.apache.ignite.internal.client.GridClientException)1 ClusterGroupEmptyCheckedException (org.apache.ignite.internal.cluster.ClusterGroupEmptyCheckedException)1 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)1 GridRestResponse (org.apache.ignite.internal.processors.rest.GridRestResponse)1 GridClientTaskRequest (org.apache.ignite.internal.processors.rest.client.message.GridClientTaskRequest)1 GridRestTaskRequest (org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest)1 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)1 MARSHALLER (org.apache.ignite.internal.util.nio.GridNioSessionMetaKey.MARSHALLER)1 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)1 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)1