Skip to content

Commit 82ae4fe

Browse files
authored
Add TablePathPrefix. (#16894)
1 parent 664ac4a commit 82ae4fe

File tree

8 files changed

+23
-11
lines changed

8 files changed

+23
-11
lines changed

ydb/apps/etcd_proxy/proxy.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int TProxy::Discovery() {
6161
}
6262

6363
int TProxy::StartServer() {
64-
if (const auto res = Stuff->Client->ExecuteQuery(NEtcd::GetLastRevisionSQL(), NYdb::NQuery::TTxControl::NoTx()).ExtractValueSync(); res.IsSuccess()) {
64+
if (const auto res = Stuff->Client->ExecuteQuery(NEtcd::GetLastRevisionSQL(Stuff->TablePrefix), NYdb::NQuery::TTxControl::NoTx()).ExtractValueSync(); res.IsSuccess()) {
6565
if (auto result = res.GetResultSetParser(0); result.TryNextRow()) {
6666
Stuff->Revision.store(NYdb::TValueParser(result.GetValue(0)).GetInt64());
6767
} else {
@@ -129,7 +129,7 @@ int TProxy::Run() {
129129
}
130130

131131
int TProxy::InitDatabase() {
132-
if (const auto res = Stuff->Client->ExecuteQuery(NEtcd::GetCreateTablesSQL(), NYdb::NQuery::TTxControl::NoTx()).ExtractValueSync(); res.IsSuccess()) {
132+
if (const auto res = Stuff->Client->ExecuteQuery(NEtcd::GetCreateTablesSQL(Stuff->TablePrefix), NYdb::NQuery::TTxControl::NoTx()).ExtractValueSync(); res.IsSuccess()) {
133133
std::cout << "Database " << Database << " on " << Endpoint << " was initialized." << std::endl;
134134
return 0;
135135
} else {
@@ -210,7 +210,7 @@ int TProxy::ImportDatabase() {
210210
const auto driver = NYdb::TDriver(config);
211211
auto client = NYdb::NTable::TTableClient(driver);
212212

213-
if (const auto res = client.BulkUpsert(Database + "/content", std::move(value)).ExtractValueSync(); !res.IsSuccess()) {
213+
if (const auto res = client.BulkUpsert(Database + Folder + "/content", std::move(value)).ExtractValueSync(); !res.IsSuccess()) {
214214
std::cout << res.GetIssues().ToString() << std::endl;
215215
return 1;
216216
}
@@ -235,6 +235,7 @@ TProxy::TProxy(int argc, char** argv)
235235

236236
opts.AddLongOption("database", "YDB etcd databse").Required().RequiredArgument("DATABASE").StoreResult(&Database);
237237
opts.AddLongOption("endpoint", "YDB endpoint to connect").Required().RequiredArgument("ENDPOINT").StoreResult(&Endpoint);
238+
opts.AddLongOption("folder", "YDB etcd root folder").Required().RequiredArgument("FOLDER").StoreResult(&Folder);
238239
opts.AddLongOption("token", "YDB token for connection").Optional().RequiredArgument("TOKEN").StoreResult(&Token);
239240
opts.AddLongOption("ydbca", "YDB CA for connection").Optional().RequiredArgument("CA").StoreResult(&CA);
240241

@@ -256,6 +257,12 @@ TProxy::TProxy(int argc, char** argv)
256257
LockAllMemory(LockCurrentMemory);
257258
}
258259

260+
if (!Folder.empty()) {
261+
std::ostringstream prefix;
262+
prefix << "pragma TablePathPrefix = '" << Database << Folder << "';" << std::endl;
263+
Stuff->TablePrefix = prefix.str();
264+
}
265+
259266
THolder<NActors::TActorSystemSetup> actorSystemSetup = BuildActorSystemSetup();
260267

261268
TIntrusivePtr<NActors::NLog::TSettings> loggerSettings = BuildLoggerSettings();

ydb/apps/etcd_proxy/proxy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class TProxy {
4545
std::unique_ptr<NYdbGrpc::TGRpcServer> GRpcServer;
4646

4747
bool Initialize_ = false;
48-
std::string Database, Endpoint, Token, CA;
48+
std::string Database, Endpoint, Token, CA, Folder;
4949
ui16 ListeningPort = 2379U;
5050
std::string Root, Cert, Key;
5151
std::string ImportFrom_, ImportPrefix_;

ydb/apps/etcd_proxy/service/etcd_base_init.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace NEtcd {
66

7-
std::string GetCreateTablesSQL() {
8-
return NResource::Find("create.sql"sv);
7+
std::string GetCreateTablesSQL(const std::string& prefix) {
8+
return prefix + NResource::Find("create.sql"sv);
99
}
1010

11-
std::string GetLastRevisionSQL() {
12-
return "select nvl(max(`modified`), 1L) from `content`; select nvl(max(`id`), 1L) from `leases`;";
11+
std::string GetLastRevisionSQL(const std::string& prefix) {
12+
return prefix + "select nvl(max(`modified`), 1L) from `content`; select nvl(max(`id`), 1L) from `leases`;";
1313
}
1414

1515
}

ydb/apps/etcd_proxy/service/etcd_base_init.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace NEtcd {
66

7-
std::string GetCreateTablesSQL();
7+
std::string GetCreateTablesSQL(const std::string& prefix);
88

9-
std::string GetLastRevisionSQL();
9+
std::string GetLastRevisionSQL(const std::string& prefix);
1010

1111
}
1212

ydb/apps/etcd_proxy/service/etcd_impl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ class TEtcdRequestGrpc
816816
std::ostringstream sql;
817817
NYdb::TParamsBuilder params;
818818
sql << "-- " << GetRequestName() << " >>>>" << std::endl;
819+
sql << Stuff->TablePrefix;
819820
this->MakeQueryWithParams(sql, params);
820821
sql << "-- " << GetRequestName() << " <<<<" << std::endl;
821822
// std::cout << std::endl << sql.view() << std::endl;

ydb/apps/etcd_proxy/service/etcd_shared.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct TSharedStuff {
1717
std::unique_ptr<NYdb::NQuery::TQueryClient> Client;
1818
std::atomic<i64> Revision = 0LL, Lease = 0LL;
1919
NActors::TActorId Watchtower;
20+
std::string TablePrefix;
2021
};
2122

2223
std::string IncrementKey(std::string key);

ydb/apps/etcd_proxy/service/etcd_watch.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class TKeysKeeper : public TActorBootstrapped<TKeysKeeper> {
6565
const auto& leasePraramName = AddParam<i64>("Lease", params, ev->Get()->Record.id());
6666

6767
std::ostringstream sql;
68+
sql << Stuff->TablePrefix;
6869
sql << "update `leases` set `updated` = CurrentUtcDatetime(`id`) where " << leasePraramName << " = `id`;" << std::endl;
6970
sql << "select `id`, `ttl` - unwrap(cast(CurrentUtcDatetime(`id`) - `updated` as Int64) / 1000000L) as `granted` from `leases` where " << leasePraramName << " = `id`;" << std::endl;
7071

@@ -165,6 +166,7 @@ class TWatch : public TActorBootstrapped<TWatch> {
165166
MakeSimplePredicate(Key, RangeEnd, where, params);
166167

167168
std::ostringstream sql;
169+
sql << Stuff->TablePrefix;
168170
if (WithPrevious) {
169171
sql << "select * from (select max_by(TableRow(), `modified`) from `content` where " << revName << " > `modified` and " << where.view() << " group by `key`) flatten columns union all" << std::endl;
170172
}
@@ -586,6 +588,7 @@ class TWatchtower : public TActorBootstrapped<TWatchtower> {
586588
Revision = Stuff->Revision.fetch_add(1LL) + 1LL;
587589

588590
std::ostringstream sql;
591+
sql << Stuff->TablePrefix;
589592
NYdb::TParamsBuilder params;
590593
const auto& revName = AddParam("Revision", params, Revision);
591594

ydb/apps/etcd_proxy/service/ut/etcd_service_ut.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void MakeTables(auto &channel) {
105105
const auto stub = Ydb::Query::V1::QueryService::NewStub(channel);
106106
Ydb::Query::ExecuteQueryRequest request;
107107
request.set_exec_mode(Ydb::Query::EXEC_MODE_EXECUTE);
108-
request.mutable_query_content()->set_text(std::string("PRAGMA TablePathPrefix='/Root';\n") + NEtcd::GetCreateTablesSQL());
108+
request.mutable_query_content()->set_text(NEtcd::GetCreateTablesSQL(std::string("PRAGMA TablePathPrefix='/Root';\n")));
109109

110110
grpc::ClientContext executeCtx;
111111
Ydb::Query::ExecuteQueryResponsePart response;

0 commit comments

Comments
 (0)