use of org.apache.servicecomb.loadbalance.ServiceCombServerStats in project java-chassis by ServiceComb.
the class IsolationDiscoveryFilterTest method discovery_not_try_isolated_instance_concurrently.
@Test
public void discovery_not_try_isolated_instance_concurrently() {
ServiceCombServer server0 = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(data.get("i0"));
ServiceCombServerStats serviceCombServerStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0);
for (int i = 0; i < 5; ++i) {
serviceCombServerStats.markFailure();
}
ServiceCombLoadBalancerStats.INSTANCE.markIsolated(server0, true);
letIsolatedInstancePassSingleTestTime(serviceCombServerStats);
Assert.assertTrue(ServiceCombServerStats.isolatedServerCanTry());
// The first invocation can occupy the trying chance
DiscoveryTreeNode childNode = filter.discovery(discoveryContext, discoveryTreeNode);
Map<String, MicroserviceInstance> childNodeData = childNode.data();
Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i0", "i1", "i2"));
Assert.assertEquals(data.get("i0"), childNodeData.get("i0"));
Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
Assert.assertFalse(ServiceCombServerStats.isolatedServerCanTry());
// Other invocation cannot get trying chance concurrently
childNode = filter.discovery(discoveryContext, discoveryTreeNode);
childNodeData = childNode.data();
Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i1", "i2"));
Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
ServiceCombServerStats.checkAndReleaseTryingChance(// after the first invocation releases the trying chance
invocation);
// Other invocation can get the trying chance
childNode = filter.discovery(discoveryContext, discoveryTreeNode);
childNodeData = childNode.data();
Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i0", "i1", "i2"));
Assert.assertEquals(data.get("i0"), childNodeData.get("i0"));
Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
Assert.assertFalse(ServiceCombServerStats.isolatedServerCanTry());
}
use of org.apache.servicecomb.loadbalance.ServiceCombServerStats in project java-chassis by ServiceComb.
the class IsolationDiscoveryFilterTest method discovery_try_isolated_instance_after_singleTestTime.
@Test
public void discovery_try_isolated_instance_after_singleTestTime() {
ServiceCombServer server0 = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(data.get("i0"));
ServiceCombServerStats serviceCombServerStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0);
for (int i = 0; i < 5; ++i) {
serviceCombServerStats.markFailure();
}
letIsolatedInstancePassSingleTestTime(serviceCombServerStats);
ServiceCombLoadBalancerStats.INSTANCE.markIsolated(server0, true);
Assert.assertTrue(ServiceCombServerStats.isolatedServerCanTry());
Assert.assertNull(TestServiceCombServerStats.getTryingIsolatedServerInvocation());
DiscoveryTreeNode childNode = filter.discovery(discoveryContext, discoveryTreeNode);
Map<String, MicroserviceInstance> childNodeData = childNode.data();
Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i0", "i1", "i2"));
Assert.assertEquals(data.get("i0"), childNodeData.get("i0"));
Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
Assert.assertTrue(serviceCombServerStats.isIsolated());
Assert.assertFalse(ServiceCombServerStats.isolatedServerCanTry());
Assert.assertSame(invocation, TestServiceCombServerStats.getTryingIsolatedServerInvocation());
}
use of org.apache.servicecomb.loadbalance.ServiceCombServerStats in project java-chassis by ServiceComb.
the class IsolationDiscoveryFilterTest method discovery_keep_minIsolationTime.
@Test
public void discovery_keep_minIsolationTime() {
ServiceCombServer server0 = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(data.get("i0"));
ServiceCombLoadBalancerStats.INSTANCE.markIsolated(server0, true);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server0);
DiscoveryTreeNode childNode = filter.discovery(discoveryContext, discoveryTreeNode);
Map<String, MicroserviceInstance> childNodeData = childNode.data();
Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i1", "i2"));
Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
ServiceCombServerStats serviceCombServerStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0);
Deencapsulation.setField(serviceCombServerStats, "isolatedTime", System.currentTimeMillis() - Configuration.INSTANCE.getMinIsolationTime(invocation.getMicroserviceName()) - 1);
childNode = filter.discovery(discoveryContext, discoveryTreeNode);
childNodeData = childNode.data();
Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i0", "i1", "i2"));
Assert.assertEquals(data.get("i0"), childNodeData.get("i0"));
Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
}
use of org.apache.servicecomb.loadbalance.ServiceCombServerStats in project java-chassis by ServiceComb.
the class IsolationDiscoveryFilter method allowVisit.
private boolean allowVisit(Invocation invocation, MicroserviceInstance instance) {
ServiceCombServer server = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(instance);
if (server == null) {
// first time accessed.
return true;
}
ServiceCombServerStats serverStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server);
Settings settings = createSettings(invocation);
if (!checkThresholdAllowed(settings, serverStats)) {
if (serverStats.isIsolated() && (System.currentTimeMillis() - serverStats.getLastVisitTime()) > settings.singleTestTime) {
return ServiceCombServerStats.applyForTryingChance(invocation);
}
if (!serverStats.isIsolated()) {
// checkThresholdAllowed is not concurrent control, may print several logs/events in current access.
serverStats.markIsolated(true);
eventBus.post(new IsolationServerEvent(invocation, instance, serverStats, settings, Type.OPEN, server.getEndpoint()));
LOGGER.warn("Isolate service {}'s instance {}.", invocation.getMicroserviceName(), instance.getInstanceId());
}
return false;
}
if (serverStats.isIsolated()) {
// high volume of concurrent requests with a percentage of error(e.g. 50%) scenario with no isolation
if ((System.currentTimeMillis() - serverStats.getIsolatedTime()) <= settings.minIsolationTime) {
return false;
}
serverStats.markIsolated(false);
eventBus.post(new IsolationServerEvent(invocation, instance, serverStats, settings, Type.CLOSE, server.getEndpoint()));
LOGGER.warn("Recover service {}'s instance {} from isolation.", invocation.getMicroserviceName(), instance.getInstanceId());
}
return true;
}
use of org.apache.servicecomb.loadbalance.ServiceCombServerStats in project java-chassis by ServiceComb.
the class IsolationDiscoveryFilterTest method discovery_recover_instance.
@Test
public void discovery_recover_instance() {
ServiceCombServer server0 = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(data.get("i0"));
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server0);
ServiceCombServerStats serviceCombServerStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0);
ServiceCombLoadBalancerStats.INSTANCE.markIsolated(server0, true);
Deencapsulation.setField(serviceCombServerStats, "isolatedTime", System.currentTimeMillis() - Configuration.INSTANCE.getMinIsolationTime(invocation.getMicroserviceName()) - 1);
DiscoveryTreeNode childNode = filter.discovery(discoveryContext, discoveryTreeNode);
Map<String, MicroserviceInstance> childNodeData = childNode.data();
Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i0", "i1", "i2"));
Assert.assertEquals(data.get("i0"), childNodeData.get("i0"));
Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
Assert.assertFalse(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0).isIsolated());
}
Aggregations