-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ydb cli replace and fetch storage config commands (#12312)
- Loading branch information
Showing
8 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include "ydb_storage_config.h" | ||
|
||
#include <ydb/public/sdk/cpp/client/ydb_bsconfig/ydb_storage_config.h> | ||
#include <ydb/library/yaml_config/public/yaml_config.h> | ||
|
||
#include <openssl/sha.h> | ||
|
||
#include <util/folder/path.h> | ||
#include <util/string/hex.h> | ||
|
||
using namespace NKikimr; | ||
|
||
namespace NYdb::NConsoleClient::NStorageConfig { | ||
|
||
TString WrapYaml(const TString& yaml) { | ||
auto doc = NFyaml::TDocument::Parse(yaml); | ||
|
||
TStringStream out; | ||
out << (doc.HasExplicitDocumentStart() ? "" : "---\n") | ||
<< doc << (yaml[yaml.size() - 1] != '\n' ? "\n" : ""); | ||
|
||
return out.Str(); | ||
} | ||
|
||
TCommandStorageConfig::TCommandStorageConfig() | ||
: TClientCommandTree("storage", {}, "Storage config") | ||
{ | ||
AddCommand(std::make_unique<TCommandStorageConfigFetch>()); | ||
AddCommand(std::make_unique<TCommandStorageConfigReplace>()); | ||
} | ||
|
||
TCommandStorageConfigFetch::TCommandStorageConfigFetch() | ||
: TYdbCommand("fetch", {}, "Fetch storage config") | ||
{ | ||
} | ||
|
||
void TCommandStorageConfigFetch::Config(TConfig& config) { | ||
TYdbCommand::Config(config); | ||
config.SetFreeArgsNum(0); | ||
} | ||
|
||
void TCommandStorageConfigFetch::Parse(TConfig& config) { | ||
TClientCommand::Parse(config); | ||
} | ||
|
||
int TCommandStorageConfigFetch::Run(TConfig& config) { | ||
auto driver = std::make_unique<NYdb::TDriver>(CreateDriver(config)); | ||
auto client = NYdb::NStorageConfig::TStorageConfigClient(*driver); | ||
auto result = client.FetchStorageConfig().GetValueSync(); | ||
ThrowOnError(result); | ||
auto cfg = result.GetConfig(); | ||
|
||
if (!cfg) { | ||
Cerr << "YAML config is absent on this cluster." << Endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
Cout << WrapYaml(cfg); | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
|
||
TCommandStorageConfigReplace::TCommandStorageConfigReplace() | ||
: TYdbCommand("replace", {}, "Replace storage config") | ||
{ | ||
} | ||
|
||
void TCommandStorageConfigReplace::Config(TConfig& config) { | ||
TYdbCommand::Config(config); | ||
config.Opts->AddLongOption('f', "filename", "Filename of the file containing configuration") | ||
.Required().RequiredArgument("[config.yaml]").StoreResult(&Filename); | ||
config.SetFreeArgsNum(0); | ||
} | ||
|
||
void TCommandStorageConfigReplace::Parse(TConfig& config) { | ||
TClientCommand::Parse(config); | ||
|
||
if (Filename == "") { | ||
ythrow yexception() << "Must specify non-empty -f (--filename)"; | ||
} | ||
|
||
const auto configStr = Filename == "-" ? Cin.ReadAll() : TFileInput(Filename).ReadAll(); | ||
|
||
Cout << "Config: " << configStr << Endl; | ||
|
||
StorageConfig = configStr; | ||
} | ||
|
||
int TCommandStorageConfigReplace::Run(TConfig& config) { | ||
std::unique_ptr<NYdb::TDriver> driver = std::make_unique<NYdb::TDriver>(CreateDriver(config)); | ||
auto client = NYdb::NStorageConfig::TStorageConfigClient(*driver); | ||
auto exec = [&]() { | ||
return client.ReplaceStorageConfig(StorageConfig).GetValueSync(); | ||
}; | ||
auto status = exec(); | ||
ThrowOnError(status); | ||
|
||
if (!status.GetIssues()) { | ||
Cout << status << Endl; | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include "ydb_command.h" | ||
#include "ydb_common.h" | ||
|
||
#include <util/generic/set.h> | ||
|
||
namespace NYdb::NConsoleClient::NStorageConfig { | ||
|
||
class TCommandStorageConfig : public TClientCommandTree { | ||
public: | ||
TCommandStorageConfig(); | ||
}; | ||
|
||
class TCommandStorageConfigReplace : public TYdbCommand { | ||
public: | ||
TCommandStorageConfigReplace(); | ||
void Config(TConfig& config) override; | ||
void Parse(TConfig& config) override; | ||
int Run(TConfig& config) override; | ||
|
||
private: | ||
TString StorageConfig; | ||
TString Filename; | ||
}; | ||
|
||
class TCommandStorageConfigFetch : public TYdbCommand { | ||
public: | ||
TCommandStorageConfigFetch(); | ||
void Config(TConfig&) override; | ||
void Parse(TConfig&) override; | ||
int Run(TConfig& config) override; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
LIBRARY() | ||
|
||
SRCS( | ||
ydb_storage_config.cpp | ||
) | ||
|
||
PEERDIR( | ||
ydb/public/api/grpc | ||
ydb/public/sdk/cpp/client/ydb_table | ||
ydb/public/sdk/cpp/client/ydb_types/operation | ||
ydb/public/sdk/cpp/client/ydb_value | ||
) | ||
|
||
END() |
65 changes: 65 additions & 0 deletions
65
ydb/public/sdk/cpp/client/ydb_bsconfig/ydb_storage_config.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "ydb_storage_config.h" | ||
|
||
#include <ydb/public/sdk/cpp/client/ydb_common_client/impl/client.h> | ||
#include <ydb/public/sdk/cpp/client/impl/ydb_internal/make_request/make.h> | ||
|
||
namespace NYdb::NStorageConfig { | ||
|
||
class TStorageConfigClient::TImpl : public TClientImplCommon<TStorageConfigClient::TImpl> { | ||
public: | ||
TImpl(std::shared_ptr<TGRpcConnectionsImpl> connections, const TCommonClientSettings& settings) | ||
: TClientImplCommon(std::move(connections), settings) | ||
{ | ||
} | ||
|
||
TAsyncStatus ReplaceStorageConfig(const TString& config) { | ||
auto request = MakeRequest<Ydb::BSConfig::ReplaceStorageConfigRequest>(); | ||
request.set_yaml_config(config); | ||
|
||
return RunSimple<Ydb::BSConfig::V1::BSConfigService, Ydb::BSConfig::ReplaceStorageConfigRequest, Ydb::BSConfig::ReplaceStorageConfigResponse>( | ||
std::move(request), | ||
&Ydb::BSConfig::V1::BSConfigService::Stub::AsyncReplaceStorageConfig); | ||
} | ||
|
||
TAsyncFetchStorageConfigResult FetchStorageConfig(const TStorageConfigSettings& settings = {}) { | ||
auto request = MakeOperationRequest<Ydb::BSConfig::FetchStorageConfigRequest>(settings); | ||
auto promise = NThreading::NewPromise<TFetchStorageConfigResult>(); | ||
|
||
auto extractor = [promise] (google::protobuf::Any* any, TPlainStatus status) mutable { | ||
TString config; | ||
if (Ydb::BSConfig::FetchStorageConfigResult result; any && any->UnpackTo(&result)) { | ||
config = result.yaml_config(); | ||
} | ||
|
||
TFetchStorageConfigResult val(TStatus(std::move(status)), std::move(config)); | ||
promise.SetValue(std::move(val)); | ||
}; | ||
|
||
Connections_->RunDeferred<Ydb::BSConfig::V1::BSConfigService, Ydb::BSConfig::FetchStorageConfigRequest, Ydb::BSConfig::FetchStorageConfigResponse>( | ||
std::move(request), | ||
extractor, | ||
&Ydb::BSConfig::V1::BSConfigService::Stub::AsyncFetchStorageConfig, | ||
DbDriverState_, | ||
INITIAL_DEFERRED_CALL_DELAY, | ||
TRpcRequestSettings::Make(settings)); | ||
return promise.GetFuture(); | ||
} | ||
|
||
}; | ||
|
||
TStorageConfigClient::TStorageConfigClient(const TDriver& driver, const TCommonClientSettings& settings) | ||
: Impl_(new TStorageConfigClient::TImpl(CreateInternalInterface(driver), settings)) | ||
{} | ||
|
||
TStorageConfigClient::~TStorageConfigClient() = default; | ||
|
||
TAsyncStatus TStorageConfigClient::ReplaceStorageConfig(const TString& config) { | ||
return Impl_->ReplaceStorageConfig(config); | ||
} | ||
|
||
TAsyncFetchStorageConfigResult TStorageConfigClient::FetchStorageConfig(const TStorageConfigSettings& settings) { | ||
return Impl_->FetchStorageConfig(settings); | ||
} | ||
|
||
|
||
} |
55 changes: 55 additions & 0 deletions
55
ydb/public/sdk/cpp/client/ydb_bsconfig/ydb_storage_config.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#pragma once | ||
|
||
#include <ydb/public/api/grpc/ydb_bsconfig_v1.grpc.pb.h> | ||
#include <ydb/public/sdk/cpp/client/ydb_types/ydb.h> | ||
#include <ydb/public/sdk/cpp/client/ydb_types/status/status.h> | ||
#include <ydb/public/sdk/cpp/client/ydb_common_client/settings.h> | ||
#include <ydb/public/sdk/cpp/client/ydb_types/request_settings.h> | ||
#include <ydb/public/sdk/cpp/client/ydb_driver/driver.h> | ||
|
||
#include <util/generic/string.h> | ||
|
||
#include <memory> | ||
|
||
namespace NYdb::NStorageConfig { | ||
|
||
struct TFetchStorageConfigResult : public TStatus { | ||
TFetchStorageConfigResult( | ||
TStatus&& status, | ||
TString&& config) | ||
: TStatus(std::move(status)) | ||
, Config_(std::move(config)) | ||
{} | ||
|
||
const TString& GetConfig() const { | ||
return Config_; | ||
} | ||
|
||
private: | ||
TString Config_; | ||
}; | ||
|
||
using TAsyncFetchStorageConfigResult = NThreading::TFuture<TFetchStorageConfigResult>; | ||
|
||
struct TStorageConfigSettings : public NYdb::TOperationRequestSettings<TStorageConfigSettings> {}; | ||
|
||
class TStorageConfigClient { | ||
public: | ||
|
||
explicit TStorageConfigClient(const TDriver& driver, const TCommonClientSettings& settings = {}); | ||
|
||
~TStorageConfigClient(); | ||
|
||
// Replace config | ||
TAsyncStatus ReplaceStorageConfig(const TString& config); | ||
|
||
// Fetch current cluster storage config | ||
TAsyncFetchStorageConfigResult FetchStorageConfig(const TStorageConfigSettings& settings = {}); | ||
|
||
private: | ||
class TImpl; | ||
|
||
std::unique_ptr<TImpl> Impl_; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters