nicktelford commented on code in PR #24189:
URL: https://github.com/apache/kafka/pull/24189#discussion_r4540181889


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/NamedCache.java:
##########
@@ -365,10 +367,23 @@ synchronized void close() {
     synchronized void clear() {
         head = tail = null;
         currentSizeBytes = 0;
-        dirtyKeys.clear();
+        resetDirtyKeys();
         cache.clear();
     }
 
+    // HashMap#clear() nulls every slot of a table that never shrinks, so 
clearing a set that once held
+    // many keys costs as much as that peak. Replace the set once the load 
drops well below the peak.
+    private void resetDirtyKeys() {
+        final int size = dirtyKeys.size();
+        dirtyKeysPeak = Math.max(dirtyKeysPeak, size);

Review Comment:
   Done: the peak now updates in `put()` after each dirty add, so deletes 
cannot hide it. `shouldTrackDirtyKeysPeakAcrossDeletes` covers this. Nothing in 
production calls `NamedCache#delete` today, so the case could not occur yet, 
but the check is cheap and keeps the invariant for any future caller.



##########
streams/src/test/java/org/apache/kafka/streams/state/internals/NamedCacheTest.java:
##########
@@ -193,6 +193,31 @@ public void shouldRemoveDeletedValuesOnFlush() {
         assertNotNull(cache.get(Bytes.wrap(new byte[]{1})));
     }
 
+    @Test
+    public void shouldFlushDirtyEntriesInOrderAfterDirtyKeysShrink() {
+        final List<Bytes> flushed = new ArrayList<>();
+        cache.setListener(dirty -> dirty.forEach(entry -> 
flushed.add(entry.key())));
+
+        for (byte i = 0; i < 16; i++) {
+            cache.put(Bytes.wrap(new byte[]{i}), new LRUCacheEntry(new 
byte[]{i}, headers, true, 0, 0, 0, "", rawKey, rawValue));
+        }
+        cache.flush();
+        assertEquals(16, flushed.size());
+
+        // far fewer dirty keys than the previous flush, followed by an idle 
flush
+        flushed.clear();
+        cache.put(Bytes.wrap(new byte[]{1}), new LRUCacheEntry(new byte[]{1}, 
headers, true, 0, 0, 0, "", rawKey, rawValue));
+        cache.flush();
+        cache.flush();
+        assertEquals(List.of(Bytes.wrap(new byte[]{1})), flushed);

Review Comment:
   Done: the shrink test is replaced by five tests that use 
`assertSame`/`assertNotSame` on a new package-private `dirtyKeys()` accessor. 
They cover a flush at exactly a quarter of the peak (kept), below it (replaced, 
with flush order checked), a large flush followed directly by an idle flush, 
the `clear()` path, and deletes before a flush.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to