platoneko commented on code in PR #30128:
URL: https://github.com/apache/doris/pull/30128#discussion_r1458431344


##########
be/src/olap/base_tablet.cpp:
##########
@@ -119,4 +118,149 @@ bool 
BaseTablet::_reconstruct_version_tracker_if_necessary() {
     return false;
 }
 
-} /* namespace doris */
+// snapshot manager may call this api to check if version exists, so that
+// the version maybe not exist
+RowsetSharedPtr BaseTablet::get_rowset_by_version(const Version& version,
+                                                  bool find_in_stale) const {
+    auto iter = _rs_version_map.find(version);
+    if (iter == _rs_version_map.end()) {
+        if (find_in_stale) {
+            return get_stale_rowset_by_version(version);
+        }
+        return nullptr;
+    }
+    return iter->second;
+}
+
+RowsetSharedPtr BaseTablet::get_stale_rowset_by_version(const Version& 
version) const {
+    auto iter = _stale_rs_version_map.find(version);
+    if (iter == _stale_rs_version_map.end()) {
+        VLOG_NOTICE << "no rowset for version:" << version << ", tablet: " << 
tablet_id();
+        return nullptr;
+    }
+    return iter->second;
+}
+
+// Already under _meta_lock
+RowsetSharedPtr BaseTablet::get_rowset_with_max_version() const {
+    Version max_version = _tablet_meta->max_version();
+    if (max_version.first == -1) {
+        return nullptr;
+    }
+
+    auto iter = _rs_version_map.find(max_version);
+    if (iter == _rs_version_map.end()) {
+        DCHECK(false) << "invalid version:" << max_version;
+        return nullptr;
+    }
+    return iter->second;
+}
+
+Status BaseTablet::all_rs_id(int64_t max_version, RowsetIdUnorderedSet* 
rowset_ids) const {
+    //  Ensure that the obtained versions of rowsets are continuous
+    std::vector<Version> version_path;
+    RETURN_IF_ERROR(capture_consistent_versions_unlocked(Version(0, 
max_version), &version_path,
+                                                         false, false));
+    for (auto& ver : version_path) {
+        if (ver.second == 1) {
+            // [0-1] rowset is empty for each tablet, skip it
+            continue;
+        }
+        auto it = _rs_version_map.find(ver);
+        if (it == _rs_version_map.end()) {
+            return Status::Error<CAPTURE_ROWSET_ERROR, false>(
+                    "fail to find Rowset for version. tablet={}, version={}", 
tablet_id(),
+                    ver.to_string());
+        }
+        rowset_ids->emplace(it->second->rowset_id());
+    }
+    return Status::OK();
+}
+
+Status BaseTablet::capture_consistent_versions_unlocked(const Version& 
spec_version,
+                                                        std::vector<Version>* 
version_path,
+                                                        bool 
skip_missing_version,
+                                                        bool quiet) const {
+    Status status =
+            
_timestamped_version_tracker.capture_consistent_versions(spec_version, 
version_path);
+    if (!status.ok() && !quiet) {
+        std::vector<Version> missed_versions;
+        calc_missed_versions_unlocked(spec_version.second, &missed_versions);
+        if (missed_versions.empty()) {
+            // if version_path is null, it may be a compaction check logic.
+            // so to avoid print too many logs.
+            if (version_path != nullptr) {
+                LOG(WARNING) << "tablet:" << tablet_id()
+                             << ", version already has been merged. 
spec_version: " << spec_version
+                             << ", max_version: " << max_version_unlocked();
+            }
+            status = Status::Error<VERSION_ALREADY_MERGED>(
+                    "missed_versions is empty, spec_version "
+                    "{}, max_version {}, tablet_id {}",
+                    spec_version.second, max_version_unlocked().second, 
tablet_id());
+        } else {
+            if (version_path != nullptr) {
+                LOG(WARNING) << "status:" << status << ", tablet:" << 
tablet_id()
+                             << ", missed version for version:" << 
spec_version;
+                _print_missed_versions(missed_versions);
+                if (skip_missing_version) {
+                    LOG(WARNING) << "force skipping missing version for 
tablet:" << tablet_id();
+                    return Status::OK();
+                }
+            }
+        }
+    }
+    return status;
+}
+
+void BaseTablet::_print_missed_versions(const std::vector<Version>& 
missed_versions) const {
+    std::stringstream ss;
+    ss << tablet_id() << " has " << missed_versions.size() << " missed 
version:";
+    // print at most 10 version
+    for (int i = 0; i < 10 && i < missed_versions.size(); ++i) {
+        ss << missed_versions[i] << ",";
+    }
+    LOG(WARNING) << ss.str();
+}
+
+void BaseTablet::calc_missed_versions(int64_t spec_version, 
std::vector<Version>* missed_versions) {
+    std::shared_lock rdlock(_meta_lock);
+    calc_missed_versions_unlocked(spec_version, missed_versions);
+}
+
+// for example:
+//     [0-4][5-5][8-8][9-9]
+// if spec_version = 6, we still return {7} other than {6, 7}
+void BaseTablet::calc_missed_versions_unlocked(int64_t spec_version,

Review Comment:
   `CloudTablet` 好像已经有同名不同实现类似语义的函数



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to