Skip to content

Commit 38199f1

Browse files
authored
[feat](snapshot) fix recycle deleted instance (#59134)
1 parent 6d98eab commit 38199f1

File tree

9 files changed

+405
-38
lines changed

9 files changed

+405
-38
lines changed

cloud/src/meta-service/meta_service.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,10 @@ void internal_create_tablet(const CreateTabletsRequest* request, MetaServiceCode
681681
}
682682
txn->put(rs_key, rs_val);
683683
if (is_versioned_write) {
684+
std::string meta_rowset_key = versioned::meta_rowset_key(
685+
{instance_id, tablet_id, first_rowset->rowset_id_v2()});
686+
blob_put(txn.get(), meta_rowset_key, rs_val, 0);
687+
684688
std::string rowset_ref_count_key = versioned::data_rowset_ref_count_key(
685689
{instance_id, tablet_id, first_rowset->rowset_id_v2()});
686690
txn->atomic_add(rowset_ref_count_key, 1);
@@ -693,9 +697,11 @@ void internal_create_tablet(const CreateTabletsRequest* request, MetaServiceCode
693697
return;
694698
}
695699
LOG(INFO) << "put first versioned rowset meta, tablet_id=" << tablet_id
700+
<< " rowset_id=" << first_rowset->rowset_id_v2()
696701
<< " end_version=" << first_rowset->end_version()
697702
<< " key=" << hex(versioned_rs_key)
698-
<< " rowset_ref_count_key=" << hex(rowset_ref_count_key);
703+
<< " rowset_ref_count_key=" << hex(rowset_ref_count_key)
704+
<< " meta_rowset_key=" << hex(meta_rowset_key);
699705
}
700706

701707
tablet_meta.clear_rs_metas(); // Strip off rowset meta
@@ -2697,20 +2703,38 @@ void MetaServiceImpl::commit_rowset(::google::protobuf::RpcController* controlle
26972703
rowset_meta.set_allocated_tablet_schema(nullptr);
26982704
}
26992705

2706+
auto recycle_rs_key = recycle_rowset_key({instance_id, tablet_id, rowset_id});
2707+
txn->remove(recycle_rs_key);
2708+
2709+
DCHECK_GT(rowset_meta.txn_expiration(), 0);
2710+
auto tmp_rs_val = rowset_meta.SerializeAsString();
2711+
txn->put(tmp_rs_key, tmp_rs_val);
2712+
27002713
if (is_version_write_enabled(instance_id)) {
27012714
std::string rowset_ref_count_key =
27022715
versioned::data_rowset_ref_count_key({instance_id, tablet_id, rowset_id});
27032716
LOG(INFO) << "add rowset ref count key, instance_id=" << instance_id
27042717
<< "key=" << hex(rowset_ref_count_key);
27052718
txn->atomic_add(rowset_ref_count_key, 1);
2706-
}
27072719

2708-
auto recycle_rs_key = recycle_rowset_key({instance_id, tablet_id, rowset_id});
2709-
txn->remove(recycle_rs_key);
2720+
// Recycler uses end_version to check if the meta_rowset_compact_key or
2721+
// meta_rowset_load_key exists.
2722+
// The end_version is not set if rowset is committed by load, later commit_txn will write
2723+
// this key again to save end_version.
2724+
std::string meta_rowset_key =
2725+
versioned::meta_rowset_key({instance_id, tablet_id, rowset_id});
2726+
if (config::enable_recycle_rowset_strip_key_bounds) {
2727+
doris::RowsetMetaCloudPB rowset_meta_copy = rowset_meta;
2728+
// Strip key bounds to shrink operation log for ts compaction recycle entries
2729+
rowset_meta_copy.clear_segments_key_bounds();
2730+
rowset_meta_copy.clear_segments_key_bounds_truncated();
2731+
blob_put(txn.get(), meta_rowset_key, rowset_meta_copy.SerializeAsString(), 0);
2732+
} else {
2733+
blob_put(txn.get(), meta_rowset_key, tmp_rs_val, 0);
2734+
}
2735+
LOG(INFO) << "put versioned meta_rowset_key=" << hex(meta_rowset_key);
2736+
}
27102737

2711-
DCHECK_GT(rowset_meta.txn_expiration(), 0);
2712-
auto tmp_rs_val = rowset_meta.SerializeAsString();
2713-
txn->put(tmp_rs_key, tmp_rs_val);
27142738
std::size_t segment_key_bounds_bytes = get_segments_key_bounds_bytes(rowset_meta);
27152739
LOG(INFO) << "put tmp_rs_key " << hex(tmp_rs_key) << " delete recycle_rs_key "
27162740
<< hex(recycle_rs_key) << " value_size " << tmp_rs_val.size() << " txn_id "

cloud/src/meta-service/meta_service_job.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,10 +1121,9 @@ void process_compaction_job(MetaServiceCode& code, std::string& msg, std::string
11211121
} else {
11221122
auto recycle_val = recycle_rowset.SerializeAsString();
11231123
txn->put(recycle_key, recycle_val);
1124+
INSTANCE_LOG(INFO) << "put recycle rowset, tablet_id=" << tablet_id
1125+
<< " key=" << hex(recycle_key);
11241126
}
1125-
1126-
INSTANCE_LOG(INFO) << "put recycle rowset, tablet_id=" << tablet_id
1127-
<< " key=" << hex(recycle_key);
11281127
};
11291128
if (!is_versioned_read) {
11301129
std::tie(code, msg) =

cloud/src/meta-service/meta_service_txn.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,20 @@ void MetaServiceImpl::commit_txn_immediately(
16621662
<< " rowset_size=" << rowset_size;
16631663

16641664
if (is_versioned_write) {
1665+
auto& rowset_meta = i.second;
1666+
std::string meta_rowset_key = versioned::meta_rowset_key(
1667+
{instance_id, tablet_id, rowset_meta.rowset_id_v2()});
1668+
if (config::enable_recycle_rowset_strip_key_bounds) {
1669+
doris::RowsetMetaCloudPB rowset_meta_copy = rowset_meta;
1670+
// Strip key bounds to shrink operation log for ts compaction recycle entries
1671+
rowset_meta_copy.clear_segments_key_bounds();
1672+
rowset_meta_copy.clear_segments_key_bounds_truncated();
1673+
blob_put(txn.get(), meta_rowset_key, rowset_meta_copy.SerializeAsString(), 0);
1674+
} else {
1675+
blob_put(txn.get(), meta_rowset_key, rowset_meta.SerializeAsString(), 0);
1676+
}
1677+
LOG(INFO) << "put versioned meta_rowset_key=" << hex(meta_rowset_key);
1678+
16651679
std::string versioned_rowset_key =
16661680
versioned::meta_rowset_load_key({instance_id, tablet_id, version});
16671681
RowsetMetaCloudPB copied_rowset_meta(i.second);
@@ -2705,6 +2719,20 @@ void MetaServiceImpl::commit_txn_with_sub_txn(const CommitTxnRequest* request,
27052719
<< " rowset_size=" << rowset_size;
27062720

27072721
if (is_versioned_write) {
2722+
auto& rowset_meta = i.second;
2723+
std::string meta_rowset_key = versioned::meta_rowset_key(
2724+
{instance_id, rowset_meta.tablet_id(), rowset_meta.rowset_id_v2()});
2725+
if (config::enable_recycle_rowset_strip_key_bounds) {
2726+
doris::RowsetMetaCloudPB rowset_meta_copy = rowset_meta;
2727+
// Strip key bounds to shrink operation log for ts compaction recycle entries
2728+
rowset_meta_copy.clear_segments_key_bounds();
2729+
rowset_meta_copy.clear_segments_key_bounds_truncated();
2730+
blob_put(txn.get(), meta_rowset_key, rowset_meta_copy.SerializeAsString(), 0);
2731+
} else {
2732+
blob_put(txn.get(), meta_rowset_key, rowset_meta.SerializeAsString(), 0);
2733+
}
2734+
LOG(INFO) << "put versioned meta_rowset_key=" << hex(meta_rowset_key);
2735+
27082736
std::string versioned_rowset_key =
27092737
versioned::meta_rowset_load_key({instance_id, tablet_id, version});
27102738
if (!versioned::document_put(txn.get(), versioned_rowset_key,

cloud/src/meta-service/txn_lazy_committer.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "cpp/sync_point.h"
3030
#include "meta-service/meta_service_helper.h"
3131
#include "meta-service/meta_service_tablet_stats.h"
32+
#include "meta-store/blob_message.h"
3233
#include "meta-store/document_message.h"
3334
#include "meta-store/keys.h"
3435
#include "meta-store/meta_reader.h"
@@ -338,6 +339,19 @@ void convert_tmp_rowsets(
338339
stats.segment_size += tmp_rowset_pb.data_disk_size();
339340

340341
if (is_versioned_write) {
342+
std::string meta_rowset_key = versioned::meta_rowset_key(
343+
{instance_id, tmp_rowset_pb.tablet_id(), tmp_rowset_pb.rowset_id_v2()});
344+
if (config::enable_recycle_rowset_strip_key_bounds) {
345+
doris::RowsetMetaCloudPB rowset_meta_copy = tmp_rowset_pb;
346+
// Strip key bounds to shrink operation log for ts compaction recycle entries
347+
rowset_meta_copy.clear_segments_key_bounds();
348+
rowset_meta_copy.clear_segments_key_bounds_truncated();
349+
blob_put(txn.get(), meta_rowset_key, rowset_meta_copy.SerializeAsString(), 0);
350+
} else {
351+
blob_put(txn.get(), meta_rowset_key, tmp_rowset_pb.SerializeAsString(), 0);
352+
}
353+
LOG(INFO) << "put versioned meta_rowset_key=" << hex(meta_rowset_key);
354+
341355
// If this is a versioned write, we need to put the rowset with versionstamp
342356
RowsetMetaCloudPB copied_rowset_meta(tmp_rowset_pb);
343357
std::string rowset_load_key = versioned::meta_rowset_load_key(

cloud/src/meta-store/keys.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,15 @@ void data_rowset_ref_count_key(const DataRowsetRefCountKeyInfo& in, std::string*
825825
encode_bytes(std::get<2>(in), out); // rowset_id
826826
}
827827

828+
void meta_rowset_key(const MetaRowsetKeyInfo& in, std::string* out) {
829+
out->push_back(CLOUD_VERSIONED_KEY_SPACE03);
830+
encode_bytes(META_KEY_PREFIX, out); // "meta"
831+
encode_bytes(std::get<0>(in), out); // instance_id
832+
encode_bytes(META_KEY_INFIX_ROWSET, out); // "rowset"
833+
encode_int64(std::get<1>(in), out); // tablet_id
834+
encode_bytes(std::get<2>(in), out); // rowset_id
835+
}
836+
828837
//==============================================================================
829838
// Snapshot keys
830839
//==============================================================================
@@ -1154,6 +1163,34 @@ bool decode_snapshot_ref_key(std::string_view* in, std::string* instance_id,
11541163

11551164
return true;
11561165
}
1166+
1167+
bool decode_data_rowset_ref_count_key(std::string_view* in, int64_t* tablet_id,
1168+
std::string* rowset_id) {
1169+
// 0x03 "data" ${instance_id} "rowset_ref_count" ${tablet_id} ${rowset_id}
1170+
if (in->empty() || static_cast<uint8_t>((*in)[0]) != CLOUD_VERSIONED_KEY_SPACE03) {
1171+
return false;
1172+
}
1173+
in->remove_prefix(1);
1174+
1175+
std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out;
1176+
auto res = decode_key(in, &out);
1177+
if (res != 0 || out.size() != 5) {
1178+
return false;
1179+
}
1180+
1181+
try {
1182+
if (std::get<std::string>(std::get<0>(out[0])) != DATA_KEY_PREFIX ||
1183+
std::get<std::string>(std::get<0>(out[2])) != META_ROWSET_REF_COUNT_KEY_INFIX) {
1184+
return false;
1185+
}
1186+
*tablet_id = std::get<int64_t>(std::get<0>(out[3]));
1187+
*rowset_id = std::get<std::string>(std::get<0>(out[4]));
1188+
} catch (const std::bad_variant_access& e) {
1189+
return false;
1190+
}
1191+
1192+
return true;
1193+
}
11571194
} // namespace versioned
11581195

11591196
// Decode stats tablet key to extract table_id, index_id, partition_id and tablet_id

cloud/src/meta-store/keys.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ using MetaDeleteBitmapInfo = BasicKeyInfo<__LINE__ , std::tuple<std::string, int
322322
// 0:instance_id 1:tablet_id 2:rowset_id
323323
using DataRowsetRefCountKeyInfo = BasicKeyInfo<__LINE__, std::tuple<std::string, int64_t, std::string>>;
324324

325+
// 0x03 "meta" ${instance_id} "rowset" ${tablet_id} ${rowset_id} -> RowsetMetaPB
326+
// 0:instance_id 1:tablet_id 2:rowset_id
327+
using MetaRowsetKeyInfo = BasicKeyInfo<__LINE__, std::tuple<std::string, int64_t, std::string>>;
328+
325329
// 0x03 "snapshot" ${instance_id} "full" ${timestamp} -> SnapshotPB
326330
// 0:instance_id
327331
using SnapshotFullKeyInfo = BasicKeyInfo<__LINE__, std::tuple<std::string>>;
@@ -529,6 +533,9 @@ static inline std::string meta_delete_bitmap_key(const MetaDeleteBitmapInfo& in)
529533
void data_rowset_ref_count_key(const DataRowsetRefCountKeyInfo& in, std::string* out);
530534
static inline std::string data_rowset_ref_count_key(const DataRowsetRefCountKeyInfo& in) { std::string s; data_rowset_ref_count_key(in, &s); return s; }
531535

536+
void meta_rowset_key(const MetaRowsetKeyInfo& in, std::string* out);
537+
static inline std::string meta_rowset_key(const MetaRowsetKeyInfo& in) { std::string s; meta_rowset_key(in, &s); return s; }
538+
532539
void snapshot_full_key(const SnapshotFullKeyInfo& in, std::string* out);
533540
static inline std::string snapshot_full_key(const SnapshotFullKeyInfo& in) { std::string s; snapshot_full_key(in, &s); return s; }
534541

@@ -594,6 +601,11 @@ bool decode_meta_tablet_key(std::string_view* in, int64_t* tablet_id, Versionsta
594601
// Return true if decode successfully, otherwise false
595602
bool decode_snapshot_ref_key(std::string_view* in, std::string* instance_id,
596603
Versionstamp* timestamp, std::string* ref_instance_id);
604+
605+
// Decode data rowset ref count key
606+
// Return true if decode successfully, otherwise false
607+
bool decode_data_rowset_ref_count_key(std::string_view* in, int64_t* tablet_id,
608+
std::string* rowset_id);
597609
} // namespace versioned
598610

599611
// Decode stats tablet key

0 commit comments

Comments
 (0)