Skip to content

[BUG] leastActive counts lifetime selections instead of active requests and overloads newly added upstreams #7972

Closed

Description

@Aias00

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

LeastActiveLoadBalance increments a private count on selection but never decrements it when a request terminates. Each entry starts at Long.MIN_VALUE. Consequently, this compares cumulative selections rather than concurrent active requests. A newly added upstream receives consecutive requests until its counter catches up with existing upstreams, irrespective of its outstanding work. Weight and warmup do not protect the new node in this selection path.

Expected Behavior

Compare outstanding requests, release accounting on success/error/cancellation, and avoid sending a catch-up burst to a newly added node.

Steps To Reproduce

Using the compiled loadbalancer classes:

var data = new LoadBalanceData();
var a = Upstream.builder().protocol("http://").url("a:80").weight(100).status(true).build();
var b = Upstream.builder().protocol("http://").url("b:80").weight(100).status(true).build();
var c = Upstream.builder().protocol("http://").url("c:80").weight(100).status(true).build();
var balancer = new LeastActiveLoadBalance();
for (int i = 0; i < 100; i++) {
    balancer.select(List.of(a, b), data);
}
int newNodeHits = 0;
for (int i = 0; i < 50; i++) {
    if (balancer.select(List.of(a, b, c), data) == c) {
        newNodeHits++;
    }
}
System.out.println(newNodeHits); // observed: 50

The first 100 selections can represent already-completed requests: completion cannot change the private count. This reproduction was executed against the commit below.

Anything else?

Source:

@Override
protected Upstream doSelect(final List<Upstream> upstreamList, final LoadBalanceData data) {
long now = System.currentTimeMillis();
Map<String, Upstream> domainMap = upstreamList.stream()
.collect(Collectors.toConcurrentMap(upstream -> upstream.identity().leastActiveKey(), upstream -> upstream));
domainMap.keySet().forEach(domain -> {
ActiveCount activeCount = countMap.computeIfAbsent(domain, key -> new ActiveCount(now));
activeCount.setLastUpdate(now);
});
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(entry -> entry.getValue().getCount()))
.map(Map.Entry::getKey)
.orElse(upstreamList.get(0).buildDomain());
ActiveCount activeCount = countMap.get(domain);
if (Objects.nonNull(activeCount)) {
activeCount.increase();
}
// A removed domain's entry lingers for up to recyclePeriod, safely excluded from selection meanwhile.
if (!updateLock.get() && now - lastRecycle > recyclePeriod && updateLock.compareAndSet(false, true)) {
try {
countMap.entrySet().removeIf(item -> now - item.getValue().getLastUpdate() > recyclePeriod);
lastRecycle = now;
} finally {
updateLock.set(false);
}
}
return domainMap.get(domain);
}
/**
* The type Active count.
*/
protected static class ActiveCount {
private final AtomicLong count = new AtomicLong(Long.MIN_VALUE);
private volatile long lastUpdate;
ActiveCount(final long lastUpdate) {
this.lastUpdate = lastUpdate;
}
/**
* Increase count.
*/
void increase() {
count.addAndGet(1);
}

Please add regression coverage for slow outstanding requests, completed requests and adding an upstream after a long-running gateway has accumulated selections.

Environment

  • ShenYu: master, 2.7.2-SNAPSHOT
  • Commit: 862f5e37e894bd8cef85ef540a8629f313e9ecdf
  • Java 17

Activity

  1. added
    type: bug
    on Oct 22, 2026
  2. hanzhe-one commented on Oct 22, 2026

    @hanzhe-one
    Contributor

    /assign

  3. added a commit that references this issue on Oct 23, 2026
    dc59d7d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions