summaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/server/proton_config_fetcher.cpp
blob: b5d76c3f60a5c5d5fdd8de140ba87a43622ec858 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "proton_config_fetcher.h"
#include "bootstrapconfig.h"
#include <vespa/vespalib/util/exceptions.h>
#include <thread>
#include "proton_config_snapshot.h"
#include "i_proton_configurer.h"
#include <vespa/log/log.h>
LOG_SETUP(".proton.server.proton_config_fetcher");

using namespace vespa::config::search;
using namespace vespa::config::search::core;
using namespace config;
using namespace std::chrono_literals;

namespace proton {

ProtonConfigFetcher::ProtonConfigFetcher(const config::ConfigUri & configUri, IProtonConfigurer &owner, uint64_t subscribeTimeout)
    : _bootstrapConfigManager(configUri.getConfigId()),
      _retriever(_bootstrapConfigManager.createConfigKeySet(), configUri.getContext(), subscribeTimeout),
      _owner(owner),
      _mutex(),
      _dbManagerMap(),
      _threadPool(128 * 1024, 1)
{
}

ProtonConfigFetcher::~ProtonConfigFetcher()
{
    close();
}

void
ProtonConfigFetcher::Run(FastOS_ThreadInterface * thread, void *arg)
{
    (void) arg;
    (void) thread;
    while (!_retriever.isClosed()) {
        try {
            fetchConfigs();
        } catch (const config::InvalidConfigException & e) {
            LOG(warning, "Invalid config received. Ignoring and continuing with old config : %s", e.what());
            std::this_thread::sleep_for(100ms);
        }
    }
}

const ConfigKeySet
ProtonConfigFetcher::pruneManagerMap(const BootstrapConfig::SP & config)
{
    const ProtonConfig & protonConfig = config->getProtonConfig();
    DBManagerMap newMap;
    ConfigKeySet set;

    lock_guard guard(_mutex);
    for (size_t i = 0; i < protonConfig.documentdb.size(); i++) {
        const ProtonConfig::Documentdb & ddb(protonConfig.documentdb[i]);
        DocTypeName docTypeName(ddb.inputdoctypename);
        LOG(debug, "Document type(%s), configid(%s)", ddb.inputdoctypename.c_str(), ddb.configid.c_str());
        DocumentDBConfigManager::SP mgr;
        if (_dbManagerMap.find(docTypeName) != _dbManagerMap.end()) {
            mgr = _dbManagerMap[docTypeName];
        } else {
            mgr = DocumentDBConfigManager::SP(new DocumentDBConfigManager
                    (ddb.configid, docTypeName.getName()));
        }
        set.add(mgr->createConfigKeySet());
        newMap[docTypeName] = mgr;
    }
    std::swap(_dbManagerMap, newMap);
    return set;
}

void
ProtonConfigFetcher::updateDocumentDBConfigs(const BootstrapConfig::SP & bootstrapConfig, const ConfigSnapshot & snapshot)
{
    lock_guard guard(_mutex);
    for (DBManagerMap::iterator it(_dbManagerMap.begin()), mt(_dbManagerMap.end());
         it != mt;
         it++) {
        it->second->forwardConfig(bootstrapConfig);
        it->second->update(snapshot);
    }
}

void
ProtonConfigFetcher::reconfigure()
{
    auto bootstrapConfig = _bootstrapConfigManager.getConfig();
    int64_t generation = bootstrapConfig->getGeneration();
    std::map<DocTypeName, DocumentDBConfig::SP> dbConfigs;
    {
        lock_guard guard(_mutex);
        for (auto &kv : _dbManagerMap) {
            auto insres = dbConfigs.insert(std::make_pair(kv.first, kv.second->getConfig()));
            assert(insres.second);
            assert(insres.first->second->getGeneration() == generation);
        }
    }
    auto configSnapshot = std::make_shared<ProtonConfigSnapshot>(std::move(bootstrapConfig), std::move(dbConfigs));
    LOG(debug, "Reconfiguring proton with gen %" PRId64, generation);
    _owner.reconfigure(std::move(configSnapshot));
    LOG(debug, "Reconfigured proton with gen %" PRId64, generation);
}

void
ProtonConfigFetcher::fetchConfigs()
{
    LOG(debug, "Waiting for new config generation");
    bool configured = false;
    while (!configured) {
        ConfigSnapshot bootstrapSnapshot = _retriever.getBootstrapConfigs(5000);
        if (_retriever.isClosed())
            return;
        LOG(debug, "Fetching snapshot");
        if (!bootstrapSnapshot.empty()) {
            _bootstrapConfigManager.update(bootstrapSnapshot);
            BootstrapConfig::SP config = _bootstrapConfigManager.getConfig();
            for (bool needsMoreConfig(true); needsMoreConfig && !_retriever.bootstrapRequired(); ) {
                const ConfigKeySet configKeySet(pruneManagerMap(config));
                // If key set is empty, we have no document databases to configure.
                // This is currently not a fatal error, so it will just try to fetch
                // the bootstrap config again.
                if (!configKeySet.empty()) {
                    ConfigSnapshot snapshot;
                    do {
                        snapshot = _retriever.getConfigs(configKeySet);
                        if (_retriever.isClosed()) {
                            return;
                        }
                    } while(snapshot.empty() && ! _retriever.bootstrapRequired());
                    if (!snapshot.empty()) {
                        LOG(debug, "Set is not empty, reconfiguring with generation %" PRId64, _retriever.getGeneration());
                        // Update document dbs first, so that we are prepared for
                        // getConfigs.
                        _bootstrapConfigManager.update(bootstrapSnapshot);
                        updateDocumentDBConfigs(config, snapshot);
                        needsMoreConfig = false;

                        // Perform callbacks
                        reconfigure();
                        configured = true;
                    }
                } else {
                    LOG(warning, "No document databases in config, trying to re-fetch bootstrap config");
                    break;
                }
            }
        }
    }
}

int64_t
ProtonConfigFetcher::getGeneration() const
{
    return _retriever.getGeneration();
}

void
ProtonConfigFetcher::start()
{
    fetchConfigs();
    if (_threadPool.NewThread(this, NULL) == NULL) {
        throw vespalib::IllegalStateException(
                "Failed starting thread for proton config fetcher");
    }
}

void
ProtonConfigFetcher::close()
{
    if (!_retriever.isClosed()) {
        _retriever.close();
        _threadPool.Close();
    }
}

DocumentDBConfig::SP
ProtonConfigFetcher::getDocumentDBConfig(const DocTypeName & docTypeName) const
{
    lock_guard guard(_mutex);
    DBManagerMap::const_iterator it(_dbManagerMap.find(docTypeName));
    if (it == _dbManagerMap.end())
        return DocumentDBConfig::SP();

    return it->second->getConfig();
}

} // namespace proton