Skip to content

Commit 03ea37e

Browse files
authored
Introduce logging in 'tools restore' (#10681)
1 parent 2c08f12 commit 03ea37e

File tree

16 files changed

+260
-191
lines changed

16 files changed

+260
-191
lines changed

ydb/apps/ydb/ut/ydb-dump.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@
99

1010
#include <ydb/public/api/protos/ydb_table.pb.h>
1111

12+
#include <util/stream/file.h>
13+
#include <util/string/printf.h>
14+
1215
Y_UNIT_TEST_SUITE(YdbDump) {
1316

1417
Y_UNIT_TEST(NotNullTypeDump) {
18+
const char* tableName = "TableWithNotNullTypeForDump";
1519
RunYdb({"-v", "yql", "-s",
16-
R"(CREATE TABLE TableWithNotNullTypeForDump (
20+
Sprintf(R"(CREATE TABLE %s (
1721
k Uint32 NOT NULL,
1822
v String NOT NULL,
1923
ov String,
2024
PRIMARY KEY(k));
21-
)"},
25+
)", tableName)},
2226
TList<TString>());
2327

24-
const TString output = RunYdb({"-v", "tools", "dump", "--scheme-only"}, TList<TString>());
28+
const auto dumpPath = GetOutputPath() / "dump";
29+
RunYdb({"-v", "tools", "dump", "--scheme-only", "--output", dumpPath.GetPath()}, TList<TString>());
2530

2631
struct TFlags {
2732
const bool HasNullFlag;
@@ -35,6 +40,7 @@ Y_UNIT_TEST(NotNullTypeDump) {
3540
column_flags.emplace_back(TFlags{meta.has_not_null(), meta.type().has_optional_type()});
3641
};
3742

43+
const auto output = TFileInput(dumpPath / tableName / "scheme.pb").ReadAll();
3844
const TString token = "columns {";
3945
size_t start = 0;
4046
while (true) {

ydb/library/backup/backup.cpp

+83-104
Large diffs are not rendered by default.

ydb/library/backup/backup.h

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <library/cpp/regex/pcre/regexp.h>
88

99
#include <util/folder/path.h>
10-
#include <util/generic/singleton.h>
1110
#include <util/stream/output.h>
1211
#include <util/system/file.h>
1312

ydb/library/backup/query_uploader.cpp

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include <util/datetime/cputimer.h>
2-
31
#include "query_uploader.h"
42
#include "util.h"
53

@@ -62,17 +60,12 @@ bool TUploader::Push(const TString& path, TValue&& value) {
6260

6361
if (status.IsSuccess()) {
6462
if (status.GetIssues()) {
65-
LOG_ERR("BulkUpsert has finished successfull, but has issues# {"
66-
<< status.GetIssues().ToString() << "}");
63+
LOG_W("Bulk upsert was completed with issues: " << status.GetIssues().ToOneLineString());
6764
}
6865
return;
6966
// Since upsert of data is an idempotent operation it is possible to retry transport errors
7067
} else if (status.IsTransportError() && retry < Opts.TransportErrorsMaxRetries) {
71-
LOG_DEBUG("Notice: transport error in BulkUpsert, issues# {" << status.GetIssues().ToString() << "}"
72-
<< " current Retry is " << retry
73-
<< " < MaxRetries# " << Opts.TransportErrorsMaxRetries
74-
<< ", so sleep for " << retrySleep.Seconds() << "s"
75-
<< " and try again");
68+
LOG_D("Retry bulk upsert: " << status.GetIssues().ToOneLineString());
7669
++retry;
7770
TInstant deadline = retrySleep.ToDeadLine();
7871
while (TInstant::Now() < deadline) {
@@ -84,9 +77,7 @@ bool TUploader::Push(const TString& path, TValue&& value) {
8477
retrySleep *= 2;
8578
continue;
8679
} else {
87-
LOG_ERR("Error in BulkUpsert, so stop working. Issues# {" << status.GetIssues().ToString() << "}"
88-
<< " IsTransportError# " << (status.IsTransportError() ? "true" : "false")
89-
<< " retries done# " << retry);
80+
LOG_E("Bulk upsert failed: " << status.GetIssues().ToOneLineString());
9081
PleaseStop();
9182
return;
9283
}
@@ -136,11 +127,10 @@ bool TUploader::Push(TParams params) {
136127

137128
if (status.IsSuccess()) {
138129
if (status.GetIssues()) {
139-
LOG_ERR("Upload tx has finished successfull, but has issues# {"
140-
<< status.GetIssues().ToString() << "}");
130+
LOG_W("Write tx was completed with issues: " << status.GetIssues().ToOneLineString());
141131
}
142132
} else {
143-
LOG_ERR("Error in upload tx, issues# {" << status.GetIssues().ToString() << "}");
133+
LOG_E("Write tx failed: " << status.GetIssues().ToOneLineString());
144134
PleaseStop();
145135
return;
146136
}

ydb/library/backup/util.cpp

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
#include "util.h"
22

3-
#include <util/generic/yexception.h>
4-
#include <util/string/builder.h>
5-
#include <util/string/cast.h>
63
#include <util/generic/map.h>
4+
#include <util/generic/singleton.h>
5+
#include <util/generic/yexception.h>
76
#include <util/generic/ymath.h>
7+
#include <util/string/cast.h>
88

99
#include <ctype.h>
1010

1111
namespace NYdb {
1212

13+
namespace NBackup {
14+
15+
struct TLog {
16+
std::shared_ptr<::TLog> Log;
17+
18+
TLog()
19+
: Log(std::make_shared<::TLog>(CreateLogBackend("cerr")))
20+
{}
21+
};
22+
23+
void SetLog(std::shared_ptr<::TLog>&& log) {
24+
Singleton<TLog>()->Log = std::move(log);
25+
}
26+
27+
std::shared_ptr<::TLog>& GetLog() {
28+
return Singleton<TLog>()->Log;
29+
}
30+
31+
} // NBackup
32+
1333
TString RelPathFromAbsolute(TString db, TString path) {
1434
if (!db.StartsWith('/')) {
1535
db.prepend('/');
@@ -77,21 +97,4 @@ ui64 SizeFromString(TStringBuf s) {
7797
return FromString<ui64>(number) * it->second;
7898
}
7999

80-
namespace {
81-
82-
struct TIsVerbosePrint {
83-
bool IsVerbose = false;
84-
};
85-
86-
}
87-
88-
void SetVerbosity(bool isVerbose) {
89-
Singleton<TIsVerbosePrint>()->IsVerbose = isVerbose;
90-
}
91-
92-
bool GetVerbosity() {
93-
return Singleton<TIsVerbosePrint>()->IsVerbose;
94-
}
95-
96-
97100
}

ydb/library/backup/util.h

+16-28
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
#pragma once
22

3-
#include <util/datetime/base.h>
4-
#include <util/generic/string.h>
5-
#include <util/stream/output.h>
6-
#include <util/stream/null.h>
3+
#include <library/cpp/logger/log.h>
74

8-
namespace NYdb {
9-
10-
void SetVerbosity(bool isVerbose);
11-
bool GetVerbosity();
5+
#include <util/string/builder.h>
126

13-
#define LOG_NULL(s) Cnull << s
14-
#define EXTEND_MSG(s) TInstant::Now().ToIsoStringLocal() << ": " << s << Endl
7+
#define LOG_IMPL(log, level, message) \
8+
if (log->FiltrationLevel() >= level) { \
9+
log->Write(level, TStringBuilder() << message); \
10+
} \
11+
Y_SEMICOLON_GUARD
1512

16-
#define LOG_INFO(s) Cout << EXTEND_MSG(s)
17-
#define LOG_ERR(s) Cerr << EXTEND_MSG(s)
18-
#define LOG_DEBUG(s) (NYdb::GetVerbosity() ? LOG_INFO(s) : LOG_NULL(s))
13+
#define LOG_D(message) LOG_IMPL(NYdb::NBackup::GetLog(), ELogPriority::TLOG_DEBUG, message)
14+
#define LOG_I(message) LOG_IMPL(NYdb::NBackup::GetLog(), ELogPriority::TLOG_INFO, message)
15+
#define LOG_W(message) LOG_IMPL(NYdb::NBackup::GetLog(), ELogPriority::TLOG_WARNING, message)
16+
#define LOG_E(message) LOG_IMPL(NYdb::NBackup::GetLog(), ELogPriority::TLOG_ERR, message)
1917

18+
namespace NYdb {
2019

20+
namespace NBackup {
21+
void SetLog(std::shared_ptr<::TLog>&& log);
22+
std::shared_ptr<::TLog>& GetLog();
23+
}
2124

2225
// Retrive path relative to database root from absolute
2326
TString RelPathFromAbsolute(TString db, TString path);
@@ -28,19 +31,4 @@ TString RelPathFromAbsolute(TString db, TString path);
2831
// Example: "2Ki" -> 2048
2932
ui64 SizeFromString(TStringBuf s);
3033

31-
class TScopedTimer {
32-
TInstant Start;
33-
TString Msg;
34-
35-
public:
36-
TScopedTimer(const TString& msg)
37-
: Start(TInstant::Now())
38-
, Msg(msg)
39-
{}
40-
41-
~TScopedTimer() {
42-
LOG_INFO(Msg << (TInstant::Now() - Start).SecondsFloat() << "s");
43-
}
44-
};
45-
4634
}

ydb/library/backup/ya.make

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ LIBRARY(kikimr_backup)
22

33
PEERDIR(
44
library/cpp/bucket_quoter
5+
library/cpp/logger
56
library/cpp/regex/pcre
67
library/cpp/string_utils/quote
78
util

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "ydb_tools.h"
22

3+
#define INCLUDE_YDB_INTERNAL_H
4+
#include <ydb/public/sdk/cpp/client/impl/ydb_internal/logger/log.h>
5+
#undef INCLUDE_YDB_INTERNAL_H
6+
37
#include <ydb/public/lib/ydb_cli/common/normalize_path.h>
48
#include <ydb/public/lib/ydb_cli/common/pg_dump_parser.h>
59
#include <ydb/public/lib/ydb_cli/dump/dump.h>
@@ -94,7 +98,9 @@ int TCommandDump::Run(TConfig& config) {
9498
throw yexception() << "Incorrect consistency level. Available options: \"database\", \"table\"" << Endl;
9599
}
96100

97-
NYdb::SetVerbosity(config.IsVerbose());
101+
auto log = std::make_shared<TLog>(CreateLogBackend("cerr", TConfig::VerbosityLevelToELogPriority(config.VerbosityLevel)));
102+
log->SetFormatter(GetPrefixLogFormatter(""));
103+
NYdb::NBackup::SetLog(std::move(log));
98104

99105
try {
100106
TString relPath = NYdb::RelPathFromAbsolute(config.Database, Path);
@@ -196,8 +202,6 @@ void TCommandRestore::Parse(TConfig& config) {
196202
}
197203

198204
int TCommandRestore::Run(TConfig& config) {
199-
NYdb::SetVerbosity(config.IsVerbose());
200-
201205
auto settings = NDump::TRestoreSettings()
202206
.DryRun(IsDryRun)
203207
.RestoreData(RestoreData)
@@ -234,7 +238,10 @@ int TCommandRestore::Run(TConfig& config) {
234238
settings.Mode(NDump::TRestoreSettings::EMode::ImportData);
235239
}
236240

237-
NDump::TClient client(CreateDriver(config));
241+
auto log = std::make_shared<TLog>(CreateLogBackend("cerr", TConfig::VerbosityLevelToELogPriority(config.VerbosityLevel)));
242+
log->SetFormatter(GetPrefixLogFormatter(""));
243+
244+
NDump::TClient client(CreateDriver(config), std::move(log));
238245
ThrowOnError(client.Restore(FilePath, Path, settings));
239246

240247
return EXIT_SUCCESS;

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <ydb/public/sdk/cpp/client/ydb_scheme/scheme.h>
88
#include <ydb/public/sdk/cpp/client/ydb_table/table.h>
99

10+
#include <library/cpp/logger/log.h>
11+
1012
#include <util/string/printf.h>
1113

1214
namespace NYdb {
@@ -23,8 +25,9 @@ TString DataFileName(ui32 id) {
2325

2426
class TClient::TImpl {
2527
public:
26-
explicit TImpl(const TDriver& driver)
27-
: ImportClient(driver)
28+
explicit TImpl(const TDriver& driver, std::shared_ptr<TLog>&& log)
29+
: Log(log)
30+
, ImportClient(driver)
2831
, OperationClient(driver)
2932
, SchemeClient(driver)
3033
, TableClient(driver)
@@ -37,11 +40,12 @@ class TClient::TImpl {
3740
}
3841

3942
TRestoreResult Restore(const TString& fsPath, const TString& dbPath, const TRestoreSettings& settings) {
40-
auto client = TRestoreClient(ImportClient, OperationClient, SchemeClient, TableClient);
43+
auto client = TRestoreClient(*Log, ImportClient, OperationClient, SchemeClient, TableClient);
4144
return client.Restore(fsPath, dbPath, settings);
4245
}
4346

4447
private:
48+
std::shared_ptr<TLog> Log;
4549
NImport::TImportClient ImportClient;
4650
NOperation::TOperationClient OperationClient;
4751
NScheme::TSchemeClient SchemeClient;
@@ -60,7 +64,12 @@ TRestoreResult::TRestoreResult(TStatus&& status)
6064
}
6165

6266
TClient::TClient(const TDriver& driver)
63-
: Impl_(new TImpl(driver))
67+
: Impl_(new TImpl(driver, std::make_shared<TLog>(CreateLogBackend("cerr"))))
68+
{
69+
}
70+
71+
TClient::TClient(const TDriver& driver, std::shared_ptr<TLog>&& log)
72+
: Impl_(new TImpl(driver, std::move(log)))
6473
{
6574
}
6675

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

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <util/generic/size_literals.h>
88

9+
class TLog;
10+
911
namespace NYdb {
1012
namespace NDump {
1113

@@ -92,6 +94,7 @@ class TClient {
9294

9395
public:
9496
explicit TClient(const TDriver& driver);
97+
explicit TClient(const TDriver& driver, std::shared_ptr<TLog>&& log);
9598

9699
TDumpResult Dump(const TString& dbPath, const TString& fsPath, const TDumpSettings& settings = {});
97100
TRestoreResult Restore(const TString& fsPath, const TString& dbPath, const TRestoreSettings& settings = {});

0 commit comments

Comments
 (0)