This is an automated email from the ASF dual-hosted git repository.

airborne pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 66c74459ebc [fix](cloud) fix error in get detached tablet stat (#45449)
66c74459ebc is described below

commit 66c74459ebce77695561b33f1bda56fc58c889c5
Author: Sun Chenyang <suncheny...@selectdb.com>
AuthorDate: Tue Dec 17 11:05:01 2024 +0800

    [fix](cloud) fix error in get detached tablet stat (#45449)
    
    ### What problem does this PR solve?
    
    Related PR: #44120
    
    Problem Summary:
    
    - `detached tablet stats` should maintain a fixed size.
---
 cloud/src/meta-service/keys.h                      |   6 ++
 .../src/meta-service/meta_service_tablet_stats.cpp |  10 +-
 cloud/test/CMakeLists.txt                          |   1 +
 cloud/test/meta_service_tablet_stats_test.cpp      | 101 +++++++++++++++++++++
 4 files changed, 115 insertions(+), 3 deletions(-)

diff --git a/cloud/src/meta-service/keys.h b/cloud/src/meta-service/keys.h
index 855171c1dc6..c2c9a9dd4da 100644
--- a/cloud/src/meta-service/keys.h
+++ b/cloud/src/meta-service/keys.h
@@ -254,6 +254,12 @@ void stats_tablet_num_segs_key(const StatsTabletKeyInfo& 
in, std::string* out);
 void stats_tablet_index_size_key(const StatsTabletKeyInfo& in, std::string* 
out);
 void stats_tablet_segment_size_key(const StatsTabletKeyInfo& in, std::string* 
out);
 static inline std::string stats_tablet_key(const StatsTabletKeyInfo& in) { 
std::string s; stats_tablet_key(in, &s); return s; }
+static inline std::string stats_tablet_data_size_key(const StatsTabletKeyInfo& 
in) { std::string s; stats_tablet_data_size_key(in, &s); return s; }
+static inline std::string stats_tablet_num_rows_key(const StatsTabletKeyInfo& 
in) { std::string s; stats_tablet_num_rows_key(in, &s); return s; }
+static inline std::string stats_tablet_num_rowsets_key(const 
StatsTabletKeyInfo& in) { std::string s; stats_tablet_num_rowsets_key(in, &s); 
return s; }
+static inline std::string stats_tablet_num_segs_key(const StatsTabletKeyInfo& 
in) { std::string s; stats_tablet_num_segs_key(in, &s); return s; }
+static inline std::string stats_tablet_index_size_key(const 
StatsTabletKeyInfo& in) { std::string s; stats_tablet_index_size_key(in, &s); 
return s; }
+static inline std::string stats_tablet_segment_size_key(const 
StatsTabletKeyInfo& in) { std::string s; stats_tablet_segment_size_key(in, &s); 
return s; }
 
 void job_recycle_key(const JobRecycleKeyInfo& in, std::string* out);
 void job_check_key(const JobRecycleKeyInfo& in, std::string* out);
diff --git a/cloud/src/meta-service/meta_service_tablet_stats.cpp 
b/cloud/src/meta-service/meta_service_tablet_stats.cpp
index 4cbf629c9a3..da556b6c574 100644
--- a/cloud/src/meta-service/meta_service_tablet_stats.cpp
+++ b/cloud/src/meta-service/meta_service_tablet_stats.cpp
@@ -94,7 +94,8 @@ int get_detached_tablet_stats(const 
std::vector<std::pair<std::string, std::stri
                               TabletStats& detached_stats) {
     bool unexpected_size = false;
     // clang-format off
-    if (stats_kvs.size() != 7    // aggregated stats and 4 splitted stats: 
num_rowsets num_segs data_size num_rows index_size segment_size
+    if (stats_kvs.size() != 7    // aggregated stats and 6 splitted stats: 
num_rowsets num_segs data_size num_rows index_size segment_size
+        && stats_kvs.size() != 5 // aggregated stats and 4 splitted stats: 
num_rowsets num_segs data_size num_rows
         && stats_kvs.size() != 2 // aggregated stats and 1 splitted stats: 
num_rowsets
         && stats_kvs.size() != 1 // aggregated stats only (nothing has been 
imported since created)
         ) {
@@ -148,8 +149,11 @@ int get_detached_tablet_stats(const 
std::vector<std::pair<std::string, std::stri
     }
 
     if (unexpected_size) {
-        LOG_EVERY_N(WARNING, 100) << "unexpected tablet stats_kvs, it should 
be 1 or 2 or 5, size="
-                                  << stats_kvs.size() << " suffix=" << 
ss.str();
+        DCHECK(false) << "unexpected tablet stats_kvs, it should be 1 or 2 or 
5 or 7, size="
+                      << stats_kvs.size() << " suffix=" << ss.str();
+        LOG_EVERY_N(WARNING, 100)
+                << "unexpected tablet stats_kvs, it should be 1 or 2 or 5 or 
7, size="
+                << stats_kvs.size() << " suffix=" << ss.str();
     }
 
     return 0;
diff --git a/cloud/test/CMakeLists.txt b/cloud/test/CMakeLists.txt
index c529b0b1c9e..51affb8a46f 100644
--- a/cloud/test/CMakeLists.txt
+++ b/cloud/test/CMakeLists.txt
@@ -25,6 +25,7 @@ add_executable(meta_service_test
    meta_service_test.cpp
    meta_service_job_test.cpp
    meta_service_http_test.cpp
+   meta_service_tablet_stats_test.cpp
    schema_kv_test.cpp
 )
 
diff --git a/cloud/test/meta_service_tablet_stats_test.cpp 
b/cloud/test/meta_service_tablet_stats_test.cpp
new file mode 100644
index 00000000000..000d33602e5
--- /dev/null
+++ b/cloud/test/meta_service_tablet_stats_test.cpp
@@ -0,0 +1,101 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "meta-service/meta_service_tablet_stats.h"
+
+#include <gen_cpp/cloud.pb.h>
+#include <gtest/gtest.h>
+
+#include "meta-service/codec.h"
+#include "meta-service/keys.h"
+
+namespace doris::cloud {
+
+std::string size_value(int64_t tablet_stat_data_size) {
+    std::string tablet_stat_data_size_value(sizeof(tablet_stat_data_size), 
'\0');
+    memcpy(tablet_stat_data_size_value.data(), &tablet_stat_data_size,
+           sizeof(tablet_stat_data_size));
+    return tablet_stat_data_size_value;
+}
+
+TEST(MetaServiceTabletStatsTest, test_get_detached_tablet_stats) {
+    std::vector<std::pair<std::string, std::string>> stats_kvs;
+    StatsTabletKeyInfo tablet_key_info {"instance_0", 10000, 10001, 10002, 
10003};
+
+    // key->TabletStatsPB
+    TabletStatsPB tablet_stat;
+    std::string tablet_stat_value;
+    auto tablet_stat_key = stats_tablet_key(tablet_key_info);
+    tablet_stat.SerializeToString(&tablet_stat_value);
+    stats_kvs.emplace_back(tablet_stat_key, tablet_stat_value);
+
+    // key->data_size
+    auto tablet_stat_data_size_key = 
stats_tablet_data_size_key(tablet_key_info);
+    stats_kvs.emplace_back(tablet_stat_data_size_key, size_value(100));
+
+    // key->num_rows
+    auto tablet_stat_num_rows_key = stats_tablet_num_rows_key(tablet_key_info);
+    stats_kvs.emplace_back(tablet_stat_num_rows_key, size_value(10));
+
+    // key->num_rowsets
+    auto tablet_stat_num_rowsets_key = 
stats_tablet_num_rowsets_key(tablet_key_info);
+    stats_kvs.emplace_back(tablet_stat_num_rowsets_key, size_value(1));
+
+    // key->num_segs
+    auto tablet_stat_num_segs_key = stats_tablet_num_segs_key(tablet_key_info);
+    stats_kvs.emplace_back(tablet_stat_num_segs_key, size_value(1));
+
+    // key->index_size
+    auto tablet_stat_index_size_key = 
stats_tablet_index_size_key(tablet_key_info);
+    stats_kvs.emplace_back(tablet_stat_index_size_key, size_value(50));
+
+    // key->segment_size
+    auto tablet_stat_segment_size_key = 
stats_tablet_segment_size_key(tablet_key_info);
+    stats_kvs.emplace_back(tablet_stat_segment_size_key, size_value(50));
+
+    TabletStats res1;
+    int ret = get_detached_tablet_stats(stats_kvs, res1);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(res1.data_size, 100);
+    EXPECT_EQ(res1.num_rows, 10);
+    EXPECT_EQ(res1.num_rowsets, 1);
+    EXPECT_EQ(res1.num_segs, 1);
+    EXPECT_EQ(res1.index_size, 50);
+    EXPECT_EQ(res1.segment_size, 50);
+
+    stats_kvs.resize(5);
+    TabletStats res2;
+    ret = get_detached_tablet_stats(stats_kvs, res2);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(res1.data_size, 100);
+    EXPECT_EQ(res1.num_rows, 10);
+    EXPECT_EQ(res1.num_rowsets, 1);
+    EXPECT_EQ(res1.num_segs, 1);
+
+    stats_kvs.resize(2);
+    TabletStats res3;
+    ret = get_detached_tablet_stats(stats_kvs, res3);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(res1.data_size, 100);
+
+    stats_kvs.resize(1);
+    TabletStats res4;
+    ret = get_detached_tablet_stats(stats_kvs, res4);
+    EXPECT_EQ(ret, 0);
+}
+
+} // namespace doris::cloud


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

Reply via email to