@@ -433,7 +433,7 @@ Status TxnImpl::PreWriteAndCommit() {
433433}
434434
435435void 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
697712Status 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
711748Status 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}
0 commit comments