Per Dahlén created KAFKA-21673:
----------------------------------

             Summary: CachingKeyValueStore serves a stale value after a read 
during its own cache flush, so a later read-modify-write silently loses an 
update
                 Key: KAFKA-21673
                 URL: https://issues.apache.org/jira/browse/KAFKA-21673
             Project: Kafka
          Issue Type: Bug
          Components: streams
    Affects Versions: 4.2.2, 4.4.0
         Environment: Kafka Streams 4.2.2 and 4.4.0, Java 21; reproduced with 
TopologyTestDriver (no broker). Observed in production on Kafka Streams 4.2 
(Confluent 8.2.1-ce).
            Reporter: Per Dahlén
         Attachments: CachingStoreReadDuringFlushTest.java, build.gradle

{\{NamedCache#flush}} marks every dirty entry *clean* before the flush listener 
writes the entries to the underlying store one by one 
(\{{CachingKeyValueStore#putAndMaybeForward}}). If the stream thread reads the 
same caching store while that loop is running, a cache miss in 
\{{CachingKeyValueStore#getInternal}} puts a clean copy into the cache 
(\{{ThreadCache#put}} -> \{{maybeEvict}}). That can evict entries that are 
marked clean but *not yet written*; they are dropped silently because they look 
clean. A later read of such a key misses, loads the *old* value from the 
underlying store and caches it as clean. The flush loop then writes the *new* 
value to the underlying store and the changelog, but the cache keeps serving 
the old value. The next read-modify-write of that key (e.g. a KTable 
aggregation) builds on the stale value, and the update that was already written 
is overwritten.

Nothing fails and nothing is logged. The changelog briefly contains the correct 
value and then a value derived from the older one, so a restored store is wrong 
as well.

h3. How real topologies reach it
During a flush the listener forwards each entry downstream. Downstream 
KTable-KTable joins read other caching stores through value getters; their 
misses evict (the thread cache is shared and full) and can run nested flushes 
of those stores, whose forwarded entries read the first store again, for other 
keys. The stack trace in KAFKA-9310 shows this re-entrant path 
(\{{getInternal}} -> \{{NamedCache.evict}} -> \{{flush}} -> 
\{{putAndMaybeForward}}) in a real application.

h3. Observed
Kafka Streams 4.2 (Confluent 8.2.1-ce), at_least_once, one stream thread per 
instance, \{{statestore.cache.max.bytes}} 4 GiB, \{{commit.interval.ms}} 
300000. One sub-topology holds ten KTable \{{groupBy().aggregate()}} stores 
(sets per key) feeding a chain of non-materialized outer joins. During full 
rebuilds the three largest aggregates (1-1.6 M keys each) lose updates; the 
small ones never do.

A diagnostic rebuild with repartition purging off and changelog compaction held 
(so every write stayed visible) lost 44 values on 37 keys of one aggregate. For 
every one of them: the groupBy record reached the aggregate, the changelog 
*did* contain a write with the update, and a later write for the same key was 
computed from the value *before* that write (36 of 37 exactly the previous 
write). All of them came from one task and one contiguous changelog window 
(~7,400 writes), written by a cache *eviction* flush (not a commit) in the 
middle of a burst, during which the same task's sibling stores flushed within 
the same seconds, one of them without any input of its own (so its flush was 
triggered by reads). No rebalance, restart, producer error or GC problem.

The bug needs (1) a full thread cache, so that reads during a flush evict, and 
(2) a stale entry that survives in the cache until the key's next update. That 
is why it only shows with large stores and large caches, and why small-cache 
tests do not reproduce it.

h3. Reproduction
Attached \{{CachingStoreReadDuringFlushTest.java}} + \{{build.gradle}} (JUnit 5 
+ kafka-streams-test-utils, no broker). TopologyTestDriver uses the real 
\{{ThreadCache}} / \{{NamedCache}} / \{{CachingKeyValueStore}}. One record 
updates 100 keys of a caching store; while the commit flush writes the first 
entry, the underlying store's \{{put()}} reads 2,000 other keys and then a 
not-yet-written key through the caching store. The test asserts correct 
behaviour and fails on 4.2.2 and 4.4.0:
{noformat}
after the flush: underlying store has 'new', cache serves 'old'
after the next update: k99 = 'old+x', lost updates: 1 of 100
org.opentest4j.AssertionFailedError at CachingStoreReadDuringFlushTest.java:154
{noformat}
Run: \{{./gradlew test -PkafkaVersion=4.4.0}}

h3. Possible fixes
* Keep entries dirty (or otherwise non-evictable) until the listener has 
written them, i.e. mark an entry clean only after \{{putAndMaybeForward}} has 
handled it, or
* do not let \{{evict()}} drop entries that belong to an in-progress flush of 
the same \{{NamedCache}}, or
* do not insert into the cache from \{{getInternal}} while the same namespace 
is flushing.

h3. Workaround
Disable caching on the affected stores 
(\{{Materialized#withCachingDisabled()}}).

Related: KAFKA-4311 (eviction during flush, different symptom), KAFKA-9310 
(stack trace of the re-entrant path), KAFKA-21469 (NamedCache flush cost).



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to