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


##########
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:
   The output assertions also pass with the original clear-only implementation, 
and this idle flush runs after the one-key flush has already replaced the large 
set. Add assertions that the set is replaced below the threshold and retained 
at steady cardinality, including a large flush followed directly by an idle 
flush and the `clear()` path, so regressions in the allocation policy are 
detected.



##########
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:
   `dirtyKeysPeak` only observes the size at reset, so inserting 100,000 dirty 
keys and deleting all but one before the first flush records a peak of 1. 
Subsequent one-key and idle flushes never replace the large backing table, 
retaining its memory and the cost of clearing it on nonempty flushes. Update 
the peak with `Math.max(dirtyKeysPeak, dirtyKeys.size())` immediately after 
`dirtyKeys.add(key)` in `put()` so deletions cannot hide the allocation's 
high-water mark.



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