diff options
| author | Dan Engelbrecht <[email protected]> | 2026-03-03 20:45:52 +0100 |
|---|---|---|
| committer | Dan Engelbrecht <[email protected]> | 2026-03-03 20:45:52 +0100 |
| commit | 610dac1a31e19edb5fb9ba9080487ad68ddf85d8 (patch) | |
| tree | 8c384f4dda2eef7530e08e4a45c23b507389000c /src/zenserver/hub/hubservice.cpp | |
| parent | fix objectstore uri path parsing (#801) (diff) | |
| download | zen-610dac1a31e19edb5fb9ba9080487ad68ddf85d8.tar.xz zen-610dac1a31e19edb5fb9ba9080487ad68ddf85d8.zip | |
initial implementation from
Diffstat (limited to 'src/zenserver/hub/hubservice.cpp')
| -rw-r--r-- | src/zenserver/hub/hubservice.cpp | 71 |
1 files changed, 64 insertions, 7 deletions
diff --git a/src/zenserver/hub/hubservice.cpp b/src/zenserver/hub/hubservice.cpp index bf0e294c5..cc34e86bb 100644 --- a/src/zenserver/hub/hubservice.cpp +++ b/src/zenserver/hub/hubservice.cpp @@ -8,9 +8,9 @@ #include <zencore/filesystem.h> #include <zencore/fmtutils.h> #include <zencore/logging.h> -#include <zencore/process.h> #include <zencore/scopeguard.h> #include <zencore/system.h> +#include <zenutil/consul.h> #include <zenutil/zenserverprocess.h> ZEN_THIRD_PARTY_INCLUDES_START @@ -135,7 +135,8 @@ struct StorageServerInstance StorageServerInstance(ZenServerEnvironment& RunEnvironment, std::string_view ModuleId, std::filesystem::path FileHydrationPath, - std::filesystem::path HydrationTempPath); + std::filesystem::path HydrationTempPath, + consul::ConsulClient* ConsulClient = nullptr); ~StorageServerInstance(); void Provision(); @@ -169,6 +170,7 @@ private: #if ZEN_PLATFORM_WINDOWS JobObject* m_JobObject = nullptr; #endif + consul::ConsulClient* m_ConsulClient; void SpawnServerProcess(); @@ -179,10 +181,12 @@ private: StorageServerInstance::StorageServerInstance(ZenServerEnvironment& RunEnvironment, std::string_view ModuleId, std::filesystem::path FileHydrationPath, - std::filesystem::path HydrationTempPath) + std::filesystem::path HydrationTempPath, + consul::ConsulClient* ConsulClient) : m_ModuleId(ModuleId) , m_ServerInstance(RunEnvironment, ZenServerInstance::ServerMode::kStorageServer) , m_HydrationPath(FileHydrationPath) +, m_ConsulClient(ConsulClient) { m_BaseDir = RunEnvironment.CreateChildDir(ModuleId); m_TempDir = HydrationTempPath / ModuleId; @@ -234,6 +238,28 @@ StorageServerInstance::Provision() SpawnServerProcess(); } + // Register with Consul if client is available + if (m_ConsulClient != nullptr) + { + consul::ServiceRegistrationInfo Info; + Info.ServiceId = m_ModuleId; + Info.ServiceName = "zen-service"; + Info.Address = "localhost"; + Info.Port = GetBasePort(); + Info.HealthEndpoint = "health"; + Info.HealthIntervalSeconds = 10; + Info.DeregisterAfterSeconds = 30; + + if (!m_ConsulClient->RegisterService(Info)) + { + ZEN_WARN("Failed to register storage server instance for module '{}' with Consul, continuing anyway", m_ModuleId); + } + else + { + ZEN_INFO("Registered storage server instance for module '{}' with Consul as '{}'", m_ModuleId, Info.ServiceName); + } + } + m_IsProvisioned = true; } @@ -251,6 +277,19 @@ StorageServerInstance::Deprovision() ZEN_INFO("Deprovisioning storage server instance for module '{}'", m_ModuleId); + // Deregister from Consul before shutdown + if (m_ConsulClient != nullptr) + { + if (!m_ConsulClient->DeregisterService(m_ModuleId)) + { + ZEN_WARN("Failed to deregister storage server instance for module '{}' from Consul, continuing anyway", m_ModuleId); + } + else + { + ZEN_INFO("Deregistered storage server instance for module '{}' from Consul", m_ModuleId); + } + } + m_ServerInstance.Shutdown(); Dehydrate(); @@ -445,9 +484,12 @@ struct HttpHubService::Impl return false; } - IsNewInstance = true; - auto NewInstance = - std::make_unique<StorageServerInstance>(m_RunEnvironment, ModuleId, m_FileHydrationPath, m_HydrationTempPath); + IsNewInstance = true; + auto NewInstance = std::make_unique<StorageServerInstance>(m_RunEnvironment, + ModuleId, + m_FileHydrationPath, + m_HydrationTempPath, + m_ConsulClient); #if ZEN_PLATFORM_WINDOWS if (m_JobObject.IsValid()) { @@ -625,6 +667,7 @@ private: std::unordered_set<std::string> m_DeprovisioningModules; std::unordered_set<std::string> m_ProvisioningModules; int m_MaxInstanceCount = 0; + consul::ConsulClient* m_ConsulClient = nullptr; void UpdateStats(); // Capacity tracking @@ -635,6 +678,9 @@ private: void UpdateCapacityMetrics(); bool CanProvisionInstance(std::string_view ModuleId, std::string& OutReason); + +public: + void SetConsulClient(consul::ConsulClient* Client) { m_ConsulClient = Client; } }; HttpHubService::Impl::Impl() @@ -718,7 +764,7 @@ HttpHubService::HttpHubService(std::filesystem::path HubBaseDir, std::filesystem m_Router.AddMatcher("moduleid", [](std::string_view Str) -> bool { for (const auto C : Str) { - if (std::isalnum(C) || C == '-') + if (std::isalnum(C) || C == '-' || C == ':') { // fine } @@ -749,6 +795,11 @@ HttpHubService::HttpHubService(std::filesystem::path HubBaseDir, std::filesystem HttpVerb::kGet); m_Router.RegisterRoute( + "health", + [](HttpRouterRequest& Req) { Req.ServerRequest().WriteResponse(HttpResponseCode::OK); }, + HttpVerb::kGet); + + m_Router.RegisterRoute( "modules/{moduleid}", [this](HttpRouterRequest& Req) { std::string_view ModuleId = Req.GetCapture(1); @@ -874,6 +925,12 @@ HttpHubService::SetNotificationEndpoint(std::string_view UpstreamNotificationEnd } void +HttpHubService::SetConsulClient(consul::ConsulClient* Client) +{ + m_Impl->SetConsulClient(Client); +} + +void HttpHubService::HandleRequest(zen::HttpServerRequest& Request) { m_Router.HandleRequest(Request); |