-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Make MemBuffer Simple and Robust #58289
Comments
To the developers of TiDB, we need to reach a consensus that during execution, read executors (see below) should exclusively use snapshot read operations on the MemBuffer. It's clear that we cannot completely avoid concurrent read and write operations, but at the very least, we must ensure that the read results are consistent (from a snapshot). The related datasource executors are: The following usage can be considered improper:
*: Executor Thread 1 and 2 are two concurrent threads of the same SQL. The read-only check is performed by My proposal is that we introduce a snapshot interface for The execution procedure is like:
Welcome to vote by emoji, 👍 if agree, 👎 if you have other ideas. |
It seems a safer way. Just need to know if there will be regressions, in both latency and memory |
I haven't tested for it. I suppose no regression in most cases, for latency sensitive cases, the size of For memory consumption, the returned KVs are just slice headers, it's not cloned memory, the risk is not such high I think. |
Based on discussion, here are the subtasks:
|
Do we also need to abandon the thread unsafe exposed interfaces as well? |
We hope to make memdb fully thread safe by locking. In practice, they need to be addressed case by case, particularly when assessing potential performance impacts |
MemBuffer stores staging mutations before a transaction is committed. It offers a rich interface to meet TiDB's requirements.
The current usage in TiDB exceeds the capabilities
MemBuffer
was designed for. This issue identifies improper usages and tracks improvements.Unsafe Concurrent Opearations
MemBuffer
is not thread-safe, meaning it may panic due to data races if TiDB attempts concurrent operations.Even with mutexes to avoid data races, TiDB cannot guarantee correctness, as explained in this comment.
There is already an
RWMutex
inMemBuffer
, used to preventSnapshotGetter
races with write operations. While it feels unusual to have anRWMutex
in a single-threaded data structure, at least the write operations will not affect snapshot read results.Improper Snapshot Usage
Consider a simple scenario: we have a B-tree, and how do we update it during iteration?
MemBuffer
providesSnapshotIter
andSnapshotIterReverse
methods. However, during iteration over a snapshot, TiDB does not fully drain it at once and may write new data intoMemBuffer
midway. To ensure the correctness of the snapshot iterator,ART
introduces a complex node retention mechanism (tikv/client-go#1503). This seems overly complex for an in-memory data structure. IMO, the iterator should fully drain the data in a single call, asMemBuffer
is not designed to support MVCC.Little changes.
More changes.
During each statement execution, TiDB should using the snapshot following this procedure:
MemBuffer
is dirty, if so, create aMemBufferSnapshot
bySnapshot
method.Snapshot
fetched in step1, it's thread safe.The text was updated successfully, but these errors were encountered: