Search in sources :

Example 16 with ServiceCallResult

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

the class CircuitBreakerTest method instanceToResult.

private ServiceCallResult instanceToResult(Instance instance) {
    ServiceCallResult result = new ServiceCallResult();
    result.setNamespace(instance.getNamespace());
    result.setService(instance.getService());
    result.setHost(instance.getHost());
    result.setPort(instance.getPort());
    // result.setInstance(instance);
    return result;
}
Also used : ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult)

Example 17 with ServiceCallResult

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

the class DefaultProviderAPI method deRegister.

@Override
public void deRegister(InstanceDeregisterRequest req) throws PolarisException {
    checkAvailable("ProviderAPI");
    Validator.validateInstanceDeregisterRequest(req);
    long retryInterval = sdkContext.getConfig().getGlobal().getAPI().getRetryInterval();
    long timeout = getTimeout(req);
    while (timeout > 0) {
        long start = System.currentTimeMillis();
        ServiceCallResult serviceCallResult = new ServiceCallResult();
        CommonProviderRequest request = req.getRequest();
        try {
            serverConnector.deregisterInstance(request);
            serviceCallResult.setRetStatus(RetStatus.RetSuccess);
            serviceCallResult.setRetCode(ErrorCode.Success.getCode());
            LOG.info("deregister instance {} succ", req);
            return;
        } catch (PolarisException e) {
            serviceCallResult.setRetStatus(RetStatus.RetFail);
            serviceCallResult.setRetCode(exceptionToErrorCode(e).getCode());
            if (e instanceof RetriableException) {
                LOG.warn("instance deregister request error, retrying.", e);
                Utils.sleepUninterrupted(retryInterval);
                continue;
            }
            throw e;
        } finally {
            long delay = System.currentTimeMillis() - start;
            serviceCallResult.setDelay(delay);
            reportServerCall(serviceCallResult, request.getTargetServer(), "deRegister");
            timeout -= delay;
        }
    }
    throw new PolarisException(ErrorCode.API_TIMEOUT, "instance deregister 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 18 with ServiceCallResult

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

the class DefaultProviderAPI method register.

@Override
public InstanceRegisterResponse register(InstanceRegisterRequest req) throws PolarisException {
    checkAvailable("ProviderAPI");
    Validator.validateInstanceRegisterRequest(req);
    long retryInterval = sdkContext.getConfig().getGlobal().getAPI().getRetryInterval();
    long timeout = getTimeout(req);
    while (timeout > 0) {
        long start = System.currentTimeMillis();
        ServiceCallResult serviceCallResult = new ServiceCallResult();
        CommonProviderRequest request = req.getRequest();
        try {
            CommonProviderResponse response = serverConnector.registerInstance(request);
            LOG.info("register {}/{} instance {} succ", req.getNamespace(), req.getService(), response.getInstanceID());
            serviceCallResult.setRetStatus(RetStatus.RetSuccess);
            serviceCallResult.setRetCode(ErrorCode.Success.getCode());
            return new InstanceRegisterResponse(response.getInstanceID(), response.isExists());
        } catch (PolarisException e) {
            serviceCallResult.setRetStatus(RetStatus.RetFail);
            serviceCallResult.setRetCode(exceptionToErrorCode(e).getCode());
            if (e instanceof RetriableException) {
                LOG.warn("instance register request error, retrying.", e);
                Utils.sleepUninterrupted(retryInterval);
                continue;
            }
            throw e;
        } finally {
            long delay = System.currentTimeMillis() - start;
            serviceCallResult.setDelay(delay);
            reportServerCall(serviceCallResult, request.getTargetServer(), "register");
            timeout -= delay;
        }
    }
    throw new PolarisException(ErrorCode.API_TIMEOUT, "instance register 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) CommonProviderResponse(com.tencent.polaris.api.plugin.server.CommonProviderResponse) InstanceRegisterResponse(com.tencent.polaris.api.rpc.InstanceRegisterResponse) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 19 with ServiceCallResult

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

the class PrometheusPushHandlerTest method testServiceCallSumAndMaxStrategy.

@Test
public void testServiceCallSumAndMaxStrategy() throws InterruptedException {
    List<Integer> delayList = Collections.synchronizedList(new ArrayList<>());
    int count = 20;
    batchDone(() -> {
        int delay = random.nextInt(1000) + 100;
        delayList.add(delay);
        StatInfo statInfo = new StatInfo();
        ServiceCallResult callResult = mockFixedLabelServiceCallResult(200, delay);
        statInfo.setRouterGauge(callResult);
        handler.handle(statInfo);
    }, count);
    // mock pushing
    Thread.sleep(pushInterval + 1000);
    handler.stopHandle();
    int maxExpected = 0;
    int sumExpected = 0;
    for (Integer i : delayList) {
        if (i > maxExpected) {
            maxExpected = i;
        }
        sumExpected += i;
    }
    ServiceCallResult example = mockFixedLabelServiceCallResult(200, 1000);
    Double maxResult = getServiceCallMaxResult(example);
    Double sumResult = getServiceCallSumResult(example);
    Assert.assertEquals(new Double(maxExpected), maxResult);
    Assert.assertEquals(new Double(sumExpected), sumResult);
}
Also used : ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) StatInfo(com.tencent.polaris.api.plugin.stat.StatInfo) Test(org.junit.Test)

Example 20 with ServiceCallResult

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

the class PrometheusPushHandlerTest method mockFixedLabelServiceCallResult.

private ServiceCallResult mockFixedLabelServiceCallResult(int retCode, int Delay) {
    ServiceCallResult serviceCallResult = new ServiceCallResult();
    serviceCallResult.setService("callService");
    serviceCallResult.setNamespace("callNamespace");
    serviceCallResult.setHost("callHost");
    serviceCallResult.setPort(8080);
    serviceCallResult.setRetStatus(RetStatus.RetSuccess);
    serviceCallResult.setRetCode(retCode);
    serviceCallResult.setDelay(Delay);
    serviceCallResult.setMethod("GET");
    serviceCallResult.setCallerService(mockCallerService());
    return 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