Skip to content

Commit 7003b55

Browse files
committed
[feat][sdk]Support txn async rollback ordinary keys
1 parent be7eeaf commit 7003b55

12 files changed

Lines changed: 104 additions & 20 deletions

File tree

python/src/client_bindings.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ void DefineClientBindings(pybind11::module& m) {
247247
Status status = transaction.Scan(start_key, end_key, limit, kvs);
248248
return std::make_tuple(status, kvs);
249249
})
250-
.def("PreCommit", &Transaction::PreCommit)
251250
.def("Commit", &Transaction::Commit)
252251
.def("Rollback", &Transaction::Rollback);
253252

src/sdk/client_stub.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ Status ClientStub::Open(const std::vector<EndPoint>& endpoints) {
7070
actuator_ = std::make_shared<ThreadPoolActuator>();
7171
actuator_->Start(FLAGS_actuator_thread_num);
7272

73+
txn_actuator_ = std::make_shared<ThreadPoolActuator>();
74+
txn_actuator_->Start(FLAGS_txn_actuator_thread_num);
75+
7376
vector_index_cache_ = std::make_shared<VectorIndexCache>(*this);
7477

7578
document_index_cache_ = std::make_shared<DocumentIndexCache>(*this);
@@ -91,6 +94,9 @@ void ClientStub::Stop() {
9194
if (actuator_) {
9295
actuator_->Stop();
9396
}
97+
if (txn_actuator_) {
98+
txn_actuator_->Stop();
99+
}
94100
}
95101

96102
} // namespace sdk

src/sdk/client_stub.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ class ClientStub {
9595
return actuator_;
9696
}
9797

98+
virtual std::shared_ptr<Actuator> GetTxnActuator() const {
99+
DCHECK_NOTNULL(txn_actuator_.get());
100+
return txn_actuator_;
101+
}
102+
98103
virtual std::shared_ptr<VectorIndexCache> GetVectorIndexCache() const {
99104
DCHECK_NOTNULL(vector_index_cache_.get());
100105
return vector_index_cache_;
@@ -132,6 +137,7 @@ class ClientStub {
132137
std::shared_ptr<AdminTool> admin_tool_;
133138
std::shared_ptr<TxnLockResolver> txn_lock_resolver_;
134139
std::shared_ptr<Actuator> actuator_;
140+
std::shared_ptr<Actuator> txn_actuator_;
135141
std::shared_ptr<VectorIndexCache> vector_index_cache_;
136142
std::shared_ptr<DocumentIndexCache> document_index_cache_;
137143
std::shared_ptr<AutoIncrementerManager> auto_increment_manager_;

src/sdk/common/param_config.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
// sdk config
2121
DEFINE_int64(actuator_thread_num, 8, "actuator thread num");
22+
DEFINE_int64(txn_actuator_thread_num, 16, "txn actuator thread num");
2223

2324
// coordinator config
2425
DEFINE_int64(coordinator_interaction_delay_ms, 500, "coordinator interaction delay ms");
@@ -56,6 +57,7 @@ DEFINE_int64(vector_op_delay_ms, 500, "vector task base backoff delay ms");
5657
DEFINE_int64(vector_op_max_retry, 30, "vector task max retry times");
5758

5859
DEFINE_int64(txn_max_batch_count, 1000, "txn max batch count");
60+
DEFINE_int64(txn_max_async_commit_count, 256, "txn max async commit count");
5961

6062
DEFINE_bool(log_rpc_time, false, "log rpc time");
6163

src/sdk/common/param_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
// sdk config
2727
const int64_t kSdkVlogLevel = 60;
2828
DECLARE_int64(actuator_thread_num);
29+
DECLARE_int64(txn_actuator_thread_num);
2930

3031
// coordinator config
3132
const int64_t kPrefetchRegionCount = 3;
@@ -70,6 +71,7 @@ DECLARE_int64(vector_op_delay_ms);
7071
DECLARE_int64(vector_op_max_retry);
7172

7273
DECLARE_int64(txn_max_batch_count);
74+
DECLARE_int64(txn_max_async_commit_count);
7375
DECLARE_bool(log_rpc_time);
7476

7577
DECLARE_int64(txn_heartbeat_interval_ms);

src/sdk/transaction/txn_impl.cc

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ Status TxnImpl::PreWriteAndCommit() {
433433
}
434434

435435
void TxnImpl::ScheduleHeartBeat() {
436-
stub_.GetActuator()->Schedule(
436+
stub_.GetTxnActuator()->Schedule(
437437
[shared_this = shared_from_this(), start_ts = start_ts_.load(), primary_key = buffer_->GetPrimaryKey()] {
438438
shared_this->DoHeartBeat(start_ts, primary_key);
439439
},
@@ -608,9 +608,11 @@ Status TxnImpl::CommitOrdinaryKey() {
608608
}
609609
}
610610
// async commit ordinary keys
611-
stub_.GetActuator()->Schedule([shared_this = shared_from_this(),
612-
ordinary_keys = std::move(keys)] { shared_this->DoCommitOrdinaryKey(ordinary_keys); },
613-
0);
611+
stub_.GetTxnActuator()->Schedule(
612+
[shared_this = shared_from_this(), ordinary_keys = std::move(keys)] {
613+
shared_this->DoCommitOrdinaryKey(ordinary_keys);
614+
},
615+
0);
614616
return Status::OK();
615617
}
616618

@@ -683,15 +685,28 @@ Status TxnImpl::RollbackPrimaryKey() {
683685
std::vector<std::string> keys;
684686
std::string pk = buffer_->GetPrimaryKey();
685687
keys.push_back(pk);
686-
if (is_one_pc_) {
688+
bool is_one_pc = is_one_pc_.load();
689+
if (is_one_pc) {
687690
for (const auto& [key, _] : buffer_->Mutations()) {
688691
if (key != pk) {
689692
keys.push_back(key);
690693
}
691694
}
692695
}
693-
TxnBatchRollbackTask task(stub_, keys, shared_from_this());
694-
return task.Run();
696+
TxnBatchRollbackTask task(stub_, std::move(keys), shared_from_this(), is_one_pc);
697+
Status status = task.Run();
698+
if (!status.ok()) {
699+
DINGO_LOG(WARNING) << fmt::format("[sdk.txn.{}] 1pc rollback key fail, status({}).", ID(), status.ToString());
700+
if (!is_one_pc) {
701+
// retry rollback primary key
702+
DINGO_LOG(INFO) << fmt::format("[sdk.txn.{}] retry rollback primary key.", ID());
703+
is_one_pc_.store(false);
704+
std::vector<std::string> primary_key = {pk};
705+
TxnBatchRollbackTask task(stub_, std::move(primary_key), shared_from_this(), is_one_pc);
706+
status = task.Run();
707+
}
708+
}
709+
return status;
695710
}
696711

697712
Status TxnImpl::RollbackOrdinaryKey() {
@@ -704,8 +719,30 @@ Status TxnImpl::RollbackOrdinaryKey() {
704719
}
705720
keys.push_back(key);
706721
}
707-
TxnBatchRollbackTask task(stub_, keys, shared_from_this());
708-
return task.Run();
722+
// async rollback ordinary keys
723+
stub_.GetTxnActuator()->Schedule(
724+
[shared_this = shared_from_this(), ordinary_keys = std::move(keys)] {
725+
shared_this->DoRollbackOrdinaryKey(ordinary_keys);
726+
},
727+
0);
728+
return Status::OK();
729+
}
730+
731+
void TxnImpl::DoRollbackOrdinaryKey(std::vector<std::string> keys) {
732+
CHECK(state_.load() == kRollbacked) << "state is not rollbacked, state:" << StateName(state_.load());
733+
bool is_one_pc = is_one_pc_.load();
734+
CHECK(!is_one_pc) << fmt::format("[sdk.txn.{}] 1pc txn should not rollback ordinary keys.", ID());
735+
std::shared_ptr<TxnBatchRollbackTask> txn_rollback_task =
736+
std::make_shared<TxnBatchRollbackTask>(stub_, keys, shared_from_this(), is_one_pc);
737+
738+
DINGO_LOG(DEBUG) << fmt::format("[sdk.txn.{}]rollback ordinary keys, size({}).", ID(), keys.size());
739+
740+
auto status = txn_rollback_task->Run();
741+
if (!status.ok()) {
742+
DINGO_LOG(WARNING) << fmt::format("[sdk.txn.{}] rollback ordinary keys fail. status({}).", ID(), status.ToString());
743+
}
744+
state_.store(kFinshed);
745+
Cleanup();
709746
}
710747

711748
Status TxnImpl::DoRollback() {
@@ -742,8 +779,6 @@ Status TxnImpl::DoRollback() {
742779
if (!status.IsOK()) {
743780
DINGO_LOG(WARNING) << fmt::format("[sdk.txn.{}] rollback ordinary keys fail, status({}).", ID(), status.ToString());
744781
}
745-
state_.store(kFinshed);
746-
Cleanup();
747782

748783
return Status::OK();
749784
}

src/sdk/transaction/txn_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ class TxnImpl : public std::enable_shared_from_this<TxnImpl> {
212212
// txn rollback
213213
Status RollbackPrimaryKey();
214214
Status RollbackOrdinaryKey();
215+
void DoRollbackOrdinaryKey(std::vector<std::string> keys);
215216
Status DoRollback();
216217

217218
void DoHeartBeat(int64_t start_ts, std::string primary_key);

src/sdk/transaction/txn_task/txn_batch_rollback_task.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ void TxnBatchRollbackTask::DoAsync() {
8080
std::string msg = fmt::format("[sdk.txn.{}] one pc rollback only support one region, but got {} regions",
8181
txn_impl_->ID(), region_id_to_region.size());
8282
DINGO_LOG(ERROR) << msg;
83+
is_one_pc_ = false;
8384
DoAsyncDone(Status::InvalidArgument(msg));
8485
return;
8586
}

src/sdk/transaction/txn_task/txn_batch_rollback_task.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ namespace sdk {
3333

3434
class TxnBatchRollbackTask : public TxnTask {
3535
public:
36-
TxnBatchRollbackTask(const ClientStub& stub, const std::vector<std::string>& keys, std::shared_ptr<TxnImpl> txn_impl,
37-
bool is_one_pc = false)
36+
TxnBatchRollbackTask(const ClientStub& stub, const std::vector<std::string> keys, std::shared_ptr<TxnImpl> txn_impl,
37+
bool& is_one_pc)
3838
: TxnTask(stub), keys_(keys), txn_impl_(txn_impl), is_one_pc_(is_one_pc) {}
3939

4040
~TxnBatchRollbackTask() override = default;
@@ -48,15 +48,15 @@ class TxnBatchRollbackTask : public TxnTask {
4848

4949
void TxnBatchRollbackRpcCallback(const Status& status, TxnBatchRollbackRpc* rpc);
5050

51-
const std::vector<std::string>& keys_;
51+
const std::vector<std::string> keys_;
5252
std::shared_ptr<TxnImpl> txn_impl_;
53-
bool is_one_pc_;
53+
bool& is_one_pc_;
5454

5555
std::vector<StoreRpcController> controllers_;
5656
std::vector<std::unique_ptr<TxnBatchRollbackRpc>> rpcs_;
5757
std::set<std::string_view> next_keys_;
5858
std::atomic<int> sub_tasks_count_{0};
59-
59+
6060
RWLock rw_lock_;
6161
Status status_;
6262
};

test/unit_test/sdk/mock_client_stub.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class MockClientStub final : public ClientStub {
3535
MOCK_METHOD(std::shared_ptr<AdminTool>, GetAdminTool, (), (const, override));
3636
MOCK_METHOD(std::shared_ptr<TxnLockResolver>, GetTxnLockResolver, (), (const, override));
3737
MOCK_METHOD(std::shared_ptr<Actuator>, GetActuator, (), (const, override));
38+
MOCK_METHOD(std::shared_ptr<Actuator>, GetTxnActuator, (), (const, override));
3839
MOCK_METHOD(std::shared_ptr<VectorIndexCache>, GetVectorIndexCache, (), (const, override));
3940
MOCK_METHOD(std::shared_ptr<AutoIncrementerManager>, GetAutoIncrementerManager, (), (const, override));
4041
MOCK_METHOD(std::shared_ptr<TsoProvider>, GetTsoProvider, (), (const, override));

0 commit comments

Comments
 (0)