Search in sources :

Example 1 with ConnectionSetupRequest

use of com.alibaba.nacos.api.remote.request.ConnectionSetupRequest 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 ConnectionSetupRequest

use of com.alibaba.nacos.api.remote.request.ConnectionSetupRequest in project nacos by alibaba.

the class GrpcBiStreamRequestAcceptor method requestBiStream.

@Override
public StreamObserver<Payload> requestBiStream(StreamObserver<Payload> responseObserver) {
    StreamObserver<Payload> streamObserver = new StreamObserver<Payload>() {

        final String connectionId = CONTEXT_KEY_CONN_ID.get();

        final Integer localPort = CONTEXT_KEY_CONN_LOCAL_PORT.get();

        final int remotePort = CONTEXT_KEY_CONN_REMOTE_PORT.get();

        String remoteIp = CONTEXT_KEY_CONN_REMOTE_IP.get();

        String clientIp = "";

        @Override
        public void onNext(Payload payload) {
            clientIp = payload.getMetadata().getClientIp();
            traceDetailIfNecessary(payload);
            Object parseObj;
            try {
                parseObj = GrpcUtils.parse(payload);
            } catch (Throwable throwable) {
                Loggers.REMOTE_DIGEST.warn("[{}]Grpc request bi stream,payload parse error={}", connectionId, throwable);
                return;
            }
            if (parseObj == null) {
                Loggers.REMOTE_DIGEST.warn("[{}]Grpc request bi stream,payload parse null ,body={},meta={}", connectionId, payload.getBody().getValue().toStringUtf8(), payload.getMetadata());
                return;
            }
            if (parseObj instanceof ConnectionSetupRequest) {
                ConnectionSetupRequest setUpRequest = (ConnectionSetupRequest) parseObj;
                Map<String, String> labels = setUpRequest.getLabels();
                String appName = "-";
                if (labels != null && labels.containsKey(Constants.APPNAME)) {
                    appName = labels.get(Constants.APPNAME);
                }
                ConnectionMeta metaInfo = new ConnectionMeta(connectionId, payload.getMetadata().getClientIp(), remoteIp, remotePort, localPort, ConnectionType.GRPC.getType(), setUpRequest.getClientVersion(), appName, setUpRequest.getLabels());
                metaInfo.setTenant(setUpRequest.getTenant());
                Connection connection = new GrpcConnection(metaInfo, responseObserver, CONTEXT_KEY_CHANNEL.get());
                connection.setAbilities(setUpRequest.getAbilities());
                boolean rejectSdkOnStarting = metaInfo.isSdkSource() && !ApplicationUtils.isStarted();
                if (rejectSdkOnStarting || !connectionManager.register(connectionId, connection)) {
                    // Not register to the connection manager if current server is over limit or server is starting.
                    try {
                        Loggers.REMOTE_DIGEST.warn("[{}]Connection register fail,reason:{}", connectionId, rejectSdkOnStarting ? " server is not started" : " server is over limited.");
                        connection.request(new ConnectResetRequest(), 3000L);
                        connection.close();
                    } catch (Exception e) {
                        // Do nothing.
                        if (connectionManager.traced(clientIp)) {
                            Loggers.REMOTE_DIGEST.warn("[{}]Send connect reset request error,error={}", connectionId, e);
                        }
                    }
                }
            } else if (parseObj instanceof Response) {
                Response response = (Response) parseObj;
                if (connectionManager.traced(clientIp)) {
                    Loggers.REMOTE_DIGEST.warn("[{}]Receive response of server request  ,response={}", connectionId, response);
                }
                RpcAckCallbackSynchronizer.ackNotify(connectionId, response);
                connectionManager.refreshActiveTime(connectionId);
            } else {
                Loggers.REMOTE_DIGEST.warn("[{}]Grpc request bi stream,unknown payload receive ,parseObj={}", connectionId, parseObj);
            }
        }

        @Override
        public void onError(Throwable t) {
            if (connectionManager.traced(clientIp)) {
                Loggers.REMOTE_DIGEST.warn("[{}]Bi stream on error,error={}", connectionId, t);
            }
            if (responseObserver instanceof ServerCallStreamObserver) {
                ServerCallStreamObserver serverCallStreamObserver = ((ServerCallStreamObserver) responseObserver);
                if (serverCallStreamObserver.isCancelled()) {
                // client close the stream.
                } else {
                    try {
                        serverCallStreamObserver.onCompleted();
                    } catch (Throwable throwable) {
                    // ignore
                    }
                }
            }
        }

        @Override
        public void onCompleted() {
            if (connectionManager.traced(clientIp)) {
                Loggers.REMOTE_DIGEST.warn("[{}]Bi stream on completed", connectionId);
            }
            if (responseObserver instanceof ServerCallStreamObserver) {
                ServerCallStreamObserver serverCallStreamObserver = ((ServerCallStreamObserver) responseObserver);
                if (serverCallStreamObserver.isCancelled()) {
                // client close the stream.
                } else {
                    try {
                        serverCallStreamObserver.onCompleted();
                    } catch (Throwable throwable) {
                    // ignore
                    }
                }
            }
        }
    };
    return streamObserver;
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) Connection(com.alibaba.nacos.core.remote.Connection) ConnectResetRequest(com.alibaba.nacos.api.remote.request.ConnectResetRequest) Response(com.alibaba.nacos.api.remote.response.Response) Payload(com.alibaba.nacos.api.grpc.auto.Payload) ConnectionMeta(com.alibaba.nacos.core.remote.ConnectionMeta) ConnectionSetupRequest(com.alibaba.nacos.api.remote.request.ConnectionSetupRequest)

Example 3 with ConnectionSetupRequest

use of com.alibaba.nacos.api.remote.request.ConnectionSetupRequest in project nacos by alibaba.

the class GrpcBiStreamRequestAcceptorTest method testConnectionSetupRequest.

@Test
public void testConnectionSetupRequest() {
    StreamObserver<Payload> streamObserver = new StreamObserver<Payload>() {

        @Override
        public void onNext(Payload payload) {
            System.out.println("Receive data from server, data: " + payload);
            Assert.assertNotNull(payload);
            ConnectResetRequest connectResetRequest = (ConnectResetRequest) GrpcUtils.parse(payload);
            Response response = new ConnectResetResponse();
            response.setRequestId(connectResetRequest.getRequestId());
            Payload res = GrpcUtils.convert(response);
            payloadStreamObserver.onNext(res);
            payloadStreamObserver.onCompleted();
        }

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

        @Override
        public void onCompleted() {
            System.out.println("complete");
        }
    };
    payloadStreamObserver = streamStub.requestBiStream(streamObserver);
    RequestMeta metadata = new RequestMeta();
    metadata.setClientIp("127.0.0.1");
    metadata.setConnectionId(connectId);
    ConnectionSetupRequest connectionSetupRequest = new ConnectionSetupRequest();
    connectionSetupRequest.setRequestId(requestId);
    connectionSetupRequest.setClientVersion("2.0.3");
    Payload payload = GrpcUtils.convert(connectionSetupRequest, metadata);
    payloadStreamObserver.onNext(payload);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ConnectResetResponse(com.alibaba.nacos.api.remote.response.ConnectResetResponse) Response(com.alibaba.nacos.api.remote.response.Response) ConnectResetResponse(com.alibaba.nacos.api.remote.response.ConnectResetResponse) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) Payload(com.alibaba.nacos.api.grpc.auto.Payload) ConnectionSetupRequest(com.alibaba.nacos.api.remote.request.ConnectionSetupRequest) ConnectResetRequest(com.alibaba.nacos.api.remote.request.ConnectResetRequest) Test(org.junit.Test)

Aggregations

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