hello-stephen commented on code in PR #71405:
URL: https://github.com/apache/doris/pull/71405#discussion_r4794547058


##########
cloud/src/resource-manager/resource_manager.cpp:
##########
@@ -1442,19 +1443,63 @@ std::pair<MetaServiceCode, std::string> 
ResourceManager::refresh_instance(
         return ret0;
     }
 
-    refresh_instance(instance_id, instance);
-    LOG(INFO) << "finish refreshing instance, instance_id=" << instance_id << 
" seq=" << seq;
+    int64_t read_version = -1;
+    err = txn->get_read_version(&read_version);
+    if (err != TxnErrorCode::TXN_OK || read_version < 0) {
+        code = err != TxnErrorCode::TXN_OK ? cast_as<ErrCategory::READ>(err)
+                                           : MetaServiceCode::KV_TXN_GET_ERR;
+        msg = "failed to get read version during refresh_instance";
+        LOG(WARNING) << msg << " instance_id=" << instance_id << " err=" << err
+                     << " read_version=" << read_version;
+        return ret0;
+    }
+
+    refresh_instance_with_version(instance_id, instance, read_version);
+    LOG(INFO) << "finish refreshing instance, instance_id=" << instance_id << 
" seq=" << seq
+              << " read_version=" << read_version;
 
     return ret0;
 }
 
+void ResourceManager::publish_committed_instance(const std::string& 
instance_id,
+                                                 const InstanceInfoPB& 
instance, Transaction* txn) {
+    int64_t committed_version = -1;
+    if (txn != nullptr &&
+        txn->get_committed_version(&committed_version) == TxnErrorCode::TXN_OK 
&&
+        committed_version >= 0) {
+        refresh_instance_with_version(instance_id, instance, 
committed_version);
+        return;
+    }
+    auto [code, msg] = refresh_instance(instance_id);
+    if (code != MetaServiceCode::OK) {
+        LOG(WARNING) << "failed to refresh_instance after commit without 
version, instance_id="
+                     << instance_id << " code=" << code << " msg=" << msg;
+    }
+}
+
 void ResourceManager::refresh_instance(const std::string& instance_id,
                                        const InstanceInfoPB& instance) {
+    refresh_instance_with_version(instance_id, instance, 
/*snapshot_version=*/-1);
+}
+
+void ResourceManager::refresh_instance_with_version(const std::string& 
instance_id,
+                                                    const InstanceInfoPB& 
instance,
+                                                    int64_t snapshot_version) {
     bool is_successor_instance = instance.has_original_instance_id();
     std::string predecessor_instance_id =
             is_successor_instance ? instance.predecessor_instance_id() : "";
 
     std::lock_guard l(mtx_);
+    if (snapshot_version >= 0) {
+        auto ver_it = instance_cache_version_.find(instance_id);
+        if (ver_it != instance_cache_version_.end() && snapshot_version < 
ver_it->second) {
+            LOG(INFO) << "skip stale refresh_instance for instance_id=" << 
instance_id
+                      << " snapshot_version=" << snapshot_version
+                      << " current_version=" << ver_it->second;
+            return;
+        }
+        instance_cache_version_[instance_id] = snapshot_version;

Review Comment:
   [P3] Reclaim watermarks after physical instance deletion
   
   With `retain_deleted_instance_tombstone=false`, the recycler removes the 
instance key and the watcher refreshes only IDs still present in its scan. This 
entry survives even for an instance whose last cluster was dropped, so a 
long-lived MetaService accumulates one watermark for each distinct instance ID 
it has refreshed. Reclaim these entries after final deletion while preserving 
the guard against older publications already in flight.



##########
cloud/test/resource_test.cpp:
##########
@@ -1373,4 +1380,102 @@ TEST(ResourceTest, RollbackInstance) {
     sp->clear_all_call_backs();
 }
 
+TEST(ResourceManagerTest, OutOfOrderCacheRefreshDoesNotRegress) {
+    auto txn_kv = std::make_shared<MemTxnKv>();
+    ASSERT_EQ(txn_kv->init(), 0);
+    auto resource_mgr = std::make_shared<ResourceManager>(txn_kv);
+    ASSERT_EQ(resource_mgr->init(), 0);
+
+    const std::string inst_id = "inst_ver_guard";
+    const std::string cluster_id = "cluster_ver_guard";
+    const std::string cluster_name = "cluster_ver_guard_name";
+
+    auto make_node_pb = [](const std::string& uid, const std::string& ip, 
int32_t port) {
+        NodeInfoPB node;
+        node.set_cloud_unique_id(uid);
+        node.set_ip(ip);
+        node.set_host(ip);
+        node.set_heartbeat_port(port);
+        return node;
+    };
+
+    InstanceInfoPB initial_inst;
+    initial_inst.set_instance_id(inst_id);
+    initial_inst.set_status(InstanceInfoPB::NORMAL);
+    {
+        auto* cluster = initial_inst.add_clusters();
+        cluster->set_cluster_id(cluster_id);
+        cluster->set_cluster_name(cluster_name);
+        cluster->set_type(ClusterPB::COMPUTE);
+        *cluster->add_nodes() = make_node_pb("opaque_node_0", "10.0.0.1", 
9050);
+    }
+    {
+        std::unique_ptr<Transaction> txn;
+        ASSERT_EQ(txn_kv->create_txn(&txn), TxnErrorCode::TXN_OK);
+        std::string key;
+        instance_key({inst_id}, &key);
+        txn->put(key, initial_inst.SerializeAsString());
+        ASSERT_EQ(txn->commit(), TxnErrorCode::TXN_OK);
+        resource_mgr->publish_committed_instance(inst_id, initial_inst, 
txn.get());
+    }
+
+    auto* sp = SyncPoint::get_instance();
+    sp->clear_all_call_backs();
+    sp->enable_processing();
+    DORIS_CLOUD_DEFER {
+        sp->clear_all_call_backs();
+        sp->disable_processing();
+    };
+
+    // Pause T1 (adding opaque_node_1) strictly after FDB commit and before 
cache publish.
+    std::atomic<bool> t1_paused {false};
+    std::atomic<bool> release_t1 {false};
+    
sp->set_call_back("ResourceManager::modify_nodes:after_commit_before_publish", 
[&](auto&&) {
+        if (!t1_paused.exchange(true)) {
+            while (!release_t1.load()) {
+                std::this_thread::yield();
+            }
+        }
+    });
+
+    std::string t1_err;
+    std::thread t1([&]() {
+        NodeInfo n1 {Role::COMPUTE_NODE, inst_id, cluster_name, cluster_id,
+                     make_node_pb("opaque_node_1", "10.0.0.2", 9050)};
+        t1_err = resource_mgr->modify_nodes(inst_id, {n1}, {});
+    });
+
+    while (!t1_paused.load()) {
+        std::this_thread::yield();
+    }
+
+    // While T1 is paused holding PB1 ({node_0, node_1}), T2 commits and 
publishes PB2
+    // ({node_0, node_1, node_2}), and watcher also refreshes PB2 from KV.
+    NodeInfo n2 {Role::COMPUTE_NODE, inst_id, cluster_name, cluster_id,
+                 make_node_pb("opaque_node_2", "10.0.0.3", 9050)};
+    ASSERT_EQ(resource_mgr->modify_nodes(inst_id, {n2}, {}), "");

Review Comment:
   [P2] Release and join the worker on test failures
   
   If T1 returns before the post-commit hook, the test thread spins 
indefinitely waiting for `t1_paused`. Once T1 pauses, a failure in either of 
these fatal assertions exits the test with `t1` still joinable, so its 
destructor calls `std::terminate` and aborts `resource_test`. Wait for the 
pause or worker completion with a deadline, and guarantee that cleanup releases 
and joins T1 on every exit path.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to