Skip to content

Commit

Permalink
kesus resource: restore
Browse files Browse the repository at this point in the history
  • Loading branch information
jepett0 committed Feb 7, 2025
1 parent 235bd02 commit 4cddcb7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
66 changes: 66 additions & 0 deletions ydb/public/lib/ydb_cli/dump/restore_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "restore_import_data.h"
#include "restore_compat.h"

#include <ydb/public/api/protos/ydb_rate_limiter.pb.h>
#include <ydb/public/api/protos/ydb_table.pb.h>
#include <ydb/public/lib/ydb_cli/common/recursive_list.h>
#include <ydb/public/lib/ydb_cli/common/recursive_remove.h>
Expand Down Expand Up @@ -70,6 +71,10 @@ Ydb::Coordination::CreateNodeRequest ReadCoordinationNodeCreationRequest(const T
return ReadProtoFromFile<Ydb::Coordination::CreateNodeRequest>(fsDirPath, log, NDump::NFiles::CreateCoordinationNode());
}

Ydb::RateLimiter::CreateResourceRequest ReadResourceCreationRequest(const TFsPath& fsDirPath, const TLog* log) {
return ReadProtoFromFile<Ydb::RateLimiter::CreateResourceRequest>(fsDirPath, log, NDump::NFiles::CreateResource());
}

Ydb::Scheme::ModifyPermissionsRequest ReadPermissions(const TFsPath& fsDirPath, const TLog* log) {
return ReadProtoFromFile<Ydb::Scheme::ModifyPermissionsRequest>(fsDirPath, log, NFiles::Permissions());
}
Expand Down Expand Up @@ -181,6 +186,22 @@ TStatus CreateCoordinationNode(
return result;
}

TStatus CreateResource(
NRateLimiter::TRateLimiterClient& client,
const std::string& coordinationNodePath,
const std::string& resourcePath,
const Ydb::RateLimiter::CreateResourceRequest& request)
{
// to do: use the request
// const auto settings = NRateLimiter::TCreateResourceSettings(request);
Y_UNUSED(request);
NRateLimiter::TCreateResourceSettings settings;
auto result = RetryFunction([&]() {
return client.CreateResource(coordinationNodePath, resourcePath, settings).ExtractValueSync();
});
return result;
}

} // anonymous

namespace NPrivate {
Expand Down Expand Up @@ -245,6 +266,7 @@ TRestoreClient::TRestoreClient(const TDriver& driver, const std::shared_ptr<TLog
, TableClient(driver)
, TopicClient(driver)
, CoordinationNodeClient(driver)
, RateLimiterClient(driver)
, QueryClient(driver)
, Log(log)
{
Expand Down Expand Up @@ -549,6 +571,46 @@ TRestoreResult TRestoreClient::RestoreTopic(
return Result<TRestoreResult>(dbPath, std::move(result));
}

TRestoreResult TRestoreClient::RestoreResource(
const TFsPath& fsPath,
const TString& coordinationNodePath,
const TString& resourcePath)
{
LOG_D("Process " << fsPath.GetPath().Quote());

if (auto error = ErrorOnIncomplete(fsPath)) {
return *error;
}

const auto creationRequest = ReadResourceCreationRequest(fsPath, Log.get());
auto result = CreateResource(RateLimiterClient, coordinationNodePath, resourcePath, creationRequest);

const auto absolutePathToResource = JoinFsPaths(coordinationNodePath, resourcePath);
if (result.IsSuccess()) {
LOG_D("Created " << absolutePathToResource.Quote());
return Result<TRestoreResult>();
}

LOG_E("Failed to create " << absolutePathToResource.Quote());
return Result<TRestoreResult>(absolutePathToResource, std::move(result));
}

TRestoreResult TRestoreClient::RestoreDependentResources(const TFsPath& fsPath, const TString& dbPath) {
LOG_I("Restore coordination node's resources " << fsPath.GetPath().Quote() << " to " << dbPath.Quote());

TVector<TFsPath> resources;
fsPath.List(resources);
for (const auto& resourceFsPath : resources) {
if (IsFileExists(resourceFsPath.Child(NFiles::CreateResource().FileName))) {
auto result = RestoreResource(resourceFsPath, dbPath, resourceFsPath.RelativeTo(fsPath).GetPath());
if (!result.IsSuccess()) {
return result;
}
}
}
return Result<TRestoreResult>();
}

TRestoreResult TRestoreClient::RestoreCoordinationNode(
const TFsPath& fsPath,
const TString& dbPath,
Expand All @@ -570,6 +632,10 @@ TRestoreResult TRestoreClient::RestoreCoordinationNode(
const auto creationRequest = ReadCoordinationNodeCreationRequest(fsPath, Log.get());
auto result = CreateCoordinationNode(CoordinationNodeClient, dbPath, creationRequest);
if (result.IsSuccess()) {
if (auto result = RestoreDependentResources(fsPath, dbPath); !result.IsSuccess()) {
LOG_E("Failed to create coordination node's resources " << dbPath.Quote());
return Result<TRestoreResult>(dbPath, std::move(result));
}
LOG_D("Created " << dbPath.Quote());
return RestorePermissions(fsPath, dbPath, settings, isAlreadyExisting);
}
Expand Down
4 changes: 4 additions & 0 deletions ydb/public/lib/ydb_cli/dump/restore_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <ydb-cpp-sdk/client/import/import.h>
#include <ydb-cpp-sdk/client/operation/operation.h>
#include <ydb-cpp-sdk/client/query/client.h>
#include <ydb-cpp-sdk/client/rate_limiter/rate_limiter.h>
#include <ydb-cpp-sdk/client/scheme/scheme.h>
#include <ydb-cpp-sdk/client/table/table.h>
#include <ydb-cpp-sdk/client/topic/client.h>
Expand Down Expand Up @@ -131,6 +132,8 @@ class TRestoreClient {
TRestoreResult RestoreView(const TFsPath& fsPath, const TString& dbRestoreRoot, const TString& dbPathRelativeToRestoreRoot, const TRestoreSettings& settings, bool isAlreadyExisting);
TRestoreResult RestoreTopic(const TFsPath& fsPath, const TString& dbPath, const TRestoreSettings& settings, bool isAlreadyExisting);
TRestoreResult RestoreCoordinationNode(const TFsPath& fsPath, const TString& dbPath, const TRestoreSettings& settings, bool isAlreadyExisting);
TRestoreResult RestoreDependentResources(const TFsPath& fsPath, const TString& dbPath);
TRestoreResult RestoreResource(const TFsPath& fsPath, const TString& coordinationNodePath, const TString& resourcePath);

TRestoreResult CheckSchema(const TString& dbPath, const NTable::TTableDescription& desc);
TRestoreResult RestoreData(const TFsPath& fsPath, const TString& dbPath, const TRestoreSettings& settings, const NTable::TTableDescription& desc);
Expand All @@ -157,6 +160,7 @@ class TRestoreClient {
NTable::TTableClient TableClient;
NTopic::TTopicClient TopicClient;
NCoordination::TClient CoordinationNodeClient;
NRateLimiter::TRateLimiterClient RateLimiterClient;
NQuery::TQueryClient QueryClient;
std::shared_ptr<TLog> Log;

Expand Down

0 comments on commit 4cddcb7

Please sign in to comment.