Search in sources :

Example 1 with GridRestCommandHandler

use of org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler in project ignite by apache.

the class TaskCommandHandlerSelfTest method testManyTasksRun.

/**
     * @throws Exception If failed.
     */
public void testManyTasksRun() throws Exception {
    GridClientCompute compute = client.compute();
    for (int i = 0; i < 1000; i++) assertEquals(new Integer("executing".length()), compute.execute(TestTask.class.getName(), "executing"));
    GridClientFactory.stop(client.id(), true);
    IgniteKernal g = (IgniteKernal) grid(0);
    Map<GridRestCommand, GridRestCommandHandler> handlers = U.field(g.context().rest(), "handlers");
    GridTaskCommandHandler taskHnd = (GridTaskCommandHandler) F.find(handlers.values(), null, new P1<GridRestCommandHandler>() {

        @Override
        public boolean apply(GridRestCommandHandler e) {
            return e instanceof GridTaskCommandHandler;
        }
    });
    assertNotNull("GridTaskCommandHandler was not found", taskHnd);
    ConcurrentLinkedHashMap taskDesc = U.field(taskHnd, "taskDescs");
    assertTrue("Task result map size exceeded max value [mapSize=" + taskDesc.sizex() + ", " + "maxSize=" + MAX_TASK_RESULTS + ']', taskDesc.sizex() <= MAX_TASK_RESULTS);
}
Also used : GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) GridRestCommandHandler(org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler) IgniteKernal(org.apache.ignite.internal.IgniteKernal) P1(org.apache.ignite.internal.util.typedef.P1) ConcurrentLinkedHashMap(org.jsr166.ConcurrentLinkedHashMap) GridTaskCommandHandler(org.apache.ignite.internal.processors.rest.handlers.task.GridTaskCommandHandler)

Example 2 with GridRestCommandHandler

use of org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler in project ignite by apache.

the class GridCacheCommandHandlerSelfTest method testCacheGetFailsSyncNotify.

/**
     * Tests the cache failure during the execution of the CACHE_GET command.
     *
     * @throws Exception If failed.
     */
public void testCacheGetFailsSyncNotify() throws Exception {
    GridRestCommandHandler hnd = new TestableCacheCommandHandler(grid().context(), "getAsync");
    GridRestCacheRequest req = new GridRestCacheRequest();
    req.cacheName(DEFAULT_CACHE_NAME);
    req.command(GridRestCommand.CACHE_GET);
    req.key("k1");
    try {
        hnd.handleAsync(req).get();
        fail("Expected exception not thrown.");
    } catch (IgniteCheckedException e) {
        info("Got expected exception: " + e);
    }
}
Also used : GridRestCommandHandler(org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler) GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 3 with GridRestCommandHandler

use of org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler in project ignite by apache.

the class GridCacheCommandHandlerSelfTest method testAppend.

/**
     * Test cache handler append/prepend commands with specified environment.
     *
     * @param curVal Current value in cache.
     * @param newVal New value to append/prepend.
     * @param append Append or prepend flag.
     * @param <T> Cache value type.
     * @return Resulting value in cache.
     * @throws IgniteCheckedException In case of any grid exception.
     */
private <T> T testAppend(T curVal, T newVal, boolean append) throws IgniteCheckedException, EntryProcessorException {
    GridRestCommandHandler hnd = new GridCacheCommandHandler(((IgniteKernal) grid()).context());
    String key = UUID.randomUUID().toString();
    GridRestCacheRequest req = new GridRestCacheRequest();
    req.cacheName(DEFAULT_CACHE_NAME);
    req.command(append ? GridRestCommand.CACHE_APPEND : GridRestCommand.CACHE_PREPEND);
    req.key(key);
    req.value(newVal);
    assertFalse("Expects failure due to no value in cache.", (Boolean) hnd.handleAsync(req).get().getResponse());
    T res;
    try {
        // Change cache state.
        jcache().put(key, curVal);
        // Validate behavior for initialized cache (has current value).
        assertTrue((Boolean) hnd.handleAsync(req).get().getResponse());
    } finally {
        res = (T) jcache().getAndRemove(key);
    }
    return res;
}
Also used : GridRestCommandHandler(org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler) GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)

Example 4 with GridRestCommandHandler

use of org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler in project ignite by apache.

the class JettyRestProcessorAbstractSelfTest method queryCursorFound.

/**
     * @return True if any query cursor is available.
     */
private boolean queryCursorFound() {
    boolean found = false;
    for (int i = 0; i < GRID_CNT; ++i) {
        Map<GridRestCommand, GridRestCommandHandler> handlers = GridTestUtils.getFieldValue(grid(i).context().rest(), "handlers");
        GridRestCommandHandler qryHnd = handlers.get(GridRestCommand.CLOSE_SQL_QUERY);
        ConcurrentHashMap<Long, Iterator> its = GridTestUtils.getFieldValue(qryHnd, "qryCurs");
        found |= its.size() != 0;
    }
    return found;
}
Also used : GridRestCommandHandler(org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler) Iterator(java.util.Iterator)

Example 5 with GridRestCommandHandler

use of org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler in project ignite by apache.

the class GridRestProcessor method handleRequest.

/**
     * @param req Request.
     * @return Future.
     */
private IgniteInternalFuture<GridRestResponse> handleRequest(final GridRestRequest req) {
    if (startLatch.getCount() > 0) {
        try {
            startLatch.await();
        } catch (InterruptedException e) {
            return new GridFinishedFuture<>(new IgniteCheckedException("Failed to handle request " + "(protocol handler was interrupted when awaiting grid start).", e));
        }
    }
    if (log.isDebugEnabled())
        log.debug("Received request from client: " + req);
    if (ctx.security().enabled()) {
        Session ses;
        try {
            ses = session(req);
        } catch (IgniteCheckedException e) {
            GridRestResponse res = new GridRestResponse(STATUS_FAILED, e.getMessage());
            return new GridFinishedFuture<>(res);
        }
        assert ses != null;
        req.clientId(ses.clientId);
        req.sessionToken(U.uuidToBytes(ses.sesId));
        if (log.isDebugEnabled())
            log.debug("Next clientId and sessionToken were extracted according to request: " + "[clientId=" + req.clientId() + ", sesTok=" + Arrays.toString(req.sessionToken()) + "]");
        SecurityContext secCtx0 = ses.secCtx;
        try {
            if (secCtx0 == null)
                ses.secCtx = secCtx0 = authenticate(req);
            authorize(req, secCtx0);
        } catch (SecurityException e) {
            assert secCtx0 != null;
            GridRestResponse res = new GridRestResponse(STATUS_SECURITY_CHECK_FAILED, e.getMessage());
            return new GridFinishedFuture<>(res);
        } catch (IgniteCheckedException e) {
            return new GridFinishedFuture<>(new GridRestResponse(STATUS_AUTH_FAILED, e.getMessage()));
        }
    }
    interceptRequest(req);
    GridRestCommandHandler hnd = handlers.get(req.command());
    IgniteInternalFuture<GridRestResponse> res = hnd == null ? null : hnd.handleAsync(req);
    if (res == null)
        return new GridFinishedFuture<>(new IgniteCheckedException("Failed to find registered handler for command: " + req.command()));
    return res.chain(new C1<IgniteInternalFuture<GridRestResponse>, GridRestResponse>() {

        @Override
        public GridRestResponse apply(IgniteInternalFuture<GridRestResponse> f) {
            GridRestResponse res;
            boolean failed = false;
            try {
                res = f.get();
            } catch (Exception e) {
                failed = true;
                if (!X.hasCause(e, VisorClusterGroupEmptyException.class))
                    LT.error(log, e, "Failed to handle request: " + req.command());
                if (log.isDebugEnabled())
                    log.debug("Failed to handle request [req=" + req + ", e=" + e + "]");
                // Prepare error message:
                SB sb = new SB(256);
                sb.a("Failed to handle request: [req=").a(req.command());
                if (req instanceof GridRestTaskRequest) {
                    GridRestTaskRequest tskReq = (GridRestTaskRequest) req;
                    sb.a(", taskName=").a(tskReq.taskName()).a(", params=").a(tskReq.params());
                }
                sb.a(", err=").a(e.getMessage() != null ? e.getMessage() : e.getClass().getName()).a(']');
                res = new GridRestResponse(STATUS_FAILED, sb.toString());
            }
            assert res != null;
            if (ctx.security().enabled() && !failed)
                res.sessionTokenBytes(req.sessionToken());
            interceptResponse(res, req);
            return res;
        }
    });
}
Also used : SecurityException(org.apache.ignite.plugin.security.SecurityException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SecurityException(org.apache.ignite.plugin.security.SecurityException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) VisorClusterGroupEmptyException(org.apache.ignite.internal.visor.util.VisorClusterGroupEmptyException) SB(org.apache.ignite.internal.util.typedef.internal.SB) GridRestCommandHandler(org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRestTaskRequest(org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest) SecurityContext(org.apache.ignite.internal.processors.security.SecurityContext) VisorClusterGroupEmptyException(org.apache.ignite.internal.visor.util.VisorClusterGroupEmptyException)

Aggregations

GridRestCommandHandler (org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 GridRestCacheRequest (org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Iterator (java.util.Iterator)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 IgniteKernal (org.apache.ignite.internal.IgniteKernal)1 GridClientCompute (org.apache.ignite.internal.client.GridClientCompute)1 GridTaskCommandHandler (org.apache.ignite.internal.processors.rest.handlers.task.GridTaskCommandHandler)1 GridRestTaskRequest (org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest)1 SecurityContext (org.apache.ignite.internal.processors.security.SecurityContext)1 P1 (org.apache.ignite.internal.util.typedef.P1)1 SB (org.apache.ignite.internal.util.typedef.internal.SB)1 VisorClusterGroupEmptyException (org.apache.ignite.internal.visor.util.VisorClusterGroupEmptyException)1 SecurityException (org.apache.ignite.plugin.security.SecurityException)1 ConcurrentLinkedHashMap (org.jsr166.ConcurrentLinkedHashMap)1