Savonitar opened a new pull request, #30241:
URL: https://github.com/apache/flink/pull/30241

   ## What is the purpose of the change
   
   Backport of #30190 to `release-2.4` 
([FLINK-41519](https://issues.apache.org/jira/browse/FLINK-41519)).
   
   RocksDB MapState uses an exclusive upper bound to keep a scan from visiting 
deletion markers belonging to the next map. That bound came with FLINK-40584, 
which is in 2.4. Creating a native slice for the bound and a bounded copy of 
the read options on every `isEmpty()` call, `clear()`, and iterator cache 
refill adds overhead even when the scan reads few entries. This change reuses 
both across successive scans.
   
   This PR depends on #30190, which is not merged yet. It stays a draft until 
then and will follow any change made there.
   
   ## Brief change log
   
   Same as #30190:
   
   - Lazily allocate, once per backend, an upper-bound slice and a bounded copy 
of the read options that points to it. A scan writes the prefix successor 
directly into the slice's buffer and creates its iterator with the cached 
options.
   - Reuse both once the iterator that uses them closes. Overlapping scans and 
bounds larger than 256 bytes get a private slice and their own copy of the read 
options, which is the code path before this change. So does every scan of a 
backend that cannot allocate the 256-byte buffer from direct memory; the 
backend logs that once.
   - The bounded copy of the read options is made once, at the first scan that 
reuses the bound. This adds a rule: the backend's read options must not change 
after the first MapState scan and must not carry a snapshot. No main code of 
`release-2.4` changes them. The rule is documented on the factory and on 
`RocksDBKeyedStateBackend#getReadOptions()`.
   
   Different from #30190:
   
   - `release-2.4` still has Queryable State. Its server threads scan MapState 
through `RocksDBMapState#getSerializedValue` while the task thread accesses the 
state, and the factory is not thread-safe. Those scans therefore stay out of 
the factory: `RocksDBMapIterator` gets a method that creates the iterator of 
one cache refill, and the iterator of `getSerializedValue` overrides it and 
keeps calling `getRocksIteratorBoundedByPrefix` directly, which is what 2.4 
does today. No lock is added, and Queryable State reads keep their current cost.
   - The commits mirror those of #30190, each prefixed with `[BP-2.4]`. The 
Queryable State adaptation is part of the first commit, so that every commit is 
correct on its own.
   
   ## Verifying this change
   
   - The unit tests of `flink-statebackend-rocksdb` pass locally on this 
branch, and so do the Queryable State tests that use RocksDB: 
`KVStateRequestSerializerRocksDBTest`, 
`NonHAQueryableStateRocksDBBackendITCase` and 
`HAQueryableStateRocksDBBackendITCase`. `RocksDBPrefixIteratorTest` and 
`RocksDBMapStateTest` pass after each of the four commits. A full `./mvnw clean 
verify` was not run locally.
   - Two tests in `RocksDBMapStateTest` cover the part that differs from 
master. A query of a map that needs three cache refills returns all entries, 
stays bounded and does not touch the factory. A query that runs while an 
iterator of the factory is open leaves both scans correct.
   - The tests of #30190 are included unchanged.
   - No benchmark was run on this branch. The code of the factory is the same 
as in #30190; its description has the numbers.
   
   ## Does this pull request potentially affect one of the following parts?
   
   - Runtime per-record code paths — Yes, RocksDB MapState scans.
   - Dependencies, public API, serializers, deployment or recovery, and the S3 
filesystem connector — No.
   
   ## Documentation
   
   This is an internal performance improvement. It introduces no new feature or 
configuration option.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [x] Yes
   
   Generated-by: Claude Code (Fable 5.2)
   Generated-by: Claude Code (Opus 5.5)
   Generated-by: OpenAI Codex (GPT-6)
   


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