gavinchou commented on code in PR #52430:
URL: https://github.com/apache/doris/pull/52430#discussion_r2182642233


##########
cloud/src/common/metric.cpp:
##########
@@ -228,10 +231,121 @@ static void export_fdb_status_details(const std::string& 
status_str) {
     }
 }
 
+// boundaries include the key category{meta, txn, recycle...} instance_id and 
specific key type{rowset, txn_label...}
+// see details: meta-service/keys.h, meta-service/keys.cpp
+// the func count same key to hashmap partition_count
+// exmaple:
+// partition_boundaries: meta|instance1|rowset|..., meta|instance1|rowset|..., 
meta|instance2|rowset|..., txn|instance1|txn_label|...
+// partition_count will output: <meta|instance1|rowset, 2>, 
<meta|instance2|rowset, 1>, <txn|instance1|txn_label, 1>
+void get_partition_boundaries_count(std::vector<std::string>& 
partition_boundaries,
+                                    std::unordered_map<std::string, size_t>& 
partition_count) {
+    size_t prefix_size = FdbTxnKv::fdb_partition_key_prefix().size();
+    for (auto&& boundary : partition_boundaries) {
+        if (boundary.size() < prefix_size + 1 || boundary[prefix_size] != 
CLOUD_USER_KEY_SPACE01) {
+            continue;
+        }
+
+        std::string_view user_key(boundary);
+        user_key.remove_prefix(prefix_size + 1); // Skip the KEY_SPACE prefix.
+        std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> 
out;
+        decode_key(&user_key, &out); // ignore any error, since the boundary 
key might be truncated.
+
+        auto visitor = [](auto&& arg) -> std::string {
+            using T = std::decay_t<decltype(arg)>;
+            if constexpr (std::is_same_v<T, std::string>) {
+                return arg;
+            } else {
+                return std::to_string(arg);
+            }
+        };
+
+        if (!out.empty()) {
+            std::string key;
+            for (size_t i = 0; i < 3 && i < out.size(); ++i) {
+                key += std::visit(visitor, std::get<0>(out[i])) + '|';
+            }
+            key.pop_back();
+            partition_count[key]++;
+        }
+    }
+}
+
+static void export_meta_ranges_details(TxnKv* kv) {
+    auto* txn_kv = dynamic_cast<FdbTxnKv*>(kv);
+    if (!txn_kv) {
+        LOG(WARNING) << "this method only support fdb txn kv";
+        return;
+    }
+
+    std::vector<std::string> partition_boundaries;
+    TxnErrorCode code = 
txn_kv->get_partition_boundaries(&partition_boundaries);
+    if (code != TxnErrorCode::TXN_OK) {
+        auto msg = fmt::format("failed to get boundaries, code={}", code);
+        return;
+    }
+
+    std::unordered_map<std::string, size_t> partition_count;
+    get_partition_boundaries_count(partition_boundaries, partition_count);
+
+    std::unordered_map<std::string, int64_t> category_count;
+    for (auto&& [key, count] : partition_count) {
+        std::vector<std::string> keys;
+        size_t pos {};
+        // split key with '|'
+        do {
+            size_t p = std::min(key.size(), key.find('|', pos));
+            keys.emplace_back(key.substr(pos, p - pos));
+            pos = p + 1;
+        } while (pos < key.size());
+        keys.resize(3);
+        if (keys[0] == TXN_KEY_PREFIX) {

Review Comment:
   using a map instead of a bunch of `if elese`, try this
   
   ```
   for (auto& i : get_key_prefixes_contants()) {
       if (partition_count.find(i) != partition_count.end()) category_count[i] 
+= partition_count[i];
       // for bvar, use label instead of declare statically
       g_meta_ranges {"type"= i ...}
   }
   ```
   



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