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


##########
cloud/src/resource-manager/resource_manager.cpp:
##########
@@ -1501,6 +1558,120 @@ void ResourceManager::refresh_instance(const 
std::string& instance_id,
     }
 }
 
+std::pair<MetaServiceCode, std::string> 
ResourceManager::evict_deleted_instance(
+        const std::string& instance_id) {
+    std::pair<MetaServiceCode, std::string> ret0 {MetaServiceCode::OK, "OK"};
+    auto& [code, msg] = ret0;
+
+    std::unique_ptr<Transaction> txn0;
+    TxnErrorCode err = txn_kv_->create_txn(&txn0);
+    if (err != TxnErrorCode::TXN_OK) {
+        code = MetaServiceCode::KV_TXN_CREATE_ERR;
+        msg = "failed to create txn";
+        LOG(WARNING) << msg << " instance_id=" << instance_id << " err=" << 
err;
+        return ret0;
+    }
+    std::shared_ptr<Transaction> txn(txn0.release());
+    InstanceInfoPB instance;
+    auto [c0, m0] = get_instance(txn, instance_id, &instance);
+    if (c0 == TxnErrorCode::TXN_OK) {
+        // Instance still exists (e.g. created concurrently with a range 
scan); do not evict.
+        return ret0;
+    }
+    if (c0 != TxnErrorCode::TXN_KEY_NOT_FOUND) {
+        code = cast_as<ErrCategory::READ>(c0);
+        msg = m0;
+        return ret0;
+    }
+
+    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 evict_deleted_instance";
+        LOG(WARNING) << msg << " instance_id=" << instance_id << " err=" << err
+                     << " read_version=" << read_version;
+        return ret0;
+    }
+
+    evict_deleted_instance_with_version(instance_id, read_version);
+    return ret0;
+}
+
+ResourceManager::PublishGuard ResourceManager::acquire_publish_guard(
+        const std::string& instance_id) {
+    std::lock_guard l(mtx_);
+    ++instance_cache_version_[instance_id].in_flight;
+    return PublishGuard(this, instance_id);
+}
+
+void ResourceManager::release_publish_guard(const std::string& instance_id) {
+    std::lock_guard l(mtx_);
+    auto ver_it = instance_cache_version_.find(instance_id);
+    CHECK(ver_it != instance_cache_version_.end());
+    CHECK_GT(ver_it->second.in_flight, 0);
+    --ver_it->second.in_flight;
+    if (ver_it->second.in_flight == 0 &&
+        (ver_it->second.deleted || ver_it->second.version < 0)) {
+        instance_cache_version_.erase(ver_it);
+    }
+}
+
+void ResourceManager::evict_deleted_instance_with_version(const std::string& 
instance_id,
+                                                          int64_t 
read_version) {
+    std::lock_guard l(mtx_);
+    auto ver_it = instance_cache_version_.find(instance_id);
+    if (ver_it != instance_cache_version_.end() && ver_it->second.version >= 0 
&&
+        read_version < ver_it->second.version) {
+        LOG(INFO) << "skip stale evict_deleted_instance for instance_id=" << 
instance_id
+                  << " read_version=" << read_version
+                  << " current_version=" << ver_it->second.version;
+        return;
+    }
+
+    for (auto i = node_info_.begin(); i != node_info_.end();) {
+        if (i->second.instance_id == instance_id) {
+            i = node_info_.erase(i);
+        } else {
+            ++i;
+        }
+    }
+    instance_multi_version_status_.erase(instance_id);
+    instance_source_snapshot_info_.erase(instance_id);
+
+    if (ver_it != instance_cache_version_.end()) {
+        if (ver_it->second.in_flight > 0) {
+            ver_it->second.version = std::max(ver_it->second.version, 
read_version);
+            ver_it->second.deleted = true;
+            LOG(INFO) << "evicted deleted instance with in-flight publishers, 
instance_id="
+                      << instance_id << " read_version=" << 
ver_it->second.version
+                      << " in_flight=" << ver_it->second.in_flight;
+        } else {
+            instance_cache_version_.erase(ver_it);
+            LOG(INFO) << "evicted deleted instance and reclaimed watermark, 
instance_id="
+                      << instance_id << " read_version=" << read_version;
+        }
+    }
+}
+
+std::vector<std::string> ResourceManager::get_active_version_instance_ids() 
const {
+    std::shared_lock l(mtx_);
+    std::vector<std::string> ids;
+    ids.reserve(instance_cache_version_.size());
+    for (const auto& [id, ver] : instance_cache_version_) {
+        if (ver.version >= 0 && !ver.deleted) {

Review Comment:
   [P2] Include pending publications in deletion discovery
   
   A refresh acquires its guard before reading KV, so an instance’s first 
publication can be in flight with `version == -1`. For example, a REFRESH can 
read a cleanup-complete tombstone before the first watcher scan and pause 
before publishing. If the recycler removes the key and the watcher scans during 
that pause, neither scan map contains the ID and this filter excludes it. The 
watcher consumes the deletion event, then the delayed REFRESH republishes the 
removed instance’s multi-version status and leaves a live watermark until 
another update triggers a scan. Include every in-flight ID and IDs seeded by 
`init()` in the deletion candidates, and cover deletion before the first 
publication in a watcher test.



-- 
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