aboutsummaryrefslogtreecommitdiffstats
path: root/slobrok/src/vespa/slobrok/server/reconfigurable_stateserver.cpp
blob: 2c64c7fa2df1b7972ebf7f9ad8cd58865919a4c9 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "reconfigurable_stateserver.h"
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/net/http/state_server.h>
#include <vespa/config/helper/configfetcher.hpp>
#include <thread>

#include <vespa/log/log.h>
LOG_SETUP(".slobrok.server.reconfigurable_stateserver");

using namespace std::chrono_literals;

namespace slobrok {

ReconfigurableStateServer::ReconfigurableStateServer(const config::ConfigUri & configUri,
                                                     vespalib::HealthProducer & health,
                                                     vespalib::MetricsProducer & metrics,
                                                     vespalib::ComponentConfigProducer & components)
    : _health(health),
      _metrics(metrics),
      _components(components),
      _configFetcher(std::make_unique<config::ConfigFetcher>(configUri.getContext())),
      _server()
{
    _configFetcher->subscribe<vespa::config::core::StateserverConfig>(configUri.getConfigId(), this);
    _configFetcher->start();
}

ReconfigurableStateServer::~ReconfigurableStateServer()
{
    _configFetcher->close();
}

void
ReconfigurableStateServer::configure(std::unique_ptr<vespa::config::core::StateserverConfig> config)
{
    _server.reset();
    for (size_t retryTime(1); !_server && (retryTime < 10); retryTime++) {
        try {
            _server = std::make_unique<vespalib::StateServer>(config->httpport, _health, _metrics, _components);
        } catch (vespalib::PortListenException & e) {
            LOG(warning, "Failed listening to network port(%d) with protocol(%s): '%s', will retry for 60s",
                e.get_port(), e.get_protocol().c_str(), e.what());
            std::this_thread::sleep_for(retryTime * 1s);
        }
    }
    if (!_server) {
        try {
            _server = std::make_unique<vespalib::StateServer>(config->httpport, _health, _metrics, _components);
        } catch (vespalib::PortListenException & e) {
            LOG(error, "Failed listening to network port(%d) with protocol(%s): '%s', giving up and restarting.",
                e.get_port(), e.get_protocol().c_str(), e.what());
            std::_Exit(17);
        }
    }

}

}