aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/server/rpc_hooks.cpp
blob: d4ae635d760a000402fc8bb76f9ca52a41871ddc (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "rpc_hooks.h"
#include "proton.h"
#include <vespa/searchcore/proton/matchengine/matchengine.h>
#include <vespa/vespalib/util/lambdatask.h>
#include <vespa/vespalib/util/compressionconfig.h>
#include <vespa/fnet/frt/require_capabilities.h>
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/transport.h>

#include <vespa/log/log.h>
LOG_SETUP(".proton.server.rtchooks");

using namespace vespalib;
using vespalib::compression::CompressionConfig;

namespace {

string delayed_configs_string("delayedConfigs");

using Pair = std::pair<string, string>;

}

namespace proton {

void
RPCHooksBase::reportState(FRT_RPCRequest * req)
{
    std::vector<Pair> res;
    int64_t numDocs(_proton.getNumDocs());
    std::string delayedConfigs = _proton.getDelayedConfigs();

    if (_proton.getMatchEngine().isOnline()) {
        res.emplace_back("online", "true");
        res.emplace_back("onlineState", "online");
    } else {
        res.emplace_back("online", "false");
        res.emplace_back("onlineState", "onlineSoon");
    }

    res.emplace_back(delayed_configs_string, delayedConfigs);
    res.emplace_back("onlineDocs", make_string("%" PRId64, numDocs));

    FRT_Values &ret = *req->GetReturn();
    FRT_StringValue *k = ret.AddStringArray(res.size());
    FRT_StringValue *v = ret.AddStringArray(res.size());
    for (uint32_t i = 0; i < res.size(); ++i) {
        ret.SetString(&k[i], res[i].first.c_str());
        ret.SetString(&v[i], res[i].second.c_str());
    }
    for (const auto & r : res) {
        LOG(debug, "key=%s, value=%s", r.first.c_str(), r.second.c_str());
    }
    ret.AddInt32(0);
}

namespace {

std::unique_ptr<FRT_RequireCapabilities> make_proton_admin_api_capability_filter() {
    return FRT_RequireCapabilities::of(vespalib::net::tls::Capability::content_proton_admin_api());
}

}

void
RPCHooksBase::initRPC()
{
    FRT_ReflectionBuilder rb(_orb.get());
    //-------------------------------------------------------------------------
    rb.DefineMethod("pandora.rtc.getState", "ii", "SSi",
                    FRT_METHOD(RPCHooksBase::rpc_GetState), this);
    rb.MethodDesc("Get the current state of node");
    rb.ParamDesc("gencnt", "old state generation held by the client");
    rb.ParamDesc("timeout", "How many milliseconds to wait for state update");
    rb.ReturnDesc("keys", "Array of state keys");
    rb.ReturnDesc("values", "Array of state values");
    rb.ReturnDesc("newgen", "New state generation count");
    rb.RequestAccessFilter(make_proton_admin_api_capability_filter());
    //-------------------------------------------------------------------------
    rb.DefineMethod("proton.getStatus", "s", "SSSS",
                    FRT_METHOD(RPCHooksBase::rpc_GetProtonStatus), this);
    rb.MethodDesc("Get the current state of proton or a proton component");
    rb.ParamDesc("component", "Which component to check the status for");
    rb.ReturnDesc("components", "Array of component names");
    rb.ReturnDesc("states", "Array of states ");
    rb.ReturnDesc("internalStates", "Array of internal states ");
    rb.ReturnDesc("message", "Array of status messages");
    rb.RequestAccessFilter(make_proton_admin_api_capability_filter());
    //-------------------------------------------------------------------------
    rb.DefineMethod("pandora.rtc.die", "", "",
                    FRT_METHOD(RPCHooksBase::rpc_die), this);
    rb.MethodDesc("Exit the rtc application without cleanup");
    rb.RequestAccessFilter(make_proton_admin_api_capability_filter());
    //-------------------------------------------------------------------------
    rb.DefineMethod("proton.triggerFlush", "", "b",
                    FRT_METHOD(RPCHooksBase::rpc_triggerFlush), this);
    rb.MethodDesc("Tell the node to trigger flush ASAP");
    rb.ReturnDesc("success", "Whether or not a flush was triggered.");
    rb.RequestAccessFilter(make_proton_admin_api_capability_filter());
    //-------------------------------------------------------------------------
    rb.DefineMethod("proton.prepareRestart", "", "b",
                    FRT_METHOD(RPCHooksBase::rpc_prepareRestart), this);
    rb.MethodDesc("Tell the node to prepare for a restart by flushing components "
            "such that TLS replay time + time spent flushing components is as low as possible");
    rb.ReturnDesc("success", "Whether or not prepare for restart was triggered.");
    rb.RequestAccessFilter(make_proton_admin_api_capability_filter());
}

RPCHooksBase::Params::Params(Proton &parent, uint32_t port, const config::ConfigUri & configUri,
                             vespalib::stringref slobrokId, uint32_t transportThreads)
    : proton(parent),
      slobrok_config(configUri.createWithNewId(slobrokId)),
      identity(configUri.getConfigId()),
      rtcPort(port),
      numTranportThreads(transportThreads)
{ }

RPCHooksBase::Params::~Params() = default;

RPCHooksBase::RPCHooksBase(Params &params)
    : _proton(params.proton),
      _transport(std::make_unique<FNET_Transport>(params.numTranportThreads)),
      _orb(std::make_unique<FRT_Supervisor>(_transport.get())),
      _proto_rpc_adapter(std::make_unique<ProtoRpcAdapter>(
                      _proton.get_search_server(),
                      _proton.get_docsum_server(),
                      _proton.get_monitor_server(), *_orb)),
      _regAPI(*_orb, slobrok::ConfiguratorFactory(params.slobrok_config))
{ }

void
RPCHooksBase::open(Params & params)
{
    initRPC();
    _regAPI.registerName((params.identity + "/realtimecontroller").c_str());
    _orb->Listen(params.rtcPort);
    _transport->Start();
    LOG(debug, "started monitoring interface");
}

void
RPCHooksBase::set_online()
{
    _proto_rpc_adapter->set_online();
}

RPCHooksBase::~RPCHooksBase() = default;

void
RPCHooksBase::close()
{
    LOG(info, "shutting down monitoring interface");
    _transport->ShutDown(true);
}

void
RPCHooksBase::letProtonDo(Executor::Task::UP task)
{
    _proton.getExecutor().execute(std::move(task));
}

void
RPCHooksBase::triggerFlush(FRT_RPCRequest *req)
{
    if (_proton.triggerFlush()) {
        req->GetReturn()->AddInt8(1);
        LOG(info, "RPCHooksBase::Flush finished successfully");
    } else {
        req->GetReturn()->AddInt8(0);
        LOG(warning, "RPCHooksBase::Flush failed");
    }
    req->Return();
}

void
RPCHooksBase::prepareRestart(FRT_RPCRequest *req)
{
    if (_proton.prepareRestart()) {
        req->GetReturn()->AddInt8(1);
        LOG(info, "RPCHooksBase::prepareRestart finished successfully");
    } else {
        req->GetReturn()->AddInt8(0);
        LOG(warning, "RPCHooksBase::prepareRestart failed");
    }
    req->Return();
}

void
RPCHooksBase::rpc_GetState(FRT_RPCRequest *req)
{
    FRT_Values &arg = *req->GetParams();
    uint32_t gen = arg[0]._intval32;
    uint32_t timeoutMS = arg[1]._intval32;
    LOG(debug, "RPCHooksBase::rpc_GetState(gen=%d, timeoutMS=%d)", gen, timeoutMS);

    reportState(req);
}

void
RPCHooksBase::rpc_GetProtonStatus(FRT_RPCRequest *req)
{
    LOG(debug, "RPCHooksBase::rpc_GetProtonStatus started");
    req->Detach();
    letProtonDo(makeLambdaTask([this, req]() { getProtonStatus(req); }));
}

void
RPCHooksBase::getProtonStatus(FRT_RPCRequest *req)
{
    StatusReport::List reports(_proton.getStatusReports());
    FRT_Values &ret = *req->GetReturn();
    FRT_StringValue *r = ret.AddStringArray(reports.size());
    FRT_StringValue *k = ret.AddStringArray(reports.size());
    FRT_StringValue *internalStates = ret.AddStringArray(reports.size());
    FRT_StringValue *v = ret.AddStringArray(reports.size());
    for (uint32_t i = 0; i < reports.size(); ++i) {
        const StatusReport & report = *reports[i];
        ret.SetString(&r[i], report.getComponent().c_str());
        switch (report.getState()) {
        case StatusReport::UPOK:
            ret.SetString(&k[i], "OK");
            break;
        case StatusReport::PARTIAL:
            ret.SetString(&k[i], "WARNING");
            break;
        case StatusReport::DOWN:
            ret.SetString(&k[i], "CRITICAL");
            break;
        default:
            ret.SetString(&k[i], "UNKNOWN");
            break;
        }
        ret.SetString(&internalStates[i], report.getInternalStatesStr().c_str());
        ret.SetString(&v[i], report.getMessage().c_str());
        LOG(debug, "component(%s), status(%s), internalState(%s), message(%s)",
                     report.getComponent().c_str(), k[i]._str, internalStates[i]._str, report.getMessage().c_str());
    }
    req->Return();
}

void
RPCHooksBase::rpc_die(FRT_RPCRequest * req)
{
    LOG(debug, "RPCHooksBase::rpc_die");
    req->Detach();
    letProtonDo(makeLambdaTask([req]() {
        LOG(debug, "Nap for 10ms and then quickly exit.");
        req->Return();
        std::this_thread::sleep_for(10ms);
        std::quick_exit(0);
    }));
}

void
RPCHooksBase::rpc_triggerFlush(FRT_RPCRequest *req)
{
    LOG(info, "RPCHooksBase::rpc_triggerFlush started");
    req->Detach();
    letProtonDo(makeLambdaTask([this, req]() { triggerFlush(req); }));
}

void
RPCHooksBase::rpc_prepareRestart(FRT_RPCRequest *req)
{
    LOG(info, "RPCHooksBase::rpc_prepareRestart started");
    req->Detach();
    letProtonDo(makeLambdaTask([this, req]() { prepareRestart(req); }));
}

RPCHooks::RPCHooks(Params &params) :
    RPCHooksBase(params)
{
    open(params);
}

}