@@ -127,6 +127,39 @@ static void append_table_stream_commit_size_error(TxnErrorCode err, std::string&
127127 }
128128}
129129
130+ static bool check_txn_commit_tso_fence (Transaction* txn, const std::string& instance_id,
131+ int64_t commit_tso, CommitTxnResponse* response,
132+ MetaServiceCode& code, std::string& msg) {
133+ if (commit_tso <= 0 ) {
134+ return true ;
135+ }
136+
137+ std::string value;
138+ TxnErrorCode err = txn->get (txn_tso_fence_key ({instance_id}), &value);
139+ if (err == TxnErrorCode::TXN_KEY_NOT_FOUND ) {
140+ return true ;
141+ }
142+ if (err != TxnErrorCode::TXN_OK ) {
143+ code = cast_as<ErrCategory::READ >(err);
144+ msg = fmt::format (" failed to read TSO fence, err={}" , err);
145+ return false ;
146+ }
147+
148+ TxnTsoFencePB fence;
149+ if (!fence.ParseFromString (value) || !fence.has_fence_tso () || fence.fence_tso () <= 0 ) {
150+ code = MetaServiceCode::PROTOBUF_PARSE_ERR ;
151+ msg = " failed to parse TSO fence" ;
152+ return false ;
153+ }
154+ if (commit_tso <= fence.fence_tso ()) {
155+ response->set_tso_fence (fence.fence_tso ());
156+ code = MetaServiceCode::TXN_COMMIT_TSO_FENCED ;
157+ msg = fmt::format (" commit TSO {} is fenced by {}" , commit_tso, fence.fence_tso ());
158+ return false ;
159+ }
160+ return true ;
161+ }
162+
130163class TableStreamUpdateTxnContext {
131164public:
132165 TableStreamUpdateTxnContext (Transaction* txn, const std::string& instance_id,
@@ -1911,6 +1944,11 @@ void MetaServiceImpl::commit_txn_immediately(
19111944 return ;
19121945 }
19131946
1947+ if (txn_info.status () != TxnStatusPB::TXN_STATUS_COMMITTED &&
1948+ !check_txn_commit_tso_fence (txn.get (), instance_id, commit_tso, response, code, msg)) {
1949+ return ;
1950+ }
1951+
19141952 MultiVersionStatus table_stream_multi_version_status =
19151953 MultiVersionStatus::MULTI_VERSION_DISABLED ;
19161954 if (!request->table_stream_updates ().empty ()) {
@@ -2743,6 +2781,11 @@ void MetaServiceImpl::commit_txn_eventually(
27432781 return ;
27442782 }
27452783
2784+ if (txn_info.status () != TxnStatusPB::TXN_STATUS_COMMITTED &&
2785+ !check_txn_commit_tso_fence (txn.get (), instance_id, commit_tso, response, code, msg)) {
2786+ return ;
2787+ }
2788+
27462789 auto now_time = system_clock::now ();
27472790 uint64_t commit_time = duration_cast<milliseconds>(now_time.time_since_epoch ()).count ();
27482791 if ((txn_info.prepare_time () + txn_info.timeout_ms ()) < commit_time) {
@@ -3143,6 +3186,11 @@ void MetaServiceImpl::commit_txn_with_sub_txn(const CommitTxnRequest* request,
31433186 return ;
31443187 }
31453188
3189+ if (txn_info.status () != TxnStatusPB::TXN_STATUS_COMMITTED &&
3190+ !check_txn_commit_tso_fence (txn.get (), instance_id, commit_tso, response, code, msg)) {
3191+ return ;
3192+ }
3193+
31463194 LOG (INFO ) << " txn_id=" << txn_id << " txn_info=" << txn_info.ShortDebugString ();
31473195
31483196 AnnotateTag txn_tag (" txn_id" , txn_id);
@@ -4724,12 +4772,76 @@ std::string get_txn_info_key_from_txn_running_key(std::string_view txn_running_k
47244772 return conflict_txn_info_key;
47254773}
47264774
4775+ void MetaServiceImpl::advance_tso_fence (::google::protobuf::RpcController* controller,
4776+ const AdvanceTsoFenceRequest* request,
4777+ AdvanceTsoFenceResponse* response,
4778+ ::google::protobuf::Closure* done) {
4779+ RPC_PREPROCESS (advance_tso_fence, get, put);
4780+ if (!request->has_proposed_fence_tso () || request->proposed_fence_tso () <= 0 ) {
4781+ code = MetaServiceCode::INVALID_ARGUMENT ;
4782+ msg = " invalid proposed TSO fence" ;
4783+ return ;
4784+ }
4785+ instance_id = get_instance_id (resource_mgr_, request->cloud_unique_id ());
4786+ if (instance_id.empty ()) {
4787+ code = MetaServiceCode::INVALID_ARGUMENT ;
4788+ msg = " cannot find instance_id for TSO fence" ;
4789+ return ;
4790+ }
4791+ RPC_RATE_LIMIT (advance_tso_fence)
4792+
4793+ TxnErrorCode err = txn_kv_->create_txn (&txn);
4794+ if (err != TxnErrorCode::TXN_OK ) {
4795+ code = cast_as<ErrCategory::CREATE >(err);
4796+ msg = " failed to create TSO fence transaction" ;
4797+ return ;
4798+ }
4799+
4800+ const std::string key = txn_tso_fence_key ({instance_id});
4801+ std::string value;
4802+ err = txn->get (key, &value);
4803+ int64_t current_fence_tso = 0 ;
4804+ if (err == TxnErrorCode::TXN_OK ) {
4805+ TxnTsoFencePB fence;
4806+ if (!fence.ParseFromString (value) || !fence.has_fence_tso () || fence.fence_tso () <= 0 ) {
4807+ code = MetaServiceCode::PROTOBUF_PARSE_ERR ;
4808+ msg = " failed to parse TSO fence" ;
4809+ return ;
4810+ }
4811+ current_fence_tso = fence.fence_tso ();
4812+ } else if (err != TxnErrorCode::TXN_KEY_NOT_FOUND ) {
4813+ code = cast_as<ErrCategory::READ >(err);
4814+ msg = " failed to read TSO fence" ;
4815+ return ;
4816+ }
4817+
4818+ const int64_t effective_fence_tso = std::max (current_fence_tso, request->proposed_fence_tso ());
4819+ if (effective_fence_tso > current_fence_tso) {
4820+ TxnTsoFencePB fence;
4821+ fence.set_fence_tso (effective_fence_tso);
4822+ if (!fence.SerializeToString (&value)) {
4823+ code = MetaServiceCode::PROTOBUF_SERIALIZE_ERR ;
4824+ msg = " failed to serialize TSO fence" ;
4825+ return ;
4826+ }
4827+ txn->put (key, value);
4828+ err = txn->commit ();
4829+ if (err != TxnErrorCode::TXN_OK ) {
4830+ code = cast_as<ErrCategory::COMMIT >(err);
4831+ msg = " failed to commit TSO fence" ;
4832+ return ;
4833+ }
4834+ }
4835+ response->set_tso_fence (effective_fence_tso);
4836+ }
4837+
47274838void MetaServiceImpl::get_tso_recovery_transactions (
47284839 ::google::protobuf::RpcController* controller,
47294840 const GetTsoRecoveryTransactionsRequest* request,
47304841 GetTsoRecoveryTransactionsResponse* response, ::google::protobuf::Closure* done) {
47314842 RPC_PREPROCESS (get_tso_recovery_transactions, get);
4732- if (request->end_txn_id () <= 0 || request->batch_size () <= 0 || request->batch_size () > 1000 ) {
4843+ if (request->end_txn_id () <= 0 || request->batch_size () <= 0 || request->batch_size () > 1000 ||
4844+ request->tso_fence () <= 0 ) {
47334845 code = MetaServiceCode::INVALID_ARGUMENT ;
47344846 msg = " invalid TSO recovery transaction bound or batch size" ;
47354847 return ;
@@ -4809,15 +4921,17 @@ void MetaServiceImpl::get_tso_recovery_transactions(
48094921 msg = " invalid TSO recovery transaction identity or tables" ;
48104922 return ;
48114923 }
4924+ if (info.status () != TxnStatusPB::TXN_STATUS_COMMITTED || !info.has_commit_tso () ||
4925+ info.commit_tso () <= 0 || info.commit_tso () > request->tso_fence ()) {
4926+ continue ;
4927+ }
48124928 // Recovery needs identities and visibility boundaries, not commit attachments.
48134929 auto * recovered = response->add_txn_infos ();
48144930 recovered->set_db_id (db_id);
48154931 recovered->set_txn_id (txn_id);
48164932 recovered->mutable_table_ids ()->CopyFrom (info.table_ids ());
48174933 recovered->set_status (info.status ());
4818- if (info.has_commit_tso ()) {
4819- recovered->set_commit_tso (info.commit_tso ());
4820- }
4934+ recovered->set_commit_tso (info.commit_tso ());
48214935 }
48224936 begin_key.push_back (' \x00 ' );
48234937 response->set_next_start_key (it->more () ? begin_key : " " );
0 commit comments