aboutsummaryrefslogtreecommitdiffstats
path: root/configd/src/apps/sentinel/model-owner.cpp
blob: 93024911f4e9bb0c5c90daa0c192ddc8f1c25ff1 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "model-owner.h"
#include <vespa/config/common/exceptions.h>
#include <vespa/config/subscription/configsubscriber.hpp>
#include <cinttypes>

#include <vespa/log/log.h>
LOG_SETUP(".sentinel.model-owner");

using namespace std::chrono_literals;

namespace config::sentinel {

std::optional<ModelConfig> ModelOwner::getModelConfig() {
    std::lock_guard<std::mutex> guard(_lock);
    if (_modelConfig) {
        return ModelConfig(*_modelConfig);
    } else {
        return {};
    }
}


ModelOwner::ModelOwner(const std::string &configId)
  : _configId(configId)
{}

ModelOwner::~ModelOwner() = default;

void
ModelOwner::start(std::chrono::milliseconds timeout, bool firstTime) {
    try {
        _modelHandle =_subscriber.subscribe<ModelConfig>(_configId, timeout);
    } catch (ConfigTimeoutException & ex) {
        if (firstTime) {
            LOG(warning, "Timeout getting model config: %s [skipping connectivity checks]", ex.message());
        }
    } catch (InvalidConfigException& ex) {
        if (firstTime) {
            LOG(warning, "Invalid model config: %s [skipping connectivity checks]", ex.message());
        }
    } catch (ConfigRuntimeException& ex) {
        if (firstTime) {
            LOG(warning, "Runtime exception getting model config: %s [skipping connectivity checks]", ex.message());
        }
    }
}

void
ModelOwner::checkForUpdates() {
    if (! _modelHandle) {
        start(250ms, false);
    }
    if (_modelHandle && _subscriber.nextGenerationNow()) {
        if (auto newModel = _modelHandle->getConfig()) {
            LOG(config, "Sentinel got model info [version %s] for %zd hosts [config generation %" PRId64 "]",
                newModel->vespaVersion.c_str(), newModel->hosts.size(), _subscriber.getGeneration());
            std::lock_guard<std::mutex> guard(_lock);
            _modelConfig = std::move(newModel);
        }
    }
}

}