use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class InstancesDetectTask method doInstanceDetectForService.
private void doInstanceDetectForService(ServiceKey serviceKey) throws PolarisException {
ServiceEventKey svcEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
ServiceInstances instances = extensions.getLocalRegistry().getInstances(new ResourceFilter(svcEventKey, true, true));
if (!instances.isInitialized() || instances.getInstances().size() == 0) {
return;
}
Map<Instance, DetectResult> aliveResults = new HashMap<>();
for (Instance instance : instances.getInstances()) {
if (destroy.get()) {
// 如果要停止定时任务,则剩下的实例探测也没必要进行下去了
break;
}
DetectResult result = detectInstance(instance);
if (result == null || result.getRetStatus() != RetStatus.RetSuccess) {
continue;
}
aliveResults.put(instance, result);
}
if (MapUtils.isNotEmpty(aliveResults)) {
ServiceUpdateRequest updateRequest = buildInstanceUpdateResult(serviceKey, aliveResults);
LOG.info("update cache for outlier detect, value is {}", updateRequest);
extensions.getLocalRegistry().updateInstances(updateRequest);
}
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class InstancesDetectTask method buildInstanceUpdateResult.
private ServiceUpdateRequest buildInstanceUpdateResult(ServiceKey serviceKey, Map<Instance, DetectResult> aliveResults) {
List<InstanceProperty> instances = new ArrayList<>();
for (Map.Entry<Instance, DetectResult> entry : aliveResults.entrySet()) {
Map<String, Object> properties = new HashMap<>();
properties.put(PROPERTY_DETECT_RESULT, entry.getValue());
InstanceProperty instanceProperty = new InstanceProperty(entry.getKey(), properties);
instances.add(instanceProperty);
}
return new ServiceUpdateRequest(serviceKey, instances);
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class CircuitBreakerTest method testCircuitBreakByErrorRate.
@Test
public void testCircuitBreakByErrorRate() {
Configuration configuration = TestUtils.configWithEnvAddress();
try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
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.setDelay(1000L);
if (i % 2 == 0) {
result.setRetCode(0);
result.setRetStatus(RetStatus.RetSuccess);
Utils.sleepUninterrupted(1);
} else {
result.setRetCode(-1);
result.setRetStatus(RetStatus.RetFail);
}
consumerAPI.updateServiceCallResult(result);
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);
Utils.sleepUninterrupted(10000);
// 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.Instance in project polaris-java by polarismesh.
the class CircuitBreakerTest method testUpdateServiceCallResult.
@Test
public void testUpdateServiceCallResult() {
Configuration configuration = TestUtils.configWithEnvAddress();
try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
int index = 1;
GetInstancesRequest req = new GetInstancesRequest();
req.setNamespace(NAMESPACE_TEST);
req.setService(SERVICE_CIRCUIT_BREAKER);
InstancesResponse instances = consumerAPI.getInstances(req);
Assert.assertEquals(MAX_COUNT, instances.getInstances().length);
Instance instanceToLimit = instances.getInstances()[index];
ServiceCallResult result = instanceToResult(instanceToLimit);
result.setRetCode(-1);
result.setDelay(1000L);
result.setRetStatus(RetStatus.RetFail);
consumerAPI.updateServiceCallResult(result);
}
}
use of com.tencent.polaris.api.pojo.Instance 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);
}
}
Aggregations