From 0b7718394707defebbd0f7bf4ee524fba74ee902 Mon Sep 17 00:00:00 2001 From: liubao Date: Sat, 27 Jul 2024 10:51:17 +0800 Subject: [PATCH] [#4427]able to disable both consumer and provider zone-aware --- .../filterext/ZoneAwareDiscoveryFilter.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/filterext/ZoneAwareDiscoveryFilter.java b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/filterext/ZoneAwareDiscoveryFilter.java index 4702f53c813..fdf621892bf 100644 --- a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/filterext/ZoneAwareDiscoveryFilter.java +++ b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/filterext/ZoneAwareDiscoveryFilter.java @@ -31,6 +31,8 @@ public class ZoneAwareDiscoveryFilter implements ServerListFilterExt { public static final String CONFIG_RATIO = "servicecomb.loadbalance.filter.zoneaware.ratio"; + public static final String CONFIG_RATIO_CEILING = "servicecomb.loadbalance.filter.zoneaware.ratioCeiling"; + @Override public int getOrder() { return ORDER_ZONE_AWARE; @@ -48,6 +50,11 @@ private int getRatio() { .getIntProperty(CONFIG_RATIO, 30).get(); } + private int getRatioCeiling() { + return DynamicPropertyFactory.getInstance() + .getIntProperty(CONFIG_RATIO_CEILING, 70).get(); + } + @Override public List getFilteredListOfServers(List servers, Invocation invocation) { @@ -67,14 +74,15 @@ public List getFilteredListOfServers(List }); int ratio = getRatio(); + int ratioCeiling = getRatioCeiling(); - if (hasEnoughMembers(servers.size(), instancesRegionAndAZMatch.size(), ratio)) { + if (hasEnoughMembers(servers.size(), instancesRegionAndAZMatch.size(), ratio, ratioCeiling)) { return instancesRegionAndAZMatch; } else { instancesAZMatch.addAll(instancesRegionAndAZMatch); } - if (hasEnoughMembers(servers.size(), instancesAZMatch.size(), ratio)) { + if (hasEnoughMembers(servers.size(), instancesAZMatch.size(), ratio, ratioCeiling)) { return instancesAZMatch; } else { instancesNoMatch.addAll(instancesAZMatch); @@ -82,11 +90,12 @@ public List getFilteredListOfServers(List return instancesNoMatch; } - private boolean hasEnoughMembers(int totalSize, int groupSize, int ratio) { + private boolean hasEnoughMembers(int totalSize, int groupSize, int ratio, int ratioCeiling) { if (totalSize == 0 || groupSize == 0) { return false; } - return Math.floorDiv(groupSize * 100, totalSize) >= ratio; + int actual = Math.floorDiv(groupSize * 100, totalSize); + return actual >= ratio && actual <= ratioCeiling; } private boolean regionAndAZMatch(MicroserviceInstance myself, MicroserviceInstance target) {