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;
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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());
}
Aggregations