aboutsummaryrefslogtreecommitdiffstats
path: root/fnet/src/vespa/fnet/frt/invoker.cpp
blob: 421bd0c4d0bf9657ea3f48b28c0760cad74018ca (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "invoker.h"
#include "supervisor.h"
#include <vespa/fnet/channel.h>

#include <vespa/log/log.h>
LOG_SETUP(".fnet.frt.invoker");

FRT_SingleReqWait::FRT_SingleReqWait()
    : _lock(),
      _cond(),
      _done(false),
      _waiting(false)
{ }

FRT_SingleReqWait::~FRT_SingleReqWait() {}

void
FRT_SingleReqWait::WaitReq()
{
    std::unique_lock<std::mutex> guard(_lock);
    _waiting = true;
    while(!_done) {
        _cond.wait(guard);
    }
    _waiting = false;
}


void
FRT_SingleReqWait::RequestDone(FRT_RPCRequest *req)
{
    (void) req;
    std::lock_guard<std::mutex> guard(_lock);
    _done = true;
    if (_waiting) {
        _cond.notify_one();
    }
}


FRT_RPCInvoker::FRT_RPCInvoker(FRT_Supervisor *supervisor,
                               FRT_RPCRequest *req,
                               bool noReply)
    : _req(req),
      _method(supervisor->GetReflectionManager()
              ->LookupMethod(req->GetMethodName())),
      _noReply(noReply)
{
    if (LOG_WOULD_LOG(debug)) {
        std::string methodName(_req->GetMethodName(), _req->GetMethodNameLen());
        LOG(debug, "invoke(server) init: '%s'", methodName.c_str());
    }
    req->SetReturnHandler(this); // RPC req -> FNET_Connection link is via this ptr; set prior to access filter invocation.
    if (_method == nullptr) {
        if (!req->IsError()) { // may be BAD_REQUEST
            req->SetError(FRTE_RPC_NO_SUCH_METHOD);
        }
    } else if (!FRT_Values::CheckTypes(_method->GetParamSpec(),
                                       req->GetParamSpec()))
    {
        req->SetError(FRTE_RPC_WRONG_PARAMS);
    } else if (_method->GetRequestAccessFilter() &&
               !_method->GetRequestAccessFilter()->allow(*req))
    {
        req->SetError(FRTE_RPC_PERMISSION_DENIED);
    }
}

bool FRT_RPCInvoker::Invoke()
{
    bool detached = false;
    _req->SetDetachedPT(&detached);
    (_method->GetHandler()->*_method->GetMethod())(_req);
    if (detached)
        return false;
    HandleDone(false);
    return true;
}

void
FRT_RPCInvoker::HandleDone(bool freeChannel)
{
    FNET_Channel *ch = _req->GetContext()._value.CHANNEL;

    // check return value(s)
    if (!_req->IsError() &&
        !FRT_Values::CheckTypes(_method->GetReturnSpec(),
                                _req->GetReturnSpec()))
    {
        _req->SetError(FRTE_RPC_WRONG_RETURN);
    }
    if (LOG_WOULD_LOG(debug)) {
        std::string methodName(_req->GetMethodName(), _req->GetMethodNameLen());
        LOG(debug, "invoke(server) done: '%s': '%s'",
            methodName.c_str(), FRT_GetErrorCodeName(_req->GetErrorCode()));
    }
    // send response to client or get rid of it
    if (_noReply || (_req->GetErrorCode() == FRTE_RPC_BAD_REQUEST))
        _req->internal_subref();
    else
        ch->Send(_req->CreateReplyPacket());

    // free FNET channel (if not in packet delivery callback)
    if (freeChannel)
        ch->Free();
}

void
FRT_RPCInvoker::HandleReturn()
{
    HandleDone(true);
}


FNET_Connection *
FRT_RPCInvoker::GetConnection()
{
    return _req->GetContext()._value.CHANNEL->GetConnection();
}

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

void FRT_HookInvoker::Invoke()
{
    bool detached = false;
    _req->SetDetachedPT(&detached);
    (_hook->GetHandler()->*_hook->GetMethod())(_req);
    assert(!detached);
    _req->internal_subref();
}

void
FRT_HookInvoker::HandleReturn()
{
    // hooks cannot be detached
    LOG_ABORT("should not be reached");
}


FNET_Connection *
FRT_HookInvoker::GetConnection()
{
    return _conn;
}

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

FRT_RPCAdapter::FRT_RPCAdapter(FNET_Scheduler *scheduler,
                               FRT_RPCRequest *req,
                               FRT_IRequestWait *waiter)
    : FNET_Task(scheduler),
      _req(req),
      _waiter(waiter),
      _channel(nullptr)
{
    if (LOG_WOULD_LOG(debug)) {
        std::string methodName(_req->GetMethodName(), _req->GetMethodNameLen());
        LOG(debug, "invoke(client) init: '%s'", methodName.c_str());
    }
    req->SetAbortHandler(this);
}

void
FRT_RPCAdapter::HandleDone()
{
    if (LOG_WOULD_LOG(debug)) {
        std::string methodName(_req->GetMethodName(), _req->GetMethodNameLen());
        LOG(debug, "invoke(client) done: '%s': '%s'",
            methodName.c_str(), FRT_GetErrorCodeName(_req->GetErrorCode()));
    }
    // give req back to caller
    _waiter->RequestDone(_req);
}

bool
FRT_RPCAdapter::HandleAbort()
{
    if (!_req->GetCompletionToken()) { // too late
        return false;
    }
    if (_channel != nullptr) {
        _channel->CloseAndFree();
    }
    Kill();
    _req->SetError(FRTE_RPC_ABORT);
    HandleDone();
    return true;
}


void
FRT_RPCAdapter::PerformTask()
{
    if (!_req->GetCompletionToken()) { // too late
        return;
    }
    if (_channel != nullptr) {
        _channel->CloseAndFree();
    }
    if (!_req->IsError()) {
        _req->SetError(FRTE_RPC_TIMEOUT);
    }
    HandleDone();
}


FNET_IPacketHandler::HP_RetCode
FRT_RPCAdapter::HandlePacket(FNET_Packet *packet, FNET_Context)
{
    if (!_req->GetCompletionToken()) { // too late
        packet->Free();
        return FNET_KEEP_CHANNEL;
    }
    Kill();
    if (!packet->IsRegularPacket()) {
        if (packet->IsChannelLostCMD()) {
            _req->SetError(FRTE_RPC_CONNECTION);
        }
        if (packet->IsBadPacketCMD()) {
            _req->SetError(FRTE_RPC_BAD_REPLY);
        }
    }
    packet->Free();
    HandleDone();
    return FNET_FREE_CHANNEL;
}