use of com.tencent.polaris.api.exception.PolarisException in project sofa-rpc by sofastack.
the class PolarisRegistryTest method testRegister.
@Test
public void testRegister() {
polaris.getNamingService().addService(new ServiceKey(NAMESPACE, SERVICE));
// register
ProviderConfig<?> providerConfig = providerConfig("polaris-test-1", 12200, 12201, 12202);
registry.register(providerConfig);
// check register
ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPI();
GetAllInstancesRequest getAllInstancesRequest = new GetAllInstancesRequest();
getAllInstancesRequest.setNamespace(APPNAME);
getAllInstancesRequest.setService(SERVICE);
InstancesResponse allInstance = consumerAPI.getAllInstance(getAllInstancesRequest);
Assert.assertEquals(3, allInstance.getInstances().length);
// unregister
registry.unRegister(providerConfig);
// check unregister ,sleep to wait remove catch
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// if no service will throw a exception
try {
consumerAPI.getAllInstance(getAllInstancesRequest);
} catch (PolarisException e) {
Assert.assertEquals(SERVER_USER_ERROR, e.getCode());
}
}
use of com.tencent.polaris.api.exception.PolarisException in project polaris-java-agent by polarismesh.
the class PolarisServiceObservability method updateServiceCallResult.
public static void updateServiceCallResult(Long delay, Throwable throwable, Integer status, PolarisAgentProperties polarisAgentProperties) {
ServiceInstance instance = InvokeContextHolder.get().getServiceInstance();
if (instance == null) {
LOGGER.warn("fail to call updateServiceCallResult for polaris with instance empty");
return;
}
ServiceCallResult serviceCallResult = new ServiceCallResult();
serviceCallResult.setNamespace(polarisAgentProperties.getNamespace());
serviceCallResult.setService(instance.getServiceId());
serviceCallResult.setHost(instance.getHost());
serviceCallResult.setPort(instance.getPort());
serviceCallResult.setDelay(delay);
serviceCallResult.setRetStatus(null == throwable ? RetStatus.RetSuccess : RetStatus.RetFail);
serviceCallResult.setRetCode(null != status ? status : -1);
try {
PolarisAPIFactory.getConsumerApi().updateServiceCallResult(serviceCallResult);
LOGGER.info("success to call updateServiceCallResult for polaris with status:{}, code:{}", serviceCallResult.getRetStatus(), serviceCallResult.getRetCode());
} catch (PolarisException e) {
LOGGER.error(e.getMessage());
} finally {
InvokeContextHolder.remove();
}
}
use of com.tencent.polaris.api.exception.PolarisException in project spring-cloud-tencent by Tencent.
the class PolarisReactiveDiscoveryClientTest method testGetInstances.
@Test
public void testGetInstances() throws PolarisException {
when(serviceDiscovery.getInstances(anyString())).thenAnswer(invocation -> {
String serviceName = invocation.getArgument(0);
if (SERVICE_PROVIDER.equalsIgnoreCase(serviceName)) {
return singletonList(mock(ServiceInstance.class));
} else {
throw new PolarisException(ErrorCode.UNKNOWN_SERVER_ERROR);
}
});
// Normal
Flux<ServiceInstance> instances = this.client.getInstances(SERVICE_PROVIDER);
StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
// PolarisException
instances = this.client.getInstances(SERVICE_PROVIDER + 1);
StepVerifier.create(instances).expectNextCount(0).expectComplete().verify();
}
use of com.tencent.polaris.api.exception.PolarisException 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.PolarisException 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);
}
}
}
Aggregations