Search in sources :

Example 1 with ServerCheckResponse

use of com.alibaba.nacos.api.remote.response.ServerCheckResponse in project nacos by alibaba.

the class GrpcClient method connectToServer.

@Override
public Connection connectToServer(ServerInfo serverInfo) {
    try {
        if (grpcExecutor == null) {
            this.grpcExecutor = createGrpcExecutor(serverInfo.getServerIp());
        }
        int port = serverInfo.getServerPort() + rpcPortOffset();
        RequestGrpc.RequestFutureStub newChannelStubTemp = createNewChannelStub(serverInfo.getServerIp(), port);
        if (newChannelStubTemp != null) {
            Response response = serverCheck(serverInfo.getServerIp(), port, newChannelStubTemp);
            if (response == null || !(response instanceof ServerCheckResponse)) {
                shuntDownChannel((ManagedChannel) newChannelStubTemp.getChannel());
                return null;
            }
            BiRequestStreamGrpc.BiRequestStreamStub biRequestStreamStub = BiRequestStreamGrpc.newStub(newChannelStubTemp.getChannel());
            GrpcConnection grpcConn = new GrpcConnection(serverInfo, grpcExecutor);
            grpcConn.setConnectionId(((ServerCheckResponse) response).getConnectionId());
            // create stream request and bind connection event to this connection.
            StreamObserver<Payload> payloadStreamObserver = bindRequestStream(biRequestStreamStub, grpcConn);
            // stream observer to send response to server
            grpcConn.setPayloadStreamObserver(payloadStreamObserver);
            grpcConn.setGrpcFutureServiceStub(newChannelStubTemp);
            grpcConn.setChannel((ManagedChannel) newChannelStubTemp.getChannel());
            // send a  setup request.
            ConnectionSetupRequest conSetupRequest = new ConnectionSetupRequest();
            conSetupRequest.setClientVersion(VersionUtils.getFullClientVersion());
            conSetupRequest.setLabels(super.getLabels());
            conSetupRequest.setAbilities(super.clientAbilities);
            conSetupRequest.setTenant(super.getTenant());
            grpcConn.sendRequest(conSetupRequest);
            // wait to register connection setup
            Thread.sleep(100L);
            return grpcConn;
        }
        return null;
    } catch (Exception e) {
        LOGGER.error("[{}]Fail to connect to server!,error={}", GrpcClient.this.getName(), e);
    }
    return null;
}
Also used : ServerCheckResponse(com.alibaba.nacos.api.remote.response.ServerCheckResponse) Response(com.alibaba.nacos.api.remote.response.Response) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) ServerCheckResponse(com.alibaba.nacos.api.remote.response.ServerCheckResponse) BiRequestStreamGrpc(com.alibaba.nacos.api.grpc.auto.BiRequestStreamGrpc) Payload(com.alibaba.nacos.api.grpc.auto.Payload) ConnectionSetupRequest(com.alibaba.nacos.api.remote.request.ConnectionSetupRequest) RequestGrpc(com.alibaba.nacos.api.grpc.auto.RequestGrpc) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 2 with ServerCheckResponse

use of com.alibaba.nacos.api.remote.response.ServerCheckResponse in project nacos by alibaba.

the class GrpcRequestAcceptorTest method testServerCheckRequest.

@Test
public void testServerCheckRequest() {
    ApplicationUtils.setStarted(true);
    RequestMeta metadata = new RequestMeta();
    metadata.setClientIp("127.0.0.1");
    metadata.setConnectionId(connectId);
    ServerCheckRequest serverCheckRequest = new ServerCheckRequest();
    serverCheckRequest.setRequestId(requestId);
    Payload request = GrpcUtils.convert(serverCheckRequest, metadata);
    StreamObserver<Payload> streamObserver = new StreamObserver<Payload>() {

        @Override
        public void onNext(Payload payload) {
            System.out.println("Receive data from server: " + payload);
            Object res = GrpcUtils.parse(payload);
            Assert.assertTrue(res instanceof ServerCheckResponse);
        }

        @Override
        public void onError(Throwable throwable) {
            Assert.fail(throwable.getMessage());
        }

        @Override
        public void onCompleted() {
            System.out.println("complete");
        }
    };
    streamStub.request(request, streamObserver);
    ApplicationUtils.setStarted(false);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCheckResponse(com.alibaba.nacos.api.remote.response.ServerCheckResponse) ServerCheckRequest(com.alibaba.nacos.api.remote.request.ServerCheckRequest) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) Payload(com.alibaba.nacos.api.grpc.auto.Payload) Test(org.junit.Test)

Example 3 with ServerCheckResponse

use of com.alibaba.nacos.api.remote.response.ServerCheckResponse in project nacos by alibaba.

the class GrpcRequestAcceptor method request.

@Override
public void request(Payload grpcRequest, StreamObserver<Payload> responseObserver) {
    traceIfNecessary(grpcRequest, true);
    String type = grpcRequest.getMetadata().getType();
    // server is on starting.
    if (!ApplicationUtils.isStarted()) {
        Payload payloadResponse = GrpcUtils.convert(ErrorResponse.build(NacosException.INVALID_SERVER_STATUS, "Server is starting,please try later."));
        traceIfNecessary(payloadResponse, false);
        responseObserver.onNext(payloadResponse);
        responseObserver.onCompleted();
        return;
    }
    // server check.
    if (ServerCheckRequest.class.getSimpleName().equals(type)) {
        Payload serverCheckResponseP = GrpcUtils.convert(new ServerCheckResponse(CONTEXT_KEY_CONN_ID.get()));
        traceIfNecessary(serverCheckResponseP, false);
        responseObserver.onNext(serverCheckResponseP);
        responseObserver.onCompleted();
        return;
    }
    RequestHandler requestHandler = requestHandlerRegistry.getByRequestType(type);
    // no handler found.
    if (requestHandler == null) {
        Loggers.REMOTE_DIGEST.warn(String.format("[%s] No handler for request type : %s :", "grpc", type));
        Payload payloadResponse = GrpcUtils.convert(ErrorResponse.build(NacosException.NO_HANDLER, "RequestHandler Not Found"));
        traceIfNecessary(payloadResponse, false);
        responseObserver.onNext(payloadResponse);
        responseObserver.onCompleted();
        return;
    }
    // check connection status.
    String connectionId = CONTEXT_KEY_CONN_ID.get();
    boolean requestValid = connectionManager.checkValid(connectionId);
    if (!requestValid) {
        Loggers.REMOTE_DIGEST.warn("[{}] Invalid connection Id ,connection [{}] is un registered ,", "grpc", connectionId);
        Payload payloadResponse = GrpcUtils.convert(ErrorResponse.build(NacosException.UN_REGISTER, "Connection is unregistered."));
        traceIfNecessary(payloadResponse, false);
        responseObserver.onNext(payloadResponse);
        responseObserver.onCompleted();
        return;
    }
    Object parseObj = null;
    try {
        parseObj = GrpcUtils.parse(grpcRequest);
    } catch (Exception e) {
        Loggers.REMOTE_DIGEST.warn("[{}] Invalid request receive from connection [{}] ,error={}", "grpc", connectionId, e);
        Payload payloadResponse = GrpcUtils.convert(ErrorResponse.build(NacosException.BAD_GATEWAY, e.getMessage()));
        traceIfNecessary(payloadResponse, false);
        responseObserver.onNext(payloadResponse);
        responseObserver.onCompleted();
        return;
    }
    if (parseObj == null) {
        Loggers.REMOTE_DIGEST.warn("[{}] Invalid request receive  ,parse request is null", connectionId);
        Payload payloadResponse = GrpcUtils.convert(ErrorResponse.build(NacosException.BAD_GATEWAY, "Invalid request"));
        traceIfNecessary(payloadResponse, false);
        responseObserver.onNext(payloadResponse);
        responseObserver.onCompleted();
        return;
    }
    if (!(parseObj instanceof Request)) {
        Loggers.REMOTE_DIGEST.warn("[{}] Invalid request receive  ,parsed payload is not a request,parseObj={}", connectionId, parseObj);
        Payload payloadResponse = GrpcUtils.convert(ErrorResponse.build(NacosException.BAD_GATEWAY, "Invalid request"));
        traceIfNecessary(payloadResponse, false);
        responseObserver.onNext(payloadResponse);
        responseObserver.onCompleted();
        return;
    }
    Request request = (Request) parseObj;
    try {
        Connection connection = connectionManager.getConnection(CONTEXT_KEY_CONN_ID.get());
        RequestMeta requestMeta = new RequestMeta();
        requestMeta.setClientIp(connection.getMetaInfo().getClientIp());
        requestMeta.setConnectionId(CONTEXT_KEY_CONN_ID.get());
        requestMeta.setClientVersion(connection.getMetaInfo().getVersion());
        requestMeta.setLabels(connection.getMetaInfo().getLabels());
        connectionManager.refreshActiveTime(requestMeta.getConnectionId());
        Response response = requestHandler.handleRequest(request, requestMeta);
        Payload payloadResponse = GrpcUtils.convert(response);
        traceIfNecessary(payloadResponse, false);
        responseObserver.onNext(payloadResponse);
        responseObserver.onCompleted();
    } catch (Throwable e) {
        Loggers.REMOTE_DIGEST.error("[{}] Fail to handle request from connection [{}] ,error message :{}", "grpc", connectionId, e);
        Payload payloadResponse = GrpcUtils.convert(ErrorResponse.build((e instanceof NacosException) ? ((NacosException) e).getErrCode() : ResponseCode.FAIL.getCode(), e.getMessage()));
        traceIfNecessary(payloadResponse, false);
        responseObserver.onNext(payloadResponse);
        responseObserver.onCompleted();
    }
}
Also used : ServerCheckResponse(com.alibaba.nacos.api.remote.response.ServerCheckResponse) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) ServerCheckRequest(com.alibaba.nacos.api.remote.request.ServerCheckRequest) Request(com.alibaba.nacos.api.remote.request.Request) Connection(com.alibaba.nacos.core.remote.Connection) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosException(com.alibaba.nacos.api.exception.NacosException) Response(com.alibaba.nacos.api.remote.response.Response) ServerCheckResponse(com.alibaba.nacos.api.remote.response.ServerCheckResponse) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) ServerCheckRequest(com.alibaba.nacos.api.remote.request.ServerCheckRequest) RequestHandler(com.alibaba.nacos.core.remote.RequestHandler) Payload(com.alibaba.nacos.api.grpc.auto.Payload)

Aggregations

Payload (com.alibaba.nacos.api.grpc.auto.Payload)3 ServerCheckResponse (com.alibaba.nacos.api.remote.response.ServerCheckResponse)3 NacosException (com.alibaba.nacos.api.exception.NacosException)2 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)2 ServerCheckRequest (com.alibaba.nacos.api.remote.request.ServerCheckRequest)2 ErrorResponse (com.alibaba.nacos.api.remote.response.ErrorResponse)2 Response (com.alibaba.nacos.api.remote.response.Response)2 BiRequestStreamGrpc (com.alibaba.nacos.api.grpc.auto.BiRequestStreamGrpc)1 RequestGrpc (com.alibaba.nacos.api.grpc.auto.RequestGrpc)1 ConnectionSetupRequest (com.alibaba.nacos.api.remote.request.ConnectionSetupRequest)1 Request (com.alibaba.nacos.api.remote.request.Request)1 Connection (com.alibaba.nacos.core.remote.Connection)1 RequestHandler (com.alibaba.nacos.core.remote.RequestHandler)1 StreamObserver (io.grpc.stub.StreamObserver)1 Test (org.junit.Test)1