From 95e353b52f1c8e34727d7bb385fcc5baf89f481f Mon Sep 17 00:00:00 2001 From: Arne Juul Date: Thu, 27 May 2021 13:52:08 +0000 Subject: use environment and bootstrap it before creating manager --- configd/src/apps/sentinel/manager.cpp | 66 ++++++++-------------------------- configd/src/apps/sentinel/manager.h | 29 +++++---------- configd/src/apps/sentinel/sentinel.cpp | 13 +++---- 3 files changed, 30 insertions(+), 78 deletions(-) (limited to 'configd') diff --git a/configd/src/apps/sentinel/manager.cpp b/configd/src/apps/sentinel/manager.cpp index bfd9a5e2bef..d1d06c6039a 100644 --- a/configd/src/apps/sentinel/manager.cpp +++ b/configd/src/apps/sentinel/manager.cpp @@ -15,36 +15,11 @@ LOG_SETUP(".manager"); namespace config::sentinel { -void -Manager::configure_port(int port) -{ - if (port == 0) { - port = 19098; - const char *portString = getenv("VESPA_SENTINEL_PORT"); - if (portString) { - port = strtoul(portString, nullptr, 10); - } - } - if (port <= 0 || port > 65535) { - throw vespalib::FatalException("Bad port " + std::to_string(port) + ", expected range [1, 65535]", VESPA_STRLOC); - } - if (port != _boundPort) { - LOG(debug, "Config-sentinel accepts connections on port %d", port); - _stateServer = std::make_unique( - port, _stateApi.myHealth, _startMetrics.producer, _stateApi.myComponents); - _boundPort = port; - } -} - -Manager::Manager() - : _subscriber(), - _services(), - _outputConnections(), - _boundPort(0), - _startMetrics(), - _stateApi() +Manager::Manager(Env &env) + : _env(env), + _services(), + _outputConnections() { - _startMetrics.startedTime = vespalib::steady_clock::now(); } Manager::~Manager() @@ -97,25 +72,14 @@ Manager::terminate() return !doWork(); } -void -Manager::subscribe(const std::string & configId, std::chrono::milliseconds timeout) -{ - _sentinelHandle = _subscriber.subscribe(configId, timeout); -} - void Manager::doConfigure() { - std::unique_ptr cfg(_sentinelHandle->getConfig()); - const SentinelConfig& config(*cfg); + LOG_ASSERT(_env.configOwner().hasConfig()); + const SentinelConfig& config(_env.configOwner().getConfig()); - if (config.port.telnet != _boundPort) { - configure_port(config.port.telnet); - } - - if (!_rpcServer || config.port.rpc != _rpcServer->getPort()) { - _rpcServer = std::make_unique(config.port.rpc, _cmdQ); - } + _env.rpcPort(config.port.rpc); + _env.statePort(config.port.telnet); LOG(debug, "Manager::configure() %d config elements, tenant(%s), application(%s), instance(%s)", (int)config.service.size(), config.application.tenant.c_str(), config.application.name.c_str(), @@ -126,7 +90,7 @@ Manager::doConfigure() const vespalib::string name(serviceConfig.name); auto found(_services.find(name)); if (found == _services.end()) { - services[name] = std::make_unique(serviceConfig, config.application, _outputConnections, _startMetrics); + services[name] = std::make_unique(serviceConfig, config.application, _outputConnections, _env.metrics()); } else { found->second->reconfigure(serviceConfig); services[name] = std::move(found->second); @@ -140,8 +104,7 @@ Manager::doConfigure() _orphans[entry.first] = std::move(svc); } } - vespalib::ComponentConfigProducer::Config current("sentinel", _subscriber.getGeneration(), "ok"); - _stateApi.myComponents.addConfig(current); + _env.notifyConfigUpdated(); } @@ -149,15 +112,14 @@ int Manager::doWork() { // Return true if there are any running services, false if not. - - if (_subscriber.nextGenerationNow()) { + if (_env.configOwner().checkForConfigUpdate()) { doConfigure(); } handleRestarts(); handleCommands(); handleOutputs(); handleChildDeaths(); - _startMetrics.maybeLog(); + _env.metrics().maybeLog(); // Check for active services. for (const auto & service : _services) { @@ -244,7 +206,7 @@ void Manager::handleCommands() { // handle RPC commands - std::vector got = _cmdQ.drain(); + std::vector got = _env.commandQueue().drain(); for (const Cmd::UP & cmd : got) { handleCmd(*cmd); } @@ -355,7 +317,7 @@ Manager::handleCmd(const Cmd& cmd) void Manager::updateMetrics() { - _startMetrics.maybeLog(); + _env.metrics().maybeLog(); } } diff --git a/configd/src/apps/sentinel/manager.h b/configd/src/apps/sentinel/manager.h index e157d5e003d..dabe1590d20 100644 --- a/configd/src/apps/sentinel/manager.h +++ b/configd/src/apps/sentinel/manager.h @@ -1,11 +1,12 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #pragma once -#include "service.h" -#include "metrics.h" -#include "state-api.h" #include "cmdq.h" +#include "env.h" +#include "metrics.h" #include "rpcserver.h" +#include "service.h" +#include "state-api.h" #include #include #include @@ -31,20 +32,13 @@ class Manager { private: typedef std::map ServiceMap; - ConfigSubscriber _subscriber; - ConfigHandle::UP _sentinelHandle; + Env &_env; ServiceMap _services; ServiceMap _orphans; std::list _outputConnections; - CommandQueue _cmdQ; - std::unique_ptr _rpcServer; - int _boundPort; - StartMetrics _startMetrics; - StateApi _stateApi; - std::unique_ptr _stateServer; - Manager(const Manager&); - Manager& operator =(const Manager&); + Manager(const Manager&) = delete; + Manager& operator =(const Manager&) = delete; Service *serviceByPid(pid_t pid); Service *serviceByName(const vespalib::string & name); @@ -54,19 +48,14 @@ private: void handleChildDeaths(); void handleRestarts(); - static int listen(int port); - void configure_port(int port); - void updateMetrics(); void terminateServices(bool catchable, bool printDebug = false); - void doConfigure(); - public: - Manager(); + Manager(Env &env); virtual ~Manager(); - void subscribe(const std::string & configId, std::chrono::milliseconds timeout); + void doConfigure(); bool terminate(); int doWork(); void updateActiveFdset(fd_set *fds, int *maxNum); diff --git a/configd/src/apps/sentinel/sentinel.cpp b/configd/src/apps/sentinel/sentinel.cpp index 9d5a18d249f..53c9ae7fbc2 100644 --- a/configd/src/apps/sentinel/sentinel.cpp +++ b/configd/src/apps/sentinel/sentinel.cpp @@ -61,11 +61,10 @@ main(int argc, char **argv) } setlocale(LC_ALL, "C"); - sentinel::Manager handler; - + sentinel::Env environment; LOG(debug, "Reading configuration"); try { - handler.subscribe(configId, CONFIG_TIMEOUT_MS); + environment.boot(configId); } catch (ConfigTimeoutException & ex) { LOG(warning, "Timeout getting config, please check your setup. Will exit and restart: %s", ex.getMessage().c_str()); EV_STOPPING("config-sentinel", ex.what()); @@ -80,11 +79,13 @@ main(int argc, char **argv) return EXIT_FAILURE; } + sentinel::Manager manager(environment); + manager.doConfigure(); vespalib::steady_time lastTime = vespalib::steady_clock::now(); while (!stop()) { try { vespalib::SignalHandler::CHLD.clear(); - handler.doWork(); // Check for child procs & commands + manager.doWork(); // Check for child procs & commands } catch (InvalidConfigException& ex) { LOG(warning, "Configuration problem: (ignoring): %s", ex.what()); } catch (vespalib::PortListenException& ex) { @@ -102,7 +103,7 @@ main(int argc, char **argv) int maxNum = 0; fd_set fds; FD_ZERO(&fds); - handler.updateActiveFdset(&fds, &maxNum); + manager.updateActiveFdset(&fds, &maxNum); struct timeval tv; tv.tv_sec = 0; @@ -118,6 +119,6 @@ main(int argc, char **argv) } EV_STOPPING("config-sentinel", "normal exit"); - int rv = handler.terminate(); + int rv = manager.terminate(); return rv; } -- cgit v1.2.3