Skip to content

Round refill-based expiration up to whole milliseconds - #644

Merged
vladimir-bukhtoyarov merged 1 commit into
bucket4j:masterfrom
PHJ2000:fix-refill-expiration-rounding
Oct 26, 2026
Merged

vladimir-bukhtoyarov merged 1 commit into
bucket4j:masterfrom
PHJ2000:fix-refill-expiration-rounding

Conversation

@PHJ2000

@PHJ2000 PHJ2000 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #643.

The refill-based expiration strategy truncates fractional milliseconds, so a bucket needing 1,999,999 ns to refill gets a 1 ms TTL and can expire before its consumed token is restored. Round the refill delay up without adding to the nanosecond value, avoiding overflow at Long.MAX_VALUE.

Exact-millisecond delays, the keep-after-refill setting, the minimum 1 ms TTL, and serialization stay unchanged. Adds ten regression/control cases for greedy and interval refills, boundaries, a full bucket, and a nonzero retention setting.

Validation on OpenJDK 17:

  • Before the fix: five of the ten new cases fail with the expected truncated TTL.
  • ./mvnw --no-transfer-progress -B -pl bucket4j-core -am clean package: 1,012 tests, no failures/errors/skips.
  • Expanded dependent reactor (-pl bucket4j-core -am -amd clean package): 34 modules succeed; 3,736 tests run, with 8 Cassandra errors and no assertion failures. The same eight compatibility-checker configuration errors reproduce at base 959d97c in the two affected methods. They are unrelated to the expiration change. The reactor stops before Memcached.
  • Memcached subsequently passes separately: 32 tests (-pl bucket4j-memcached -am -Dtest=MemcachedTest -Dsurefire.failIfNoSpecifiedTests=false package).
  • git diff --check: passed.

AI disclosure: OpenAI Codex performed the investigation, implementation, test execution, and drafted this description. The original core regressions were also independently checked by a separate AI review; this is not a human review.

@vladimir-bukhtoyarov

Copy link
Copy Markdown
Collaborator

Good catch, I will look at weekend

Copilot AI 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.

🟢 Approval recommended

The focused rounding change addresses the defect safely and has targeted regression coverage, with no blocking issues identified.

No open issues 🎉

What changed in this PR

Fixes #643 by rounding refill-based expiration up to whole milliseconds, preventing distributed bucket state from expiring before refill completes.

Changes:

  • Uses division and remainder to round up safely, including at Long.MAX_VALUE nanoseconds.
  • Adds ten regression and control cases covering refill styles, rounding boundaries, retention, and full buckets.
File Description
bucket4j-core/​src/​test/​java/​io/​github/​bucket4j/​distributed/​expiration/​BasedOnTimeForRefillingBucketUpToMaxExpirationAfterWriteStrategyTest.java Adds regression tests and unchanged-behavior checks.
bucket4j-core/​src/​main/​java/​io/​github/​bucket4j/​distributed/​expiration/​BasedOnTimeForRefillingBucketUpToMaxExpirationAfterWriteStrategy.java Rounds fractional refill milliseconds up while preserving retention and minimum TTL behavior.

Review effort: Balanced

@vladimir-bukhtoyarov
vladimir-bukhtoyarov merged commit 8c48cc2 into bucket4j:master Oct 26, 2026
@vladimir-bukhtoyarov

Copy link
Copy Markdown
Collaborator

Will be released in few days

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.

Refill-based expiration truncates fractional milliseconds and can expire a bucket before refill

3 participants