kv: acquire latches during READ_UNCOMMITTED scans#160257
kv: acquire latches during READ_UNCOMMITTED scans#160257MASA-JAPAN wants to merge 1 commit intocockroachdb:masterfrom
Conversation
|
Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR. Before a member of our team reviews your PR, I have some potential action items for you:
🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
|
Thanks for your interest in contributing to CockroachDB! While this change looks straightforward on the surface, changes to transaction isolation require analysis and testing. If you are interested in pursuing this change, let's start by having a discussion on the motivation and desired end state in the original issue (#98862) so that we can make sure we don't ask you do to wasted work. A few preliminary notes:
This may still be a worthwhile change, but we'll probably want to first discuss a issues:
If you are interested in pursuing this change, please do follow up with a discussion on the original issue (#98862) about the motivation for this change at this time. For tracking purposes, I'm going to close this PR for now, but it can be re-opened once we are sure about the direction. |
kv: acquire latches during READ_UNCOMMITTED scans
Fixes #98862
Summary
This PR modifies READ_UNCOMMITTED requests to acquire latches, providing real-time ordering guarantees while still allowing dirty reads. Previously, READ_UNCOMMITTED requests skipped latches (behaving identically to INCONSISTENT), which sacrificed real-time ordering guarantees.
Problem
READ_UNCOMMITTED scans could violate real-time ordering by missing recently written data. Consider this scenario:
Before this change (buggy behavior):
The issue: Process B's read happened AFTER Process A's write completed, but Process B didn't see the write because READ_UNCOMMITTED skipped latches. The read could execute in parallel with the write being applied to storage, leading to stale data being returned.
After this change (correct behavior):
Real-time ordering guaranteed: Reads that start after a write completes will see that write's data.
Real-world example:
In an e-commerce system using READ_UNCOMMITTED for performance:
Two customers could both see "1 in stock" simultaneously and both successfully order, even though one update completed before the other's read started.
Solution
This PR makes READ_UNCOMMITTED acquire latches (as shown in the "After" section above), ensuring real-time ordering guarantees while still differing from CONSISTENT reads in that it can see uncommitted/in-flight data (dirty reads).
Changes
pkg/kv/kvserver/concurrency/concurrency_manager.go: ModifiedshouldIgnoreLatches()to check specifically forINCONSISTENTrather than!= CONSISTENT, allowing READ_UNCOMMITTED to acquire latches.pkg/kv/kvpb/api.proto: Updated documentation to clarify that READ_UNCOMMITTED now acquires latches for real-time ordering, distinguishing it from INCONSISTENT reads.pkg/kv/kvserver/concurrency/concurrency_manager_test.go: Added support forread-uncommittedflag in the test DSL.pkg/kv/kvserver/concurrency/testdata/concurrency_manager/no_latches: Added test case verifying READ_UNCOMMITTED acquires latches while INCONSISTENT does not.Background: Latches vs Locks
CockroachDB uses two synchronization mechanisms:
This PR is about latches, which provide ordering for individual operations. Without latches, READ_UNCOMMITTED operations could run in parallel with writes on the same keys, breaking real-time ordering guarantees.
Behavior Comparison
Testing
TestConcurrencyManagerBasic/no_latchesto verify READ_UNCOMMITTED acquires latchesRelease Notes: None
Epic: None