Repository navigation
[fix][sdk] Serialize TSO refills and reuse cached batches - #157
Merged
Merged
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the SDK TSO provider to avoid duplicate coordinator fetches under concurrent allocation by serializing refills and reusing cached batches, while keeping physical-time reads responsive during in-flight fetches.
Changes:
- Refactors
TsoProviderallocation/refill flow to run coordinator RPCs outside the allocation lock and add single-flight refill serialization. - Adds reloadable support for
stale_period_usvia a gflags validator. - Introduces new concurrency/batch-reuse/non-blocking physical timestamp unit tests.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| test/unit_test/sdk/transaction/test_tso.cc | Adds concurrent TSO allocation, batch reuse, and non-blocking physical timestamp tests. |
| src/sdk/transaction/tso.h | Adds refill/allocate helpers and a mutex to serialize refill fetches. |
| src/sdk/transaction/tso.cc | Refactors allocation to avoid holding rwlock_ across coordinator RPC and enable batch reuse. |
| src/sdk/common/param_config.cc | Adds a validator to make stale_period_us reloadable at runtime. |
Suppressed comments (2)
test/unit_test/sdk/transaction/test_tso.cc:142
- Same here: avoid unconditional
std::coutin tests; fold the timing data into the assertion message so it’s only emitted on failure.
std::cout << "GetPhysicalTs elapsed=" << elapsed_ms << "ms (fetch takes 300ms)" << std::endl;
EXPECT_LT(elapsed_ms, 100) << "physical ts blocked behind the fetch rpc";
src/sdk/transaction/tso.cc:104
GenPhysicalTs()now narrows the lock scope, but the post-loop error logging/status creation readsmax_logical_,next_logical_, andphysical_withoutrwlock_, which is a data race. Snapshot these fields under a read lock before logging/building the error status.
}
} while (retry++ < FLAGS_txn_op_max_retry);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
21
to
25
| #include "sdk/client_stub.h" | ||
| #include "sdk/common/common.h" | ||
| #include "sdk/common/helper.h" | ||
| #include "sdk/common/param_config.h" | ||
| #include "sdk/rpc/coordinator_rpc.h" | ||
| #include "sdk/utils/async_util.h" | ||
| #include "sdk/utils/rw_lock.h" |
Comment on lines
+66
to
+68
| // serializes fetchers so that concurrent callers share a single coordinator | ||
| // round trip instead of each issuing one; never held together with rwlock_ | ||
| bthread::Mutex fetch_mutex_; |
Comment on lines
+72
to
+85
| std::vector<std::vector<int64_t>> results(kThreadNum); | ||
| std::vector<std::thread> threads; | ||
| threads.reserve(kThreadNum); | ||
| for (int i = 0; i < kThreadNum; i++) { | ||
| threads.emplace_back([&, i]() { | ||
| for (int j = 0; j < kPerThread; j++) { | ||
| int64_t ts = 0; | ||
| ASSERT_TRUE(tso_provider->GenTs(2, ts).ok()); | ||
| results[i].push_back(ts); | ||
| } | ||
| }); | ||
| } | ||
| for (auto& thread : threads) thread.join(); | ||
|
|
Comment on lines
+95
to
+97
| std::cout << "fetches=" << fetch_count_.load() << " allocations=" << kThreadNum * kPerThread << std::endl; | ||
| // concurrent callers must share a round trip instead of each doing one | ||
| EXPECT_LT(fetch_count_.load(), static_cast<int64_t>(kThreadNum * kPerThread) / 10); |
Comment on lines
+55
to
58
| { | ||
| WriteLockGuard guard(rwlock_); | ||
| if (TryAllocate(count, ts, check_stale)) return Status::OK(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Prevent duplicate coordinator fetches under concurrent TSO allocation and keep cached batches usable until exhaustion or staleness.
Changes
stale_period_usreloadable and add concurrency, batch reuse, and non-blocking physical timestamp tests.Test plan
test_tso.cc: verifies unique concurrent timestamps, cached batch reuse, and physical timestamp latency during an in-flight fetch.