Search in sources :

Example 1 with CircuitBreakerStatus

use of com.tencent.polaris.api.pojo.CircuitBreakerStatus in project polaris-java by polarismesh.

the class CircuitBreakerTest method testCircuitBreakByErrorCount.

@Test
public void testCircuitBreakByErrorCount() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        Utils.sleepUninterrupted(10000);
        Assert.assertNotNull(consumerAPI);
        GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
        getInstancesRequest.setNamespace(NAMESPACE_TEST);
        getInstancesRequest.setService(SERVICE_CIRCUIT_BREAKER);
        InstancesResponse instances = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MAX_COUNT, instances.getInstances().length);
        Instance instanceToLimit = instances.getInstances()[1];
        // report 60 fail in 500ms
        for (int i = 0; i < 60; ++i) {
            ServiceCallResult result = instanceToResult(instanceToLimit);
            result.setRetCode(-1);
            result.setDelay(1000L);
            result.setRetStatus(RetStatus.RetFail);
            consumerAPI.updateServiceCallResult(result);
            if (i % 10 == 0) {
                Utils.sleepUninterrupted(1);
            }
        }
        Utils.sleepUninterrupted(1000);
        instances = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MAX_COUNT - 1, instances.getInstances().length);
        Instance[] instanceArray = instances.getInstances();
        boolean exists = false;
        for (int i = 0; i < instanceArray.length; ++i) {
            if (instanceArray[i].getId().equals(instanceToLimit.getId())) {
                exists = true;
            }
        }
        Assert.assertFalse(exists);
        LOG.info("start to test half open by error rate");
        Utils.sleepUninterrupted(10000);
        instances = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MAX_COUNT, instances.getInstances().length);
        for (Instance instance : instances.getInstances()) {
            CircuitBreakerStatus circuitBreakerStatus = instance.getCircuitBreakerStatus();
            if (null != circuitBreakerStatus && circuitBreakerStatus.getStatus() == CircuitBreakerStatus.Status.HALF_OPEN) {
                LOG.info("half open instance is {}", instance);
            }
        }
        // default halfopen pass 3 success
        int requestCountAfterHalfOpen = configuration.getConsumer().getCircuitBreaker().getRequestCountAfterHalfOpen();
        for (int i = 0; i < requestCountAfterHalfOpen; i++) {
            ServiceCallResult result = instanceToResult(instanceToLimit);
            result.setRetCode(-1);
            result.setRetStatus(RetStatus.RetSuccess);
            consumerAPI.updateServiceCallResult(result);
            Utils.sleepUninterrupted(200);
            consumerAPI.updateServiceCallResult(result);
        }
        LOG.info("start to test half open to close");
        Utils.sleepUninterrupted(1000);
        instances = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MAX_COUNT, instances.getInstances().length);
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest) Instance(com.tencent.polaris.api.pojo.Instance) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) CircuitBreakerStatus(com.tencent.polaris.api.pojo.CircuitBreakerStatus) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 2 with CircuitBreakerStatus

use of com.tencent.polaris.api.pojo.CircuitBreakerStatus in project polaris-java by polarismesh.

the class PrometheusPushHandlerTest method mockFixedCircuitResult.

private DefaultCircuitBreakResult mockFixedCircuitResult(CircuitBreakerStatus.Status status) {
    DefaultCircuitBreakResult circuitBreakResult = new DefaultCircuitBreakResult();
    circuitBreakResult.setService("callService");
    circuitBreakResult.setNamespace("callNamespace");
    circuitBreakResult.setMethod("GET");
    circuitBreakResult.setSubset("callSubset");
    circuitBreakResult.setHost("callHost");
    circuitBreakResult.setPort(8080);
    circuitBreakResult.setInstanceId("callInstanceId");
    circuitBreakResult.setCallerService(mockCallerService());
    CircuitBreakerStatus circuitBreakerStatus = new CircuitBreakerStatus("mockCB", status, System.currentTimeMillis());
    circuitBreakResult.setCircuitBreakStatus(circuitBreakerStatus);
    return circuitBreakResult;
}
Also used : DefaultCircuitBreakResult(com.tencent.polaris.api.plugin.stat.DefaultCircuitBreakResult) CircuitBreakerStatus(com.tencent.polaris.api.pojo.CircuitBreakerStatus)

Example 3 with CircuitBreakerStatus

use of com.tencent.polaris.api.pojo.CircuitBreakerStatus in project polaris-java by polarismesh.

the class AbstractStateMachine method getHalfOpenCounterByParameter.

private HalfOpenCounter getHalfOpenCounterByParameter(Instance instance, StatusDimension statusDimension, Parameter parameter) {
    String cbName = parameter.getCircuitBreakerName();
    CircuitBreakerStatus cbStatus = instance.getCircuitBreakerStatus(statusDimension);
    if (null == cbStatus || !cbStatus.getCircuitBreaker().equals(cbName) || cbStatus.getStatus() != CircuitBreakerStatus.Status.HALF_OPEN) {
        return null;
    }
    HalfOpenCounter halfOpenCounter = getHalfOpenCounter(parameter.getPluginId(), instance);
    if (null == halfOpenCounter) {
        return null;
    }
    return halfOpenCounter;
}
Also used : CircuitBreakerStatus(com.tencent.polaris.api.pojo.CircuitBreakerStatus)

Example 4 with CircuitBreakerStatus

use of com.tencent.polaris.api.pojo.CircuitBreakerStatus in project polaris-java by polarismesh.

the class AbstractStateMachine method openToHalfOpen.

@Override
public boolean openToHalfOpen(Instance instance, StatusDimension statusDimension, Parameter parameter) {
    String cbName = parameter.getCircuitBreakerName();
    CircuitBreakerStatus cbStatus = instance.getCircuitBreakerStatus(statusDimension);
    if (null == cbStatus || !cbStatus.getCircuitBreaker().equals(cbName) || cbStatus.getStatus() != CircuitBreakerStatus.Status.OPEN) {
        return false;
    }
    boolean detectSuccess = false;
    if (instance instanceof InstanceByProto) {
        DetectResult detectResult = ((InstanceByProto) instance).getDetectResult();
        detectSuccess = null != detectResult && detectResult.getRetStatus() == RetStatus.RetSuccess;
    }
    // 清空halfOpen的计数器
    HalfOpenCounter halfOpenCounter = getHalfOpenCounter(parameter.getPluginId(), instance);
    if (null == halfOpenCounter) {
        return false;
    }
    long startTimeMs = cbStatus.getStartTimeMs();
    HalfOpenConfig halfOpenConfig = getHalfOpenConfigOnHalfOpen(instance, statusDimension);
    boolean matched;
    if (detectSuccess && halfOpenConfig.getWhenToDetect() != When.never) {
        matched = true;
    } else {
        matched = parameter.getCurrentTimeMs() - startTimeMs >= halfOpenConfig.getSleepWindowMs();
    }
    if (matched) {
        // 时间窗已经过去, 则恢复半开
        halfOpenCounter.resetHalfOpen(statusDimension);
    }
    return matched;
}
Also used : InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) DetectResult(com.tencent.polaris.api.pojo.DetectResult) CircuitBreakerStatus(com.tencent.polaris.api.pojo.CircuitBreakerStatus)

Example 5 with CircuitBreakerStatus

use of com.tencent.polaris.api.pojo.CircuitBreakerStatus in project polaris-java by polarismesh.

the class InstancesCircuitBreakTask method buildInstanceProperty.

@SuppressWarnings("unchecked")
private void buildInstanceProperty(long now, Map<ResultKey, Instance> results, int maxRequestAfterHalfOpen, Map<String, InstanceProperty> instanceProperties, String cbName, CircuitBreakerStatus.Status status) {
    if (MapUtils.isEmpty(results)) {
        return;
    }
    for (Map.Entry<ResultKey, Instance> entry : results.entrySet()) {
        ResultKey resultKey = entry.getKey();
        Instance instance = entry.getValue();
        String instId = resultKey.getInstId();
        InstanceProperty instanceProperty = instanceProperties.get(instId);
        if (null == instanceProperty) {
            Map<String, Object> properties = new HashMap<>();
            properties.put(PROPERTY_CIRCUIT_BREAKER_STATUS, new HashMap<StatusDimension, CircuitBreakerStatus>());
            instanceProperty = new InstanceProperty(instance, properties);
            instanceProperties.put(instId, instanceProperty);
        }
        Map<StatusDimension, CircuitBreakerStatus> statusMap = (Map<StatusDimension, CircuitBreakerStatus>) instanceProperty.getProperties().get(PROPERTY_CIRCUIT_BREAKER_STATUS);
        statusMap.put(resultKey.getStatusDimension(), new CircuitBreakerStatus(cbName, status, now));
    }
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) HashMap(java.util.HashMap) InstanceProperty(com.tencent.polaris.api.plugin.registry.InstanceProperty) StatusDimension(com.tencent.polaris.api.pojo.StatusDimension) CircuitBreakerStatus(com.tencent.polaris.api.pojo.CircuitBreakerStatus) HashMap(java.util.HashMap) Map(java.util.Map) ResultKey(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)

Aggregations

CircuitBreakerStatus (com.tencent.polaris.api.pojo.CircuitBreakerStatus)9 StatusDimension (com.tencent.polaris.api.pojo.StatusDimension)5 Instance (com.tencent.polaris.api.pojo.Instance)2 HashMap (java.util.HashMap)2 Configuration (com.tencent.polaris.api.config.Configuration)1 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)1 ResultKey (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)1 InstanceProperty (com.tencent.polaris.api.plugin.registry.InstanceProperty)1 DefaultCircuitBreakResult (com.tencent.polaris.api.plugin.stat.DefaultCircuitBreakResult)1 DetectResult (com.tencent.polaris.api.pojo.DetectResult)1 GetInstancesRequest (com.tencent.polaris.api.rpc.GetInstancesRequest)1 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)1 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)1 InstanceByProto (com.tencent.polaris.client.pojo.InstanceByProto)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Test (org.junit.Test)1