Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/sdk/rpc/brpc/unary_rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ class UnaryRpc : public Rpc {
}
}

brpc_ctx->cb();
// Move the callback onto this frame before invoking: the chain may
// release the last owner of this rpc, so neither the callback storage nor
// any other member may be touched once it starts running.
RpcCallback cb = std::move(brpc_ctx->cb);
cb();
}

void Reset() override {
Expand Down
6 changes: 5 additions & 1 deletion src/sdk/rpc/grpc/unary_rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ class UnaryRpc : public Rpc {
}
TraceRpcPerformance(end_time - start_time, Method(), context->peer(), str);

grpc_ctx->cb();
// Move the callback onto this frame before invoking: the chain may
// release the last owner of this rpc, so neither the callback storage nor
// any other member may be touched once it starts running.
RpcCallback cb = std::move(grpc_ctx->cb);
cb();
}

void Reset() override {
Expand Down
18 changes: 18 additions & 0 deletions src/sdk/rpc/store_rpc_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#ifndef DINGODB_SDK_STORE_RPC_CONTROLLER_H_
#define DINGODB_SDK_STORE_RPC_CONTROLLER_H_

#include <atomic>
#include <memory>
#include <vector>

#include "dingosdk/status.h"
#include "proto/error.pb.h"
Expand Down Expand Up @@ -98,6 +100,22 @@ class StoreRpcController {
StatusCallback call_back_;
};

// One retry round of fan-out store rpcs. Tasks that retry by re-running
// DoAsync must keep each round's rpcs/controllers in a StoreRpcRound owned by
// the AsyncCall callbacks (capture the shared_ptr by value), never in task
// members that the next round clears: a zero-delay retry round runs while the
// previous round's issuing frame and callback chains are still on other
// threads' stacks. pending must be set to rpcs.size() + 1 before issuing; the
// extra count is released by the issuing frame after its last use of task
// state, so the task cannot complete (and be destroyed) underneath that frame.
// See TxnBatchGetTask for the reference usage.
template <typename RpcType>
struct StoreRpcRound {
std::vector<std::shared_ptr<RpcType>> rpcs;
std::vector<StoreRpcController> controllers;
std::atomic<int> pending{0};
};

} // namespace sdk
} // namespace dingodb

Expand Down
104 changes: 68 additions & 36 deletions src/sdk/transaction/txn_task/txn_batch_get_task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,37 @@ Status TxnBatchGetTask::Init() {
}

void TxnBatchGetTask::DoAsync() {
// Owns this frame's +1 pending count. Declared before the tracer cleanup so
// it runs after it (reverse destruction order): once released, the last
// finished sub task may complete the task and destroy it, so nothing may
// touch task state afterwards.
std::shared_ptr<Round> round;
SCOPED_CLEANUP({
if (round) {
FinishSubTask(round);
}
});

auto start_time = TimestampUs();
SCOPED_CLEANUP(txn_impl_->GetTracer()->IncrementReadSdkTime(TimestampUs() - start_time););

std::set<std::string_view> next_batch;
std::unordered_map<int64_t, uint64_t> resolved_locks;
{
WriteLockGuard guard(rw_lock_);
next_batch = next_keys_;
need_retry_ = false;
status_ = Status::OK();
resolved_locks.swap(region_id_to_resolved_lock_);
}

if (next_batch.empty()) {
DoAsyncDone(Status::OK());
// completes via the round release above
round = std::make_shared<Round>();
round->pending.store(1);
return;
}

rpcs_.clear();
controllers_.clear();

std::unordered_map<int64_t, std::shared_ptr<Region>> region_id_to_region;
std::unordered_map<int64_t, std::vector<std::string_view>> region_id_to_keys;

Expand All @@ -74,7 +86,13 @@ void TxnBatchGetTask::DoAsync() {
std::shared_ptr<Region> tmp;
Status s = meta_cache->LookupRegionByKey(key, tmp);
if (!s.ok()) {
DoAsyncDone(s);
{
WriteLockGuard guard(rw_lock_);
status_ = s;
need_retry_ = false;
}
round = std::make_shared<Round>();
round->pending.store(1);
return;
}
auto iter = region_id_to_region.find(tmp->RegionId());
Expand All @@ -84,17 +102,19 @@ void TxnBatchGetTask::DoAsync() {
region_id_to_keys[tmp->RegionId()].push_back(key);
}

controllers_.reserve(region_id_to_keys.size());
rpcs_.reserve(region_id_to_keys.size());
round = std::make_shared<Round>();
round->controllers.reserve(region_id_to_keys.size());
round->rpcs.reserve(region_id_to_keys.size());
for (const auto& entry : region_id_to_keys) {
auto region_id = entry.first;
auto iter = region_id_to_region.find(region_id);
CHECK(iter != region_id_to_region.end());
auto region = iter->second;

uint64_t resolved_lock = 0;
if (region_id_to_resolved_lock_.find(region_id) != region_id_to_resolved_lock_.end()) {
resolved_lock = region_id_to_resolved_lock_[region_id];
auto resolved_iter = resolved_locks.find(region_id);
if (resolved_iter != resolved_locks.end()) {
resolved_lock = resolved_iter->second;
}

auto rpc = std::make_shared<TxnBatchGetRpc>();
Expand All @@ -106,21 +126,24 @@ void TxnBatchGetTask::DoAsync() {
for (const auto& key : entry.second) {
*rpc->MutableRequest()->add_keys() = key;
}
controllers_.emplace_back(stub, rpc, region);
rpcs_.push_back(rpc);
round->controllers.emplace_back(stub, rpc, region);
round->rpcs.push_back(rpc);
}
CHECK(rpcs_.size() == controllers_.size());
sub_tasks_count_.store(rpcs_.size());

region_id_to_resolved_lock_.clear();

for (int i = 0; i < rpcs_.size(); ++i) {
auto& controller = controllers_[i];
controller.AsyncCall([this, rpc = rpcs_[i].get()](const Status& s) { TxnBatchGetRpcCallback(s, rpc); });
CHECK(round->rpcs.size() == round->controllers.size());
// +1 for this issuing frame, released by the scoped cleanup above
round->pending.store(static_cast<int>(round->rpcs.size()) + 1);

for (size_t i = 0; i < round->rpcs.size(); ++i) {
auto& controller = round->controllers[i];
// round is captured by value: the callbacks collectively keep this
// round's rpcs/controllers alive until every chain fully unwinds
controller.AsyncCall(
[this, round, rpc = round->rpcs[i].get()](const Status& s) { TxnBatchGetRpcCallback(round, s, rpc); });
}
}

void TxnBatchGetTask::TxnBatchGetRpcCallback(const Status& status, TxnBatchGetRpc* rpc) {
void TxnBatchGetTask::TxnBatchGetRpcCallback(const std::shared_ptr<Round>& round, const Status& status,
TxnBatchGetRpc* rpc) {
DINGO_LOG(DEBUG) << fmt::format("[sdk.txn.{}] rpc: {} request: {} response: {}", txn_impl_->ID(), rpc->Method(),
rpc->Request()->ShortDebugString(), rpc->Response()->ShortDebugString());

Expand All @@ -131,6 +154,7 @@ void TxnBatchGetTask::TxnBatchGetRpcCallback(const Status& status, TxnBatchGetRp

Status s;
bool need_retry = false;
uint64_t resolved_lock_ts = 0;
const auto* response = rpc->Response();
if (!status.ok()) {
DINGO_LOG(WARNING) << fmt::format("[sdk.txn.{}] rpc: {} send to region: {} fail: {}", txn_impl_->ID(),
Expand All @@ -145,8 +169,7 @@ void TxnBatchGetTask::TxnBatchGetRpcCallback(const Status& status, TxnBatchGetRp
if (s.ok()) {
need_retry = true;
} else if (s.IsPushMinCommitTs()) {
region_id_to_resolved_lock_[rpc->Request()->context().region_id()] =
response->txn_result().locked().lock_ts();
resolved_lock_ts = response->txn_result().locked().lock_ts();
need_retry = true;
s = Status::OK();
}
Expand All @@ -172,6 +195,9 @@ void TxnBatchGetTask::TxnBatchGetRpcCallback(const Status& status, TxnBatchGetRp
}
} else {
need_retry_ = true;
if (resolved_lock_ts != 0) {
region_id_to_resolved_lock_[rpc->Request()->context().region_id()] = resolved_lock_ts;
}
}
} else {
if (status_.ok()) {
Expand All @@ -184,21 +210,27 @@ void TxnBatchGetTask::TxnBatchGetRpcCallback(const Status& status, TxnBatchGetRp
}
}

if (sub_tasks_count_.fetch_sub(1) == 1) {
Status tmp;
bool tmp_need_retry = false;
{
ReadLockGuard guard(rw_lock_);
tmp = status_;
tmp_need_retry = need_retry_;
}
if (tmp.ok() && tmp_need_retry) {
txn_impl_->GetTracer()->IncrementReadRetryCount(1);
DoAsyncRetry();
return;
}
DoAsyncDone(tmp);
FinishSubTask(round);
}

void TxnBatchGetTask::FinishSubTask(const std::shared_ptr<Round>& round) {
if (round->pending.fetch_sub(1) != 1) {
return;
}

Status tmp;
bool tmp_need_retry = false;
{
ReadLockGuard guard(rw_lock_);
tmp = status_;
tmp_need_retry = need_retry_;
}
if (tmp.ok() && tmp_need_retry) {
txn_impl_->GetTracer()->IncrementReadRetryCount(1);
DoAsyncRetry();
return;
}
DoAsyncDone(tmp);
}

} // namespace sdk
Expand Down
12 changes: 7 additions & 5 deletions src/sdk/transaction/txn_task/txn_batch_get_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,26 @@ class TxnBatchGetTask : public TxnTask {

std::string Name() const override { return "TxnBatchGetTask"; }

void TxnBatchGetRpcCallback(const Status& status, TxnBatchGetRpc* rpc);
using Round = StoreRpcRound<TxnBatchGetRpc>;

void TxnBatchGetRpcCallback(const std::shared_ptr<Round>& round, const Status& status, TxnBatchGetRpc* rpc);

// drops one pending count of the round; the last owner decides retry/done
void FinishSubTask(const std::shared_ptr<Round>& round);

const std::vector<std::string>& keys_;
std::vector<KVPair>& out_kvs_;

std::set<std::string_view> next_keys_;
std::shared_ptr<TxnImpl> txn_impl_;

// guarded by rw_lock_
std::unordered_map<int64_t, uint64_t> region_id_to_resolved_lock_;

std::vector<StoreRpcController> controllers_;
std::vector<std::shared_ptr<TxnBatchGetRpc>> rpcs_;
bool need_retry_{false};

RWLock rw_lock_;
Status status_;

std::atomic<int> sub_tasks_count_{0};
};

} // namespace sdk
Expand Down
Loading
Loading