Skip to content

fix(common): keep memory-limited queue accounting with removals - #7496

Merged
Aias00 merged 1 commit into
apache:masterfrom
kalayciburak:fix/memory-limited-queue-accounting
Oct 10, 2026
Merged

Aias00 merged 1 commit into
apache:masterfrom
kalayciburak:fix/memory-limited-queue-accounting

Conversation

@kalayciburak

Copy link
Copy Markdown
Contributor

Fixes #7461

Seeded elements were enqueued by the collection constructor without a memory charge, so take() waited in releaseInterruptibly on an empty budget. drainTo, iterator removal, removeIf, removeAll and retainAll dropped elements without releasing their reservation, so a later offer could reject work on an empty queue.

  • Charge each seed after construction. If the seed does not fit, throw IllegalStateException and do not return the queue.
  • Release the reservation for every element removed by drainTo (both overloads), iterator remove, removeIf, removeAll and retainAll.
  • drainTo still rejects a null destination and drainTo(this). A destination that throws still releases the drained reservation, so admission is not left stuck.

Regression coverage is in MemoryLimitedLinkedBlockingQueueAccountingTest, separate from the open edit of MemoryLimitedLinkedBlockingQueueTest.

Tests run

  • JAVA_HOME=17 ./mvnw -pl shenyu-common test -Dtest=MemoryLimitedLinkedBlockingQueueAccountingTest -Djacoco.skip=true — 9 run, 0 failed. Before the fix, seed charge was expected 16 and was 0; after drainTo, memory was expected 0 and was 32.
  • ./mvnw -pl shenyu-common test -Djacoco.skip=true — 420 run, 0 failed, 1 skipped (UpstreamCheckUtilsTest). Checkstyle reported 0 violations.
  • ./mvnw -pl shenyu-common apache-rat:check — unapproved 0.

./mvnw clean install -Dmaven.javadoc.skip=true was not run.

Seeded elements were not charged, so take() waited in release on an empty budget. drainTo and iterator, removeIf, removeAll and retainAll dropped elements without releasing their reservation.

Fixes apache#7461

@wy471x wy471x 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.

Hi, @kalayciburak, thanks for your PR. The direction is right and the coverage is good; I have one correctness nit on the iterator override.

Verification

On 42980c30e:

./mvnw -pl shenyu-common test -Dtest='MemoryLimitedLinkedBlockingQueueAccountingTest,MemoryLimitedLinkedBlockingQueueTest,MemoryLimiterTest'

  • 31 tests, 0 failures (9 of them new).
  • checkstyle:check passes for shenyu-common.

Notes

  • Charging seeded elements in the collection constructor and releasing on drainTo / iterator remove() / removeIf / removeAll / retainAll closes the accounting gaps the issue describes; the constructor case is the important one, since take() no longer waits for memory that was never charged.
  • clear() resets the limiter while MemoryLimiter.reset() holds its own fullyLock, so the reset cannot lose a concurrent update.

Possible Improvements

  • iterator().remove() releases current (the element returned by the most recent next()), but LinkedBlockingQueue.Itr.remove() removes lastRet (the element from the previous next()). I verified the JDK behaviour with a small program: after next(); next(); remove(); the queue drops the first element while this override releases the second element's size. With equal-sized elements it is invisible, but with mixed sizes the accounting drifts. removeIf/removeAll/retainAll are unaffected because they always remove immediately after next(). Mirroring lastRet in the wrapper (or releasing the value the delegate actually removed) would make this exact.
  • The overrides replace LinkedBlockingQueue's batched removeIf/removeAll/retainAll (which hold both locks for the whole batch) with a weakly consistent, one-lock-per-element iterator walk. Semantics are equivalent for single-threaded use, but a concurrent producer/consumer can now interleave with a bulk removal. A javadoc sentence about the weaker atomicity would help; the default Collection.removeIf would have used the same iterator anyway, so the overrides are mainly for clarity.
  • drainTo(c, maxElements) buffers the drained elements in an ArrayList before releasing them. For a queue whose purpose is OOM protection, a large drain transiently doubles the retained memory; draining into c and releasing as elements are handed over would avoid that.
  • Adjacent and pre-existing: remove(Object o) releases inst.getObjectSize(o) of the argument rather than of the stored element, which drifts in the same way for equal-but-differently-sized objects.

@kalayciburak

Copy link
Copy Markdown
Contributor Author

I checked this on JDK 17 and 25 with mixed-size Integer/Long elements. After next(); next(); remove(), LinkedBlockingQueue removes the second element, so current matches the delegate and the accounting stays correct. I left the iterator part unchanged. The other points look like follow-ups, and remove(Object) is pre-existing.

@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.

Summary

Approving. This fixes a real memory-accounting leak in MemoryLimitedLinkedBlockingQueue.

What's fixed

The queue only accounted bytes in offer/put and released in take/poll/clear/remove(Object), but drainTo, iterator().remove(), removeIf, removeAll, and retainAll removed elements without releasing their reserved bytes. After such removals, a later offer of the same total size would be wrongly rejected because the limiter still counted the freed memory.

The PR overrides those five methods to release from memoryLimiter (the iterator wrapper releases on remove(); drainTo releases in finally after transfer; removeIf/removeAll/retainAll delegate to the iterator), and seeds accounting in the collection constructor (throwing IllegalStateException if the seed exceeds the budget — matching the existing offer contract).

I verified getCurrentMemory() is public, and MemoryLimiter.acquire returns boolean / release is void — all consistent with the new call sites.

Tests

New MemoryLimitedLinkedBlockingQueueAccountingTest (174 lines) covers: collection-constructor seed charge, drainTo (full + maxElements) release, iterator().remove() release, removeIf release, take/poll release, over-budget seed rejection, and self-drainTo (IllegalArgumentException, accounting untouched). All branches are asserted against getCurrentMemory().

CI

build + the full shenyu-integrated-test-* matrix and Analyze (java) are green.

Non-blocking: the collection-constructor acquire failure path leaves the queue unbuilt (object discarded), which is fine since the seed is rejected before publication.

@Aias00
Aias00 merged commit 6bd8d20 into apache:master Oct 10, 2026
48 checks passed
@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.

[BUG] Memory-limited queue seed/bulk operations desynchronize byte reservations and queue contents

3 participants