Search in sources :

Example 1 with GridClientPingPacket

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

the class GridTcpRouterNioListenerAdapter method onMessage.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("TypeMayBeWeakened")
@Override
public void onMessage(final GridNioSession ses, final GridClientMessage msg) {
    if (msg instanceof GridRouterRequest) {
        GridRouterRequest routerMsg = (GridRouterRequest) msg;
        final UUID clientId = routerMsg.clientId();
        final long reqId = routerMsg.requestId();
        try {
            client.forwardMessage(routerMsg, routerMsg.destinationId(), ses.<Byte>meta(MARSHALLER_ID.ordinal())).listen(new GridClientFutureListener() {

                @Override
                public void onDone(GridClientFuture fut) {
                    try {
                        GridRouterResponse res = (GridRouterResponse) fut.get();
                        // Restoring original request id, because it was overwritten by the client.
                        res.requestId(reqId);
                        ses.send(res);
                    } catch (GridClientException e) {
                        ses.send(makeFailureResponse(e, clientId, reqId));
                    }
                }
            });
        } catch (GridClientException e) {
            ses.send(makeFailureResponse(e, clientId, reqId));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            U.warn(log, "Message forwarding was interrupted (will ignore last message): " + e.getMessage());
        }
    } 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 + ']');
            ses.close();
        } else {
            byte marshId = hs.marshallerId();
            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 + ']');
                ses.close();
            } else {
                ses.addMeta(MARSHALLER_ID.ordinal(), marshId);
                ses.addMeta(MARSHALLER.ordinal(), marsh);
                ses.send(GridClientHandshakeResponse.OK);
            }
        }
    } else if (msg instanceof GridClientPingPacket)
        ses.send(GridClientPingPacket.PING_MESSAGE);
    else
        throw new IllegalArgumentException("Unsupported input message: " + msg);
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridClientFuture(org.apache.ignite.internal.client.GridClientFuture) GridRouterResponse(org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse) GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) GridRouterRequest(org.apache.ignite.internal.processors.rest.client.message.GridRouterRequest) GridClientPingPacket(org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket) GridClientFutureListener(org.apache.ignite.internal.client.GridClientFutureListener) GridClientHandshakeRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeRequest) UUID(java.util.UUID)

Example 2 with GridClientPingPacket

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

the class GridTcpRouterNioParser method encode.

/**
 * {@inheritDoc}
 */
@Override
public ByteBuffer encode(GridNioSession ses, Object msg) throws IOException, IgniteCheckedException {
    sndCnt++;
    if (msg instanceof GridRouterResponse) {
        GridRouterResponse resp = (GridRouterResponse) msg;
        ByteBuffer res = ByteBuffer.allocate(resp.body().length + 45);
        res.put(IGNITE_REQ_FLAG);
        res.putInt(resp.body().length + 40);
        res.putLong(resp.requestId());
        res.put(U.uuidToBytes(resp.clientId()));
        res.put(U.uuidToBytes(resp.destinationId()));
        res.put(resp.body());
        res.flip();
        return res;
    } else if (msg instanceof GridClientResponse) {
        GridClientMarshaller marsh = marshaller(ses);
        GridClientMessage clientMsg = (GridClientMessage) msg;
        ByteBuffer res = marsh.marshal(msg, 45);
        ByteBuffer slice = res.slice();
        slice.put(IGNITE_REQ_FLAG);
        slice.putInt(res.remaining() - 5);
        slice.putLong(clientMsg.requestId());
        slice.put(U.uuidToBytes(clientMsg.clientId()));
        slice.put(U.uuidToBytes(clientMsg.destinationId()));
        return res;
    } else if (msg instanceof GridClientPingPacket || msg instanceof GridClientHandshakeResponse)
        return super.encode(ses, msg);
    else
        throw new IgniteCheckedException("Unsupported message: " + msg);
}
Also used : GridClientResponse(org.apache.ignite.internal.processors.rest.client.message.GridClientResponse) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRouterResponse(org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse) GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) GridClientHandshakeResponse(org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeResponse) GridClientPingPacket(org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket) ByteBuffer(java.nio.ByteBuffer) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Example 3 with GridClientPingPacket

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

the class GridClientNioTcpConnection method makeRequest.

/**
 * Makes request to server via tcp protocol and returns a future that will be completed when response is received.
 *
 * @param msg Message to request,
 * @param fut Future that will handle response.
 * @param routeMode If {@code true} then this method should overwrite session token by the cached one,
 *     otherwise keep original value.
 * @return Response object.
 * @throws GridClientConnectionResetException If request failed.
 * @throws GridClientClosedException If client closed.
 */
private <R> GridClientFutureAdapter<R> makeRequest(GridClientMessage msg, final TcpClientFuture<R> fut, boolean routeMode) throws GridClientConnectionResetException, GridClientClosedException {
    assert msg != null;
    if (msg instanceof GridClientPingPacket) {
        long now = System.currentTimeMillis();
        if (Math.min(now, lastPingRcvTime) - lastPingSndTime >= pingTimeout)
            close(FAILED, false, new IOException("Did not receive any packets within ping response interval (connection is " + "considered to be half-opened) [lastPingReceiveTime=" + lastPingRcvTime + ", lastPingSendTime=" + lastPingSndTime + ", now=" + now + ", timeout=" + pingTimeout + ", addr=" + serverAddress() + ']'));
        else // or we've already waiting for ping response.
        if (now - lastPingSndTime > pingInterval && lastPingRcvTime != Long.MAX_VALUE) {
            lastPingRcvTime = Long.MAX_VALUE;
            ses.send(GridClientPingPacket.PING_MESSAGE);
            lastPingSndTime = now;
        }
    } else {
        long reqId = reqIdCntr.getAndIncrement();
        msg.requestId(reqId);
        if (!routeMode) {
            msg.clientId(clientId);
            msg.sessionToken(sesTok);
        }
        fut.pendingMessage(msg);
        checkClosed(closeReason);
        GridClientFutureAdapter old = pendingReqs.putIfAbsent(reqId, fut);
        assert old == null;
        GridNioFuture<?> sndFut = ses.send(msg);
        lastMsgSndTime = System.currentTimeMillis();
        if (routeMode) {
            sndFut.listen(new CI1<IgniteInternalFuture<?>>() {

                @Override
                public void apply(IgniteInternalFuture<?> sndFut) {
                    try {
                        sndFut.get();
                    } catch (Exception e) {
                        close(FAILED, false, e);
                        fut.onDone(getCloseReasonAsException(FAILED, e));
                    }
                }
            });
        } else {
            try {
                sndFut.get();
            } catch (Exception e) {
                throw new GridClientConnectionResetException("Failed to send message over connection " + "(will try to reconnect): " + serverAddress(), e);
            }
        }
    }
    return fut;
}
Also used : GridClientFutureAdapter(org.apache.ignite.internal.client.impl.GridClientFutureAdapter) GridClientPingPacket(org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket) IOException(java.io.IOException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridClientException(org.apache.ignite.internal.client.GridClientException) GridClientAuthenticationException(org.apache.ignite.internal.client.GridClientAuthenticationException) GridClientClosedException(org.apache.ignite.internal.client.GridClientClosedException) IOException(java.io.IOException)

Example 4 with GridClientPingPacket

use of org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket 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)

Aggregations

GridClientPingPacket (org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 GridClientMarshaller (org.apache.ignite.internal.client.marshaller.GridClientMarshaller)3 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)2 GridClientException (org.apache.ignite.internal.client.GridClientException)2 GridClientHandshakeRequest (org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeRequest)2 GridClientResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientResponse)2 GridRouterResponse (org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 UUID (java.util.UUID)1 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)1 GridClientAuthenticationException (org.apache.ignite.internal.client.GridClientAuthenticationException)1 GridClientClosedException (org.apache.ignite.internal.client.GridClientClosedException)1 GridClientFuture (org.apache.ignite.internal.client.GridClientFuture)1 GridClientFutureListener (org.apache.ignite.internal.client.GridClientFutureListener)1 GridClientFutureAdapter (org.apache.ignite.internal.client.impl.GridClientFutureAdapter)1 GridRestResponse (org.apache.ignite.internal.processors.rest.GridRestResponse)1 GridClientHandshakeResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeResponse)1 GridClientMessage (org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)1