aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/common/configmanager.cpp
blob: d95fb12b06ae444b6d4800474af067ddd2380ca4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "configmanager.h"
#include "exceptions.h"
#include "configholder.h"
#include <thread>
#include <sstream>

#include <vespa/log/log.h>
LOG_SETUP(".config.common.configmanager");


namespace config {

ConfigManager::ConfigManager(std::unique_ptr<SourceFactory> sourceFactory, int64_t initialGeneration)
    : _idGenerator(0),
      _sourceFactory(std::move(sourceFactory)),
      _generation(initialGeneration),
      _subscriptionMap(),
      _lock()
{ }

ConfigManager::~ConfigManager() = default;

ConfigSubscription::SP
ConfigManager::subscribe(const ConfigKey & key, vespalib::duration timeout)
{
    LOG(debug, "subscribing on def %s, configid %s", key.getDefName().c_str(), key.getConfigId().c_str());

    SubscriptionId id(_idGenerator.fetch_add(1));

    auto holder = std::make_shared<ConfigHolder>();
    std::unique_ptr<Source> source = _sourceFactory->createSource(holder, key);
    source->reload(_generation);

    source->getConfig();
    auto subscription = std::make_shared<ConfigSubscription>(id, key, holder, std::move(source));

    vespalib::steady_time endTime = vespalib::steady_clock::now() + timeout;
    while (vespalib::steady_clock::now() < endTime) {
        if (holder->poll())
            break;
        std::this_thread::sleep_for(10ms);
    }
    if (!holder->poll()) {
        std::ostringstream oss;
        oss << "Timed out while subscribing to '" << key.getDefNamespace() << "." << key.getDefName() << "', configid '" << key.getConfigId() << "'";
        throw ConfigTimeoutException(oss.str());
    }
    LOG(debug, "done subscribing");
    std::lock_guard guard(_lock);
    _subscriptionMap[id] = subscription;
    return subscription;
}

void
ConfigManager::unsubscribe(const ConfigSubscription & subscription)
{
    std::lock_guard guard(_lock);
    const SubscriptionId id(subscription.getSubscriptionId());
    if (_subscriptionMap.find(id) != _subscriptionMap.end())
        _subscriptionMap.erase(id);
}

void
ConfigManager::reload(int64_t generation)
{
    _generation = generation;
    std::lock_guard guard(_lock);
    for (auto & entry : _subscriptionMap) {
        entry.second->reload(_generation);
    }
}

} // namespace config