Search in sources :

Example 6 with ServiceCallResult

use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java by polarismesh.

the class PrometheusPushHandlerTest method testServiceCallSuccessStrategy.

@Test
public void testServiceCallSuccessStrategy() throws InterruptedException {
    int count = 10;
    int expected = 3;
    CountDownLatch latch = new CountDownLatch(2);
    new Thread(() -> {
        try {
            batchDone(() -> {
                StatInfo statInfo = new StatInfo();
                ServiceCallResult callResult = mockFixedLabelServiceCallResult(200, 1000);
                callResult.setRetStatus(RetStatus.RetSuccess);
                statInfo.setRouterGauge(callResult);
                handler.handle(statInfo);
            }, expected);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        latch.countDown();
    }).start();
    new Thread(() -> {
        try {
            batchDone(() -> {
                StatInfo statInfo = new StatInfo();
                ServiceCallResult callResult = mockFixedLabelServiceCallResult(200, 1000);
                callResult.setRetStatus(RetStatus.RetFail);
                statInfo.setRouterGauge(callResult);
                handler.handle(statInfo);
            }, count - expected);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        latch.countDown();
    }).start();
    latch.await();
    // mock pushing
    Thread.sleep(pushInterval + 1000);
    handler.stopHandle();
    ServiceCallResult example = mockFixedLabelServiceCallResult(200, 1000);
    Double result = getServiceCallSuccessResult(example);
    Assert.assertEquals(new Double(expected), result);
}
Also used : ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) CountDownLatch(java.util.concurrent.CountDownLatch) StatInfo(com.tencent.polaris.api.plugin.stat.StatInfo) Test(org.junit.Test)

Example 7 with ServiceCallResult

use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java by polarismesh.

the class ReportClientTask method doReport.

private ReportClientResponse doReport(String clientHost, String version) {
    BaseEngine engine = BaseEngine.getEngine(shareContext);
    ReportClientRequest req = new ReportClientRequest();
    req.setClientHost(clientHost);
    req.setVersion(version);
    ReportClientResponse rsp = null;
    long start = System.currentTimeMillis();
    ServiceCallResult serviceCallResult = new ServiceCallResult();
    try {
        rsp = extensions.getServerConnector().reportClient(req);
        serviceCallResult.setRetStatus(RetStatus.RetSuccess);
        serviceCallResult.setRetCode(ErrorCode.Success.getCode());
    } catch (PolarisException e) {
        serviceCallResult.setRetStatus(RetStatus.RetFail);
        serviceCallResult.setRetCode(e.getCode().getCode());
        LOG.warn("fail to report client info(clientHost={}, version={}), cause is {}", clientHost, version, e.getMessage());
    }
    long delay = System.currentTimeMillis() - start;
    serviceCallResult.setDelay(delay);
    if (null != engine) {
        engine.reportServerCall(serviceCallResult, req.getTargetServer(), "reportClient");
    }
    return rsp;
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ReportClientRequest(com.tencent.polaris.api.plugin.server.ReportClientRequest) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) BaseEngine(com.tencent.polaris.client.api.BaseEngine) ReportClientResponse(com.tencent.polaris.api.plugin.server.ReportClientResponse)

Example 8 with ServiceCallResult

use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java by polarismesh.

the class CircuitBreakExample method main.

public static void main(String[] args) throws Throwable {
    InitResult initResult = CircuitBreakExampleUtils.initConfiguration(args);
    // 由于需要用到多个API对象,因此可以使用SDKContext使得多个API对象的资源都可以共享
    try (SDKContext sdkContext = SDKContext.initContext()) {
        ProviderAPI providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(sdkContext);
        // 1. 先注册服务实例
        registerInstances(providerAPI, initResult);
        // 2. 获取可用的服务实例列表
        ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext);
        Instance[] availableInstances = getAvailableInstances(consumerAPI, initResult);
        System.out.println("available instances count is " + availableInstances.length);
        // 3. 针对某个服务实例上报调用结果失败,默认连续10次失败,或者1分钟错误率50%,就会对实例进行熔断
        Instance targetInstance = availableInstances[0];
        System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
        for (int i = 0; i < 120000; i++) {
            // 这里要进行服务调用,用户可以写自己的服务调用代码
            for (Instance instance : availableInstances) {
                ServiceCallResult serviceCallResult = new ServiceCallResult();
                serviceCallResult.setInstance(targetInstance);
                serviceCallResult.setMethod("echo");
                if (instance.getHost().equals(targetInstance.getHost()) && instance.getPort() == targetInstance.getPort()) {
                    serviceCallResult.setRetStatus(RetStatus.RetFail);
                    serviceCallResult.setDelay(2000);
                } else {
                    serviceCallResult.setRetStatus(RetStatus.RetSuccess);
                    serviceCallResult.setDelay(20);
                }
                consumerAPI.updateServiceCallResult(serviceCallResult);
            }
            System.out.printf("report call result %d%n", i);
            CircuitBreakExampleUtils.doSleep(500);
        }
        // 4. 判断服务实例是否还在可用列表中,已经熔断的实例是不会再次返回
        availableInstances = getAvailableInstances(consumerAPI, initResult);
        System.out.println("available instances count is " + availableInstances.length);
        for (Instance instance : availableInstances) {
            System.out.printf("available instance is %s:%d%n", instance.getHost(), instance.getPort());
        }
        // 4. 反注册服务实例
        System.out.println("start to deregister instances");
        deregisterInstances(providerAPI, initResult);
    }
}
Also used : SDKContext(com.tencent.polaris.client.api.SDKContext) ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) InitResult(com.tencent.polaris.circuitbreak.example.CircuitBreakExampleUtils.InitResult) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) Instance(com.tencent.polaris.api.pojo.Instance) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI)

Example 9 with ServiceCallResult

use of com.tencent.polaris.api.rpc.ServiceCallResult 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.");
}
Also used : CommonProviderRequest(com.tencent.polaris.api.plugin.server.CommonProviderRequest) PolarisException(com.tencent.polaris.api.exception.PolarisException) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 10 with ServiceCallResult

use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java-agent by polarismesh.

the class PolarisOperator method reportInvokeResult.

/**
 * 调用CONSUMER_API上报服务请求结果
 *
 * @param delay 本次服务调用延迟,单位ms
 */
public void reportInvokeResult(String service, String method, String host, int port, long delay, boolean success, int code) {
    init();
    if (!inited.get()) {
        LOGGER.error("[POLARIS] fail to getInstances {}, polaris init failed", service);
        return;
    }
    ServiceCallResult serviceCallResult = new ServiceCallResult();
    serviceCallResult.setNamespace(polarisConfig.getNamespace());
    serviceCallResult.setService(service);
    serviceCallResult.setMethod(method);
    serviceCallResult.setHost(host);
    serviceCallResult.setPort(port);
    serviceCallResult.setDelay(delay);
    serviceCallResult.setRetStatus(success ? RetStatus.RetSuccess : RetStatus.RetFail);
    serviceCallResult.setRetCode(code);
    consumerAPI.updateServiceCallResult(serviceCallResult);
}
Also used : ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult)

Aggregations

ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)20 Test (org.junit.Test)7 PolarisException (com.tencent.polaris.api.exception.PolarisException)5 Instance (com.tencent.polaris.api.pojo.Instance)5 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)4 StatInfo (com.tencent.polaris.api.plugin.stat.StatInfo)4 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)4 Configuration (com.tencent.polaris.api.config.Configuration)3 RetriableException (com.tencent.polaris.api.exception.RetriableException)3 CommonProviderRequest (com.tencent.polaris.api.plugin.server.CommonProviderRequest)3 GetInstancesRequest (com.tencent.polaris.api.rpc.GetInstancesRequest)3 ProviderAPI (com.tencent.polaris.api.core.ProviderAPI)1 CommonProviderResponse (com.tencent.polaris.api.plugin.server.CommonProviderResponse)1 ReportClientRequest (com.tencent.polaris.api.plugin.server.ReportClientRequest)1 ReportClientResponse (com.tencent.polaris.api.plugin.server.ReportClientResponse)1 CircuitBreakerStatus (com.tencent.polaris.api.pojo.CircuitBreakerStatus)1 RetStatus (com.tencent.polaris.api.pojo.RetStatus)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)1 InstanceRegisterResponse (com.tencent.polaris.api.rpc.InstanceRegisterResponse)1