use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.
the class GrpcConnector method heartbeat.
@Override
public void heartbeat(CommonProviderRequest req) throws PolarisException {
checkDestroyed();
Connection connection = null;
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
try {
waitDiscoverReady();
connection = connectionManager.getConnection(GrpcUtil.OP_KEY_INSTANCE_HEARTBEAT, ClusterType.HEALTH_CHECK_CLUSTER);
req.setTargetServer(connectionToTargetNode(connection));
PolarisGRPCGrpc.PolarisGRPCBlockingStub stub = PolarisGRPCGrpc.newBlockingStub(connection.getChannel());
GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextHeartbeatReqId());
ResponseProto.Response heartbeatResponse = stub.heartbeat(buildHeartbeatRequest(req));
GrpcUtil.checkResponse(heartbeatResponse);
LOG.debug("received heartbeat response {}", heartbeatResponse);
} catch (Throwable t) {
if (t instanceof PolarisException) {
// 服务端异常不进行重试
throw t;
}
if (null != connection) {
connection.reportFail();
}
throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to heartbeat id %s, host %s:%d service %s", req.getInstanceID(), req.getHost(), req.getPort(), serviceKey), t);
} finally {
if (null != connection) {
connection.release(GrpcUtil.OP_KEY_INSTANCE_HEARTBEAT);
}
}
}
use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.
the class PolarisConfigFileConnector method getConfigFile.
@Override
public ConfigFileResponse getConfigFile(ConfigFile configFile) {
Connection connection = null;
try {
connection = connectionManager.getConnection(OP_KEY_GET_CONFIG_FILE, ClusterType.SERVICE_DISCOVER_CLUSTER);
// grpc 调用
PolarisConfigGRPCGrpc.PolarisConfigGRPCBlockingStub stub = PolarisConfigGRPCGrpc.newBlockingStub(connection.getChannel());
// 附加通用 header
GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextInstanceRegisterReqId());
// 执行调用
ConfigFileProto.ConfigFileResponse response = stub.getConfigFile(transfer2DTO(configFile));
return handleResponse(response);
} catch (Throwable t) {
// 网络访问异常
if (connection != null) {
connection.reportFail();
}
throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("failed to load config file. namespace = %s, group = %s, file = %s", configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()), t);
} finally {
if (connection != null) {
connection.release(OP_KEY_GET_CONFIG_FILE);
}
}
}
use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.
the class PolarisConfigFileConnector method watchConfigFiles.
@Override
public ConfigFileResponse watchConfigFiles(List<ConfigFile> configFiles) {
Connection connection = null;
try {
connection = connectionManager.getConnection(OP_KEY_GET_CONFIG_FILE, ClusterType.SERVICE_DISCOVER_CLUSTER);
// grpc 调用
PolarisConfigGRPCGrpc.PolarisConfigGRPCBlockingStub stub = PolarisConfigGRPCGrpc.newBlockingStub(connection.getChannel());
// 附加通用 header
GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextInstanceRegisterReqId());
// 执行调用
List<ConfigFileProto.ConfigFileDTO> dtos = Lists.newLinkedList();
for (ConfigFile configFile : configFiles) {
dtos.add(transfer2DTO(configFile));
}
ConfigFileProto.WatchConfigFileRequest request = ConfigFileProto.WatchConfigFileRequest.newBuilder().addAllWatchFiles(dtos).build();
ConfigFileProto.ConfigFileResponse response = stub.watchConfigFiles(request);
return handleResponse(response);
} catch (Throwable t) {
// 网络访问异常
if (connection != null) {
connection.reportFail();
}
throw new RetriableException(ErrorCode.NETWORK_ERROR, "[Config] failed to watch config file", t);
} finally {
if (connection != null) {
connection.release(OP_KEY_GET_CONFIG_FILE);
}
}
}
use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.
the class DefaultProviderAPI method heartbeat.
@Override
public void heartbeat(InstanceHeartbeatRequest req) throws PolarisException {
checkAvailable("ProviderAPI");
Validator.validateHeartbeatRequest(req);
long timeout = getTimeout(req);
long retryInterval = sdkContext.getConfig().getGlobal().getAPI().getRetryInterval();
while (timeout > 0) {
long start = System.currentTimeMillis();
ServiceCallResult serviceCallResult = new ServiceCallResult();
CommonProviderRequest request = req.getRequest();
try {
serverConnector.heartbeat(request);
serviceCallResult.setRetStatus(RetStatus.RetSuccess);
serviceCallResult.setRetCode(ErrorCode.Success.getCode());
return;
} catch (PolarisException e) {
serviceCallResult.setRetStatus(RetStatus.RetFail);
serviceCallResult.setRetCode(exceptionToErrorCode(e).getCode());
if (e instanceof RetriableException) {
LOG.warn("heartbeat request error, retrying.", e);
Utils.sleepUninterrupted(retryInterval);
continue;
}
throw e;
} finally {
long delay = System.currentTimeMillis() - start;
serviceCallResult.setDelay(delay);
reportServerCall(serviceCallResult, request.getTargetServer(), "heartbeat");
timeout -= delay;
}
}
throw new PolarisException(ErrorCode.API_TIMEOUT, "heartbeat request timeout.");
}
use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.
the class ConsulAPIConnector method deregisterInstance.
@Override
public void deregisterInstance(CommonProviderRequest req) throws PolarisException {
if (ieRegistered) {
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
try {
LOG.info("Unregistering service to Consul: " + consulContext.getInstanceId());
this.consulClient.agentServiceDeregister(consulContext.getInstanceId());
LOG.info("Unregistered service to Consul: " + consulContext.getInstanceId());
ieRegistered = false;
} catch (ConsulException e) {
throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to deregister host %s:%d service %s", req.getHost(), req.getPort(), serviceKey), e);
}
}
}
Aggregations