1717#include < fmt/format.h>
1818#include < glog/logging.h>
1919
20+ #include < cstdint>
2021#include < utility>
2122
2223#include " common/logging.h"
2324#include " dingosdk/status.h"
2425#include " sdk/common/common.h"
26+ #include " sdk/common/helper.h"
2527#include " sdk/rpc/store_rpc_controller.h"
2628#include " sdk/transaction/txn_common.h"
2729#include " sdk/utils/rw_lock.h"
@@ -55,7 +57,6 @@ void TxnCommitTask::DoAsync() {
5557 {
5658 WriteLockGuard guard (rw_lock_);
5759 next_batch = next_keys_;
58- need_retry_ = false ;
5960 status_ = Status::OK ();
6061 }
6162
@@ -136,37 +137,27 @@ void TxnCommitTask::TxnCommitRpcCallback(const Status& status, TxnCommitRpc* rpc
136137 DINGO_LOG (DEBUG ) << fmt::format (" [sdk.txn.{}] rpc: {} request: {} response: {}" , txn_impl_->ID (), rpc->Method (),
137138 rpc->Request ()->ShortDebugString (), rpc->Response ()->ShortDebugString ());
138139 Status s;
139- bool need_retry = false ;
140140 const auto * response = rpc->Response ();
141141 if (!status.ok ()) {
142142 DINGO_LOG (WARNING ) << fmt::format (" [sdk.txn.{}] rpc: {} send to region: {} fail: {}" , txn_impl_->ID (),
143143 rpc->Method (), rpc->Request ()->context ().region_id (), status.ToString ());
144144
145145 s = status;
146146 } else {
147- s = txn_impl_-> ProcessTxnCommitResponse (response, is_primary_);
147+ s = ProcessTxnCommitResponse (response, is_primary_);
148148 if (!s.ok ()) {
149149 DINGO_LOG (WARNING ) << fmt::format (" [sdk.txn.{}] commit fail, region({}) response({}) status({})." ,
150150 txn_impl_->ID (), rpc->Request ()->context ().region_id (),
151151 response->ShortDebugString (), s.ToString ());
152- if (s.IsTxnCommitTsExpired () && is_primary_) {
153- need_retry = true ;
154- s = Status::OK ();
155- }
156152 }
157153 }
158154
159155 {
160156 WriteLockGuard guard (rw_lock_);
161157 if (s.ok ()) {
162- if (!need_retry) {
163- for (const auto & key : rpc->Request ()->keys ()) {
164- next_keys_.erase (key);
165- }
166- } else {
167- need_retry_ = true ;
158+ for (const auto & key : rpc->Request ()->keys ()) {
159+ next_keys_.erase (key);
168160 }
169-
170161 } else {
171162 if (status_.ok ()) {
172163 // only return first fail status
@@ -181,16 +172,52 @@ void TxnCommitTask::TxnCommitRpcCallback(const Status& status, TxnCommitRpc* rpc
181172 {
182173 ReadLockGuard guard (rw_lock_);
183174 tmp = status_;
184- tmp_need_retry = need_retry_;
185- }
186- if (tmp.ok () && tmp_need_retry) {
187- DoAsyncRetry ();
188- return ;
189175 }
190176 DoAsyncDone (tmp);
191177 }
192178}
193179
180+ Status TxnCommitTask::ProcessTxnCommitResponse (const TxnCommitResponse* response, bool is_primary) {
181+ std::string pk = txn_impl_->GetPrimaryKey ();
182+ int64_t txn_id = txn_impl_->ID ();
183+ DINGO_LOG (DEBUG ) << fmt::format (" [sdk.txn.{}] commit response, pk({}) response({})." , txn_id, pk,
184+ response->ShortDebugString ());
185+
186+ if (!response->has_txn_result ()) {
187+ return Status::OK ();
188+ }
189+
190+ const auto & txn_result = response->txn_result ();
191+ if (txn_result.has_locked ()) {
192+ const auto & lock_info = txn_result.locked ();
193+ DINGO_LOG (FATAL ) << fmt::format (" [sdk.txn.{}] commit lock conflict, is_primary({}) pk({}) response({})." , txn_id,
194+ is_primary, StringToHex (pk), response->ShortDebugString ());
195+
196+ } else if (txn_result.has_txn_not_found ()) {
197+ DINGO_LOG (FATAL ) << fmt::format (" [sdk.txn.{}] commit not found, is_primary({}) pk({}) response({})." , txn_id,
198+ is_primary, StringToHex (pk), response->ShortDebugString ());
199+
200+ } else if (txn_result.has_write_conflict ()) {
201+ if (!is_primary) {
202+ DINGO_LOG (FATAL ) << fmt::format (" [sdk.txn.{}] commit write conlict, pk({}) response({})." , txn_id,
203+ StringToHex (pk), txn_result.write_conflict ().ShortDebugString ());
204+ }
205+ return Status::TxnWriteConflict (" txn write conflict" );
206+
207+ } else if (txn_result.has_commit_ts_expired ()) {
208+ DINGO_LOG (WARNING ) << fmt::format (" [sdk.txn.{}] commit ts expired, is_primary({}) pk({}) response({})." , txn_id,
209+ is_primary, StringToHex (pk), txn_result.commit_ts_expired ().ShortDebugString ());
210+ if (is_primary) {
211+ return Status::TxnCommitTsExpired (" txn commit ts expired" );
212+ }
213+ } else {
214+ DINGO_LOG (FATAL ) << fmt::format (" [sdk.txn.{}] commit unknown txn result, is_primary({}) pk({}) response({})." ,
215+ txn_id, is_primary, StringToHex (pk), response->ShortDebugString ());
216+ }
217+
218+ return Status::OK ();
219+ }
220+
194221} // namespace sdk
195222
196223} // namespace dingodb
0 commit comments