Repository navigation
fix(common): keep memory-limited queue accounting with removals - #7496
Conversation
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
left a comment
There was a problem hiding this comment.
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:checkpasses forshenyu-common.
Notes
- Charging seeded elements in the collection constructor and releasing on
drainTo/ iteratorremove()/removeIf/removeAll/retainAllcloses the accounting gaps the issue describes; the constructor case is the important one, sincetake()no longer waits for memory that was never charged. clear()resets the limiter whileMemoryLimiter.reset()holds its ownfullyLock, so the reset cannot lose a concurrent update.
Possible Improvements
iterator().remove()releasescurrent(the element returned by the most recentnext()), butLinkedBlockingQueue.Itr.remove()removeslastRet(the element from the previousnext()). I verified the JDK behaviour with a small program: afternext(); 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/retainAllare unaffected because they always remove immediately afternext(). MirroringlastRetin the wrapper (or releasing the value the delegate actually removed) would make this exact.- The overrides replace
LinkedBlockingQueue's batchedremoveIf/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 defaultCollection.removeIfwould have used the same iterator anyway, so the overrides are mainly for clarity. drainTo(c, maxElements)buffers the drained elements in anArrayListbefore releasing them. For a queue whose purpose is OOM protection, a large drain transiently doubles the retained memory; draining intocand releasing as elements are handed over would avoid that.- Adjacent and pre-existing:
remove(Object o)releasesinst.getObjectSize(o)of the argument rather than of the stored element, which drifts in the same way for equal-but-differently-sized objects.
|
I checked this on JDK 17 and 25 with mixed-size Integer/Long elements. After |
Aias00
left a comment
There was a problem hiding this comment.
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.
Fixes #7461
Seeded elements were enqueued by the collection constructor without a memory charge, so
take()waited inreleaseInterruptiblyon an empty budget.drainTo, iterator removal,removeIf,removeAllandretainAlldropped elements without releasing their reservation, so a lateroffercould reject work on an empty queue.IllegalStateExceptionand do not return the queue.drainTo(both overloads), iteratorremove,removeIf,removeAllandretainAll.drainTostill rejects a null destination anddrainTo(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 ofMemoryLimitedLinkedBlockingQueueTest.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; afterdrainTo, 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=truewas not run.