aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/subscription/configsubscriptionset.h
blob: 8bdd8d5832cc07fee3833407ec4284b51bbc3853 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
//
#pragma once

#include "subscriptionid.h"
#include <vespa/vespalib/util/time.h>
#include <atomic>
#include <memory>
#include <vector>
#include <condition_variable>

namespace config {

class IConfigContext;
class IConfigManager;
class ConfigSubscription;
class ConfigKey;

/**
 * A ConfigSubscriptionSet is a set of configs that can be subscribed to.
 */
class ConfigSubscriptionSet
{
public:
    /**
     * Constructs a new ConfigSubscriptionSet object which can be used to subscribe for 1
     * or more configs from a specific source.
     *
     * @param context A ConfigContext shared between all subscriptions.
     */
    explicit ConfigSubscriptionSet(std::shared_ptr<IConfigContext> context);

    ConfigSubscriptionSet(const ConfigSubscriptionSet &) = delete;
    ConfigSubscriptionSet & operator= (const ConfigSubscriptionSet &) = delete;
    ~ConfigSubscriptionSet();

    /**
     * Return the current generation number for configs.
     *
     * @return generation number
     */
    int64_t getGeneration() const noexcept {
        return _currentGeneration.load(std::memory_order_relaxed);
    }

    /**
     * Closes the set, which will interrupt acquireSnapshot and unsubscribe all
     * configs currently subscribed for.
     */
    void close();

    /**
     * Checks if this subscription set is closed.
     */
    bool isClosed() const noexcept {
        return (_state.load(std::memory_order_relaxed) == CLOSED);
    }

    // Helpers for doing the subscription
    std::shared_ptr<ConfigSubscription> subscribe(const ConfigKey & key, vespalib::duration timeout);

    // Tries to acquire a new snapshot of config within the timeout
    bool acquireSnapshot(vespalib::duration timeout, bool requireDifference);

private:
    // Describes the state of the subscriber.
    enum SubscriberState { OPEN, FROZEN, CONFIGURED, CLOSED };
    using SubscriptionList = std::vector<std::shared_ptr<ConfigSubscription>>;

    const vespalib::duration        _maxNapTime;
    std::shared_ptr<IConfigContext> _context;             // Context to keep alive managers.
    IConfigManager &                _mgr;                 // The config manager that we use.
    std::atomic<int64_t>            _currentGeneration;   // Holds the current config generation.
    SubscriptionList                _subscriptionList;    // List of current subscriptions.
    std::atomic<SubscriberState>    _state;               // Current state of this subscriber.
    std::mutex                      _lock;
    std::condition_variable         _cond;
};

} // namespace config