aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/vespa/documentapi/messagebus/policies/externslobrokpolicy.cpp
blob: a3d982575de918d6373885a8dae00f2c50d6ed5d (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "externslobrokpolicy.h"
#include "mirror_with_all.h"
#include <vespa/messagebus/routing/routingcontext.h>
#include <vespa/config/common/configcontext.h>
#include <vespa/vespalib/text/stringtokenizer.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/slobrok/sbmirror.h>
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/transport.h>
#include <thread>

using slobrok::api::IMirrorAPI;
using slobrok::api::MirrorAPI;
using slobrok::ConfiguratorFactory;

namespace documentapi {

ExternSlobrokPolicy::ExternSlobrokPolicy(const std::map<string, string>& param)
    : AsyncInitializationPolicy(param),
      _firstTry(true),
      _mirrorWithAll(),
      _slobrokConfigId("client")
{
    if (param.find("config") != param.end()) {
       vespalib::StringTokenizer configServers(param.find("config")->second, ",");
        for (uint32_t j = 0; j < configServers.size(); j++) {
            _configSources.push_back(configServers[j]);
        }
    }

    if (param.find("slobroks") != param.end()) {
        vespalib::StringTokenizer slobrokList(param.find("slobroks")->second, ",");
        for (uint32_t j = 0; j < slobrokList.size(); j++) {
            _slobroks.push_back(slobrokList[j]);
        }
    }

    if (param.find("slobrokconfigid") != param.end()) {
        _slobrokConfigId = param.find("slobrokconfigid")->second;
    }

    if (_slobroks.size() || _configSources.size()) {
        needAsynchronousInit();
    }
}

const IMirrorAPI*
ExternSlobrokPolicy::getMirror() const {
    return _mirrorWithAll ? _mirrorWithAll->mirror() : nullptr;
}

ExternSlobrokPolicy::~ExternSlobrokPolicy() = default;

string
ExternSlobrokPolicy::init() {
    std::lock_guard guard(_lock);
    if (_slobroks.size() != 0) {
        ConfiguratorFactory config(_slobroks);
        _mirrorWithAll = std::make_unique<MirrorAndStuff>(config);
    } else if (_configSources.size() != 0) {
        ConfiguratorFactory config(
                config::ConfigUri(_slobrokConfigId,
                                  std::make_shared<config::ConfigContext>(config::ServerSpec(_configSources))));
        _mirrorWithAll = std::make_unique<MirrorAndStuff>(config);
    }

    return "";
}

IMirrorAPI::SpecList
ExternSlobrokPolicy::lookup(mbus::RoutingContext& context, const string& pattern) {
    std::lock_guard guard(_lock);

    const IMirrorAPI * myMirror = getMirror();
    const IMirrorAPI& mirror(myMirror ? *myMirror : context.getMirror());

    IMirrorAPI::SpecList entries = mirror.lookup(pattern);

    if (_firstTry) {
        int count = 0;
        while (entries.empty() && count < 100) {
            std::this_thread::sleep_for(50ms);
            entries = mirror.lookup(pattern);
            count++;
        }
    }

    _firstTry = false;

    return entries;
}

}