Skip to content

Commit 9c831b2

Browse files
stanislav-shchetininStanislav Shchetinin
andauthored
Backup changefeed configurations (tools dump) (#12283)
Co-authored-by: Stanislav Shchetinin <st-shchetinin@localhost.localdomain>
1 parent d779ea8 commit 9c831b2

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

ydb/library/backup/backup.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
#include <ydb/public/api/protos/ydb_table.pb.h>
66
#include <ydb/public/lib/ydb_cli/common/recursive_remove.h>
7+
#include <ydb/public/lib/ydb_cli/common/retry_func.h>
78
#include <ydb/public/lib/ydb_cli/dump/files/files.h>
89
#include <ydb/public/lib/ydb_cli/dump/util/util.h>
910
#include <ydb/public/lib/yson_value/ydb_yson_value.h>
11+
#include <ydb/public/sdk/cpp/client/ydb_proto/accessor.h>
1012
#include <ydb/public/sdk/cpp/client/ydb_driver/driver.h>
1113
#include <ydb/public/sdk/cpp/client/ydb_result/result.h>
1214
#include <ydb/public/sdk/cpp/client/ydb_table/table.h>
15+
#include <ydb/public/sdk/cpp/client/ydb_topic/topic.h>
1316
#include <ydb/public/sdk/cpp/client/ydb_value/value.h>
1417

1518
#include <library/cpp/containers/stack_vector/stack_vec.h>
@@ -475,6 +478,36 @@ void BackupPermissions(TDriver driver, const TString& dbPrefix, const TString& p
475478
WriteProtoToFile(proto, folderPath, NDump::NFiles::Permissions());
476479
}
477480

481+
Ydb::Table::ChangefeedDescription ProtoFromChangefeedDesc(const NTable::TChangefeedDescription& changefeedDesc) {
482+
Ydb::Table::ChangefeedDescription protoChangeFeedDesc;
483+
changefeedDesc.SerializeTo(protoChangeFeedDesc);
484+
return protoChangeFeedDesc;
485+
}
486+
487+
NTopic::TDescribeTopicResult DescribeTopic(TDriver driver, const TString& path) {
488+
NYdb::NTopic::TTopicClient client(driver);
489+
return NConsoleClient::RetryFunction([&]() {
490+
return client.DescribeTopic(path).GetValueSync();
491+
});
492+
}
493+
494+
void BackupChangefeeds(TDriver driver, const TString& tablePath, const TFsPath& folderPath) {
495+
auto desc = DescribeTable(driver, tablePath);
496+
497+
for (const auto& changefeedDesc : desc.GetChangefeedDescriptions()) {
498+
TFsPath changefeedDirPath = CreateDirectory(folderPath, changefeedDesc.GetName());
499+
500+
auto protoChangeFeedDesc = ProtoFromChangefeedDesc(changefeedDesc);
501+
const auto descTopicResult = DescribeTopic(driver, JoinDatabasePath(tablePath, changefeedDesc.GetName()));
502+
VerifyStatus(descTopicResult);
503+
const auto& topicDescription = descTopicResult.GetTopicDescription();
504+
const auto protoTopicDescription = NYdb::TProtoAccessor::GetProto(topicDescription);
505+
506+
WriteProtoToFile(protoChangeFeedDesc, changefeedDirPath, NDump::NFiles::Changefeed());
507+
WriteProtoToFile(protoTopicDescription, changefeedDirPath, NDump::NFiles::Topic());
508+
}
509+
}
510+
478511
void BackupTable(TDriver driver, const TString& dbPrefix, const TString& backupPrefix, const TString& path,
479512
const TFsPath& folderPath, bool schemaOnly, bool preservePoolKinds, bool ordered) {
480513
Y_ENSURE(!path.empty());
@@ -486,8 +519,9 @@ void BackupTable(TDriver driver, const TString& dbPrefix, const TString& backupP
486519

487520
auto desc = DescribeTable(driver, fullPath);
488521
auto proto = ProtoFromTableDescription(desc, preservePoolKinds);
489-
490522
WriteProtoToFile(proto, folderPath, NDump::NFiles::TableScheme());
523+
524+
BackupChangefeeds(driver, JoinDatabasePath(dbPrefix, path), folderPath);
491525
BackupPermissions(driver, dbPrefix, path, folderPath);
492526

493527
if (!schemaOnly) {

ydb/library/backup/ya.make

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ PEERDIR(
1212
ydb/public/lib/yson_value
1313
ydb/public/lib/ydb_cli/dump/files
1414
ydb/public/sdk/cpp/client/ydb_driver
15+
ydb/public/sdk/cpp/client/ydb_proto
1516
ydb/public/sdk/cpp/client/ydb_result
1617
ydb/public/sdk/cpp/client/ydb_table
18+
ydb/public/sdk/cpp/client/ydb_topic
1719
ydb/public/sdk/cpp/client/ydb_value
1820
)
1921

ydb/public/sdk/cpp/client/ydb_table/table.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,10 +2816,10 @@ TChangefeedDescription TChangefeedDescription::FromProto(const TProto& proto) {
28162816
return ret;
28172817
}
28182818

2819-
void TChangefeedDescription::SerializeTo(Ydb::Table::Changefeed& proto) const {
2819+
template <typename TProto>
2820+
void TChangefeedDescription::SerializeCommonFields(TProto& proto) const {
28202821
proto.set_name(Name_);
28212822
proto.set_virtual_timestamps(VirtualTimestamps_);
2822-
proto.set_initial_scan(InitialScan_);
28232823
proto.set_aws_region(AwsRegion_);
28242824

28252825
switch (Mode_) {
@@ -2860,12 +2860,35 @@ void TChangefeedDescription::SerializeTo(Ydb::Table::Changefeed& proto) const {
28602860
SetDuration(*ResolvedTimestamps_, *proto.mutable_resolved_timestamps_interval());
28612861
}
28622862

2863+
for (const auto& [key, value] : Attributes_) {
2864+
(*proto.mutable_attributes())[key] = value;
2865+
}
2866+
}
2867+
2868+
void TChangefeedDescription::SerializeTo(Ydb::Table::Changefeed& proto) const {
2869+
SerializeCommonFields(proto);
2870+
proto.set_initial_scan(InitialScan_);
2871+
28632872
if (RetentionPeriod_) {
28642873
SetDuration(*RetentionPeriod_, *proto.mutable_retention_period());
28652874
}
2875+
}
28662876

2867-
for (const auto& [key, value] : Attributes_) {
2868-
(*proto.mutable_attributes())[key] = value;
2877+
void TChangefeedDescription::SerializeTo(Ydb::Table::ChangefeedDescription& proto) const {
2878+
SerializeCommonFields(proto);
2879+
2880+
switch (State_) {
2881+
case EChangefeedState::Enabled:
2882+
proto.set_state(Ydb::Table::ChangefeedDescription_State::ChangefeedDescription_State_STATE_ENABLED);
2883+
break;
2884+
case EChangefeedState::Disabled:
2885+
proto.set_state(Ydb::Table::ChangefeedDescription_State::ChangefeedDescription_State_STATE_DISABLED);
2886+
break;
2887+
case EChangefeedState::InitialScan:
2888+
proto.set_state(Ydb::Table::ChangefeedDescription_State::ChangefeedDescription_State_STATE_INITIAL_SCAN);
2889+
break;
2890+
case EChangefeedState::Unknown:
2891+
break;
28692892
}
28702893
}
28712894

ydb/public/sdk/cpp/client/ydb_table/table.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ class TChangefeedDescription {
389389
const std::optional<TInitialScanProgress>& GetInitialScanProgress() const;
390390

391391
void SerializeTo(Ydb::Table::Changefeed& proto) const;
392+
void SerializeTo(Ydb::Table::ChangefeedDescription& proto) const;
392393
TString ToString() const;
393394
void Out(IOutputStream& o) const;
394395

@@ -399,6 +400,9 @@ class TChangefeedDescription {
399400
template <typename TProto>
400401
static TChangefeedDescription FromProto(const TProto& proto);
401402

403+
template <typename TProto>
404+
void SerializeCommonFields(TProto& proto) const;
405+
402406
private:
403407
TString Name_;
404408
EChangefeedMode Mode_;

0 commit comments

Comments
 (0)