Skip to content

Commit

Permalink
Add enabled instances filter before weight-random load balance
Browse files Browse the repository at this point in the history
  • Loading branch information
HaojunRen committed Aug 11, 2024
1 parent 00b451e commit f66bcdc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.nepxion.discovery.plugin.framework.loadbalance.DiscoveryEnabledLoadBalance;
import com.nepxion.discovery.plugin.framework.loadbalance.weight.RuleWeightRandomLoadBalance;
import com.nepxion.discovery.plugin.framework.loadbalance.weight.StrategyWeightRandomLoadBalance;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.PredicateBasedRule;
import com.netflix.loadbalancer.Server;

Expand All @@ -40,55 +39,63 @@ private List<Server> getServerList(Object key) {

@Override
public Server choose(Object key) {
List<Server> serverList = getServerList(key);

boolean isTriggered = false;

WeightFilterEntity strategyWeightFilterEntity = strategyWeightRandomLoadBalance.getT();
if (strategyWeightFilterEntity != null && strategyWeightFilterEntity.hasWeight()) {
isTriggered = true;

List<Server> serverList = getServerList(key);
boolean isWeightChecked = strategyWeightRandomLoadBalance.checkWeight(serverList, strategyWeightFilterEntity);
if (isWeightChecked) {
try {
return strategyWeightRandomLoadBalance.choose(serverList, strategyWeightFilterEntity);
List<Server> filterServerList = filterEnabledServers(serverList);

return strategyWeightRandomLoadBalance.choose(filterServerList, strategyWeightFilterEntity);
} catch (Exception e) {
return filterChoose(key);
return filterChoose(serverList, key);
}
} else {
return filterChoose(key);
return filterChoose(serverList, key);
}
}

if (!isTriggered) {
WeightFilterEntity ruleWeightFilterEntity = ruleWeightRandomLoadBalance.getT();
if (ruleWeightFilterEntity != null && ruleWeightFilterEntity.hasWeight()) {
List<Server> serverList = getServerList(key);
boolean isWeightChecked = ruleWeightRandomLoadBalance.checkWeight(serverList, ruleWeightFilterEntity);
if (isWeightChecked) {
try {
return ruleWeightRandomLoadBalance.choose(serverList, ruleWeightFilterEntity);
List<Server> filterServerList = filterEnabledServers(serverList);

return ruleWeightRandomLoadBalance.choose(filterServerList, ruleWeightFilterEntity);
} catch (Exception e) {
return filterChoose(key);
return filterChoose(serverList, key);
}
} else {
return filterChoose(key);
return filterChoose(serverList, key);
}
}
}

return filterChoose(key);
return filterChoose(serverList, key);
}

public Server filterChoose(Object key) {
ILoadBalancer lb = getLoadBalancer();

public List<Server> filterEnabledServers(List<Server> servers) {
List<Server> serverList = new ArrayList<Server>();
serverList.addAll(lb.getAllServers());
serverList.addAll(servers);

if (discoveryEnabledLoadBalance != null) {
discoveryEnabledLoadBalance.filter(serverList);
}

return serverList;
}

public Server filterChoose(List<Server> servers, Object key) {
List<Server> serverList = filterEnabledServers(servers);

Optional<Server> server = getPredicate().chooseRoundRobinAfterFiltering(serverList, key);
if (server.isPresent()) {
return server.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.nepxion.discovery.plugin.framework.loadbalance.DiscoveryEnabledLoadBalance;
import com.nepxion.discovery.plugin.framework.loadbalance.weight.RuleWeightRandomLoadBalance;
import com.nepxion.discovery.plugin.framework.loadbalance.weight.StrategyWeightRandomLoadBalance;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ZoneAvoidanceRule;

Expand All @@ -40,55 +39,63 @@ private List<Server> getServerList(Object key) {

@Override
public Server choose(Object key) {
List<Server> serverList = getServerList(key);

boolean isTriggered = false;

WeightFilterEntity strategyWeightFilterEntity = strategyWeightRandomLoadBalance.getT();
if (strategyWeightFilterEntity != null && strategyWeightFilterEntity.hasWeight()) {
isTriggered = true;

List<Server> serverList = getServerList(key);
boolean isWeightChecked = strategyWeightRandomLoadBalance.checkWeight(serverList, strategyWeightFilterEntity);
if (isWeightChecked) {
try {
return strategyWeightRandomLoadBalance.choose(serverList, strategyWeightFilterEntity);
List<Server> filterServerList = filterEnabledServers(serverList);

return strategyWeightRandomLoadBalance.choose(filterServerList, strategyWeightFilterEntity);
} catch (Exception e) {
return filterChoose(key);
return filterChoose(serverList, key);
}
} else {
return filterChoose(key);
return filterChoose(serverList, key);
}
}

if (!isTriggered) {
WeightFilterEntity ruleWeightFilterEntity = ruleWeightRandomLoadBalance.getT();
if (ruleWeightFilterEntity != null && ruleWeightFilterEntity.hasWeight()) {
List<Server> serverList = getServerList(key);
boolean isWeightChecked = ruleWeightRandomLoadBalance.checkWeight(serverList, ruleWeightFilterEntity);
if (isWeightChecked) {
try {
return ruleWeightRandomLoadBalance.choose(serverList, ruleWeightFilterEntity);
List<Server> filterServerList = filterEnabledServers(serverList);

return ruleWeightRandomLoadBalance.choose(filterServerList, ruleWeightFilterEntity);
} catch (Exception e) {
return filterChoose(key);
return filterChoose(serverList, key);
}
} else {
return filterChoose(key);
return filterChoose(serverList, key);
}
}
}

return filterChoose(key);
return filterChoose(serverList, key);
}

public Server filterChoose(Object key) {
ILoadBalancer lb = getLoadBalancer();

public List<Server> filterEnabledServers(List<Server> servers) {
List<Server> serverList = new ArrayList<Server>();
serverList.addAll(lb.getAllServers());
serverList.addAll(servers);

if (discoveryEnabledLoadBalance != null) {
discoveryEnabledLoadBalance.filter(serverList);
}

return serverList;
}

public Server filterChoose(List<Server> servers, Object key) {
List<Server> serverList = filterEnabledServers(servers);

Optional<Server> server = getPredicate().chooseRoundRobinAfterFiltering(serverList, key);
if (server.isPresent()) {
return server.get();
Expand Down

0 comments on commit f66bcdc

Please sign in to comment.