summaryrefslogtreecommitdiffstats
path: root/slobrok/src/vespa/slobrok/server/managed_rpc_server.cpp
blob: 925df0e9f57f3215490f600e670ea412c58a87d7 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "managed_rpc_server.h"
#include "i_rpc_server_manager.h"
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/target.h>

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

namespace slobrok {

//-----------------------------------------------------------------------------

ManagedRpcServer::ManagedRpcServer(const char *name,
                                   const char *spec,
                                   IRpcServerManager &manager)
    : NamedService(name, spec),
      _mmanager(manager),
      _monitor(*this, *manager.getSupervisor()),
      _monitoredServer(NULL),
      _checkServerReq(NULL)
{
}


void
ManagedRpcServer::healthCheck()
{
    if (_monitoredServer == NULL) {
        _monitoredServer = _mmanager.getSupervisor()->GetTarget(_spec.c_str());
    }
    if (_checkServerReq == NULL) {
        _checkServerReq = _mmanager.getSupervisor()->AllocRPCRequest();
        _checkServerReq->SetMethodName("slobrok.callback.listNamesServed");
        _monitoredServer->InvokeAsync(_checkServerReq, 25.0, this);
    }
}


ManagedRpcServer::~ManagedRpcServer()
{
    LOG(debug, "(role[%s].~ManagedRpcServer)", _name.c_str());
    cleanupMonitor();
}


void
ManagedRpcServer::cleanupMonitor()
{
    _monitor.disable();
    if (_monitoredServer != NULL) {
        _monitoredServer->SubRef();
        _monitoredServer = NULL;
    }
    if (_checkServerReq != NULL) {
        _checkServerReq->Abort();
        // _checkServerReq cleared by RequestDone Method
        LOG_ASSERT(_checkServerReq == NULL);
    }
}

void
ManagedRpcServer::notifyDisconnected()
{
    cleanupMonitor();
    _mmanager.notifyFailedRpcSrv(this, "disconnected");
}


bool
ManagedRpcServer::validateRpcServer(uint32_t numstrings,
                                    FRT_StringValue *strings)
{
    for (uint32_t i = 0; i < numstrings; ++i) {
        if (strcmp(strings[i]._str, _name.c_str()) == 0) {
            return true;
        }
    }
    LOG(info, "REMOVE: server at %s did not have %s in listNamesServed values",
        _spec.c_str(), _name.c_str());
    return false;
}


void
ManagedRpcServer::RequestDone(FRT_RPCRequest *req)
{
    LOG_ASSERT(req == _checkServerReq);
    FRT_Values &answer = *(req->GetReturn());

    if (req->GetErrorCode() == FRTE_RPC_ABORT) {
        LOG(debug, "rpcserver[%s].check aborted", getName());
        req->SubRef();
        _checkServerReq = NULL;
        return;
    }

    if (req->IsError()
        || strcmp(answer.GetTypeString(), "S") != 0
        || ! validateRpcServer(answer[0]._string_array._len,
                               answer[0]._string_array._pt))
    {
        std::string errmsg;
        if (req->IsError()) {
            errmsg = req->GetErrorMessage();
        } else if (strcmp(answer.GetTypeString(), "S") != 0) {
            errmsg = "checkServer wrong return: ";
            errmsg += answer.GetTypeString();
        } else {
            errmsg = "checkServer failed validation";
        }
        req->SubRef();
        _checkServerReq = NULL;
        cleanupMonitor();
        _mmanager.notifyFailedRpcSrv(this, errmsg);
        return;
    }

    // start monitoring connection to server
    LOG_ASSERT(_monitoredServer != NULL);
    _monitor.enable(_monitoredServer);

    req->SubRef();
    _checkServerReq = NULL;
    _mmanager.notifyOkRpcSrv(this);
}


//-----------------------------------------------------------------------------

} // namespace slobrok