Skip to content

fix(loadbalancer): avoid int overflow of uptime in warmup weight calculation - #8005

Merged
yuluo-yx merged 1 commit into
apache:masterfrom
Duansg:fix/loadbalancer-warmup-uptime-overflow
Oct 24, 2026
Merged

yuluo-yx merged 1 commit into
apache:masterfrom
Duansg:fix/loadbalancer-warmup-uptime-overflow

Conversation

@Duansg

@Duansg Duansg commented Oct 23, 2026

Copy link
Copy Markdown
Member

What

AbstractLoadBalancer#getWeight computed int uptime = (int) (now - timestamp). The timestamp comes from the upstream's dateCreated or registration time, so for long-running upstreams the cast overflows:

  • After ~24.8 days, uptime becomes negative. The uptime > 0 check fails, so full weight is returned and there is no visible impact.
  • After ~49.7 days (2^32 ms), the low 32 bits fall into (0, warmup) again. The upstream's weight drops to near 1 and ramps back up over the default 10-minute warmup. This repeats every 49.7 days.

How

Keep uptime as long for the comparison, and cast to int only inside the warmup window.

Tests

Added AbstractLoadBalancerTest covering: within warmup, after warmup, uptime > Integer.MAX_VALUE, and uptime = 2^32 ms + 5 min. Without the fix, the last case fails (expected: <100> but was: <50>).

Make sure that:

  • You have read the contribution guidelines.
  • You submit test cases (unit or integration tests) that back your changes.
  • Your local test passed ./mvnw clean install -Dmaven.javadoc.skip=true.

@Aias00 Aias00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The overflow was real: (int) (System.currentTimeMillis() - timestamp) truncates the low 32 bits, so an upstream older than ~24.8 days could land back inside the warmup window and get a reduced weight. Widening uptime to long and only narrowing inside the uptime < warmup branch is safe because warmup is an int.

The regression tests are well chosen, especially fullWeightWhenUptimeLowBitsFallInWarmupTest, which pins the exact truncation case.

@yuluo-yx
yuluo-yx merged commit 4faa5ac into apache:master Oct 24, 2026
31 checks passed
@Duansg
Duansg deleted the fix/loadbalancer-warmup-uptime-overflow branch October 25, 2026 13:24
dmsolr pushed a commit that referenced this pull request Oct 25, 2026
Sync to current master to resolve the stable shenyu-integrated-test-http CI failure (CachePluginTest.testMemoryModle/testRedisModle). Root cause: this branch predated #8079, whose pre-fix CachedResponse.capture() refused to store any response carrying a Vary header, so the cached endpoint was never reused and both requests hit the backend. #8079 keys Vary responses on the selecting request headers instead. Also pulls in the divide/loadbalancer work (#8076, #7172, #7986, #8005, #7821, #8033, #8090).

#7172 removeStaleUpstreams reconciles via removeFromMap/triggerRemoveOne, which republish the immutable healthy snapshot under the same lock, so the read-only snapshot model stays correct with no stale retained instances.
@Aias00 Aias00 added this to the 2.7.2 milestone Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants