aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/server/proton_config_fetcher.h
blob: 3914d016734f9e883be3e9afccb7e12cd28c3265 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "bootstrapconfigmanager.h"
#include "i_document_db_config_owner.h"
#include <vespa/searchcore/proton/common/doctypename.h>
#include <vespa/config/retriever/configretriever.h>
#include <vespa/config/subscription/configuri.h>
#include <deque>
#include <thread>

class FNET_Transport;

namespace document { class DocumentTypeRepo; }

namespace proton {

class BootstrapConfig;
class DocumentDBConfigManager;
class IProtonConfigurer;

/**
 * A ProtonConfigFetcher monitors all config in proton and document dbs for change
 * and starts proton reconfiguration if config has been reloaded.
 */
class ProtonConfigFetcher
{
public:
    using BootstrapConfigSP = std::shared_ptr<BootstrapConfig>;

    ProtonConfigFetcher(FNET_Transport & transport, const config::ConfigUri & configUri, IProtonConfigurer &owner, vespalib::duration subscribeTimeout);
    ~ProtonConfigFetcher();
    /**
     * Get the current config generation.
     */
    int64_t getGeneration() const;

    /**
     * Start config fetcher, callbacks may come from now on.
     */
    void start();

    /**
     * Shutdown config fetcher, ensuring that no more callbacks arrive
     */
    void close();

    void run();

private:
    using DBManagerMap = std::map<DocTypeName, std::shared_ptr<DocumentDBConfigManager>>;
    using OldDocumentTypeRepo = std::pair<vespalib::steady_time, std::shared_ptr<const document::DocumentTypeRepo>>;
    using lock_guard = std::lock_guard<std::mutex>;


    FNET_Transport          & _transport;
    BootstrapConfigManager    _bootstrapConfigManager;
    config::ConfigRetriever   _retriever;
    IProtonConfigurer       & _owner;

    mutable std::mutex        _mutex; // Protects maps
    std::condition_variable   _cond;
    DBManagerMap              _dbManagerMap;
    bool                      _running;
    std::thread               _thread;
    
    std::deque<OldDocumentTypeRepo>                   _oldDocumentTypeRepos;
    std::shared_ptr<const document::DocumentTypeRepo> _currentDocumentTypeRepo;

    void fetchConfigs();
    void updateDocumentDBConfigs(const BootstrapConfigSP & config, const config::ConfigSnapshot & snapshot);
    void reconfigure();
    const config::ConfigKeySet pruneManagerMap(const BootstrapConfigSP & config);
    void rememberDocumentTypeRepo(std::shared_ptr<const document::DocumentTypeRepo> repo);
};

} // namespace proton