aboutsummaryrefslogtreecommitdiffstats
path: root/configutil/src/lib/configstatus.cpp
blob: 98a6bca7ba3cb0bcf79a774f4ad2e15a2e62c226 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "configstatus.h"
#include "tags.h"
#include <vespa/vespalib/data/slime/slime.h>
#include <vbench/http/http_result_handler.h>
#include <vbench/http/server_spec.h>
#include <vbench/http/http_client.h>
#include <vespa/config/common/exceptions.h>
#include <vespa/config/subscription/configsubscriber.hpp>
#include <iostream>

using configdefinitions::tagsContain;

struct ComponentTraverser : public vespalib::slime::ObjectTraverser
{
    const std::string _configId;
    std::string _component;
    enum {
        ROOT,
        COMPONENT
    } _state;
    std::map<std::string, int64_t> &_generations;

    ComponentTraverser(std::string configId,
                       std::map<std::string, int64_t> &generations)
        : _configId(configId), _state(ROOT), _generations(generations)
    {}

    ~ComponentTraverser();

    void object(const vespalib::slime::Inspector &inspector) {
        inspector.traverse(*this);
    }

    static void collect(const std::string configId, const vespalib::Slime &slime,
                        std::map<std::string, int64_t> &generations) {
        ComponentTraverser traverser(configId, generations);
        slime.get()["config"].traverse(traverser);
    }

    void field(const vespalib::Memory &symbol_name, const vespalib::slime::Inspector &inspector) override {
        switch (_state) {
        case ROOT:
            _component = symbol_name.make_string();
            _state = COMPONENT;
            inspector.traverse(*this);
            _state = ROOT;
            break;
        case COMPONENT:
            const std::string key = symbol_name.make_string();
            int64_t value;
            if (key == "generation") {
                if (inspector.type().getId() == vespalib::slime::DOUBLE::ID) {
                    value = (int64_t) inspector.asDouble();
                    _generations[_component] = value;
                } else if (inspector.type().getId() == vespalib::slime::LONG::ID) {
                    value = inspector.asLong();
                    _generations[_component] = value;
                } else {
                    value = 0;
                    std::cerr << _configId << ":" << _component <<
                        "Generation has wrong type" << std::endl;
                }
            }

            break;
        }
    }
};

ComponentTraverser::~ComponentTraverser() = default;

class MyHttpHandler : public vbench::HttpResultHandler {
private:
    std::string _json;
    std::string _error;
    std::string _configId;

public:

    MyHttpHandler(std::string configId)
        : _json(), _error(), _configId(configId)
    {}
    ~MyHttpHandler();

    void handleHeader(const vbench::string &name, const vbench::string &value) override {
        (void) name;
        (void) value;
    }

    void handleContent(const vbench::Memory &data) override {
        _json += std::string(data.data, data.size);
    }

    void handleFailure(const vbench::string &reason) override {
        std::cerr << _configId << ": Failed to fetch json: " << reason << std::endl;
        _error = reason;
    }

    bool failed() {
        return(_error.size() > 0);
    }

    std::string getJson() {
        return _json;
    }
};

MyHttpHandler::~MyHttpHandler() = default;

ConfigStatus::ConfigStatus(Flags flags, const config::ConfigUri &uri)
    : _cfg(), _flags(flags), _generation(0)
{
    if (_flags.verbose) {
        std::cerr << "Subscribing to model config with config id " <<
            uri.getConfigId() << std::endl;
    }
    try {
        config::ConfigSubscriber subscriber(uri.getContext());
        config::ConfigHandle<cloud::config::ModelConfig>::UP handle =
            subscriber.subscribe<cloud::config::ModelConfig>(uri.getConfigId());
        subscriber.nextConfigNow();
        _cfg = handle->getConfig();
        _generation = subscriber.getGeneration();
    } catch(config::ConfigRuntimeException &e) {
        std::cerr << e.getMessage() << std::endl;
    }

    if (_cfg.get() == NULL) {
        std::cerr << "FATAL ERROR: failed to get model configuration." << std::endl;
        std::_Exit(1);
    }
}

ConfigStatus::~ConfigStatus() = default;

int
ConfigStatus::action()
{
    bool allUpToDate = true;

    for (size_t i = 0; i < _cfg->hosts.size(); i++) {
        const cloud::config::ModelConfig::Hosts &hconf = _cfg->hosts[i];
        // TODO PERF: don't fetch entire model when we're only looking for
        // a subset of hosts.
        if (!_flags.host_filter.includes(hconf.name)) {
            continue;
        }

        for (size_t j = 0; j < hconf.services.size(); j++) {
            const cloud::config::ModelConfig::Hosts::Services &svc = hconf.services[j];
            if (svc.type == "configserver") {
                continue;
            }

            for (size_t k = 0; k < svc.ports.size(); k++) {
                std::string tags = svc.ports[k].tags;
                if (tagsContain(tags, "http") &&
                    tagsContain(tags, "state")) {
                    bool upToDate = checkServiceGeneration(svc.configid, hconf.name,
                                                           svc.ports[k].number,
                                                           "/state/v1/config");

                    if (!upToDate) {
                        if (svc.type == "searchnode" ||
                            svc.type == "logd")
                        {
                            std::cerr << "[generation not up-to-date ignored]" << std::endl;
                        } else {
                            allUpToDate = false;
                        }
                    }
                    break;
                }
            }
        }
    }

    return allUpToDate ? 0 : 1;
}

bool
ConfigStatus::fetch_json(std::string configId, std::string host, int port,
                         std::string path, std::string &data)
{
    MyHttpHandler myHandler(configId);
    auto crypto = vespalib::CryptoEngine::get_default();
    bool ok = vbench::HttpClient::fetch(*crypto, vbench::ServerSpec(host, port), path, myHandler);

    if (ok) {
        data = myHandler.getJson();
        return true;
    } else {
        return false;
    }
}

bool
ConfigStatus::checkServiceGeneration(std::string configId, std::string host, int port, std::string path)
{
    std::string data;
    vespalib::Slime slime;

    if (!fetch_json(configId, host, port, path, data)) {
        return false;
    }

    size_t size = vespalib::slime::JsonFormat::decode(data, slime);

    if (size == 0) {
        std::cerr << configId << ": JSON parsing failed" << std::endl;
        return false;
    }

    vespalib::SimpleBuffer buf;
    vespalib::slime::JsonFormat::encode(slime, buf, false);

    if (slime.get()["config"].valid()) {
        std::map<std::string, int64_t> generations;
        ComponentTraverser::collect(configId, slime, generations);
        bool upToDate = true;

        std::map<std::string, int64_t>::iterator iter;
        for (iter = generations.begin(); iter != generations.end(); iter++) {
            if (iter->second != _generation) {
                std::cout << configId << ":" << iter->first << " has generation " <<
                    iter->second << " not " << _generation << std::endl;
                upToDate = false;
            } else {
                if (_flags.verbose) {
                    std::cout << configId << ":" << iter->first <<
                        " has the latest generation " << iter->second << std::endl;
                }
            }
        }

        return upToDate;
    } else {
        std::cerr << configId << ": No valid config object" << std::endl;

        return false;
    }
}