Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor least active balance algorithm. #4342

Merged
merged 6 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package org.apache.shenyu.loadbalancer.spi;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import org.apache.shenyu.loadbalancer.entity.Upstream;
import org.apache.shenyu.spi.Join;

Expand All @@ -30,34 +33,26 @@
@Join
public class LeastActiveLoadBalance extends AbstractLoadBalancer {

private Map<String, Integer> countMap = new ConcurrentHashMap<>();
private final Map<String, Long> countMap = new ConcurrentHashMap<>();

@Override
protected Upstream doSelect(final List<Upstream> upstreamList, final String ip) {
Map<String, Upstream> domainList = new ConcurrentHashMap<>();
upstreamList.stream().forEach(upstream -> {
domainList.put(upstream.buildDomain(), upstream);
});
countMap.keySet().forEach(key -> {
if (!domainList.keySet().contains(key)) {
countMap.remove(key);
}
});
for (Upstream upstream:upstreamList) {
if (countMap.get(upstream.buildDomain()) == null) {
countMap.put(upstream.buildDomain(), 1);
return upstream;
} else {
countMap.put(upstream.buildDomain(), countMap.get(upstream.buildDomain()) + 1);
}
}
AtomicReference<String> leastDomainUrl = new AtomicReference<>(upstreamList.get(0).buildDomain());
countMap.keySet().forEach(key -> {
if (countMap.get(key) < countMap.get(leastDomainUrl)) {
leastDomainUrl.set(key);
}
});
return domainList.get(leastDomainUrl);
Map<String, Upstream> domainMap = upstreamList.stream()
.collect(Collectors.toConcurrentMap(Upstream::buildDomain, upstream -> upstream));

domainMap.keySet().stream()
.filter(key -> !countMap.containsKey(key))
.forEach(domain -> countMap.put(domain, Long.MIN_VALUE));

final String domain = countMap.entrySet().stream()
// Ensure that the filtered domain is included in the domainMap.
.filter(entry -> domainMap.containsKey(entry.getKey()))
.min(Comparator.comparingLong(Map.Entry::getValue))
.map(Map.Entry::getKey)
.orElse(upstreamList.get(0).buildDomain());

countMap.computeIfPresent(domain, (key, actived) -> Optional.of(actived).orElse(Long.MIN_VALUE) + 1);
return domainMap.get(domain);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,26 @@
import java.util.ArrayList;
import java.util.List;
import org.apache.shenyu.loadbalancer.entity.Upstream;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* The type least activity load balance test.
*/
public class LeastActiveLoadBalanceTest {
private List<Upstream> onlyOneList = new ArrayList<>();
private final List<Upstream> onlyOneList = new ArrayList<>();

/**
* build upstream list.
* @throws Exception exception
*/
public void buildUpstreamList() throws Exception {
public void buildUpstreamList() {
Upstream upstream1 = Upstream.builder()
.url("baidu.com")
.protocol("https")
.protocol("https://")
.build();
Upstream upstream2 = Upstream.builder()
.url("pro.jd.com")
.protocol("https")
.protocol("https://")
.build();
onlyOneList.add(upstream1);
onlyOneList.add(upstream2);
Expand All @@ -52,7 +51,7 @@ public void testResponseTimeBalancer() throws Exception {
final LeastActiveLoadBalance leastActiveLoadBalance = new LeastActiveLoadBalance();
Upstream upstream = leastActiveLoadBalance.doSelect(onlyOneList, "localhost");
Upstream upstream1 = leastActiveLoadBalance.doSelect(onlyOneList, "localhost");
Assert.assertTrue((upstream.getUrl().equals("baidu.com") && upstream1.getUrl().equals("pro.jd.com")) || upstream1.getUrl().equals("baidu.com") && upstream.getUrl().equals("pro.jd.com"));

Assertions.assertTrue((upstream.getUrl().equals("baidu.com") && upstream1.getUrl().equals("pro.jd.com"))
|| upstream1.getUrl().equals("baidu.com") && upstream.getUrl().equals("pro.jd.com"));
}
}