Search in sources :

Example 21 with PolarisException

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());
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) GetAllInstancesRequest(com.tencent.polaris.api.rpc.GetAllInstancesRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 22 with PolarisException

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();
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 23 with PolarisException

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();
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ServiceInstance(org.springframework.cloud.client.ServiceInstance) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 24 with PolarisException

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);
        }
    }
}
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 25 with PolarisException

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

Aggregations

PolarisException (com.tencent.polaris.api.exception.PolarisException)39 HashMap (java.util.HashMap)8 Test (org.junit.Test)8 RetriableException (com.tencent.polaris.api.exception.RetriableException)7 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)7 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)6 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)6 Configuration (com.tencent.polaris.api.config.Configuration)5 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)5 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)5 CommonProviderRequest (com.tencent.polaris.api.plugin.server.CommonProviderRequest)3 Instance (com.tencent.polaris.api.pojo.Instance)3 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)3 PolarisGRPCGrpc (com.tencent.polaris.client.pb.PolarisGRPCGrpc)3 ResponseProto (com.tencent.polaris.client.pb.ResponseProto)3 Map (java.util.Map)3 CircuitBreakerConfig (com.tencent.polaris.api.config.consumer.CircuitBreakerConfig)2 OutlierDetectionConfig (com.tencent.polaris.api.config.consumer.OutlierDetectionConfig)2 Verifier (com.tencent.polaris.api.config.verify.Verifier)2 Plugin (com.tencent.polaris.api.plugin.Plugin)2