summaryrefslogtreecommitdiffstats
path: root/configd/src/apps/sentinel/env.cpp
blob: 9f76df9c05a3b53322babf1b082305bba136afef (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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "env.h"
#include "check-completion-handler.h"
#include "outward-check.h"
#include <vespa/defaults.h>
#include <vespa/log/log.h>
#include <vespa/config/common/exceptions.h>
#include <vespa/vespalib/util/exceptions.h>
#include <thread>
#include <chrono>

LOG_SETUP(".env");

using namespace std::chrono_literals;

namespace config::sentinel {

constexpr std::chrono::milliseconds CONFIG_TIMEOUT_MS = 3min;
constexpr std::chrono::milliseconds MODEL_TIMEOUT_MS = 1500ms;

Env::Env()
  : _cfgOwner(),
    _rpcCommandQueue(),
    _rpcServer(),
    _stateApi(),
    _startMetrics(),
    _stateServer(),
    _statePort(0)
{
    _startMetrics.startedTime = vespalib::steady_clock::now();
}

Env::~Env() = default;

void Env::boot(const std::string &configId) {
    LOG(debug, "Reading configuration for ID: %s", configId.c_str());
    _cfgOwner.subscribe(configId, CONFIG_TIMEOUT_MS);
    bool ok = _cfgOwner.checkForConfigUpdate();
    // subscribe() should throw if something is not OK
    LOG_ASSERT(ok && _cfgOwner.hasConfig());
    const auto & cfg = _cfgOwner.getConfig();
    LOG(config, "Booting sentinel '%s' with [stateserver port %d] and [rpc port %d]",
        configId.c_str(), cfg.port.telnet, cfg.port.rpc);
    rpcPort(cfg.port.rpc);
    statePort(cfg.port.telnet);
    if (auto up = ConfigOwner::fetchModelConfig(MODEL_TIMEOUT_MS)) {
        waitForConnectivity(*up);
    }
}

void Env::rpcPort(int port) {
    if (port < 0 || port > 65535) {
        throw vespalib::FatalException("Bad port " + std::to_string(port) + ", expected range [1, 65535]", VESPA_STRLOC);
    }
    if (port == 0) {
        port = 19097; // default in config
    }
    if (_rpcServer && port == _rpcServer->getPort()) {
        return; // ok already
    }
    _rpcServer = std::make_unique<RpcServer>(port, _rpcCommandQueue);
}

void Env::statePort(int port) {
    if (port < 0 || port > 65535) {
        throw vespalib::FatalException("Bad port " + std::to_string(port) + ", expected range [1, 65535]", VESPA_STRLOC);
    }
    if (port == 0) {
        port = 19098; // default in config
    }
    if (_stateServer && port == _statePort) {
        return; // ok already
    }
    LOG(debug, "Config-sentinel accepts state connections on port %d", port);
    _stateServer = std::make_unique<vespalib::StateServer>(
        port, _stateApi.myHealth, _startMetrics.producer, _stateApi.myComponents);
    _statePort = port;
}

void Env::notifyConfigUpdated() {
    vespalib::ComponentConfigProducer::Config current("sentinel", _cfgOwner.getGeneration(), "ok");
    _stateApi.myComponents.addConfig(current);

}

void Env::respondAsEmpty() {
    auto commands = _rpcCommandQueue.drain();
    for (Cmd::UP &cmd : commands) {
        cmd->retError("still booting, not ready for all RPC commands");
    }
}

void Env::waitForConnectivity(const ModelConfig &model) {
    std::map<std::string, OutwardCheck> connectivityMap;
    for (const auto & h : model.hosts) {
        bool foundSentinelPort = false;
        for (const auto & s : h.services) {       
            if (s.name == "config-sentinel") { 
                for (const auto & p : s.ports) {
                    if (p.tags.find("rpc") != p.tags.npos) {
                        connectivityMap.try_emplace(h.name, h.name, p.number, _rpcServer->orb());
                        foundSentinelPort = true;
                    }
                }
            }
        }
        if (! foundSentinelPort) {
            LOG(warning, "Did not find 'config-sentinel' RPC port in model for host %s [%zd services]",
                h.name.c_str(), h.services.size());
        }
    }
    size_t cntOk = 0;
    size_t cntBad = 0;
    for (int retry = 1; retry <= 100; ++retry) {
        cntOk = 0;
        cntBad = 0;
        for (const auto & [hostname, check] : connectivityMap) {
            if (check.ok()) {
                ++cntOk;
            } else if (check.bad()) {
                ++cntBad;
            }
        }
        if (cntOk + cntBad == connectivityMap.size()) break;
        respondAsEmpty();
        std::this_thread::sleep_for(15ms);
        if ((retry % 20) == 0) {
            LOG(warning, "still waiting for connectivity checks after %d retries", retry);
        }
    }
    for (const auto & [hostname, check] : connectivityMap) {
        const char *s = "unknown";
        if (check.ok()) { s = "ok"; }
        if (check.bad()) { s = "bad"; }
        LOG(info, "outward check status for host %s is: %s",
            hostname.c_str(), s);
    }
    LOG_ASSERT(cntOk + cntBad == connectivityMap.size());
    const char *myName = vespa::Defaults::vespaHostname();
    int myPort = _rpcServer->getPort();
    OutwardCheck selfCheck(myName, myPort, _rpcServer->orb());
    for (int retry = 0; retry < 1000; ++retry) {
        if (selfCheck.bad()) {
            LOG(error, "Could not connect to '%s' (myself) at port %d", myName, myPort);
            throw InvalidConfigException("failed to self-connect");
        }
        if (selfCheck.ok()) {
            break;
        }
        std::this_thread::sleep_for(5ms);
    }
    LOG_ASSERT(selfCheck.ok());
}

}