diff options
| author | Stefan Boberg <[email protected]> | 2025-10-15 09:51:52 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2025-10-15 09:51:52 +0200 |
| commit | e747932819e2a64a1396cfbc3422a9b61c9470dc (patch) | |
| tree | 1db1d13cf7fe873d71128a7879d269174f8eaf16 /src/zenserver/storage | |
| parent | refactor builds cmd part3 (#573) (diff) | |
| download | zen-e747932819e2a64a1396cfbc3422a9b61c9470dc.tar.xz zen-e747932819e2a64a1396cfbc3422a9b61c9470dc.zip | |
restructured zenserver configuration (#575)
this breaks out the configuration logic to allow multiple applications to share common configuration and initialization logic whilst customizing chosen aspects of the process
Diffstat (limited to 'src/zenserver/storage')
| -rw-r--r-- | src/zenserver/storage/admin/admin.cpp | 2 | ||||
| -rw-r--r-- | src/zenserver/storage/admin/admin.h | 18 | ||||
| -rw-r--r-- | src/zenserver/storage/storageconfig.cpp | 86 | ||||
| -rw-r--r-- | src/zenserver/storage/storageconfig.h | 50 | ||||
| -rw-r--r-- | src/zenserver/storage/zenstorageserver.cpp | 17 | ||||
| -rw-r--r-- | src/zenserver/storage/zenstorageserver.h | 19 |
6 files changed, 102 insertions, 90 deletions
diff --git a/src/zenserver/storage/admin/admin.cpp b/src/zenserver/storage/admin/admin.cpp index 4803063d7..68b91825d 100644 --- a/src/zenserver/storage/admin/admin.cpp +++ b/src/zenserver/storage/admin/admin.cpp @@ -102,7 +102,7 @@ HttpAdminService::HttpAdminService(GcScheduler& Scheduler, ZenCacheStore* CacheStore, std::function<void()>&& FlushFunction, const LogPaths& LogPaths, - const ZenServerOptions& ServerOptions) + const ZenServerConfig& ServerOptions) : m_GcScheduler(Scheduler) , m_BackgroundJobQueue(BackgroundJobQueue) , m_CacheStore(CacheStore) diff --git a/src/zenserver/storage/admin/admin.h b/src/zenserver/storage/admin/admin.h index 9a49f5120..ee3da4579 100644 --- a/src/zenserver/storage/admin/admin.h +++ b/src/zenserver/storage/admin/admin.h @@ -11,7 +11,7 @@ namespace zen { class GcScheduler; class JobQueue; class ZenCacheStore; -struct ZenServerOptions; +struct ZenServerConfig; class HttpAdminService : public zen::HttpService { @@ -27,20 +27,20 @@ public: ZenCacheStore* CacheStore, std::function<void()>&& FlushFunction, const LogPaths& LogPaths, - const ZenServerOptions& ServerOptions); + const ZenServerConfig& ServerOptions); ~HttpAdminService(); virtual const char* BaseUri() const override; virtual void HandleRequest(zen::HttpServerRequest& Request) override; private: - HttpRequestRouter m_Router; - GcScheduler& m_GcScheduler; - JobQueue& m_BackgroundJobQueue; - ZenCacheStore* m_CacheStore; - std::function<void()> m_FlushFunction; - LogPaths m_LogPaths; - const ZenServerOptions& m_ServerOptions; + HttpRequestRouter m_Router; + GcScheduler& m_GcScheduler; + JobQueue& m_BackgroundJobQueue; + ZenCacheStore* m_CacheStore; + std::function<void()> m_FlushFunction; + LogPaths m_LogPaths; + const ZenServerConfig& m_ServerOptions; }; } // namespace zen diff --git a/src/zenserver/storage/storageconfig.cpp b/src/zenserver/storage/storageconfig.cpp index 86bb09c21..61844140e 100644 --- a/src/zenserver/storage/storageconfig.cpp +++ b/src/zenserver/storage/storageconfig.cpp @@ -18,8 +18,10 @@ ZEN_THIRD_PARTY_INCLUDES_END namespace zen { void -ValidateOptions(ZenStorageServerOptions& ServerOptions) +ZenStorageServerConfigurator::ValidateOptions() { + auto& ServerOptions = m_ServerOptions; + if (ServerOptions.EncryptionKey.empty() == false) { const auto Key = AesKey256Bit::FromString(ServerOptions.EncryptionKey); @@ -363,18 +365,11 @@ MakeOption(std::vector<std::pair<std::string, ZenStructuredCacheBucketConfig>>& }; void -ParseConfigFile(const std::filesystem::path& Path, - ZenStorageServerOptions& ServerOptions, - const cxxopts::ParseResult& CmdLineResult, - std::string_view OutputConfigFile) +ZenStorageServerConfigurator::AddConfigOptions(LuaConfig::Options& LuaOptions) { - ZEN_TRACE_CPU("ParseConfigFile"); - using namespace std::literals; - LuaConfig::Options LuaOptions; - - AddServerConfigOptions(LuaOptions, ServerOptions); + auto& ServerOptions = m_ServerOptions; ////// server LuaOptions.AddOption("server.pluginsconfigfile"sv, ServerOptions.PluginsConfigFile, "plugins-config"sv); @@ -507,36 +502,50 @@ ParseConfigFile(const std::filesystem::path& Path, LuaOptions.AddOption("workspaces.allowconfigchanges"sv, ServerOptions.WorksSpacesConfig.AllowConfigurationChanges, "workspaces-allow-changes"sv); +} - LuaOptions.Parse(Path, CmdLineResult); +void +ZenStorageServerConfigurator::OnConfigFileParsed(LuaConfig::Options& LuaOptions) +{ + using namespace std::literals; // These have special command line processing so we make sure we export them if they were configured on command line - if (!ServerOptions.AuthConfig.OpenIdProviders.empty()) + + if (!m_ServerOptions.AuthConfig.OpenIdProviders.empty()) { LuaOptions.Touch("security.openidproviders"sv); } - if (!ServerOptions.ObjectStoreConfig.Buckets.empty()) + if (!m_ServerOptions.ObjectStoreConfig.Buckets.empty()) { LuaOptions.Touch("server.objectstore.buckets"sv); } - if (!ServerOptions.StructuredCacheConfig.PerBucketConfigs.empty()) + if (!m_ServerOptions.StructuredCacheConfig.PerBucketConfigs.empty()) { LuaOptions.Touch("cache.buckets"sv); } - if (!OutputConfigFile.empty()) + // Also parse plugins config file + + if (!m_ServerOptions.PluginsConfigFile.empty()) { - std::filesystem::path WritePath(MakeSafeAbsolutePath(OutputConfigFile)); - ExtendableStringBuilder<512> ConfigStringBuilder; - LuaOptions.Print(ConfigStringBuilder, CmdLineResult); - BasicFile Output; - Output.Open(WritePath, BasicFile::Mode::kTruncate); - Output.Write(ConfigStringBuilder.Data(), ConfigStringBuilder.Size(), 0); + ParsePluginsConfigFile(m_ServerOptions.PluginsConfigFile); } } void -ParsePluginsConfigFile(const std::filesystem::path& Path, ZenStorageServerOptions& ServerOptions, int BasePort) +ZenStorageServerConfigurator::AddCliOptions(cxxopts::Options& options) +{ + m_StorageOptions.AddCliOptions(options, m_ServerOptions); +} + +void +ZenStorageServerConfigurator::ApplyOptions(cxxopts::Options& options) +{ + m_StorageOptions.ApplyOptions(options, m_ServerOptions); +} + +void +ZenStorageServerConfigurator::ParsePluginsConfigFile(const std::filesystem::path& Path) { using namespace std::literals; @@ -590,19 +599,16 @@ ParsePluginsConfigFile(const std::filesystem::path& Path, ZenStorageServerOption // add a default base port in case if json config didn't provide one if (bNeedsPort) { - Config.PluginOptions.push_back({"port", std::to_string(BasePort)}); + Config.PluginOptions.push_back({"port", std::to_string(m_ServerOptions.BasePort)}); } - ServerOptions.HttpServerConfig.PluginConfigs.push_back(Config); + m_ServerOptions.HttpServerConfig.PluginConfigs.push_back(Config); } } void -ZenStorageServerCmdLineOptions::AddCliOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions) +ZenStorageServerCmdLineOptions::AddCliOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions) { - options.add_options()("snapshot-dir", - "Specify a snapshot of server state to mirror into the persistence root at startup", - cxxopts::value<std::string>(BaseSnapshotDir)); options.add_options()("plugins-config", "Path to plugins config file", cxxopts::value<std::string>(PluginsConfigFile)); options.add_options()("scrub", "Validate state at startup", @@ -618,7 +624,7 @@ ZenStorageServerCmdLineOptions::AddCliOptions(cxxopts::Options& options, ZenStor } void -ZenStorageServerCmdLineOptions::AddSecurityOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions) +ZenStorageServerCmdLineOptions::AddSecurityOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions) { options.add_option("security", "", @@ -646,7 +652,7 @@ ZenStorageServerCmdLineOptions::AddSecurityOptions(cxxopts::Options& options, Ze } void -ZenStorageServerCmdLineOptions::AddCacheOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions) +ZenStorageServerCmdLineOptions::AddCacheOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions) { options.add_option("cache", "", @@ -831,7 +837,7 @@ ZenStorageServerCmdLineOptions::AddCacheOptions(cxxopts::Options& options, ZenSt } void -ZenStorageServerCmdLineOptions::AddGcOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions) +ZenStorageServerCmdLineOptions::AddGcOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions) { options.add_option("gc", "", @@ -970,7 +976,7 @@ ZenStorageServerCmdLineOptions::AddGcOptions(cxxopts::Options& options, ZenStora } void -ZenStorageServerCmdLineOptions::AddObjectStoreOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions) +ZenStorageServerCmdLineOptions::AddObjectStoreOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions) { options.add_option("objectstore", "", @@ -988,7 +994,7 @@ ZenStorageServerCmdLineOptions::AddObjectStoreOptions(cxxopts::Options& options, } void -ZenStorageServerCmdLineOptions::AddBuildStoreOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions) +ZenStorageServerCmdLineOptions::AddBuildStoreOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions) { options.add_option("buildstore", "", @@ -1005,7 +1011,7 @@ ZenStorageServerCmdLineOptions::AddBuildStoreOptions(cxxopts::Options& options, } void -ZenStorageServerCmdLineOptions::AddWorkspacesOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions) +ZenStorageServerCmdLineOptions::AddWorkspacesOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions) { options.add_option("workspaces", "", @@ -1023,21 +1029,11 @@ ZenStorageServerCmdLineOptions::AddWorkspacesOptions(cxxopts::Options& options, } void -ZenStorageServerCmdLineOptions::ApplyOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions) +ZenStorageServerCmdLineOptions::ApplyOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions) { - ServerOptions.BaseSnapshotDir = MakeSafeAbsolutePath(BaseSnapshotDir); ServerOptions.PluginsConfigFile = MakeSafeAbsolutePath(PluginsConfigFile); ServerOptions.UpstreamCacheConfig.CachePolicy = ParseUpstreamCachePolicy(UpstreamCachePolicyOptions); - if (!BaseSnapshotDir.empty()) - { - if (ServerOptions.DataDir.empty()) - throw OptionParseException("'--snapshot-dir' requires '--data-dir'", options.help()); - - if (!IsDir(ServerOptions.BaseSnapshotDir)) - throw std::runtime_error(fmt::format("'--snapshot-dir' ('{}') must be a directory", ServerOptions.BaseSnapshotDir)); - } - if (OpenIdProviderUrl.empty() == false) { if (OpenIdClientId.empty()) diff --git a/src/zenserver/storage/storageconfig.h b/src/zenserver/storage/storageconfig.h index ca0cf4135..acdaa099b 100644 --- a/src/zenserver/storage/storageconfig.h +++ b/src/zenserver/storage/storageconfig.h @@ -144,7 +144,7 @@ struct ZenWorkspacesConfig bool AllowConfigurationChanges = false; }; -struct ZenStorageServerOptions : public ZenServerOptions +struct ZenStorageServerConfig : public ZenServerConfig { ZenUpstreamCacheConfig UpstreamCacheConfig; ZenGcConfig GcConfig; @@ -155,19 +155,10 @@ struct ZenStorageServerOptions : public ZenServerOptions ZenBuildStoreConfig BuildStoreConfig; ZenWorkspacesConfig WorksSpacesConfig; std::filesystem::path PluginsConfigFile; // Path to plugins config file - std::filesystem::path BaseSnapshotDir; // Path to server state snapshot (will be copied into data dir on start) bool ObjectStoreEnabled = false; std::string ScrubOptions; }; -void ParseConfigFile(const std::filesystem::path& Path, - ZenStorageServerOptions& ServerOptions, - const cxxopts::ParseResult& CmdLineResult, - std::string_view OutputConfigFile); - -void ParsePluginsConfigFile(const std::filesystem::path& Path, ZenStorageServerOptions& ServerOptions, int BasePort); -void ValidateOptions(ZenStorageServerOptions& ServerOptions); - struct ZenStorageServerCmdLineOptions { // Note to those adding future options; std::filesystem::path-type options @@ -176,28 +167,49 @@ struct ZenStorageServerCmdLineOptions // expects paths in streams to be quoted but argv paths are unquoted. By // going into a std::string first, paths with whitespace parse correctly. std::string PluginsConfigFile; - std::string BaseSnapshotDir; - void AddCliOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions); - void ApplyOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions); + void AddCliOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions); + void ApplyOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions); std::string OpenIdProviderName; std::string OpenIdProviderUrl; std::string OpenIdClientId; - void AddSecurityOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions); + void AddSecurityOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions); std::string UpstreamCachePolicyOptions; - void AddCacheOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions); + void AddCacheOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions); - void AddGcOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions); + void AddGcOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions); std::vector<std::string> BucketConfigs; - void AddObjectStoreOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions); - void AddBuildStoreOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions); - void AddWorkspacesOptions(cxxopts::Options& options, ZenStorageServerOptions& ServerOptions); + void AddObjectStoreOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions); + void AddBuildStoreOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions); + void AddWorkspacesOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions); +}; + +struct ZenStorageServerConfigurator : public ZenServerConfiguratorBase +{ + ZenStorageServerConfigurator(ZenStorageServerConfig& ServerOptions) + : ZenServerConfiguratorBase(ServerOptions) + , m_ServerOptions(ServerOptions) + { + } + ~ZenStorageServerConfigurator() = default; + +private: + virtual void AddCliOptions(cxxopts::Options& options) override; + virtual void AddConfigOptions(LuaConfig::Options& Options) override; + virtual void ApplyOptions(cxxopts::Options& options) override; + virtual void OnConfigFileParsed(LuaConfig::Options& LuaOptions) override; + virtual void ValidateOptions() override; + + void ParsePluginsConfigFile(const std::filesystem::path& Path); + + ZenStorageServerConfig& m_ServerOptions; + ZenStorageServerCmdLineOptions m_StorageOptions; }; } // namespace zen diff --git a/src/zenserver/storage/zenstorageserver.cpp b/src/zenserver/storage/zenstorageserver.cpp index 73896512d..65de3eca2 100644 --- a/src/zenserver/storage/zenstorageserver.cpp +++ b/src/zenserver/storage/zenstorageserver.cpp @@ -100,10 +100,11 @@ ZenStorageServer::ZenStorageServer() ZenStorageServer::~ZenStorageServer() { + Cleanup(); } int -ZenStorageServer::Initialize(const ZenStorageServerOptions& ServerOptions, ZenServerState::ZenServerEntry* ServerEntry) +ZenStorageServer::Initialize(const ZenStorageServerConfig& ServerOptions, ZenServerState::ZenServerEntry* ServerEntry) { ZEN_TRACE_CPU("ZenStorageServer::Initialize"); ZEN_MEMSCOPE(GetZenserverTag()); @@ -182,7 +183,7 @@ ZenStorageServer::RegisterServices() } void -ZenStorageServer::InitializeServices(const ZenStorageServerOptions& ServerOptions) +ZenStorageServer::InitializeServices(const ZenStorageServerConfig& ServerOptions) { InitializeAuthentication(ServerOptions); @@ -300,7 +301,7 @@ ZenStorageServer::InitializeServices(const ZenStorageServerOptions& ServerOption } void -ZenStorageServer::InitializeAuthentication(const ZenStorageServerOptions& ServerOptions) +ZenStorageServer::InitializeAuthentication(const ZenStorageServerConfig& ServerOptions) { // Setup authentication manager { @@ -343,7 +344,7 @@ ZenStorageServer::InitializeAuthentication(const ZenStorageServerOptions& Server } void -ZenStorageServer::InitializeState(const ZenStorageServerOptions& ServerOptions) +ZenStorageServer::InitializeState(const ZenStorageServerConfig& ServerOptions) { ZEN_TRACE_CPU("ZenStorageServer::InitializeState"); @@ -507,7 +508,7 @@ ZenStorageServer::InitializeState(const ZenStorageServerOptions& ServerOptions) } void -ZenStorageServer::InitializeStructuredCache(const ZenStorageServerOptions& ServerOptions) +ZenStorageServer::InitializeStructuredCache(const ZenStorageServerConfig& ServerOptions) { ZEN_TRACE_CPU("ZenStorageServer::InitializeStructuredCache"); @@ -876,7 +877,7 @@ ZenStorageServer::Flush() ////////////////////////////////////////////////////////////////////////// -ZenStorageServerMain::ZenStorageServerMain(ZenStorageServerOptions& ServerOptions) +ZenStorageServerMain::ZenStorageServerMain(ZenStorageServerConfig& ServerOptions) : ZenServerMain(ServerOptions) , m_ServerOptions(ServerOptions) { @@ -891,8 +892,6 @@ ZenStorageServerMain::DoRun(ZenServerState::ZenServerEntry* Entry) Server.SetTestMode(m_ServerOptions.IsTest); Server.SetDedicatedMode(m_ServerOptions.IsDedicated); - auto ServerCleanup = MakeGuard([&Server] { Server.Cleanup(); }); - int EffectiveBasePort = Server.Initialize(m_ServerOptions, Entry); if (EffectiveBasePort == -1) { @@ -917,7 +916,7 @@ ZenStorageServerMain::DoRun(ZenServerState::ZenServerEntry* Entry) // Monitor shutdown signals ShutdownThread.reset(new std::thread{[&] { - SetCurrentThreadName("shutdown_monitor"); + SetCurrentThreadName("shutdown_mon"); ZEN_INFO("shutdown monitor thread waiting for shutdown signal '{}' for process {}", ShutdownEventName, zen::GetCurrentProcessId()); diff --git a/src/zenserver/storage/zenstorageserver.h b/src/zenserver/storage/zenstorageserver.h index e4c31399d..f79c55bc8 100644 --- a/src/zenserver/storage/zenstorageserver.h +++ b/src/zenserver/storage/zenstorageserver.h @@ -39,13 +39,13 @@ public: void SetDataRoot(std::filesystem::path Root) { m_DataRoot = Root; } void SetContentRoot(std::filesystem::path Root) { m_ContentRoot = Root; } - int Initialize(const ZenStorageServerOptions& ServerOptions, ZenServerState::ZenServerEntry* ServerEntry); + int Initialize(const ZenStorageServerConfig& ServerOptions, ZenServerState::ZenServerEntry* ServerEntry); void Run(); void Cleanup(); private: - void InitializeState(const ZenStorageServerOptions& ServerOptions); - void InitializeStructuredCache(const ZenStorageServerOptions& ServerOptions); + void InitializeState(const ZenStorageServerConfig& ServerOptions); + void InitializeStructuredCache(const ZenStorageServerConfig& ServerOptions); void Flush(); bool m_IsDedicatedMode = false; @@ -62,9 +62,9 @@ private: std::unique_ptr<AuthMgr> m_AuthMgr; std::unique_ptr<HttpAuthService> m_AuthService; - void InitializeAuthentication(const ZenStorageServerOptions& ServerOptions); + void InitializeAuthentication(const ZenStorageServerConfig& ServerOptions); - void InitializeServices(const ZenStorageServerOptions& ServerOptions); + void InitializeServices(const ZenStorageServerConfig& ServerOptions); void RegisterServices(); HttpStatsService m_StatsService; @@ -97,17 +97,22 @@ private: std::unique_ptr<HttpAdminService> m_AdminService; }; +struct ZenStorageServerConfigurator; + class ZenStorageServerMain : public ZenServerMain { public: - ZenStorageServerMain(ZenStorageServerOptions& ServerOptions); + ZenStorageServerMain(ZenStorageServerConfig& ServerOptions); virtual void DoRun(ZenServerState::ZenServerEntry* Entry) override; ZenStorageServerMain(const ZenStorageServerMain&) = delete; ZenStorageServerMain& operator=(const ZenStorageServerMain&) = delete; + typedef ZenStorageServerConfig Config; + typedef ZenStorageServerConfigurator Configurator; + private: - ZenStorageServerOptions& m_ServerOptions; + ZenStorageServerConfig& m_ServerOptions; }; } // namespace zen |