Search in sources :

Example 6 with Response

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

the class DistroClientTransportAgent method getData.

@Override
public DistroData getData(DistroKey key, String targetServer) {
    Member member = memberManager.find(targetServer);
    if (checkTargetServerStatusUnhealthy(member)) {
        throw new DistroException(String.format("[DISTRO] Cancel get snapshot caused by target server %s unhealthy", targetServer));
    }
    DistroDataRequest request = new DistroDataRequest();
    DistroData distroData = new DistroData();
    distroData.setDistroKey(key);
    distroData.setType(DataOperation.QUERY);
    request.setDistroData(distroData);
    request.setDataOperation(DataOperation.QUERY);
    try {
        Response response = clusterRpcClientProxy.sendRequest(member, request);
        if (checkResponse(response)) {
            return ((DistroDataResponse) response).getDistroData();
        } else {
            throw new DistroException(String.format("[DISTRO-FAILED] Get data request to %s failed, code: %d, message: %s", targetServer, response.getErrorCode(), response.getMessage()));
        }
    } catch (NacosException e) {
        throw new DistroException("[DISTRO-FAILED] Get distro data failed! ", e);
    }
}
Also used : DistroException(com.alibaba.nacos.core.distributed.distro.exception.DistroException) Response(com.alibaba.nacos.api.remote.response.Response) DistroDataResponse(com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse) DistroDataRequest(com.alibaba.nacos.naming.cluster.remote.request.DistroDataRequest) Member(com.alibaba.nacos.core.cluster.Member) DistroDataResponse(com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse) NacosException(com.alibaba.nacos.api.exception.NacosException) DistroData(com.alibaba.nacos.core.distributed.distro.entity.DistroData)

Example 7 with Response

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

the class DistroClientTransportAgent method syncData.

@Override
public boolean syncData(DistroData data, String targetServer) {
    if (isNoExistTarget(targetServer)) {
        return true;
    }
    DistroDataRequest request = new DistroDataRequest(data, data.getType());
    Member member = memberManager.find(targetServer);
    if (checkTargetServerStatusUnhealthy(member)) {
        Loggers.DISTRO.warn("[DISTRO] Cancel distro sync caused by target server {} unhealthy", targetServer);
        return false;
    }
    try {
        Response response = clusterRpcClientProxy.sendRequest(member, request);
        return checkResponse(response);
    } catch (NacosException e) {
        Loggers.DISTRO.error("[DISTRO-FAILED] Sync distro data failed! ", e);
    }
    return false;
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) DistroDataResponse(com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse) DistroDataRequest(com.alibaba.nacos.naming.cluster.remote.request.DistroDataRequest) Member(com.alibaba.nacos.core.cluster.Member) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 8 with Response

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

the class NamingGrpcClientProxyTest method testServerListChanged.

@Test
public void testServerListChanged() throws Exception {
    RpcClient rpc = new RpcClient("testServerListHasChanged", factory) {

        @Override
        public ConnectionType getConnectionType() {
            return ConnectionType.GRPC;
        }

        @Override
        public int rpcPortOffset() {
            return 0;
        }

        @Override
        public Connection connectToServer(ServerInfo serverInfo) throws Exception {
            return new Connection(serverInfo) {

                @Override
                public Response request(Request request, long timeoutMills) throws NacosException {
                    Response response = new Response() {
                    };
                    response.setRequestId(request.getRequestId());
                    return response;
                }

                @Override
                public RequestFuture requestFuture(Request request) throws NacosException {
                    return new DefaultRequestFuture("test", request.getRequestId());
                }

                @Override
                public void asyncRequest(Request request, RequestCallBack requestCallBack) throws NacosException {
                }

                @Override
                public void close() {
                }
            };
        }
    };
    Field rpcClient = NamingGrpcClientProxy.class.getDeclaredField("rpcClient");
    rpcClient.setAccessible(true);
    rpcClient.set(client, rpc);
    rpc.serverListFactory(factory);
    rpc.registerServerRequestHandler(new NamingPushRequestHandler(holder));
    Field listenerField = NamingGrpcClientProxy.class.getDeclaredField("redoService");
    listenerField.setAccessible(true);
    NamingGrpcRedoService listener = (NamingGrpcRedoService) listenerField.get(client);
    rpc.registerConnectionListener(listener);
    rpc.start();
    int retry = 10;
    while (!rpc.isRunning()) {
        TimeUnit.SECONDS.sleep(1);
        if (--retry < 0) {
            Assert.fail("rpc is not running");
        }
    }
    Assert.assertEquals(ORIGIN_SERVER, rpc.getCurrentServer().getServerIp());
    String newServer = "www.aliyun.com";
    when(factory.genNextServer()).thenReturn(newServer);
    when(factory.getServerList()).thenReturn(Stream.of(newServer, "anotherServer").collect(Collectors.toList()));
    NotifyCenter.publishEvent(new ServerListChangedEvent());
    retry = 10;
    while (ORIGIN_SERVER.equals(rpc.getCurrentServer().getServerIp())) {
        TimeUnit.SECONDS.sleep(1);
        if (--retry < 0) {
            Assert.fail("failed to auth switch server");
        }
    }
    Assert.assertEquals(newServer, rpc.getCurrentServer().getServerIp());
}
Also used : ServerListChangedEvent(com.alibaba.nacos.client.naming.event.ServerListChangedEvent) DefaultRequestFuture(com.alibaba.nacos.api.remote.DefaultRequestFuture) NamingGrpcRedoService(com.alibaba.nacos.client.naming.remote.gprc.redo.NamingGrpcRedoService) RequestCallBack(com.alibaba.nacos.api.remote.RequestCallBack) Connection(com.alibaba.nacos.common.remote.client.Connection) Request(com.alibaba.nacos.api.remote.request.Request) InstanceRequest(com.alibaba.nacos.api.naming.remote.request.InstanceRequest) SubscribeServiceRequest(com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest) QueryServiceResponse(com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse) Response(com.alibaba.nacos.api.remote.response.Response) SubscribeServiceResponse(com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse) ServiceListResponse(com.alibaba.nacos.api.naming.remote.response.ServiceListResponse) InstanceResponse(com.alibaba.nacos.api.naming.remote.response.InstanceResponse) Field(java.lang.reflect.Field) RpcClient(com.alibaba.nacos.common.remote.client.RpcClient) Test(org.junit.Test)

Example 9 with Response

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

the class RequestLogAspect method logClientRequestRpc.

/**
 * Client api request log rt | status | requestIp | opType | dataId | group | datumId | md5.
 */
private Object logClientRequestRpc(String requestType, ProceedingJoinPoint pjp, Request request, RequestMeta meta, String dataId, String group, String tenant, String md5) throws Throwable {
    final String requestIp = meta.getClientIp();
    String appName = request.getHeader(RequestUtil.CLIENT_APPNAME_HEADER);
    final long st = System.currentTimeMillis();
    Response retVal = (Response) pjp.proceed();
    final long rt = System.currentTimeMillis() - st;
    // rt | status | requestIp | opType | dataId | group | datumId | md5 |
    // appName
    LogUtil.CLIENT_LOG.info("{}|{}|{}|{}|{}|{}|{}|{}|{}", rt, retVal.isSuccess() ? retVal.getResultCode() : retVal.getErrorCode(), requestIp, requestType, dataId, group, tenant, md5, appName);
    return retVal;
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 10 with Response

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

the class ClusterRpcClientProxyTest method testSendRequest.

@Test
public void testSendRequest() {
    Member member = new Member();
    member.setIp("1.1.1.1");
    try {
        Response response = clusterRpcClientProxy.sendRequest(member, new HealthCheckRequest());
    } catch (NacosException e) {
        Assert.assertEquals(-401, e.getErrCode());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) HealthCheckRequest(com.alibaba.nacos.api.remote.request.HealthCheckRequest) Member(com.alibaba.nacos.core.cluster.Member) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test)

Aggregations

Response (com.alibaba.nacos.api.remote.response.Response)31 NacosException (com.alibaba.nacos.api.exception.NacosException)23 Payload (com.alibaba.nacos.api.grpc.auto.Payload)9 ErrorResponse (com.alibaba.nacos.api.remote.response.ErrorResponse)9 Member (com.alibaba.nacos.core.cluster.Member)8 Test (org.junit.Test)7 Request (com.alibaba.nacos.api.remote.request.Request)6 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)6 RequestCallBack (com.alibaba.nacos.api.remote.RequestCallBack)5 ConnectionSetupRequest (com.alibaba.nacos.api.remote.request.ConnectionSetupRequest)4 ServerCheckResponse (com.alibaba.nacos.api.remote.response.ServerCheckResponse)4 DistroDataRequest (com.alibaba.nacos.naming.cluster.remote.request.DistroDataRequest)4 DistroDataResponse (com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse)4 ConnectResetRequest (com.alibaba.nacos.api.remote.request.ConnectResetRequest)3 HealthCheckRequest (com.alibaba.nacos.api.remote.request.HealthCheckRequest)3 ServerCheckRequest (com.alibaba.nacos.api.remote.request.ServerCheckRequest)3 ConnectResetResponse (com.alibaba.nacos.api.remote.response.ConnectResetResponse)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 InstanceRequest (com.alibaba.nacos.api.naming.remote.request.InstanceRequest)2