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