diff options
author | Arne Juul <arnej@verizonmedia.com> | 2021-05-27 13:30:38 +0000 |
---|---|---|
committer | Arne Juul <arnej@verizonmedia.com> | 2021-05-27 13:30:38 +0000 |
commit | 855c5b2ab689fa15ab6f8e9512d36d691ff3bfed (patch) | |
tree | 8d5d06ecf1ddd93944b47ccaddf9acbc78117779 /configd | |
parent | f002266330e04b61f01ce901733ce83006319dc0 (diff) |
add class for config ownership
Diffstat (limited to 'configd')
-rw-r--r-- | configd/src/apps/sentinel/CMakeLists.txt | 1 | ||||
-rw-r--r-- | configd/src/apps/sentinel/config-owner.cpp | 44 | ||||
-rw-r--r-- | configd/src/apps/sentinel/config-owner.h | 40 | ||||
-rw-r--r-- | configd/src/apps/sentinel/manager.h | 5 |
4 files changed, 90 insertions, 0 deletions
diff --git a/configd/src/apps/sentinel/CMakeLists.txt b/configd/src/apps/sentinel/CMakeLists.txt index 765988fa2ef..b20aa4e190a 100644 --- a/configd/src/apps/sentinel/CMakeLists.txt +++ b/configd/src/apps/sentinel/CMakeLists.txt @@ -1,6 +1,7 @@ # Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. vespa_add_executable(configd_config-sentinel_app SOURCES + config-owner.cpp sentinel.cpp service.cpp manager.cpp diff --git a/configd/src/apps/sentinel/config-owner.cpp b/configd/src/apps/sentinel/config-owner.cpp new file mode 100644 index 00000000000..f4ea76855f6 --- /dev/null +++ b/configd/src/apps/sentinel/config-owner.cpp @@ -0,0 +1,44 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "config-owner.h" +#include <vespa/vespalib/util/exceptions.h> +#include <string> + +#include <vespa/log/log.h> +LOG_SETUP(".config-owner"); + +namespace config::sentinel { + +ConfigOwner::ConfigOwner() : _subscriber() {} + +ConfigOwner::~ConfigOwner() = default; + +void +ConfigOwner::subscribe(const std::string & configId, std::chrono::milliseconds timeout) { + _sentinelHandle = _subscriber.subscribe<SentinelConfig>(configId, timeout); +} + +void +ConfigOwner::doConfigure() +{ + _currConfig = _sentinelHandle->getConfig(); + LOG_ASSERT(_currConfig); + _currGeneration = _subscriber.getGeneration(); + const SentinelConfig& config(*_currConfig); + const auto & app = config.application; + LOG(info, "ConfigOwner got %zd service elements [tenant(%s), application(%s), instance(%s)] for config generation %zd", + config.service.size(), app.tenant.c_str(), app.name.c_str(), app.instance.c_str(), _currGeneration); +} + + +// Return true if there was a config generation change +bool +ConfigOwner::checkForConfigUpdate() { + if (_subscriber.nextGenerationNow()) { + doConfigure(); + return true; + } + return false; +} + +} diff --git a/configd/src/apps/sentinel/config-owner.h b/configd/src/apps/sentinel/config-owner.h new file mode 100644 index 00000000000..612db7fe9a1 --- /dev/null +++ b/configd/src/apps/sentinel/config-owner.h @@ -0,0 +1,40 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <vespa/config-sentinel.h> +#include <vespa/config/config.h> + +using cloud::config::SentinelConfig; + +using config::ConfigSubscriber; +using config::ConfigHandle; + +namespace config::sentinel { + +/** + * Handles config subscription and has a snapshot of current config. + **/ +class ConfigOwner { +private: + ConfigSubscriber _subscriber; + ConfigHandle<SentinelConfig>::UP _sentinelHandle; + + int64_t _currGeneration = -1; + std::unique_ptr<SentinelConfig> _currConfig; + + ConfigOwner(const ConfigOwner&) = delete; + ConfigOwner& operator =(const ConfigOwner&) = delete; + + void doConfigure(); +public: + ConfigOwner(); + virtual ~ConfigOwner(); + void subscribe(const std::string & configId, std::chrono::milliseconds timeout); + bool checkForConfigUpdate(); + bool hasConfig() const { return _currConfig.get() != nullptr; } + const SentinelConfig& getConfig() const { return *_currConfig; } + int64_t getGeneration() const { return _currGeneration; } +}; + +} diff --git a/configd/src/apps/sentinel/manager.h b/configd/src/apps/sentinel/manager.h index 4eaff7a2ef3..e157d5e003d 100644 --- a/configd/src/apps/sentinel/manager.h +++ b/configd/src/apps/sentinel/manager.h @@ -22,6 +22,11 @@ namespace config::sentinel { class OutputConnection; +/** + * Management of services. + * Handles requests from RPC, service events, + * and service configuration updates. + **/ class Manager { private: typedef std::map<vespalib::string, Service::UP> ServiceMap; |