Skip to content

Commit 29871e4

Browse files
authored
(selectdb-cloud) Pick split tablet stats from apache#1585 apache#1635 (apache#1656)
Author: platoneko <[email protected]> Date: Wed Apr 12 12:24:15 2023 +0800 Use snapshot read to get tablet stats Author: plat1ko <[email protected]> Date: Tue Apr 11 16:02:51 2023 +0800 [selectdb-cloud] Fix incorrect tablet stats in finish tablet job (apache#1635) Author: plat1ko <[email protected]> Date: Fri Apr 7 23:13:06 2023 +0800 [feature](selectdb-cloud) Split tablet stats kv to reduce transaction conflicts (apache#1585) * Split tablet stats to reduce transaction conflicts * Fix mem txn kv and add ut * Add ut for atomic
1 parent 3014521 commit 29871e4

16 files changed

Lines changed: 1262 additions & 757 deletions

‎cloud/src/common/config.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ CONF_String(arn_ak, "");
9797
CONF_String(arn_sk, "");
9898
CONF_Int64(internal_stage_objects_expire_time_second, "259200"); // 3 * 24 * 3600 seconds
9999

100-
CONF_mBool(write_schema_kv, "false"); // temporary config for upgrade
100+
CONF_mBool(write_schema_kv, "false"); // temporary config for upgrade
101+
CONF_mBool(split_tablet_stats, "false"); // temporary config for upgrade
101102

102103
} // namespace selectdb::config

‎cloud/src/meta-service/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_library(MetaService
1212
meta_server.cpp
1313
meta_service.cpp
1414
meta_service_job.cpp
15+
meta_service_tablet_stats.cpp
1516
txn_kv.cpp
1617
codec.cpp
1718
keys.cpp

‎cloud/src/meta-service/keys.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,27 @@ void stats_tablet_key(const StatsTabletKeyInfo& in, std::string* out) {
272272
encode_int64(std::get<4>(in), out); // tablet_id
273273
}
274274

275+
void stats_tablet_data_size_key(const StatsTabletKeyInfo& in, std::string* out) {
276+
stats_tablet_key(in, out);
277+
encode_bytes(STATS_KEY_SUFFIX_DATA_SIZE, out);
278+
}
279+
void stats_tablet_num_rows_key(const StatsTabletKeyInfo& in, std::string* out) {
280+
stats_tablet_key(in, out);
281+
encode_bytes(STATS_KEY_SUFFIX_NUM_ROWS, out);
282+
}
283+
void stats_tablet_num_rowsets_key(const StatsTabletKeyInfo& in, std::string* out) {
284+
stats_tablet_key(in, out);
285+
encode_bytes(STATS_KEY_SUFFIX_NUM_ROWSETS, out);
286+
}
287+
void stats_tablet_num_segs_key(const StatsTabletKeyInfo& in, std::string* out) {
288+
stats_tablet_key(in, out);
289+
encode_bytes(STATS_KEY_SUFFIX_NUM_SEGS, out);
290+
}
291+
292+
//==============================================================================
293+
// Job keys
294+
//==============================================================================
295+
275296
void job_tablet_key(const JobTabletKeyInfo& in, std::string* out) {
276297
encode_prefix(in, out); // 0x01 "job" ${instance_id}
277298
encode_bytes(JOB_KEY_INFIX_TABLET, out); // "tablet"

‎cloud/src/meta-service/keys.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
// 0x01 "meta" ${instance_id} "schema" ${index_id} ${schema_version} -> TabletSchemaPB
2828
//
2929
// 0x01 "stats" ${instance_id} "tablet" ${table_id} ${index_id} ${partition_id} ${tablet_id} -> TabletStatsPB
30+
// 0x01 "stats" ${instance_id} "tablet" ${table_id} ${index_id} ${partition_id} ${tablet_id} "data_size" -> int64
31+
// 0x01 "stats" ${instance_id} "tablet" ${table_id} ${index_id} ${partition_id} ${tablet_id} "num_rows" -> int64
32+
// 0x01 "stats" ${instance_id} "tablet" ${table_id} ${index_id} ${partition_id} ${tablet_id} "num_rowsets" -> int64
33+
// 0x01 "stats" ${instance_id} "tablet" ${table_id} ${index_id} ${partition_id} ${tablet_id} "num_segs" -> int64
3034
//
3135
// 0x01 "recycle" ${instance_id} "index" ${index_id} -> RecycleIndexPB
3236
// 0x01 "recycle" ${instance_id} "partition" ${partition_id} -> RecyclePartitionPB
@@ -49,6 +53,12 @@ static const constexpr unsigned char CLOUD_USER_KEY_SPACE01 = 0x01;
4953
static const constexpr unsigned char CLOUD_SYS_KEY_SPACE02 = 0x02;
5054
static constexpr uint32_t VERSION_STAMP_LEN = 10;
5155

56+
// Suffix
57+
static constexpr std::string_view STATS_KEY_SUFFIX_DATA_SIZE = "data_size";
58+
static constexpr std::string_view STATS_KEY_SUFFIX_NUM_ROWS = "num_rows";
59+
static constexpr std::string_view STATS_KEY_SUFFIX_NUM_ROWSETS = "num_rowsets";
60+
static constexpr std::string_view STATS_KEY_SUFFIX_NUM_SEGS = "num_segs";
61+
5262
// clang-format off
5363
/**
5464
* Wraps std::tuple for differnet types even if the underlying type is the same.
@@ -161,6 +171,10 @@ static inline std::string recycle_rowset_key(const RecycleRowsetKeyInfo& in) { s
161171
static inline std::string recycle_txn_key(const RecycleTxnKeyInfo& in) { std::string s; recycle_txn_key(in, &s); return s; }
162172

163173
void stats_tablet_key(const StatsTabletKeyInfo& in, std::string* out);
174+
void stats_tablet_data_size_key(const StatsTabletKeyInfo& in, std::string* out);
175+
void stats_tablet_num_rows_key(const StatsTabletKeyInfo& in, std::string* out);
176+
void stats_tablet_num_rowsets_key(const StatsTabletKeyInfo& in, std::string* out);
177+
void stats_tablet_num_segs_key(const StatsTabletKeyInfo& in, std::string* out);
164178
static inline std::string stats_tablet_key(const StatsTabletKeyInfo& in) { std::string s; stats_tablet_key(in, &s); return s; }
165179

166180
void job_tablet_key(const JobTabletKeyInfo& in, std::string* out);

‎cloud/src/meta-service/mem_txn_kv.cpp‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int MemTxnKv::update(const std::set<std::string>& read_set,
4242
++committed_version_;;
4343
int16_t seq = 0;
4444
for (const auto& vec: op_list) {
45-
auto [op_type, k, v] = vec;
45+
auto& [op_type, k, v] = vec;
4646
LogItem log_item{op_type, committed_version_, k, v};
4747
log_kv_[k].push_front(log_item);
4848
switch (op_type) {
@@ -63,18 +63,12 @@ int MemTxnKv::update(const std::set<std::string>& read_set,
6363
break;
6464
}
6565
case memkv::ModifyOpType::ATOMIC_ADD: {
66-
if (mem_kv_.count(k) == 0) {
67-
mem_kv_[k] = v;
68-
} else {
69-
std::string org_val = mem_kv_[k];
70-
if (org_val.size() != 8) {
71-
org_val.resize(8, '\0');
72-
}
73-
int64_t res = *reinterpret_cast<const int64_t*>(org_val.data()) + std::stoll(v);
74-
std::string res_str = std::string(sizeof(res), '\0');
75-
std::memcpy(res_str.data(), &res, res_str.size());
76-
mem_kv_[k] = res_str;
66+
auto& org_val = mem_kv_[k];
67+
if (org_val.size() != 8) {
68+
org_val.resize(8, '\0');
7769
}
70+
int64_t res = *(int64_t*)org_val.data() + *(int64_t*)v.data();
71+
std::memcpy(org_val.data(), &res, 8);
7872
break;
7973
}
8074
case memkv::ModifyOpType::REMOVE: {
@@ -250,9 +244,11 @@ void Transaction::atomic_set_ver_value(std::string_view key, std::string_view va
250244
}
251245

252246
void Transaction::atomic_add(std::string_view key, int64_t to_add) {
253-
std::lock_guard<std::mutex> l(lock_);
254247
std::string k(key.data(), key.size());
255-
op_list_.emplace_back(ModifyOpType::ATOMIC_ADD, k, std::to_string(to_add));
248+
std::string v(sizeof(to_add), '\0');
249+
memcpy(v.data(), &to_add, sizeof(to_add));
250+
std::lock_guard<std::mutex> l(lock_);
251+
op_list_.emplace_back(ModifyOpType::ATOMIC_ADD, std::move(k), std::move(v));
256252
}
257253

258254
void Transaction::remove(std::string_view key) {

0 commit comments

Comments
 (0)