GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix shared runPurgeScan eviction ordering and unify purge/scan separa… · netty/netty@73a1548

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src
    • main/java/io/netty/buffer
      • AdaptivePoolingAllocator.java
    • test/java/io/netty/buffer
      • AdaptiveByteBufAllocatorTest.java
      • SizeClassedChunkCacheTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
+53-23Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,28 @@ interface ChunkCache {
510510
// Cached chunks are detached from magazines: no readInitInto can happen, so segment count
511511
// can only grow (external releaseSegment returns) and never shrink. Once a chunk reaches
512512
// full capacity (remainingCapacity == capacity), it stays fully free while in the cache.
513+
//
514+
// Epoch-based aging invariants (both caches):
515+
//
516+
// 1. CLASSIFICATION: purge scans all chunks. Full (remainingCapacity == capacity) → epoch++.
517+
// Non-full → epoch = 0. Only full chunks can accumulate epoch.
518+
//
519+
// 2. EVICTION: full chunks with epoch > CHUNK_PURGE_THRESHOLD are evicted (markToDeallocate).
520+
// Eviction is immediate — all segments are in, no outstanding references.
521+
// Non-full chunks are never evicted (deallocation would be deferred, not immediate).
522+
// At least CHUNK_REUSE_QUEUE chunks are always retained (retention floor).
523+
//
524+
// 3. SCAN RESET: scanForCapacity resets purgeEpoch = 0 on the chunk it picks. The scan
525+
// knows the chunk is being used. The chunk gets allocated from, becomes non-full, and
526+
// the next purge resets its epoch anyway (non-full → 0). The scan reset covers the case
527+
// where all segments return before the next purge (short-lived buffers).
528+
//
529+
// 4. CONVERGENCE: idle full chunks that are never picked by scan age undisturbed across
530+
// purge cycles. After CHUNK_PURGE_THRESHOLD + 1 consecutive cycles of being full and
531+
// unpolled, they are evicted. Chunks picked by scan get epoch reset — aging interrupted.
532+
// Thread-local: deterministic, exact (partition orders epoch=0 at head, scan always
533+
// takes head). Shared: approximate, converges over multiple cycles (FIFO queue ordering,
534+
// LRU preference in scan, retained counter in purge).
513535
abstract static class SizeClassedChunkCache implements ChunkCache {
514536
static SizeClassedChunkCache create(boolean isThreadLocal) {
515537
return isThreadLocal ? new ThreadLocalSizeClassedChunkCache() : new SharedSizeClassedChunkCache();
@@ -819,8 +841,12 @@ public String toString() {
819841
* by this or a later scan, preventing livelock under concurrent access.
820842
*
821843
* <p><b>runPurgeScan</b> (every {@link #CHUNK_PURGE_POLLS_SHARED} polls):
822-
* drains the queue, ages full chunks (epoch++), resets non-full (epoch=0),
823-
* evicts past threshold while retaining at least {@link #CHUNK_REUSE_QUEUE} chunks.
844+
* drains the queue, ages full chunks (epoch++), resets non-full (epoch=0).
845+
* Non-candidate capacity chunks are re-offered inline. Eviction candidates (full,
846+
* epoch past threshold) and no-capacity chunks are deferred to a buffer. After the drain,
847+
* the buffer is walked with the known total: candidates are evicted while above
848+
* {@link #CHUNK_REUSE_QUEUE}, remainder re-offered. No selection — that is
849+
* {@code scanForCapacity}'s job (called after purge via {@code pollChunk}).
824850
*/
825851
static final class SharedSizeClassedChunkCache extends SizeClassedChunkCache {
826852
// Must exceed CHUNK_REUSE_QUEUE (the retention floor) to leave room for burst absorption.
@@ -861,24 +887,18 @@ private SizeClassedChunk scanForCapacity() {
861887
if (first.purgeEpoch == 0 && first.hasRemainingCapacity()) {
862888
return first;
863889
}
864-
if (first.hasRemainingCapacity()) {
865-
return scanForCapacitySlow(first);
866-
}
867890
long generation = scanGeneration.incrementAndGet();
868891
first.lastScanGeneration = generation;
892+
if (first.hasRemainingCapacity()) {
893+
return scanForCapacitySlow(generation, first);
894+
}
869895
if (!queue.offer(first)) {
870896
first.markToDeallocate();
871897
return null;
872898
}
873899
return scanForCapacitySlow(generation, null);
874900
}
875901

876-
private SizeClassedChunk scanForCapacitySlow(SizeClassedChunk fallback) {
877-
long generation = scanGeneration.incrementAndGet();
878-
fallback.lastScanGeneration = generation;
879-
return scanForCapacitySlow(generation, fallback);
880-
}
881-
882902
private SizeClassedChunk scanForCapacitySlow(long generation, SizeClassedChunk fallback) {
883903
SizeClassedChunk chunk;
884904
while ((chunk = queue.poll()) != null) {
@@ -914,8 +934,8 @@ private SizeClassedChunk scanForCapacitySlow(long generation, SizeClassedChunk f
914934

915935
private void runPurgeScan() {
916936
long generation = ++purgeGeneration;
917-
int noCapCount = 0;
918-
int survivors = queue.size();
937+
int bufCount = 0;
938+
int retained = 0;
919939
SizeClassedChunk[] buf = noCapacityBuffer;
920940
SizeClassedChunk chunk;
921941
while ((chunk = queue.poll()) != null) {
@@ -925,12 +945,16 @@ private void runPurgeScan() {
925945
}
926946
break;
927947
}
948+
retained++;
928949
int remaining = chunk.remainingCapacity();
929950
if (remaining == chunk.capacity()) {
930951
chunk.purgeEpoch++;
931-
if (chunk.purgeEpoch > CHUNK_PURGE_THRESHOLD && survivors > CHUNK_REUSE_QUEUE) {
932-
chunk.markToDeallocate();
933-
survivors--;
952+
if (chunk.purgeEpoch > CHUNK_PURGE_THRESHOLD) {
953+
if (bufCount == buf.length) {
954+
buf = Arrays.copyOf(buf, buf.length * 2);
955+
noCapacityBuffer = buf;
956+
}
957+
buf[bufCount++] = chunk;
934958
continue;
935959
}
936960
} else {
@@ -942,19 +966,25 @@ private void runPurgeScan() {
942966
chunk.markToDeallocate();
943967
}
944968
} else {
945-
if (noCapCount == buf.length) {
969+
if (bufCount == buf.length) {
946970
buf = Arrays.copyOf(buf, buf.length * 2);
947971
noCapacityBuffer = buf;
948972
}
949-
buf[noCapCount++] = chunk;
973+
buf[bufCount++] = chunk;
950974
}
951975
}
952-
for (int i = 0; i < noCapCount; i++) {
953-
buf[i].lastPurgeGeneration = generation;
954-
if (!queue.offer(buf[i])) {
955-
buf[i].markToDeallocate();
956-
}
976+
for (int i = 0; i < bufCount; i++) {
977+
chunk = buf[i];
957978
buf[i] = null;
979+
if (chunk.purgeEpoch > CHUNK_PURGE_THRESHOLD && retained > CHUNK_REUSE_QUEUE) {
980+
chunk.markToDeallocate();
981+
retained--;
982+
} else {
983+
chunk.lastPurgeGeneration = generation;
984+
if (!queue.offer(chunk)) {
985+
chunk.markToDeallocate();
986+
}
987+
}
958988
}
959989
purgeBudget.lazySet(CHUNK_PURGE_POLLS_SHARED);
960990
}
Collapse file

‎buffer/src/test/java/io/netty/buffer/AdaptiveByteBufAllocatorTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/AdaptiveByteBufAllocatorTest.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ private static void assertPurgeScanEvictsIdleChunks(AdaptiveByteBufAllocator all
291291
int buffersPerChunk = (int) (chunkSize / 256);
292292
probe.release();
293293

294-
int totalChunks = AdaptivePoolingAllocator.CHUNK_REUSE_QUEUE + 3;
294+
int totalChunks = (int) Math.max(purgePolls, AdaptivePoolingAllocator.CHUNK_REUSE_QUEUE) * 2 + 10;
295295
int totalBuffers = totalChunks * buffersPerChunk;
296296
java.util.List<ByteBuf> bufs = new java.util.ArrayList<>(totalBuffers);
297297
for (int i = 0; i < totalBuffers; i++) {
Collapse file

‎buffer/src/test/java/io/netty/buffer/SizeClassedChunkCacheTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/SizeClassedChunkCacheTest.java
+90-32Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,15 @@ void purgeReturnsNullWhenNoChunkHasCapacity(boolean threadLocal) {
9393

9494
// --- purge: epoch aging and eviction (both caches) ---
9595

96-
@ParameterizedTest
97-
@ValueSource(booleans = {true, false})
98-
void fullChunkAgesEachPurgeAndIsEvictedPastThreshold(boolean threadLocal) {
99-
SizeClassedChunkCache cache = SizeClassedChunkCache.create(threadLocal);
96+
@Test
97+
void fullChunkAgesEachPurgeAndIsEvictedPastThreshold_threadLocal() {
98+
SizeClassedChunkCache cache = SizeClassedChunkCache.create(true);
10099

101-
// Pad above retention floor so eviction is allowed
102100
for (int i = 0; i < AdaptivePoolingAllocator.CHUNK_REUSE_QUEUE; i++) {
103101
cache.offerChunk(chunkWithoutCapacity());
104102
}
105-
106-
// Working-set chunk absorbs scan picks — non-full, epoch=0 at head after partition
107103
SizeClassedChunk workingSet = chunkWithCapacity();
108104
cache.offerChunk(workingSet);
109-
110105
SizeClassedChunk idle = fullChunk();
111106
cache.offerChunk(idle);
112107

@@ -117,12 +112,33 @@ void fullChunkAgesEachPurgeAndIsEvictedPastThreshold(boolean threadLocal) {
117112
assertEquals(i + 1, idle.purgeEpoch);
118113
verify(idle, never()).markToDeallocate();
119114
}
120-
121115
SizeClassedChunk polled = cache.forcePurge();
122116
assertSame(workingSet, polled);
123117
verify(idle).markToDeallocate();
124118
}
125119

120+
@Test
121+
void fullChunkAgesAndIsEventuallyEvicted_shared() {
122+
SizeClassedChunkCache cache = SizeClassedChunkCache.create(false);
123+
124+
for (int i = 0; i < AdaptivePoolingAllocator.CHUNK_REUSE_QUEUE; i++) {
125+
cache.offerChunk(chunkWithoutCapacity());
126+
}
127+
SizeClassedChunk workingSet = chunkWithCapacity();
128+
cache.offerChunk(workingSet);
129+
SizeClassedChunk idle = fullChunk();
130+
cache.offerChunk(idle);
131+
132+
int maxCycles = (AdaptivePoolingAllocator.CHUNK_PURGE_THRESHOLD + 1) * 3;
133+
for (int i = 0; i < maxCycles; i++) {
134+
SizeClassedChunk polled = cache.forcePurge();
135+
if (polled != null) {
136+
cache.offerChunk(polled);
137+
}
138+
}
139+
verify(idle, atLeastOnce()).markToDeallocate();
140+
}
141+
126142
@ParameterizedTest
127143
@ValueSource(booleans = {true, false})
128144
void purgeResetsEpochForNonIdleChunks(boolean threadLocal) {
@@ -261,32 +277,25 @@ void purgeHandlesWrappedRingCorrectly() {
261277

262278
// --- bursty traffic: idle chunks are eventually evicted ---
263279

264-
@ParameterizedTest
265-
@ValueSource(booleans = {true, false})
266-
void cacheSettlesAtRetentionFloorAfterBurst(boolean threadLocal) {
267-
SizeClassedChunkCache cache = SizeClassedChunkCache.create(threadLocal);
280+
@Test
281+
void cacheSettlesAtRetentionFloorAfterBurst_threadLocal() {
282+
SizeClassedChunkCache cache = SizeClassedChunkCache.create(true);
268283

269284
int floor = AdaptivePoolingAllocator.CHUNK_REUSE_QUEUE;
270285
int excess = 10;
271286

272-
// Working-set chunk absorbs scan picks
273287
SizeClassedChunk workingSet = chunkWithCapacity();
274288
cache.offerChunk(workingSet);
275-
276-
// Retention floor padding
277289
for (int i = 0; i < floor - 1; i++) {
278290
cache.offerChunk(chunkWithoutCapacity());
279291
}
280-
281-
// Excess full chunks — should age and be evicted
282292
SizeClassedChunk[] excessChunks = new SizeClassedChunk[excess];
283293
for (int i = 0; i < excess; i++) {
284294
excessChunks[i] = fullChunk();
285295
cache.offerChunk(excessChunks[i]);
286296
}
287297

288-
int purgesNeeded = AdaptivePoolingAllocator.CHUNK_PURGE_THRESHOLD + 1;
289-
for (int i = 0; i < purgesNeeded; i++) {
298+
for (int i = 0; i < AdaptivePoolingAllocator.CHUNK_PURGE_THRESHOLD + 1; i++) {
290299
SizeClassedChunk polled = cache.forcePurge();
291300
assertSame(workingSet, polled);
292301
cache.offerChunk(workingSet);
@@ -298,29 +307,49 @@ void cacheSettlesAtRetentionFloorAfterBurst(boolean threadLocal) {
298307
verify(workingSet, never()).markToDeallocate();
299308
}
300309

310+
@Test
311+
void cacheSettlesAfterBurst_shared() {
312+
SizeClassedChunkCache cache = SizeClassedChunkCache.create(false);
313+
314+
int excess = 10;
315+
SizeClassedChunk workingSet = chunkWithCapacity();
316+
cache.offerChunk(workingSet);
317+
for (int i = 0; i < AdaptivePoolingAllocator.CHUNK_REUSE_QUEUE - 1; i++) {
318+
cache.offerChunk(chunkWithoutCapacity());
319+
}
320+
SizeClassedChunk[] excessChunks = new SizeClassedChunk[excess];
321+
for (int i = 0; i < excess; i++) {
322+
excessChunks[i] = fullChunk();
323+
cache.offerChunk(excessChunks[i]);
324+
}
325+
326+
int maxCycles = (AdaptivePoolingAllocator.CHUNK_PURGE_THRESHOLD + 1) * 3;
327+
for (int i = 0; i < maxCycles; i++) {
328+
SizeClassedChunk polled = cache.forcePurge();
329+
if (polled != null) {
330+
cache.offerChunk(polled);
331+
}
332+
}
333+
334+
for (SizeClassedChunk chunk : excessChunks) {
335+
verify(chunk, atLeastOnce()).markToDeallocate();
336+
}
337+
}
338+
301339
// --- epoch aging with working set ---
302340
// Scan resets epoch on pick (the chunk is being used). Partition sub-ordering puts
303341
// epoch=0 (recently used) at head, epoch>0 (idle) behind. Scan prefers head, so
304342
// idle chunks age undisturbed behind the working set.
305343

306-
@ParameterizedTest
307-
@ValueSource(booleans = {true, false})
308-
void excessFullChunksAgeWhileWorkingSetIsPreferred(boolean threadLocal) {
309-
SizeClassedChunkCache cache = SizeClassedChunkCache.create(threadLocal);
344+
@Test
345+
void excessFullChunksAgeWhileWorkingSetIsPreferred_threadLocal() {
346+
SizeClassedChunkCache cache = SizeClassedChunkCache.create(true);
310347

311-
// Pad to retention floor
312348
for (int i = 0; i < AdaptivePoolingAllocator.CHUNK_REUSE_QUEUE; i++) {
313349
cache.offerChunk(chunkWithoutCapacity());
314350
}
315-
316-
// Working-set chunk: non-full (remaining != capacity), has capacity.
317-
// Purge resets its epoch to 0. Partition puts it at head. Scan picks it.
318351
SizeClassedChunk workingSet = chunkWithCapacity();
319352
cache.offerChunk(workingSet);
320-
321-
// Excess full chunks: remaining == capacity, has capacity.
322-
// Purge ages them. Partition puts them behind working-set (epoch>0).
323-
// Scan doesn't reach them — working-set absorbs the pick.
324353
int excess = 3;
325354
SizeClassedChunk[] idleChunks = new SizeClassedChunk[excess];
326355
for (int i = 0; i < excess; i++) {
@@ -340,6 +369,35 @@ void excessFullChunksAgeWhileWorkingSetIsPreferred(boolean threadLocal) {
340369
verify(workingSet, never()).markToDeallocate();
341370
}
342371

372+
@Test
373+
void excessFullChunksEventuallyEvicted_shared() {
374+
SizeClassedChunkCache cache = SizeClassedChunkCache.create(false);
375+
376+
for (int i = 0; i < AdaptivePoolingAllocator.CHUNK_REUSE_QUEUE; i++) {
377+
cache.offerChunk(chunkWithoutCapacity());
378+
}
379+
SizeClassedChunk workingSet = chunkWithCapacity();
380+
cache.offerChunk(workingSet);
381+
int excess = 3;
382+
SizeClassedChunk[] idleChunks = new SizeClassedChunk[excess];
383+
for (int i = 0; i < excess; i++) {
384+
idleChunks[i] = fullChunk();
385+
cache.offerChunk(idleChunks[i]);
386+
}
387+
388+
int maxCycles = (AdaptivePoolingAllocator.CHUNK_PURGE_THRESHOLD + 1) * 3;
389+
for (int i = 0; i < maxCycles; i++) {
390+
SizeClassedChunk polled = cache.forcePurge();
391+
if (polled != null) {
392+
cache.offerChunk(polled);
393+
}
394+
}
395+
396+
for (SizeClassedChunk idle : idleChunks) {
397+
verify(idle, atLeastOnce()).markToDeallocate();
398+
}
399+
}
400+
343401
// --- full-but-active chunk must not be prematurely evicted ---
344402
// A chunk that is polled every purge cycle but whose buffers are short-lived
345403
// (all segments return before next purge) looks "full" (remaining == capacity)

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.