Skip to content

Commit 8a06f46

Browse files
authored
Encapsulate BackupFolder() in DumpClient (#10804)
1 parent 60bad7a commit 8a06f46

File tree

12 files changed

+119
-86
lines changed

12 files changed

+119
-86
lines changed

ydb/library/backup/util.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ struct TLog {
2020
{}
2121
};
2222

23-
void SetLog(std::shared_ptr<::TLog>&& log) {
24-
Singleton<TLog>()->Log = std::move(log);
23+
void SetLog(const std::shared_ptr<::TLog>& log) {
24+
Singleton<TLog>()->Log = log;
2525
}
2626

27-
std::shared_ptr<::TLog>& GetLog() {
27+
const std::shared_ptr<::TLog>& GetLog() {
2828
return Singleton<TLog>()->Log;
2929
}
3030

ydb/library/backup/util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
namespace NYdb {
1919

2020
namespace NBackup {
21-
void SetLog(std::shared_ptr<::TLog>&& log);
22-
std::shared_ptr<::TLog>& GetLog();
21+
void SetLog(const std::shared_ptr<::TLog>& log);
22+
const std::shared_ptr<::TLog>& GetLog();
2323
}
2424

2525
// Retrive path relative to database root from absolute

ydb/public/lib/ydb_cli/commands/ydb_tools.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include <ydb/public/lib/ydb_cli/common/normalize_path.h>
88
#include <ydb/public/lib/ydb_cli/common/pg_dump_parser.h>
99
#include <ydb/public/lib/ydb_cli/dump/dump.h>
10-
#include <ydb/library/backup/backup.h>
1110
#include <ydb/library/backup/util.h>
1211

12+
#include <library/cpp/regex/pcre/regexp.h>
13+
14+
#include <util/generic/serialized_enum.h>
1315
#include <util/stream/format.h>
1416
#include <util/string/split.h>
1517

@@ -58,28 +60,31 @@ void TCommandDump::Config(TConfig& config) {
5860
config.Opts->AddLongOption('o', "output", "[Required] Path in a local filesystem to a directory to place dump into."
5961
" Directory should either not exist or be empty.")
6062
.StoreResult(&FilePath);
63+
64+
NDump::TDumpSettings defaults;
65+
6166
config.Opts->AddLongOption("scheme-only", "Dump only scheme including ACL and owner")
62-
.StoreTrue(&IsSchemeOnly);
67+
.DefaultValue(defaults.SchemaOnly_).StoreTrue(&IsSchemeOnly);
6368
config.Opts->AddLongOption("avoid-copy", "Avoid copying."
6469
" By default, YDB makes a copy of a table before dumping it to reduce impact on workload and ensure consistency.\n"
6570
"In some cases (e.g. for tables with external blobs) copying should be disabled.")
66-
.StoreTrue(&AvoidCopy);
71+
.DefaultValue(defaults.AvoidCopy_).StoreTrue(&AvoidCopy);
6772
config.Opts->AddLongOption("save-partial-result", "Do not remove partial dump result."
6873
" If this option is not enabled, all files that have already been created will be removed in case of error.")
69-
.StoreTrue(&SavePartialResult);
74+
.DefaultValue(defaults.SavePartialResult_).StoreTrue(&SavePartialResult);
7075
config.Opts->AddLongOption("preserve-pool-kinds", "Preserve storage pool kind settings."
7176
" If this option is enabled, storage pool kind will be saved to dump."
7277
" In this case, if there will be no such storage pool kind in database on restore, error will occur."
7378
" By default this option is disabled and any existing storage pool kind will be used on restore.")
74-
.StoreTrue(&PreservePoolKinds);
79+
.DefaultValue(defaults.PreservePoolKinds_).StoreTrue(&PreservePoolKinds);
7580
config.Opts->AddLongOption("consistency-level", "Consistency level."
7681
" Options: database, table\n"
7782
"database - take one consistent snapshot of all tables specified for dump."
7883
" Takes more time and is more likely to impact workload;\n"
7984
"table - take consistent snapshot per each table independently.")
80-
.DefaultValue("database").StoreResult(&ConsistencyLevel);
85+
.DefaultValue(defaults.ConsistencyLevel_).StoreResult(&ConsistencyLevel);
8186
config.Opts->AddLongOption("ordered", "Preserve order by primary key in backup files.")
82-
.StoreTrue(&Ordered);
87+
.DefaultValue(defaults.Ordered_).StoreTrue(&Ordered);
8388
}
8489

8590
void TCommandDump::Parse(TConfig& config) {
@@ -88,31 +93,28 @@ void TCommandDump::Parse(TConfig& config) {
8893
}
8994

9095
int TCommandDump::Run(TConfig& config) {
91-
92-
bool useConsistentCopyTable;
93-
if (ConsistencyLevel == "database") {
94-
useConsistentCopyTable = true;
95-
} else if (ConsistencyLevel == "table") {
96-
useConsistentCopyTable = false;
97-
} else {
98-
throw yexception() << "Incorrect consistency level. Available options: \"database\", \"table\"" << Endl;
96+
NDump::TDumpSettings::EConsistencyLevel consistencyLevel;
97+
if (!TryFromString<NDump::TDumpSettings::EConsistencyLevel>(ConsistencyLevel, consistencyLevel)) {
98+
throw yexception() << "Incorrect consistency level."
99+
" Available options: " << GetEnumAllNames<NDump::TDumpSettings::EConsistencyLevel>();
99100
}
100101

102+
auto settings = NDump::TDumpSettings()
103+
.Database(config.Database)
104+
.ExclusionPatterns(std::move(ExclusionPatterns))
105+
.SchemaOnly(IsSchemeOnly)
106+
.ConsistencyLevel(consistencyLevel)
107+
.AvoidCopy(AvoidCopy)
108+
.SavePartialResult(SavePartialResult)
109+
.PreservePoolKinds(PreservePoolKinds)
110+
.Ordered(Ordered);
111+
101112
auto log = std::make_shared<TLog>(CreateLogBackend("cerr", TConfig::VerbosityLevelToELogPriority(config.VerbosityLevel)));
102113
log->SetFormatter(GetPrefixLogFormatter(""));
103-
NYdb::NBackup::SetLog(std::move(log));
104-
105-
try {
106-
TString relPath = NYdb::RelPathFromAbsolute(config.Database, Path);
107-
NYdb::NBackup::BackupFolder(CreateDriver(config), config.Database, relPath, FilePath, ExclusionPatterns,
108-
IsSchemeOnly, useConsistentCopyTable, AvoidCopy, SavePartialResult, PreservePoolKinds, Ordered);
109-
} catch (const NYdb::NBackup::TYdbErrorException& e) {
110-
e.LogToStderr();
111-
return EXIT_FAILURE;
112-
} catch (const yexception& e) {
113-
Cerr << "General error, what# " << e.what() << Endl;
114-
return EXIT_FAILURE;
115-
}
114+
115+
NDump::TClient client(CreateDriver(config), std::move(log));
116+
ThrowOnError(client.Dump(Path, FilePath, settings));
117+
116118
return EXIT_SUCCESS;
117119
}
118120

ydb/public/lib/ydb_cli/commands/ydb_tools.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <ydb/public/lib/ydb_cli/common/examples.h>
88
#include <ydb/public/lib/ydb_cli/common/parseable_struct.h>
99

10-
#include <library/cpp/regex/pcre/regexp.h>
10+
class TRegExMatch;
1111

1212
namespace NYdb {
1313
namespace NConsoleClient {

ydb/public/lib/ydb_cli/dump/dump.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
#include "dump_impl.h"
33
#include "restore_impl.h"
44

5-
#include <ydb/public/sdk/cpp/client/ydb_import/import.h>
6-
#include <ydb/public/sdk/cpp/client/ydb_operation/operation.h>
7-
#include <ydb/public/sdk/cpp/client/ydb_scheme/scheme.h>
8-
#include <ydb/public/sdk/cpp/client/ydb_table/table.h>
5+
#include <ydb/public/sdk/cpp/client/ydb_driver/driver.h>
96

107
#include <library/cpp/logger/log.h>
118

@@ -26,30 +23,24 @@ TString DataFileName(ui32 id) {
2623
class TClient::TImpl {
2724
public:
2825
explicit TImpl(const TDriver& driver, std::shared_ptr<TLog>&& log)
29-
: Log(log)
30-
, ImportClient(driver)
31-
, OperationClient(driver)
32-
, SchemeClient(driver)
33-
, TableClient(driver)
26+
: Driver(driver)
27+
, Log(std::move(log))
3428
{
3529
}
3630

3731
TDumpResult Dump(const TString& dbPath, const TString& fsPath, const TDumpSettings& settings) {
38-
auto client = TDumpClient(SchemeClient, TableClient);
32+
auto client = TDumpClient(Driver, Log);
3933
return client.Dump(dbPath, fsPath, settings);
4034
}
4135

4236
TRestoreResult Restore(const TString& fsPath, const TString& dbPath, const TRestoreSettings& settings) {
43-
auto client = TRestoreClient(*Log, ImportClient, OperationClient, SchemeClient, TableClient);
37+
auto client = TRestoreClient(Driver, *Log);
4438
return client.Restore(fsPath, dbPath, settings);
4539
}
4640

4741
private:
42+
const TDriver Driver;
4843
std::shared_ptr<TLog> Log;
49-
NImport::TImportClient ImportClient;
50-
NOperation::TOperationClient OperationClient;
51-
NScheme::TSchemeClient SchemeClient;
52-
NTable::TTableClient TableClient;
5344

5445
}; // TImpl
5546

ydb/public/lib/ydb_cli/dump/dump.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
#include <ydb/public/sdk/cpp/client/ydb_types/status/status.h>
44
#include <ydb/public/sdk/cpp/client/ydb_types/fluent_settings_helpers.h>
5-
#include <ydb/public/sdk/cpp/client/ydb_driver/driver.h>
5+
#include <ydb/public/sdk/cpp/client/ydb_types/request_settings.h>
6+
7+
#include <library/cpp/regex/pcre/regexp.h>
68

79
#include <util/generic/size_literals.h>
810

911
class TLog;
1012

1113
namespace NYdb {
14+
15+
class TDriver;
16+
1217
namespace NDump {
1318

1419
extern const char SCHEME_FILE_NAME[10];
@@ -22,6 +27,34 @@ TString DataFileName(ui32 id);
2227
struct TDumpSettings: public TOperationRequestSettings<TDumpSettings> {
2328
using TSelf = TDumpSettings;
2429

30+
enum class EConsistencyLevel {
31+
Table /* "table" */,
32+
Database /* "database" */,
33+
};
34+
35+
TVector<TRegExMatch> ExclusionPatterns_;
36+
TSelf& ExclusionPatterns(TVector<TRegExMatch>&& value) {
37+
ExclusionPatterns_ = std::move(value);
38+
return *this;
39+
}
40+
41+
FLUENT_SETTING(TString, Database);
42+
FLUENT_SETTING_DEFAULT(bool, SchemaOnly, false);
43+
FLUENT_SETTING_DEFAULT(EConsistencyLevel, ConsistencyLevel, EConsistencyLevel::Database);
44+
FLUENT_SETTING_DEFAULT(bool, AvoidCopy, false);
45+
FLUENT_SETTING_DEFAULT(bool, SavePartialResult, false);
46+
FLUENT_SETTING_DEFAULT(bool, PreservePoolKinds, false);
47+
FLUENT_SETTING_DEFAULT(bool, Ordered, false);
48+
49+
bool UseConsistentCopyTable() const {
50+
switch (ConsistencyLevel_) {
51+
case EConsistencyLevel::Table:
52+
return false;
53+
case EConsistencyLevel::Database:
54+
return true;
55+
}
56+
}
57+
2558
}; // TDumpSettings
2659

2760
class TDumpResult: public TStatus {

ydb/public/lib/ydb_cli/dump/dump_impl.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
#include "dump_impl.h"
22

3+
#include <ydb/library/backup/backup.h>
4+
#include <ydb/library/backup/util.h>
35
#include <ydb/public/lib/ydb_cli/dump/util/util.h>
46

57
namespace NYdb {
68
namespace NDump {
79

8-
TDumpClient::TDumpClient(NScheme::TSchemeClient& schemeClient, NTable::TTableClient& tableClient)
9-
: SchemeClient(schemeClient)
10-
, TableClient(tableClient)
10+
TDumpClient::TDumpClient(const TDriver& driver, const std::shared_ptr<TLog>& log)
11+
: Driver(driver)
12+
, Log(log)
1113
{
12-
Y_UNUSED(SchemeClient);
13-
Y_UNUSED(TableClient);
1414
}
1515

1616
TDumpResult TDumpClient::Dump(const TString& dbPath, const TString& fsPath, const TDumpSettings& settings) {
17-
Y_UNUSED(dbPath);
18-
Y_UNUSED(TableClient);
19-
Y_UNUSED(fsPath);
20-
Y_UNUSED(settings);
21-
return Result<TDumpResult>(EStatus::CLIENT_CALL_UNIMPLEMENTED, "Not implemented");
17+
try {
18+
NBackup::SetLog(Log);
19+
NBackup::BackupFolder(
20+
Driver,
21+
settings.Database_,
22+
RelPathFromAbsolute(settings.Database_, dbPath),
23+
fsPath,
24+
settings.ExclusionPatterns_,
25+
settings.SchemaOnly_,
26+
settings.UseConsistentCopyTable(),
27+
settings.AvoidCopy_,
28+
settings.SavePartialResult_,
29+
settings.PreservePoolKinds_,
30+
settings.Ordered_);
31+
return Result<TDumpResult>();
32+
} catch (NBackup::TYdbErrorException& e) {
33+
return TDumpResult(std::move(e.Status));
34+
} catch (const yexception& e) {
35+
return Result<TDumpResult>(EStatus::INTERNAL_ERROR, e.what());
36+
}
2237
}
2338

2439
} // NDump

ydb/public/lib/ydb_cli/dump/dump_impl.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22

33
#include "dump.h"
44

5-
#include <ydb/public/sdk/cpp/client/ydb_scheme/scheme.h>
6-
#include <ydb/public/sdk/cpp/client/ydb_table/table.h>
7-
85
namespace NYdb {
96
namespace NDump {
107

118
class TDumpClient {
129
public:
13-
explicit TDumpClient(NScheme::TSchemeClient& schemeClient, NTable::TTableClient& tableClient);
10+
explicit TDumpClient(const TDriver& driver, const std::shared_ptr<TLog>& log);
1411

1512
TDumpResult Dump(const TString& dbPath, const TString& fsPath, const TDumpSettings& settings = {});
1613

1714
private:
18-
NScheme::TSchemeClient& SchemeClient;
19-
NTable::TTableClient& TableClient;
15+
const TDriver& Driver;
16+
std::shared_ptr<TLog> Log;
2017

2118
}; // TDumpClient
2219

ydb/public/lib/ydb_cli/dump/restore_impl.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,12 @@ bool IsOperationStarted(TStatus operationStatus) {
9595

9696
} // anonymous
9797

98-
TRestoreClient::TRestoreClient(
99-
TLog& log,
100-
TImportClient& importClient,
101-
TOperationClient& operationClient,
102-
TSchemeClient& schemeClient,
103-
TTableClient& tableClient)
98+
TRestoreClient::TRestoreClient(const TDriver& driver, TLog& log)
10499
: Log(log)
105-
, ImportClient(importClient)
106-
, OperationClient(operationClient)
107-
, SchemeClient(schemeClient)
108-
, TableClient(tableClient)
100+
, ImportClient(driver)
101+
, OperationClient(driver)
102+
, SchemeClient(driver)
103+
, TableClient(driver)
109104
{
110105
}
111106

ydb/public/lib/ydb_cli/dump/restore_impl.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <ydb/public/sdk/cpp/client/ydb_table/table.h>
99

1010
#include <util/folder/path.h>
11+
#include <util/generic/hash_set.h>
1112

1213
namespace NYdb {
1314
namespace NDump {
@@ -46,21 +47,16 @@ class TRestoreClient {
4647
TRestoreResult RestorePermissions(const TFsPath& fsPath, const TString& dbPath, const TRestoreSettings& settings, const THashSet<TString>& oldEntries);
4748

4849
public:
49-
explicit TRestoreClient(
50-
TLog& log,
51-
NImport::TImportClient& importClient,
52-
NOperation::TOperationClient& operationClient,
53-
NScheme::TSchemeClient& SchemeClient,
54-
NTable::TTableClient& tableClient);
50+
explicit TRestoreClient(const TDriver& driver, TLog& log);
5551

5652
TRestoreResult Restore(const TString& fsPath, const TString& dbPath, const TRestoreSettings& settings = {});
5753

5854
private:
5955
TLog& Log;
60-
NImport::TImportClient& ImportClient;
61-
NOperation::TOperationClient& OperationClient;
62-
NScheme::TSchemeClient& SchemeClient;
63-
NTable::TTableClient& TableClient;
56+
NImport::TImportClient ImportClient;
57+
NOperation::TOperationClient OperationClient;
58+
NScheme::TSchemeClient SchemeClient;
59+
NTable::TTableClient TableClient;
6460

6561
}; // TRestoreClient
6662

ydb/public/lib/ydb_cli/dump/restore_import_data.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <ydb/public/lib/ydb_cli/common/retry_func.h>
44
#include <ydb/public/lib/ydb_cli/dump/util/util.h>
5+
56
#include <library/cpp/string_utils/quote/quote.h>
67
#include <library/cpp/bucket_quoter/bucket_quoter.h>
78

ydb/public/lib/ydb_cli/dump/ya.make

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SRCS(
1111
PEERDIR(
1212
library/cpp/bucket_quoter
1313
library/cpp/logger
14+
library/cpp/regex/pcre
1415
library/cpp/string_utils/quote
1516
ydb/library/backup
1617
ydb/public/api/protos
@@ -19,4 +20,6 @@ PEERDIR(
1920
ydb/public/sdk/cpp/client/ydb_proto
2021
)
2122

23+
GENERATE_ENUM_SERIALIZATION(dump.h)
24+
2225
END()

0 commit comments

Comments
 (0)