Search in sources :

Example 16 with GridRestResponse

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

the class GridTopologyCommandHandler method handleAsync.

/**
 * {@inheritDoc}
 */
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) {
    assert req instanceof GridRestTopologyRequest : "Invalid command for topology handler: " + req;
    assert SUPPORTED_COMMANDS.contains(req.command());
    if (log.isDebugEnabled())
        log.debug("Handling topology REST request: " + req);
    GridRestTopologyRequest req0 = (GridRestTopologyRequest) req;
    GridRestResponse res = new GridRestResponse();
    boolean mtr = req0.includeMetrics();
    boolean attr = req0.includeAttributes();
    boolean caches = req0.includeCaches();
    switch(req.command()) {
        case TOPOLOGY:
            {
                Collection<ClusterNode> allNodes = F.concat(false, ctx.discovery().allNodes(), ctx.discovery().daemonNodes());
                Collection<GridClientNodeBean> top = new ArrayList<>(allNodes.size());
                for (ClusterNode node : allNodes) top.add(createNodeBean(node, mtr, attr, caches));
                res.setResponse(top);
                break;
            }
        case NODE:
            {
                UUID id = req0.nodeId();
                final String ip = req0.nodeIp();
                if (id == null && ip == null)
                    return new GridFinishedFuture<>(new IgniteCheckedException("Failed to handle request (either id or ip should be specified)."));
                ClusterNode node;
                if (id != null) {
                    // Always refresh topology so client see most up-to-date view.
                    ctx.discovery().alive(id);
                    node = ctx.grid().cluster().node(id);
                    if (ip != null && node != null && !containsIp(node.addresses(), ip))
                        node = null;
                } else
                    node = F.find(ctx.discovery().allNodes(), null, new P1<ClusterNode>() {

                        @Override
                        public boolean apply(ClusterNode n) {
                            return containsIp(n.addresses(), ip);
                        }
                    });
                if (node != null)
                    res.setResponse(createNodeBean(node, mtr, attr, caches));
                else
                    res.setResponse(null);
                break;
            }
        default:
            assert false : "Invalid command for topology handler: " + req;
    }
    if (log.isDebugEnabled())
        log.debug("Handled topology REST request [res=" + res + ", req=" + req + ']');
    return new GridFinishedFuture<>(res);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) P1(org.apache.ignite.internal.util.typedef.P1) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRestTopologyRequest(org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) Collection(java.util.Collection) UUID(java.util.UUID) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 17 with GridRestResponse

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

the class GridTcpRestNioListener method onMessage.

/**
 * {@inheritDoc}
 */
@Override
public void onMessage(final GridNioSession ses, final GridClientMessage msg) {
    if (msg instanceof GridMemcachedMessage)
        memcachedLsnr.onMessage(ses, (GridMemcachedMessage) msg);
    else if (msg instanceof GridRedisMessage)
        redisLsnr.onMessage(ses, (GridRedisMessage) msg);
    else if (msg instanceof GridClientPingPacket)
        ses.send(msg);
    else if (msg instanceof GridClientHandshakeRequest) {
        GridClientHandshakeRequest hs = (GridClientHandshakeRequest) msg;
        short ver = hs.version();
        if (!SUPP_VERS.contains(ver)) {
            U.error(log, "Client protocol version is not supported [ses=" + ses + ", ver=" + ver + ", supported=" + SUPP_VERS + ']');
            onSessionClosed(ses);
        } else {
            byte marshId = hs.marshallerId();
            if (marshMapLatch.getCount() > 0) {
                try {
                    U.await(marshMapLatch);
                } catch (IgniteInterruptedCheckedException e) {
                    U.error(log, "Marshaller is not initialized.", e);
                    onSessionClosed(ses);
                    return;
                }
            }
            GridClientMarshaller marsh = marshMap.get(marshId);
            if (marsh == null) {
                U.error(log, "Client marshaller ID is invalid. Note that .NET and C++ clients " + "are supported only in enterprise edition [ses=" + ses + ", marshId=" + marshId + ']');
                onSessionClosed(ses);
            } else {
                ses.addMeta(MARSHALLER.ordinal(), marsh);
                ses.send(GridClientHandshakeResponse.OK);
            }
        }
    } else {
        final GridRestRequest req = createRestRequest(ses, msg);
        if (req != null) {
            IgniteInternalFuture<GridRestResponse> taskFut = hnd.handleAsync(req);
            if (isInterruptible(msg))
                addFutureToSession(ses, taskFut);
            taskFut.listen(new CI1<IgniteInternalFuture<GridRestResponse>>() {

                @Override
                public void apply(IgniteInternalFuture<GridRestResponse> fut) {
                    removeFutureFromSession(ses, taskFut);
                    GridClientResponse res = new GridClientResponse();
                    res.requestId(msg.requestId());
                    res.clientId(msg.clientId());
                    try {
                        GridRestResponse restRes = fut.get();
                        res.sessionToken(restRes.sessionTokenBytes());
                        res.successStatus(restRes.getSuccessStatus());
                        res.errorMessage(restRes.getError());
                        Object o = restRes.getResponse();
                        // In case of metrics a little adjustment is needed.
                        if (o instanceof GridCacheRestMetrics)
                            o = ((GridCacheRestMetrics) o).map();
                        res.result(o);
                    } catch (IgniteCheckedException e) {
                        U.error(log, "Failed to process client request: " + msg, e);
                        res.successStatus(GridClientResponse.STATUS_FAILED);
                        res.errorMessage("Failed to process client request: " + e.getMessage());
                    }
                    GridNioFuture<?> sf = ses.send(res);
                    // Check if send failed.
                    sf.listen(new CI1<IgniteInternalFuture<?>>() {

                        @Override
                        public void apply(IgniteInternalFuture<?> fut) {
                            try {
                                fut.get();
                            } catch (IgniteCheckedException e) {
                                U.error(log, "Failed to process client request [ses=" + ses + ", msg=" + msg + ']', e);
                            }
                        }
                    });
                }
            });
        } else
            U.error(log, "Failed to process client request (unknown packet type) [ses=" + ses + ", msg=" + msg + ']');
    }
}
Also used : GridClientResponse(org.apache.ignite.internal.processors.rest.client.message.GridClientResponse) GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) CI1(org.apache.ignite.internal.util.typedef.CI1) GridClientPingPacket(org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket) GridRedisMessage(org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisMessage) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) GridClientHandshakeRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeRequest) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) GridCacheRestMetrics(org.apache.ignite.internal.processors.rest.handlers.cache.GridCacheRestMetrics) GridRestRequest(org.apache.ignite.internal.processors.rest.request.GridRestRequest)

Example 18 with GridRestResponse

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

the class GridLogCommandHandlerTest method testHandleAsyncPathIsOutsideIgniteHome.

/**
 * @throws Exception If failed.
 */
@Test
public void testHandleAsyncPathIsOutsideIgniteHome() throws Exception {
    IgniteConfiguration cfg = new IgniteConfiguration();
    cfg.setIgniteHome(igniteHome);
    GridTestKernalContext ctx = newContext(cfg);
    GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
    GridRestLogRequest req = new GridRestLogRequest();
    req.to(5);
    req.from(2);
    req.path("/home/users/mytest.log");
    IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
    assertEquals("Request parameter 'path' must contain a path to valid log file.", resp.result().getError());
    assertEquals(GridRestResponse.STATUS_FAILED, resp.result().getSuccessStatus());
    assertNull(resp.result().getResponse());
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) GridRestLogRequest(org.apache.ignite.internal.processors.rest.request.GridRestLogRequest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 19 with GridRestResponse

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

the class GridLogCommandHandlerTest method testHandleAsync.

/**
 * @throws Exception If failed.
 */
@Test
public void testHandleAsync() throws Exception {
    IgniteConfiguration cfg = new IgniteConfiguration();
    cfg.setIgniteHome(igniteHome);
    GridTestKernalContext ctx = newContext(cfg);
    GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
    GridRestLogRequest req = new GridRestLogRequest();
    req.to(5);
    req.from(2);
    req.path(igniteHome + "/work/log/" + "test.log");
    IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
    assertNull(resp.result().getError());
    assertEquals(GridRestResponse.STATUS_SUCCESS, resp.result().getSuccessStatus());
    assertNotNull(resp.result().getResponse());
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) GridRestLogRequest(org.apache.ignite.internal.processors.rest.request.GridRestLogRequest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 20 with GridRestResponse

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

the class GridLogCommandHandlerTest method testHandleAsyncFromEqualTo.

/**
 * @throws Exception If failed.
 */
@Test
public void testHandleAsyncFromEqualTo() throws Exception {
    IgniteConfiguration cfg = new IgniteConfiguration();
    cfg.setIgniteHome(igniteHome);
    GridTestKernalContext ctx = newContext(cfg);
    GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
    GridRestLogRequest req = new GridRestLogRequest();
    req.to(5);
    req.from(5);
    req.path(igniteHome + "/work/log/" + "test.log");
    IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
    assertEquals("Request parameter 'from' must be less than 'to'.", resp.result().getError());
    assertEquals(GridRestResponse.STATUS_FAILED, resp.result().getSuccessStatus());
    assertNull(resp.result().getResponse());
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) GridRestLogRequest(org.apache.ignite.internal.processors.rest.request.GridRestLogRequest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

GridRestResponse (org.apache.ignite.internal.processors.rest.GridRestResponse)24 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)11 Test (org.junit.Test)11 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 GridRestLogRequest (org.apache.ignite.internal.processors.rest.request.GridRestLogRequest)8 GridTestKernalContext (org.apache.ignite.testframework.junits.GridTestKernalContext)7 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)6 GridRestCacheRequest (org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)4 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 GridRestCommand (org.apache.ignite.internal.processors.rest.GridRestCommand)3 RestQueryRequest (org.apache.ignite.internal.processors.rest.request.RestQueryRequest)3 GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 UUID (java.util.UUID)2 IgniteException (org.apache.ignite.IgniteException)2