Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](cloud) fix error in get detached tablet stat #45449

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cloud/src/meta-service/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions cloud/src/meta-service/meta_service_tablet_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
) {
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions cloud/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
101 changes: 101 additions & 0 deletions cloud/test/meta_service_tablet_stats_test.cpp
Original file line number Diff line number Diff line change
@@ -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"
csun5285 marked this conversation as resolved.
Show resolved Hide resolved

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