Search in sources :

Example 11 with Payload

use of com.alibaba.nacos.api.grpc.auto.Payload in project nacos by alibaba.

the class GrpcRequestAcceptorTest method testRequestContentError.

@Test
public void testRequestContentError() {
    ApplicationUtils.setStarted(true);
    Mockito.when(requestHandlerRegistry.getByRequestType(Mockito.anyString())).thenReturn(mockHandler);
    Mockito.when(connectionManager.checkValid(Mockito.any())).thenReturn(true);
    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 ErrorResponse);
            ErrorResponse errorResponse = (ErrorResponse) res;
            Assert.assertEquals(errorResponse.getErrorCode(), NacosException.BAD_GATEWAY);
        }

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

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

Example 12 with Payload

use of com.alibaba.nacos.api.grpc.auto.Payload 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 13 with Payload

use of com.alibaba.nacos.api.grpc.auto.Payload in project nacos by alibaba.

the class GrpcConnection method sendRequest.

public void sendRequest(Request request) {
    Payload convert = GrpcUtils.convert(request);
    payloadStreamObserver.onNext(convert);
}
Also used : Payload(com.alibaba.nacos.api.grpc.auto.Payload)

Example 14 with Payload

use of com.alibaba.nacos.api.grpc.auto.Payload in project nacos by alibaba.

the class GrpcConnection method asyncRequest.

@Override
public void asyncRequest(Request request, final RequestCallBack requestCallBack) throws NacosException {
    Payload grpcRequest = GrpcUtils.convert(request);
    ListenableFuture<Payload> requestFuture = grpcFutureServiceStub.request(grpcRequest);
    // set callback .
    Futures.addCallback(requestFuture, new FutureCallback<Payload>() {

        @Override
        public void onSuccess(@Nullable Payload grpcResponse) {
            Response response = (Response) GrpcUtils.parse(grpcResponse);
            if (response != null) {
                if (response instanceof ErrorResponse) {
                    requestCallBack.onException(new NacosException(response.getErrorCode(), response.getMessage()));
                } else {
                    requestCallBack.onResponse(response);
                }
            } else {
                requestCallBack.onException(new NacosException(ResponseCode.FAIL.getCode(), "response is null"));
            }
        }

        @Override
        public void onFailure(Throwable throwable) {
            if (throwable instanceof CancellationException) {
                requestCallBack.onException(new TimeoutException("Timeout after " + requestCallBack.getTimeout() + " milliseconds."));
            } else {
                requestCallBack.onException(throwable);
            }
        }
    }, requestCallBack.getExecutor() != null ? requestCallBack.getExecutor() : this.executor);
    // set timeout future.
    ListenableFuture<Payload> payloadListenableFuture = Futures.withTimeout(requestFuture, requestCallBack.getTimeout(), TimeUnit.MILLISECONDS, RpcScheduledExecutor.TIMEOUT_SCHEDULER);
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) CancellationException(java.util.concurrent.CancellationException) Payload(com.alibaba.nacos.api.grpc.auto.Payload) NacosException(com.alibaba.nacos.api.exception.NacosException) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) TimeoutException(java.util.concurrent.TimeoutException)

Example 15 with Payload

use of com.alibaba.nacos.api.grpc.auto.Payload in project nacos by alibaba.

the class GrpcClient method bindRequestStream.

private StreamObserver<Payload> bindRequestStream(final BiRequestStreamGrpc.BiRequestStreamStub streamStub, final GrpcConnection grpcConn) {
    return streamStub.requestBiStream(new StreamObserver<Payload>() {

        @Override
        public void onNext(Payload payload) {
            LoggerUtils.printIfDebugEnabled(LOGGER, "[{}]Stream server request receive, original info: {}", grpcConn.getConnectionId(), payload.toString());
            try {
                Object parseBody = GrpcUtils.parse(payload);
                final Request request = (Request) parseBody;
                if (request != null) {
                    try {
                        Response response = handleServerRequest(request);
                        if (response != null) {
                            response.setRequestId(request.getRequestId());
                            sendResponse(response);
                        } else {
                            LOGGER.warn("[{}]Fail to process server request, ackId->{}", grpcConn.getConnectionId(), request.getRequestId());
                        }
                    } catch (Exception e) {
                        LoggerUtils.printIfErrorEnabled(LOGGER, "[{}]Handle server request exception: {}", grpcConn.getConnectionId(), payload.toString(), e.getMessage());
                        Response errResponse = ErrorResponse.build(NacosException.CLIENT_ERROR, "Handle server request error");
                        errResponse.setRequestId(request.getRequestId());
                        sendResponse(errResponse);
                    }
                }
            } catch (Exception e) {
                LoggerUtils.printIfErrorEnabled(LOGGER, "[{}]Error to process server push response: {}", grpcConn.getConnectionId(), payload.getBody().getValue().toStringUtf8());
            }
        }

        @Override
        public void onError(Throwable throwable) {
            boolean isRunning = isRunning();
            boolean isAbandon = grpcConn.isAbandon();
            if (isRunning && !isAbandon) {
                LoggerUtils.printIfErrorEnabled(LOGGER, "[{}]Request stream error, switch server,error={}", grpcConn.getConnectionId(), throwable);
                if (rpcClientStatus.compareAndSet(RpcClientStatus.RUNNING, RpcClientStatus.UNHEALTHY)) {
                    switchServerAsync();
                }
            } else {
                LoggerUtils.printIfWarnEnabled(LOGGER, "[{}]Ignore error event,isRunning:{},isAbandon={}", grpcConn.getConnectionId(), isRunning, isAbandon);
            }
        }

        @Override
        public void onCompleted() {
            boolean isRunning = isRunning();
            boolean isAbandon = grpcConn.isAbandon();
            if (isRunning && !isAbandon) {
                LoggerUtils.printIfErrorEnabled(LOGGER, "[{}]Request stream onCompleted, switch server", grpcConn.getConnectionId());
                if (rpcClientStatus.compareAndSet(RpcClientStatus.RUNNING, RpcClientStatus.UNHEALTHY)) {
                    switchServerAsync();
                }
            } else {
                LoggerUtils.printIfInfoEnabled(LOGGER, "[{}]Ignore complete event,isRunning:{},isAbandon={}", grpcConn.getConnectionId(), isRunning, isAbandon);
            }
        }
    });
}
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) Request(com.alibaba.nacos.api.remote.request.Request) ConnectionSetupRequest(com.alibaba.nacos.api.remote.request.ConnectionSetupRequest) ServerCheckRequest(com.alibaba.nacos.api.remote.request.ServerCheckRequest) Payload(com.alibaba.nacos.api.grpc.auto.Payload) NacosException(com.alibaba.nacos.api.exception.NacosException)

Aggregations

Payload (com.alibaba.nacos.api.grpc.auto.Payload)22 ErrorResponse (com.alibaba.nacos.api.remote.response.ErrorResponse)12 Response (com.alibaba.nacos.api.remote.response.Response)9 StreamObserver (io.grpc.stub.StreamObserver)9 NacosException (com.alibaba.nacos.api.exception.NacosException)8 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)8 Test (org.junit.Test)8 ServerCheckRequest (com.alibaba.nacos.api.remote.request.ServerCheckRequest)5 ServerCheckResponse (com.alibaba.nacos.api.remote.response.ServerCheckResponse)5 ConnectionSetupRequest (com.alibaba.nacos.api.remote.request.ConnectionSetupRequest)4 InstanceRequest (com.alibaba.nacos.api.naming.remote.request.InstanceRequest)3 Connection (com.alibaba.nacos.core.remote.Connection)3 Metadata (com.alibaba.nacos.api.grpc.auto.Metadata)2 ConnectResetRequest (com.alibaba.nacos.api.remote.request.ConnectResetRequest)2 Request (com.alibaba.nacos.api.remote.request.Request)2 ConnectionMeta (com.alibaba.nacos.core.remote.ConnectionMeta)2 ByteString (com.google.protobuf.ByteString)2 CancellationException (java.util.concurrent.CancellationException)2 TimeoutException (java.util.concurrent.TimeoutException)2 BiRequestStreamGrpc (com.alibaba.nacos.api.grpc.auto.BiRequestStreamGrpc)1