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

#include "rpchooks.h"
#include "check-completion-handler.h"
#include "cmdq.h"
#include "peer-check.h"
#include "report-connectivity.h"
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/rpcrequest.h>

#include <vespa/log/log.h>
LOG_SETUP(".sentinel.rpchooks");

namespace config::sentinel {

RPCHooks::RPCHooks(CommandQueue &commands, FRT_Supervisor &supervisor, ModelOwner &modelOwner)
  : _commands(commands),
    _orb(supervisor),
    _modelOwner(modelOwner)
{
    initRPC(&_orb);
}

RPCHooks::~RPCHooks() = default;


void
RPCHooks::initRPC(FRT_Supervisor *supervisor)
{
    FRT_ReflectionBuilder rb(supervisor);

    //-------------------------------------------------------------------------
    rb.DefineMethod("sentinel.ls", "", "s",
                    FRT_METHOD(RPCHooks::rpc_listServices), this);
    rb.MethodDesc("list services");
    rb.ReturnDesc("status", "Status for services");
    //-------------------------------------------------------------------------
    rb.DefineMethod("sentinel.service.restart", "s", "",
                    FRT_METHOD(RPCHooks::rpc_restartService), this);
    rb.MethodDesc("restart a service");
    //-------------------------------------------------------------------------
    rb.DefineMethod("sentinel.service.stop", "s", "",
                    FRT_METHOD(RPCHooks::rpc_stopService), this);
    rb.MethodDesc("stop a service");
    //-------------------------------------------------------------------------
    rb.DefineMethod("sentinel.service.start", "s", "",
                    FRT_METHOD(RPCHooks::rpc_startService), this);
    rb.MethodDesc("start a service");
    //-------------------------------------------------------------------------
    rb.DefineMethod("sentinel.check.connectivity", "sii", "s",
                    FRT_METHOD(RPCHooks::rpc_checkConnectivity), this);
    rb.MethodDesc("check connectivity for peer sentinel");
    rb.ParamDesc("name", "Hostname of peer sentinel");
    rb.ParamDesc("port", "Port number of peer sentinel");
    rb.ParamDesc("timeout", "Timeout for check in milliseconds");
    rb.ReturnDesc("status", "Status (ok, bad, or unknown) for peer");
    //-------------------------------------------------------------------------
    rb.DefineMethod("sentinel.report.connectivity", "i", "SS",
                    FRT_METHOD(RPCHooks::rpc_reportConnectivity), this);
    rb.MethodDesc("report connectivity for peer sentinels");
    rb.ParamDesc("timeout", "Timeout for check in milliseconds");
    rb.ReturnDesc("hostnames", "Names of peers checked");
    rb.ReturnDesc("peerstatus", "Status description for each peer");
    //-------------------------------------------------------------------------
}

void
RPCHooks::rpc_listServices(FRT_RPCRequest *req)
{
    LOG(debug, "got listservices");
    req->Detach();
    _commands.enqueue(std::make_unique<Cmd>(req, Cmd::LIST));
}

void
RPCHooks::rpc_restartService(FRT_RPCRequest *req)
{
    FRT_Values &args  = *req->GetParams();
    const char *srvNM = args[0]._string._str;
    LOG(debug, "got restartservice '%s'", srvNM);
    req->Detach();
    _commands.enqueue(std::make_unique<Cmd>(req, Cmd::RESTART, srvNM));
}

void
RPCHooks::rpc_stopService(FRT_RPCRequest *req)
{
    FRT_Values &args  = *req->GetParams();
    const char *srvNM = args[0]._string._str;
    LOG(debug, "got stopservice '%s'", srvNM);
    req->Detach();
    _commands.enqueue(std::make_unique<Cmd>(req, Cmd::STOP, srvNM));
}

void
RPCHooks::rpc_startService(FRT_RPCRequest *req)
{
    FRT_Values &args  = *req->GetParams();
    const char *srvNM = args[0]._string._str;
    LOG(debug, "got startservice '%s'", srvNM);
    req->Detach();
    _commands.enqueue(std::make_unique<Cmd>(req, Cmd::START, srvNM));
}

void
RPCHooks::rpc_checkConnectivity(FRT_RPCRequest *req)
{
    FRT_Values &args  = *req->GetParams();
    const char *hostname = args[0]._string._str;
    int portnum = args[1]._intval32;
    int timeout = args[2]._intval32;
    LOG(debug, "got checkConnectivity %s [port %d] timeout %d", hostname, portnum, timeout);
    req->Detach();
    auto & completionHandler = req->getStash().create<CheckCompletionHandler>(req);
    req->getStash().create<PeerCheck>(completionHandler, hostname, portnum, _orb, timeout);
}

void
RPCHooks::rpc_reportConnectivity(FRT_RPCRequest *req)
{
    LOG(debug, "got reportConnectivity");
    FRT_Values &args  = *req->GetParams();
    int timeout = args[0]._intval32;
    req->Detach();
    req->getStash().create<ReportConnectivity>(req, timeout, _orb, _modelOwner);
}

} // namespace slobrok