Skip to content

Add config node init command #14869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ydb/core/blobstorage/nodewarden/node_warden_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,11 @@ void TNodeWarden::PersistConfig(std::optional<TString> mainYaml, ui64 mainYamlVe
TFileOutput tempFile(tempPath);
tempFile << yaml;
tempFile.Flush();
if (Chmod(tempPath.c_str(), S_IRUSR | S_IRGRP | S_IROTH) != 0) {
STLOG(PRI_ERROR, BS_NODE, NW92, "Failed to set permissions for temporary file", (Error, LastSystemErrorText()));
success = false;
return false;
}
}

if (!NFs::Rename(tempPath, configPath)) {
Expand Down
1 change: 1 addition & 0 deletions ydb/public/lib/ydb_cli/commands/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SRCS(
ydb_debug.cpp
ydb_dynamic_config.cpp
ydb_latency.cpp
ydb_node_config.cpp
ydb_ping.cpp
ydb_profile.cpp
ydb_root_common.cpp
Expand Down
5 changes: 4 additions & 1 deletion ydb/public/lib/ydb_cli/commands/ydb_admin.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ydb_admin.h"

#include "ydb_dynamic_config.h"
#include "ydb_node_config.h"
#include "ydb_storage_config.h"
#include "ydb_cluster.h"

Expand All @@ -20,7 +21,9 @@ class TCommandNode : public TClientCommandTree {
public:
TCommandNode()
: TClientCommandTree("node", {}, "Node-wide administration")
{}
{
AddCommand(std::make_unique<NNodeConfig::TCommandNodeConfig>());
}
};

class TCommandDatabase : public TClientCommandTree {
Expand Down
131 changes: 131 additions & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_node_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "ydb_node_config.h"
#include <util/system/fs.h>
#include <util/folder/dirut.h>
#include <ydb-cpp-sdk/client/config/config.h>

namespace NYdb::NConsoleClient::NNodeConfig {

constexpr const char* CONFIG_FILE_NAME = "config.yaml";
constexpr const char* STORAGE_CONFIG_FILE_NAME = "storage.yaml";

TCommandNodeConfig::TCommandNodeConfig()
: TClientCommandTree("config", {}, "Node-wide configuration")
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{
{

AddCommand(std::make_unique<TCommandNodeConfigInit>());
}

TCommandNodeConfigInit::TCommandNodeConfigInit()
: TYdbCommand("init", {}, "Initialize node configuration")
{
}

void TCommandNodeConfigInit::PropagateFlags(const TCommandFlags& flags) {
TYdbCommand::PropagateFlags(flags);
Dangerous = false;
}

void TCommandNodeConfigInit::Config(TConfig& config) {
TYdbCommand::Config(config);
config.Opts->AddLongOption('f', "from-config", "Path to the initial configuration file.")
.RequiredArgument("[config.yaml]").StoreResult(&ConfigYamlPath);
config.Opts->AddLongOption('d', "config-dir", "Path to the directory for storing configuration files.")
.Required().RequiredArgument("[directory]").StoreResult(&ConfigDirPath);
config.Opts->AddLongOption('s', "seed-node", "Endpoint of the seed node from which the configuration will be fetched.")
.RequiredArgument("[HOST:PORT]").StoreResult(&SeedNodeEndpoint);
config.Opts->MutuallyExclusive("from-config", "seed-node");
config.SetFreeArgsNum(0);
config.AllowEmptyDatabase = true;
config.AllowEmptyAddress = true;
}

bool TCommandNodeConfigInit::SaveConfig(const TString& config, const TString& configName, const TString& configDirPath) {
try {
TString tempPath = TStringBuilder() << configDirPath << "/temp_" << configName;
TString configPath = TStringBuilder() << configDirPath << "/" << configName;

{
TFileOutput tempFile(tempPath);
tempFile << config;
tempFile.Flush();

if (Chmod(tempPath.c_str(), S_IRUSR | S_IRGRP | S_IROTH) != 0) {
return false;
}
}

return NFs::Rename(tempPath, configPath);
} catch (const std::exception& e) {
return false;
}
}

int TCommandNodeConfigInit::Run(TConfig& config) {
MakePathIfNotExist(ConfigDirPath.c_str());
TString configYaml;

if (SeedNodeEndpoint) {
config.Address = SeedNodeEndpoint;
auto driver = std::make_unique<NYdb::TDriver>(CreateDriver(config));
auto client = NYdb::NConfig::TConfigClient(*driver);

auto result = client.FetchAllConfigs().GetValueSync();
NStatusHelpers::ThrowOnErrorOrPrintIssues(result);
TString clusterConfig;
TString storageConfig;
for (const auto& entry : result.GetConfigs()) {
std::visit([&](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, NYdb::NConfig::TMainConfigIdentity>) {
clusterConfig = entry.Config;
} else if constexpr (std::is_same_v<T, NYdb::NConfig::TStorageConfigIdentity>) {
storageConfig = entry.Config;
}
}, entry.Identity);
}
bool hasCluster = !clusterConfig.empty();
bool hasStorage = !storageConfig.empty();
bool clusterSaved = false;
bool storageSaved = false;

if (hasCluster && hasStorage) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

На мой взгляд ниже много boilerplate кода, по куче раз конфиги сохраняем. Лучше в как-то в одном месте сделать, и потом писать вывод на основе того, что пытались сохранить, что получилось сохранить.

Но тут скорее на твое усмотрение и на усмотрение @pnv1

clusterSaved = SaveConfig(clusterConfig, CONFIG_FILE_NAME, ConfigDirPath);
storageSaved = SaveConfig(storageConfig, STORAGE_CONFIG_FILE_NAME, ConfigDirPath);

if (clusterSaved && storageSaved) {
Cout << "Initialized main and storage configs in " << ConfigDirPath << "/"
<< CONFIG_FILE_NAME << " and " << STORAGE_CONFIG_FILE_NAME << Endl;
return EXIT_SUCCESS;
}
Cerr << "Failed to save configs: "
<< (clusterSaved ? "" : "main config ")
<< (storageSaved ? "and " : "storage config")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А что если clusterSaved = false, а storageSaved = true

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Все равно должна выдаваться ошибка. То что там теперь лежит бесполезный файл storage не должно никак повлиять, так как это файлы только для системы

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я про текст ошибки:
"Failed to save configs: main config and "

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Понял, поторопился)

<< Endl;
return EXIT_FAILURE;
}

if (hasCluster) {
clusterSaved = SaveConfig(clusterConfig, CONFIG_FILE_NAME, ConfigDirPath);
if (clusterSaved) {
Cout << "Initialized config in " << ConfigDirPath << "/" << CONFIG_FILE_NAME << Endl;
return EXIT_SUCCESS;
}
Cerr << "Failed to save config to " << ConfigDirPath << Endl;
return EXIT_FAILURE;
}

Cerr << "No main config found" << Endl;
return EXIT_FAILURE;
}

configYaml = TFileInput(ConfigYamlPath).ReadAll();
if (SaveConfig(configYaml, CONFIG_FILE_NAME, ConfigDirPath)){
Cout << "Initialized cluster config in " << ConfigDirPath << "/" << CONFIG_FILE_NAME << Endl;
return EXIT_SUCCESS;
} else {
Cout << "Failed to initialize cluster config in " << ConfigDirPath << "/" << CONFIG_FILE_NAME << Endl;
return EXIT_FAILURE;
}

}

} // namespace NYdb::NConsoleClient::NNodeConfig
25 changes: 25 additions & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_node_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "ydb_command.h"

namespace NYdb::NConsoleClient::NNodeConfig {

class TCommandNodeConfig : public TClientCommandTree {
public:
TCommandNodeConfig();
};

class TCommandNodeConfigInit : public TYdbCommand {
public:
TCommandNodeConfigInit();
void PropagateFlags(const TCommandFlags& flags) override;
void Config(TConfig& config) override;
int Run(TConfig& config) override;
bool SaveConfig(const TString& config, const TString& configName, const TString& configDirPath);
private:
TString ConfigYamlPath;
TString ConfigDirPath;
TString SeedNodeEndpoint;
};

} // namespace NYdb::NConsoleClient::NNodeConfig
Loading