ninjapanzer opened a new pull request, #24290: URL: https://github.com/apache/kafka/pull/24290
Kafka Streams record caches and in-memory suppress buffers hold the raw key and value of every source record they store, and their size limits do not count those bytes. This PR keeps the raw record only when a configured handler can read it, and counts it when it is kept. ### Problem Since KIP-1034 (Kafka Streams 4.1), each `ProcessorRecordContext` carries the raw key and value of its source record, so that an exception handler can build a dead-letter queue record from it. The caching stores copy the raw record into each `LRUCacheEntry`, and `InMemoryTimeOrderedKeyValueChangeBuffer` holds the record context it is given. Neither size estimate includes the raw record, so `statestore.cache.max.bytes` and `Suppressed.BufferConfig.maxBytes` do not bound the memory these structures hold. A topology that stores a small value derived from a large input record, such as a `count()` or a `mapValues()` to a short string, can hold raw bytes many times its configured budget. On workloads with large input records and small stored values this has been observed to exhaust the heap. ### Changes 1. Keep the raw record only when a handler can read it. The new `SourceRawRecordRetention` reads the application configs once per store. Held records keep their raw record when `errors.dead.letter.queue.topic.name` is set to a non-blank value, or when a processing or production exception handler other than the built-in ones is configured. The deprecated `default.production.exception.handler` config counts as a production handler. The built-in handlers read the raw record only to build a dead-letter queue record, and deserialization handlers never run on a held record. When neither condition holds, `CachingKeyValueStore`, `CachingWindowStore`, `CachingSessionStore` and `TimeOrderedCachingWindowStore` store `null` for the raw key and value. An exception handler that runs during a cache flush then sees `null` for the raw record, which is what it already sees for records forwarded from a punctuation. 2. Suppress buffers hold a copy of the record context. `InMemoryTimeOrderedKeyValueChangeBuffer` stores a copy that carries the raw record only when it is kept. The caller can free the raw record on its own context after the put, and with a shared context that would change the size the buffer subtracts on eviction. `RocksDBTimeOrderedKeyValueBuffer` sizes each record without the raw record, because its serialized form does not include one. The size added on put then equals the size removed on eviction in both buffers. 3. Count the raw record when it is kept. `ProcessorRecordContext#residentMemorySizeEstimate()` adds the raw key and value lengths, so record caches and in-memory suppress buffers charge a kept raw record against their budget. The buffer copy in the second change is what keeps this count symmetric between put and eviction. ### Capacity Counting the raw record in every case would bound the cache, and would also spend the budget on bytes that the default configuration never reads. The following figures are arithmetic and were not measured. With 40 KB input records and a 1 MiB cache, an entry that carries its raw record costs at least 40 KB, so the cache holds about 25 entries. Without the raw record, an entry with a small key and value costs on the order of 100 bytes, so the same cache holds on the order of 10,000 entries, which matches its capacity before 4.1. With a dead-letter queue or a custom handler configured, this PR gives the same capacity as counting in every case. In the indexed `TimeOrderedCachingWindowStore`, the base entry and the index entry share the same raw arrays and each is charged for them, which overstates their size. `RecordCacheSourceRawRecordMemoryTest` measures retention. For each caching store type and for an in-memory suppress buffer, it pipes 400 records of 40 KB each (about 16 MB) through a 1 MiB budget and counts the raw input arrays still reachable after garbage collection. Without a dead-letter queue, at most one record's raw value is reachable. With a dead-letter queue, the reachable raw bytes stay within 1.25 times the budget plus one record. ### Relation to #24278 #24278 adds the raw key and value lengths to the size of `LRUCacheEntry` and keeps the raw record in every case. It does not change the suppress buffers. This PR overlaps it: it counts the raw record in every case where it is kept, extends the count to the in-memory suppress buffer, and adds the retention check. I will rebase onto whichever of the two lands first. ### Testing - `SourceRawRecordRetentionTest` covers the retention decision for an empty config, the Streams defaults, a dead-letter queue topic, a blank topic, built-in handlers given as classes or class names, a custom processing handler, a custom production handler under the deprecated config, and deserialization handlers. It also covers the buffer copy with and without the raw record. - `ProcessorRecordContextTest` and `LRUCacheEntryTest` check that the size estimate includes the raw key and value, and drops them after `freeRawRecord()`. - `CachingInMemoryKeyValueStoreTest` checks that a cache entry keeps the raw record only with a dead-letter queue. - `TimeOrderedKeyValueBufferTest` runs against both buffers. Without a dead-letter queue, the raw record is neither kept nor counted. With one, it is kept and counted. In both cases the buffer size returns to zero after eviction, including when the caller frees the raw record while the record is buffered. `RocksDBTimeOrderedKeyValueBufferTest` checks the same size return for the RocksDB buffer. - `RecordCacheSourceRawRecordMemoryTest` is the retention test described under Capacity, with a caching-disabled control. - `NamedCacheTest` and `ThreadCacheTest` have their size assertions updated for entries that carry a raw record. Refs: KAFKA-21552, KIP-1034, #24278 -- 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]
