Search in sources :

Example 6 with RetriableException

use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.

the class ConsulAPIConnector method registerInstance.

@Override
public CommonProviderResponse registerInstance(CommonProviderRequest req) throws PolarisException {
    if (!ieRegistered) {
        ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
        try {
            LOG.info("Registering service to Consul");
            NewService service = buildRegisterInstanceRequest(req);
            this.consulClient.agentServiceRegister(service);
            CommonProviderResponse resp = new CommonProviderResponse();
            consulContext.setInstanceId(service.getId());
            resp.setInstanceID(service.getId());
            resp.setExists(true);
            LOG.info("Registered service to Consul: " + service);
            ieRegistered = true;
            return resp;
        } catch (ConsulException e) {
            throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to register host %s:%d service %s", req.getHost(), req.getPort(), serviceKey), e);
        }
    }
    return null;
}
Also used : ConsulException(com.ecwid.consul.ConsulException) CommonProviderResponse(com.tencent.polaris.api.plugin.server.CommonProviderResponse) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) NewService(com.ecwid.consul.v1.agent.model.NewService) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 7 with RetriableException

use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.

the class GrpcConnector method deregisterInstance.

@Override
public void deregisterInstance(CommonProviderRequest req) throws PolarisException {
    checkDestroyed();
    Connection connection = null;
    ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
    try {
        waitDiscoverReady();
        connection = connectionManager.getConnection(GrpcUtil.OP_KEY_DEREGISTER_INSTANCE, ClusterType.SERVICE_DISCOVER_CLUSTER);
        req.setTargetServer(connectionToTargetNode(connection));
        PolarisGRPCGrpc.PolarisGRPCBlockingStub stub = PolarisGRPCGrpc.newBlockingStub(connection.getChannel());
        GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextInstanceDeRegisterReqId());
        ResponseProto.Response deregisterInstanceResponse = stub.deregisterInstance(buildDeregisterInstanceRequest(req));
        GrpcUtil.checkResponse(deregisterInstanceResponse);
        LOG.debug("received deregister response {}", deregisterInstanceResponse);
    } catch (Throwable t) {
        if (t instanceof PolarisException) {
            // 服务端异常不进行重试
            throw t;
        }
        if (null != connection) {
            connection.reportFail();
        }
        throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to deregister id %s, host %s:%d service %s", req.getInstanceID(), req.getHost(), req.getPort(), serviceKey), t);
    } finally {
        if (null != connection) {
            connection.release(GrpcUtil.OP_KEY_DEREGISTER_INSTANCE);
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) PolarisGRPCGrpc(com.tencent.polaris.client.pb.PolarisGRPCGrpc) ResponseProto(com.tencent.polaris.client.pb.ResponseProto) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 8 with RetriableException

use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.

the class GrpcConnector method reportClient.

@Override
public ReportClientResponse reportClient(ReportClientRequest req) throws PolarisException {
    checkDestroyed();
    waitDiscoverReady();
    Connection connection = null;
    ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
    try {
        connection = connectionManager.getConnection(GrpcUtil.OP_KEY_REPORT_CLIENT, ClusterType.SERVICE_DISCOVER_CLUSTER);
        req.setTargetServer(connectionToTargetNode(connection));
        PolarisGRPCGrpc.PolarisGRPCBlockingStub stub = PolarisGRPCGrpc.newBlockingStub(connection.getChannel());
        GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextHeartbeatReqId());
        ClientProto.Client request = buildReportRequest(req);
        ResponseProto.Response response = stub.reportClient(request);
        LOG.debug("reportClient req:{}, rsp:{}", req, TextFormat.shortDebugString(response));
        GrpcUtil.checkResponse(response);
        ReportClientResponse rsp = new ReportClientResponse();
        if (null == response.getClient().getLocation()) {
            throw new IllegalStateException(String.format("unexpected null response from clientReport api, response:%s", TextFormat.shortDebugString(response)));
        }
        rsp.setCampus(response.getClient().getLocation().getCampus().getValue());
        rsp.setZone(response.getClient().getLocation().getZone().getValue());
        rsp.setRegion(response.getClient().getLocation().getRegion().getValue());
        return rsp;
    } catch (Throwable t) {
        if (t instanceof PolarisException) {
            // 服务端异常不进行重试
            throw t;
        }
        if (null != connection) {
            connection.reportFail();
        }
        throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to report client host %s, version %s service %s", req.getClientHost(), req.getVersion(), serviceKey), t);
    } finally {
        if (null != connection) {
            connection.release(GrpcUtil.OP_KEY_REPORT_CLIENT);
        }
    }
}
Also used : ClientProto(com.tencent.polaris.client.pb.ClientProto) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) PolarisGRPCGrpc(com.tencent.polaris.client.pb.PolarisGRPCGrpc) PolarisException(com.tencent.polaris.api.exception.PolarisException) ReportClientResponse(com.tencent.polaris.api.plugin.server.ReportClientResponse) ResponseProto(com.tencent.polaris.client.pb.ResponseProto) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 9 with RetriableException

use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.

the class GrpcConnector method registerInstance.

@Override
public CommonProviderResponse registerInstance(CommonProviderRequest req) throws PolarisException {
    checkDestroyed();
    Connection connection = null;
    ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
    try {
        waitDiscoverReady();
        connection = connectionManager.getConnection(GrpcUtil.OP_KEY_REGISTER_INSTANCE, ClusterType.SERVICE_DISCOVER_CLUSTER);
        req.setTargetServer(connectionToTargetNode(connection));
        PolarisGRPCGrpc.PolarisGRPCBlockingStub stub = PolarisGRPCGrpc.newBlockingStub(connection.getChannel());
        GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextInstanceRegisterReqId());
        ResponseProto.Response registerInstanceResponse = stub.registerInstance(buildRegisterInstanceRequest(req));
        GrpcUtil.checkResponse(registerInstanceResponse);
        if (!registerInstanceResponse.hasInstance()) {
            throw new PolarisException(ErrorCode.SERVER_USER_ERROR, "invalid register response: missing instance");
        }
        CommonProviderResponse resp = new CommonProviderResponse();
        resp.setInstanceID(registerInstanceResponse.getInstance().getId().getValue());
        resp.setExists(registerInstanceResponse.getCode().getValue() == ServerCodes.EXISTED_RESOURCE);
        return resp;
    } catch (Throwable t) {
        if (t instanceof PolarisException) {
            throw t;
        }
        if (null != connection) {
            connection.reportFail();
        }
        throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to register host %s:%d service %s", req.getHost(), req.getPort(), serviceKey), t);
    } finally {
        if (null != connection) {
            connection.release(GrpcUtil.OP_KEY_REGISTER_INSTANCE);
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) CommonProviderResponse(com.tencent.polaris.api.plugin.server.CommonProviderResponse) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) PolarisGRPCGrpc(com.tencent.polaris.client.pb.PolarisGRPCGrpc) ResponseProto(com.tencent.polaris.client.pb.ResponseProto) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 10 with RetriableException

use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.

the class ConfigFileLongPollingServiceTest method testThrowRetryException.

@Test
public void testThrowRetryException() throws InterruptedException {
    // 初始化 LongPollingService
    DefaultConfigFileLongPollingService longPollingService = new DefaultConfigFileLongPollingService(sdkContext, configFileConnector);
    RemoteConfigFileRepo remoteConfigFileRepo = mock(RemoteConfigFileRepo.class);
    ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
    when(remoteConfigFileRepo.getConfigFileMetadata()).thenReturn(configFileMetadata);
    longPollingService.addConfigFile(remoteConfigFileRepo);
    // 模拟抛异常
    when(configFileConnector.watchConfigFiles(anyList())).thenThrow(new RetriableException(ErrorCode.API_TIMEOUT, ""));
    // 因为LongPolling会在 5s 后开始执行
    TimeUnit.SECONDS.sleep(7);
    verify(configFileConnector, times(2)).watchConfigFiles(anyList());
    // 没有触发回调
    verify(remoteConfigFileRepo, times(0)).onLongPollNotified(anyLong());
}
Also used : ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) RetriableException(com.tencent.polaris.api.exception.RetriableException) Test(org.junit.Test)

Aggregations

RetriableException (com.tencent.polaris.api.exception.RetriableException)13 PolarisException (com.tencent.polaris.api.exception.PolarisException)7 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)6 PolarisGRPCGrpc (com.tencent.polaris.client.pb.PolarisGRPCGrpc)4 ResponseProto (com.tencent.polaris.client.pb.ResponseProto)4 CommonProviderRequest (com.tencent.polaris.api.plugin.server.CommonProviderRequest)3 CommonProviderResponse (com.tencent.polaris.api.plugin.server.CommonProviderResponse)3 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)3 ConsulException (com.ecwid.consul.ConsulException)2 ConfigFileProto (com.tencent.polaris.client.pb.ConfigFileProto)2 PolarisConfigGRPCGrpc (com.tencent.polaris.client.pb.PolarisConfigGRPCGrpc)2 ConfigFileMetadata (com.tencent.polaris.configuration.api.core.ConfigFileMetadata)2 Connection (com.tencent.polaris.plugins.connector.grpc.Connection)2 Test (org.junit.Test)2 NewService (com.ecwid.consul.v1.agent.model.NewService)1 ConfigFile (com.tencent.polaris.api.plugin.configuration.ConfigFile)1 ReportClientResponse (com.tencent.polaris.api.plugin.server.ReportClientResponse)1 InstanceRegisterResponse (com.tencent.polaris.api.rpc.InstanceRegisterResponse)1 ClientProto (com.tencent.polaris.client.pb.ClientProto)1