aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/subscription/configsubscription.cpp
blob: fa292d288df214ede16ec412588837c63bd6d726 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "configsubscription.h"
#include <vespa/config/common/configupdate.h>
#include <vespa/config/common/iconfigholder.h>
#include <vespa/config/common/exceptions.h>
#include <vespa/config/common/misc.h>

namespace config {

ConfigSubscription::ConfigSubscription(const SubscriptionId & id, const ConfigKey & key,
                                       std::shared_ptr<IConfigHolder> holder, std::unique_ptr<Source> source)
    : _id(id),
      _key(key),
      _source(std::move(source)),
      _holder(std::move(holder)),
      _next(),
      _current(),
      _isChanged(false),
      _lastGenerationChanged(-1),
      _closed(false)
{
}

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


bool
ConfigSubscription::nextUpdate(int64_t generation, vespalib::steady_time deadline)
{
    if (_closed || !_holder->poll()) {
        return false;
    }
    auto old = std::move(_next);
    _next = _holder->provide();
    if (old) {
        _next->merge(*old);
    }
    if (isGenerationNewer(_next->getGeneration(), generation)) {
        return true;
    }
    return (!_closed && _holder->wait_until(deadline));
}

bool
ConfigSubscription::hasGenerationChanged() const
{
    return (!_closed && _next && ((_current && (_current->getGeneration() != _next->getGeneration())) || ! _current));
}

bool
ConfigSubscription::hasChanged() const
{
    return (!_closed && _next && ((_next->hasChanged() && _current && (_current->getValue() != _next->getValue())) || ! _current));
}

int64_t
ConfigSubscription::getGeneration() const
{
    return _next->getGeneration();
}

void
ConfigSubscription::close()
{
    if (!_closed.exchange(true)) {
        _holder->close();
        _source->close();
    }
}

void
ConfigSubscription::flip()
{
    bool change = hasChanged();
    if (change) {
        _current = std::move(_next);
        _lastGenerationChanged = _current->getGeneration();
    } else {
        _current = std::make_unique<ConfigUpdate>(_current->getValue(), false, _next->getGeneration());
    }
    _isChanged = change;
}

const ConfigValue &
ConfigSubscription::getConfig() const
{
    if (_closed) {
        throw ConfigRuntimeException("Subscription is closed, config no longer available");
    }
    if ( ! _current) {
        throw ConfigRuntimeException("No configuration available");
    }
    return _current->getValue();
}

void
ConfigSubscription::reload(int64_t generation)
{
    _source->reload(generation);
    _source->getConfig();
}

} // namespace config