Skip to content

[fix][sdk] Serialize TSO refills and reuse cached batches - #157

Merged
visualYJD merged 1 commit into
mainfrom
feat/tso_072901
Aug 21, 2026
Merged

visualYJD merged 1 commit into
mainfrom
feat/tso_072901

Conversation

@rock-git

@rock-git rock-git commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Prevent duplicate coordinator fetches under concurrent TSO allocation and keep cached batches usable until exhaustion or staleness.

Changes

  • Serialize TSO refills so concurrent callers share one coordinator round trip.
  • Move coordinator RPCs outside the allocation lock, keeping cached allocations and physical-time reads responsive.
  • Make stale_period_us reloadable 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.

Compound Engineering
GPT_5.6_Luna_(High)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 3, 2026 08:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 TsoProvider allocation/refill flow to run coordinator RPCs outside the allocation lock and add single-flight refill serialization.
  • Adds reloadable support for stale_period_us via 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::cout in 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 reads max_logical_, next_logical_, and physical_ without rwlock_, 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 thread src/sdk/transaction/tso.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();
}

@visualYJD visualYJD left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@visualYJD
visualYJD added this pull request to the merge queue Aug 21, 2026
Merged via the queue into main with commit 1d6c987 Aug 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants