use of org.apache.servicecomb.loadbalance.ServiceCombServer in project java-chassis by ServiceComb.
the class IsolationDiscoveryFilterTest method before.
@Before
public void before() {
discoveryContext = new DiscoveryContext();
discoveryContext.setInputParameters(invocation);
discoveryTreeNode = new DiscoveryTreeNode();
Mockito.doAnswer(a -> a.getArguments()[0]).when(transport).parseAddress(Mockito.anyString());
data = new HashMap<>();
for (int i = 0; i < 3; ++i) {
MicroserviceInstance instance = new MicroserviceInstance();
instance.setInstanceId("i" + i);
String endpoint = "rest://127.0.0.1:" + i;
instance.setEndpoints(Collections.singletonList(endpoint));
data.put(instance.getInstanceId(), instance);
ServiceCombServer serviceCombServer = new ServiceCombServer(invocation.getMicroserviceName(), transport, new CacheEndpoint(endpoint, instance));
ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer);
}
discoveryTreeNode.data(data);
filter = new IsolationDiscoveryFilter();
TestServiceCombServerStats.releaseTryingChance();
}
use of org.apache.servicecomb.loadbalance.ServiceCombServer 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.ServiceCombServer 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.ServiceCombServer 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.ServiceCombServer in project java-chassis by ServiceComb.
the class DarklaunchServerListFilter method getFilteredListOfServers.
@Override
public List<ServiceCombServer> getFilteredListOfServers(List<ServiceCombServer> serverList, Invocation invocation) {
DynamicStringProperty ruleStr = DynamicPropertyFactory.getInstance().getStringProperty(String.format(POLICY_SERVICE_COMB, invocation.getMicroserviceName()), null);
if (ruleStr == null) {
ruleStr = DynamicPropertyFactory.getInstance().getStringProperty(String.format(POLICY_CSE, invocation.getMicroserviceName()), null);
}
DarklaunchRule rule = DarklaunchRule.parse(ruleStr.get());
if (rule == null) {
return serverList;
}
List<ServiceCombServer> defaultGroup = new ArrayList<>();
divideServerGroup(serverList, rule, defaultGroup);
if (rule.getPolicyType() == PolicyType.RULE) {
for (DarklaunchRuleItem item : rule.getRuleItems()) {
List<ServiceCombServer> ruleServers = getRuleServers(invocation, item, defaultGroup);
if (ruleServers != null) {
return ruleServers;
}
}
} else {
int rate = random.nextInt(HUNDRED);
for (DarklaunchRuleItem item : rule.getRuleItems()) {
item.getPolicyCondition().setActual(DarklaunchRule.PROP_PERCENT, rate);
if (item.getPolicyCondition().match()) {
if (item.getServers().isEmpty()) {
return defaultGroup;
}
return item.getServers();
}
rate = rate - Integer.parseInt(item.getPolicyCondition().expected());
}
}
return defaultGroup;
}
Aggregations