Skip to content
Closed
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
134 changes: 65 additions & 69 deletions src/sdk/transaction/txn_lock_resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,50 +79,11 @@ Status TxnLockResolver::ResolveLock(const pb::store::LockInfo& conflict_lock_inf
TxnCheckStatusTask task_check_status(stub_, conflict_lock_info.lock_ts(), conflict_lock_info.primary_lock(),
start_ts, txn_status, force_sync_commit, rollback_if_not_exist);
status = task_check_status.Run();
if (!status.ok()) {
if (status.IsTxnNotFound()) {
// check lock ttl expired
int64_t current_ts;
Status status1 = stub_.GetTsoProvider()->GenPhysicalTs(2, current_ts);
if (!status1.ok()) {
return status1;
}
if (conflict_lock_info.lock_ttl() > current_ts) {
// lock ttl not expired, wait and retry
DINGO_LOG(INFO) << fmt::format(
"[sdk.txn.{}] sleep {}ms, reason: TxnNotFound and lock_ttl({}) > current_ts({}), lock_info({}).",
start_ts, FLAGS_txn_check_status_interval_ms, conflict_lock_info.lock_ttl(), current_ts,
conflict_lock_info.ShortDebugString());
SleepUs(FLAGS_txn_check_status_interval_ms * 1000);
} else {
// lock ttl expired, next check will rollback txn if not exist
rollback_if_not_exist = true;
}
} else if (status.IsTxnLockConflict()) {
// if lock.ts > current_start_ts, return write conflict
if (txn_status.primary_lock_info.lock_ts() > start_ts) {
return Status::TxnWriteConflict(txn_status.primary_lock_info.ShortDebugString());
}
// check lock ttl expired
int64_t current_ts;
Status status1 = stub_.GetTsoProvider()->GenPhysicalTs(2, current_ts);
if (!status1.ok()) {
return status1;
}
if (txn_status.primary_lock_info.lock_ts() > 0 && txn_status.primary_lock_info.lock_ttl() > current_ts) {
// lock ttl not expired, wait and retry
DINGO_LOG(INFO) << fmt::format(
"[sdk.txn.{}] sleep {}ms, reason: TxnLockConflict and lock_ttl({}) > current_ts({}), lock_info({}).",
start_ts, FLAGS_txn_check_status_interval_ms, txn_status.primary_lock_info.lock_ttl(), current_ts,
txn_status.primary_lock_info.ShortDebugString());
SleepUs(FLAGS_txn_check_status_interval_ms * 1000);
}
} else {
return status;
}
} else {
// Save the definitive state to the cache and perform LRU eviction
if (txn_status.IsCommitted() || txn_status.IsRollbacked()) {
if (status.ok()) {
// Save the definitive state to the cache and perform LRU eviction.
// A state that still carries a live lock (e.g. an async commit primary lock reported
// via txn_result) is not definitive and must not be cached.
if ((txn_status.IsCommitted() || txn_status.IsRollbacked()) && txn_status.primary_lock_info.lock_ts() == 0) {
WriteLockGuard guard(cache_rw_lock_);
if (resolved_lock_cache_.find(conflict_lock_ts) == resolved_lock_cache_.end()) {
// insert into LRU if not present
Expand All @@ -140,14 +101,47 @@ Status TxnLockResolver::ResolveLock(const pb::store::LockInfo& conflict_lock_inf
}
break;
}

if (!status.IsTxnNotFound()) {
DINGO_LOG(WARNING) << fmt::format("[sdk.txn.{}] check primary lock status fail, lock_info({}), status({}).",
start_ts, conflict_lock_info.ShortDebugString(), status.ToString());
return status;
}

// TxnNotFound: the primary has neither a lock nor a commit/rollback record, the txn may
// still be prewriting (e.g. primary prewrite retrying after a region epoch change), so
// no lock may be removed based on this result.
int64_t current_ts;
Status status1 = stub_.GetTsoProvider()->GenPhysicalTs(2, current_ts);
if (!status1.ok()) {
return status1;
}
if (conflict_lock_info.lock_ttl() > current_ts) {
// lock ttl not expired, wait in place and re-check
DINGO_LOG(INFO) << fmt::format(
"[sdk.txn.{}] sleep {}ms, reason: TxnNotFound and lock_ttl({}) > current_ts({}), lock_info({}).", start_ts,
FLAGS_txn_check_status_interval_ms, conflict_lock_info.lock_ttl(), current_ts,
conflict_lock_info.ShortDebugString());
SleepUs(FLAGS_txn_check_status_interval_ms * 1000);
} else if (!rollback_if_not_exist) {
// Lock ttl expired: ask the store to durably write a rollback record on the primary
// if the txn still does not exist, only then may the conflict lock be removed.
rollback_if_not_exist = true;
} else {
// The store keeps reporting txn_not_found even with rollback_if_not_exist, bail out
// fail-closed without touching any lock.
DINGO_LOG(WARNING) << fmt::format(
"[sdk.txn.{}] check status still TxnNotFound with rollback_if_not_exist, lock_info({}).", start_ts,
conflict_lock_info.ShortDebugString());
return status;
}
retry_times++;
}

if (!status.ok()) {
DINGO_LOG(WARNING) << fmt::format(
"[sdk.txn.{}] check primary lock status fail, retry_times({}), max_retry_times({}), interval_ms({}) "
"rollback_if_not_exist({}), "
"lock_info({}), status({}).",
"rollback_if_not_exist({}), lock_info({}), status({}).",
start_ts, retry_times, max_retry_times, FLAGS_txn_check_status_interval_ms, rollback_if_not_exist,
conflict_lock_info.ShortDebugString(), status.ToString());
return status;
Expand All @@ -169,8 +163,9 @@ Status TxnLockResolver::ResolveNormalLock(const pb::store::LockInfo& lock_info,
return Status::PushMinCommitTs("push min_commit_ts");
}

// Although lock push min commit_ts, the lock is still active.
if (txn_status.IsLocked()) {
// Although lock push min commit_ts, the lock is still active. A primary lock reported via
// txn_result also means the txn is alive even if the top-level ttl/commit_ts are unset.
if (txn_status.IsLocked() || (txn_status.commit_ts <= 0 && txn_status.primary_lock_info.lock_ts() > 0)) {
return Status::TxnLockConflict(fmt::format("lock still exist, lock_info({})", lock_info.ShortDebugString()));
}

Expand Down Expand Up @@ -208,27 +203,28 @@ Status TxnLockResolver::ResolveLockSecondaryLocks(const pb::store::LockInfo& pri
const pb::store::LockInfo& conflict_lock_info) {
DINGO_LOG(DEBUG) << fmt::format("[sdk.txn.{}] lock use async commit, lock_info({}), txn_status({}).", start_ts,
primary_lock_info.ShortDebugString(), txn_status.ToString());
// check lock ttl expired
// int64_t retry_times = 0;
// int64_t max_retry_times = (FLAGS_txn_heartbeat_lock_delay_ms / FLAGS_txn_check_status_interval_ms) + 1;
// while (retry_times < max_retry_times) {
// int64_t current_ts;
// Status status1 = stub_.GetTsoProvider()->GenPhysicalTs(2, current_ts);
// if (!status1.ok()) {
// return status1;
// }
// if (txn_status.primary_lock_info.lock_ttl() > current_ts) {
// // lock ttl not expired, can not resolve lock now
// DINGO_LOG(DEBUG) << fmt::format(
// "[sdk.txn.{}] async commit lock ttl not expired, lock_info({}), txn_status({}), current_ts({}).", start_ts,
// primary_lock_info.ShortDebugString(), txn_status.ToString(), current_ts);

// retry_times++;
// SleepUs(FLAGS_txn_check_status_interval_ms * 1000);
// continue;
// }
// break;
// }

// The store pushed the txn's min_commit_ts above the caller's start_ts: a snapshot read can
// safely proceed without waiting for this lock, same as the normal lock path.
if (txn_status.IsMinCommitTSPushed()) {
return Status::PushMinCommitTs("push min_commit_ts");
}

// Only resolve an async commit txn whose primary lock ttl has expired: a live txn may still
// be prewriting, and breaking one of its locks would let it pass the commit point while a
// key is already rolled back (partial commit / lost update).
int64_t current_ts;
Status status1 = stub_.GetTsoProvider()->GenPhysicalTs(2, current_ts);
if (!status1.ok()) {
return status1;
}
if (primary_lock_info.lock_ttl() > current_ts) {
DINGO_LOG(INFO) << fmt::format(
"[sdk.txn.{}] async commit lock ttl not expired, can not resolve now, lock_info({}), current_ts({}).",
start_ts, primary_lock_info.ShortDebugString(), current_ts);
return Status::TxnLockConflict(
fmt::format("async commit primary lock alive, lock_info({})", primary_lock_info.ShortDebugString()));
}

// check secondary locks status
TxnSecondaryLockStatus txn_secondary_lock_status;
Expand Down
Loading
Loading