Line data Source code
1 : #include "source/common/config/config_provider_impl.h" 2 : 3 : namespace Envoy { 4 : namespace Config { 5 : 6 : ImmutableConfigProviderBase::ImmutableConfigProviderBase( 7 : Server::Configuration::ServerFactoryContext& factory_context, 8 : ConfigProviderManagerImplBase& config_provider_manager, 9 : ConfigProviderInstanceType instance_type, ApiType api_type) 10 : : last_updated_(factory_context.timeSource().systemTime()), 11 : config_provider_manager_(config_provider_manager), instance_type_(instance_type), 12 0 : api_type_(api_type) { 13 0 : ASSERT(instance_type_ == ConfigProviderInstanceType::Static || 14 0 : instance_type_ == ConfigProviderInstanceType::Inline); 15 0 : config_provider_manager_.bindImmutableConfigProvider(this); 16 0 : } 17 : 18 0 : ImmutableConfigProviderBase::~ImmutableConfigProviderBase() { 19 0 : config_provider_manager_.unbindImmutableConfigProvider(this); 20 0 : } 21 : 22 0 : ConfigSubscriptionCommonBase::~ConfigSubscriptionCommonBase() { 23 0 : local_init_target_.ready(); 24 0 : config_provider_manager_.unbindSubscription(manager_identifier_); 25 0 : } 26 : 27 0 : void ConfigSubscriptionCommonBase::applyConfigUpdate(const ConfigUpdateCb& update_fn) { 28 0 : tls_.runOnAllThreads([update_fn](OptRef<ThreadLocalConfig> thread_local_config) { 29 0 : thread_local_config->config_ = update_fn(thread_local_config->config_); 30 0 : }); 31 0 : } 32 : 33 : bool ConfigSubscriptionInstance::checkAndApplyConfigUpdate(const Protobuf::Message& config_proto, 34 : const std::string& config_name, 35 0 : const std::string& version_info) { 36 0 : const uint64_t new_hash = MessageUtil::hash(config_proto); 37 0 : if (config_info_) { 38 0 : ASSERT(config_info_.value().last_config_hash_.has_value()); 39 0 : if (config_info_.value().last_config_hash_.value() == new_hash) { 40 0 : return false; 41 0 : } 42 0 : } 43 : 44 0 : config_info_ = {new_hash, version_info}; 45 0 : ENVOY_LOG(debug, "{}: loading new configuration: config_name={} hash={}", name_, config_name, 46 0 : new_hash); 47 0 : ConfigProvider::ConfigConstSharedPtr new_config_impl = onConfigProtoUpdate(config_proto); 48 0 : applyConfigUpdate([new_config_impl](ConfigProvider::ConfigConstSharedPtr) 49 0 : -> ConfigProvider::ConfigConstSharedPtr { return new_config_impl; }); 50 0 : return true; 51 0 : } 52 : 53 : ConfigProviderManagerImplBase::ConfigProviderManagerImplBase(OptRef<Server::Admin> admin, 54 96 : const std::string& config_name) { 55 96 : if (!admin.has_value()) { 56 0 : return; 57 0 : } 58 96 : config_tracker_entry_ = admin->getConfigTracker().add( 59 96 : config_name, 60 96 : [this](const Matchers::StringMatcher& name_matcher) { return dumpConfigs(name_matcher); }); 61 : // ConfigTracker keys must be unique. We are asserting that no one has stolen the key 62 : // from us, since the returned entry will be nullptr if the key already exists. 63 96 : RELEASE_ASSERT(config_tracker_entry_, ""); 64 96 : } 65 : 66 : const ConfigProviderManagerImplBase::ConfigProviderSet& 67 70 : ConfigProviderManagerImplBase::immutableConfigProviders(ConfigProviderInstanceType type) const { 68 70 : static ConfigProviderSet empty_set; 69 70 : ConfigProviderMap::const_iterator it; 70 70 : if ((it = immutable_config_providers_map_.find(type)) == immutable_config_providers_map_.end()) { 71 70 : return empty_set; 72 70 : } 73 : 74 0 : return *it->second; 75 70 : } 76 : 77 : void ConfigProviderManagerImplBase::bindImmutableConfigProvider( 78 0 : ImmutableConfigProviderBase* provider) { 79 0 : ConfigProviderMap::iterator it; 80 0 : if ((it = immutable_config_providers_map_.find(provider->instanceType())) == 81 0 : immutable_config_providers_map_.end()) { 82 0 : immutable_config_providers_map_.insert(std::make_pair( 83 0 : provider->instanceType(), 84 0 : std::make_unique<ConfigProviderSet>(std::initializer_list<ConfigProvider*>({provider})))); 85 0 : } else { 86 0 : it->second->insert(provider); 87 0 : } 88 0 : } 89 : 90 : void ConfigProviderManagerImplBase::unbindImmutableConfigProvider( 91 0 : ImmutableConfigProviderBase* provider) { 92 0 : auto it = immutable_config_providers_map_.find(provider->instanceType()); 93 0 : ASSERT(it != immutable_config_providers_map_.end()); 94 0 : it->second->erase(provider); 95 0 : } 96 : 97 : } // namespace Config 98 : } // namespace Envoy