forked from Tencent/Pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc.cpp
More file actions
463 lines (379 loc) · 15.5 KB
/
Copy pathrpc.cpp
File metadata and controls
463 lines (379 loc) · 15.5 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Tencent is pleased to support the open source community by making Pebble available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/
#include <sstream>
#include <string.h>
#include "common/log.h"
#include "common/timer.h"
#include "common/time_utility.h"
#include "framework/rpc.h"
namespace pebble {
/// @brief RPC会话数据结构定义
struct RpcSession {
private:
RpcSession(const RpcSession& rhs) {
m_session_id = rhs.m_session_id;
m_handle = rhs.m_handle;
m_timerid = rhs.m_timerid;
m_start_time = rhs.m_start_time;
m_server_side = rhs.m_server_side;
}
public:
RpcSession() {
m_session_id = 0;
m_handle = 0;
m_timerid = -1;
m_start_time = 0;
m_server_side = false;
}
uint64_t m_session_id;
int64_t m_handle;
int64_t m_timerid;
int64_t m_start_time;
RpcHead m_rpc_head;
bool m_server_side;
OnRpcResponse m_rsp;
};
// TODO: timer改为外部传入
IRpc::IRpc() {
m_session_id = 0;
m_timer = new SequenceTimer();
m_proc_req_timeout_ms = REQ_PROC_TIMEOUT_MS;
}
IRpc::~IRpc() {
if (m_timer) {
delete m_timer;
m_timer = NULL;
}
}
int32_t IRpc::Update() {
int32_t num = 0;
if (m_timer) {
num += m_timer->Update();
}
return num;
}
int32_t IRpc::OnMessage(int64_t handle, const uint8_t* msg,
uint32_t msg_len, const MsgExternInfo* msg_info, uint32_t is_overload) {
if (NULL == msg || 0 == msg_len) {
PLOG_ERROR_N_EVERY_SECOND(1, "param invalid: buff = %p, buff_len = %u", msg, msg_len);
return kRPC_INVALID_PARAM;
}
RpcHead head;
int32_t head_len = HeadDecode(msg, msg_len, &head);
if (head_len < 0) {
PLOG_ERROR_N_EVERY_SECOND(1, "HeadDecode failed(%d).", head_len);
return kRPC_DECODE_FAILED;
}
if (head_len > static_cast<int32_t>(msg_len)) {
PLOG_ERROR_N_EVERY_SECOND(1, "head_len(%d) > buff_len(%u)", head_len, msg_len);
return kRPC_DECODE_FAILED;
}
if (msg_info) {
head.m_arrived_ms = msg_info->_msg_arrived_ms;
head.m_dst = (IProcessor*)(msg_info->_src);
}
const uint8_t* data = msg + head_len;
uint32_t data_len = msg_len - head_len;
int32_t ret = kRPC_UNKNOWN_TYPE;
switch (head.m_message_type) {
case kRPC_CALL:
if (is_overload != 0) {
ret = ResponseException(handle, kRPC_SYSTEM_OVERLOAD_BASE - is_overload, head);
RequestProcComplete(head.m_function_name, kRPC_SYSTEM_OVERLOAD_BASE - is_overload,
head.m_arrived_ms > 0 ? TimeUtility::GetCurrentMS() - head.m_arrived_ms : 0);
break;
}
case kRPC_ONEWAY:
ret = ProcessRequest(handle, head, data, data_len);
break;
case kRPC_REPLY:
case kRPC_EXCEPTION:
ret = ProcessResponse(head, data, data_len);
break;
default:
PLOG_ERROR_N_EVERY_SECOND(1, "rpc msg type error(%d)", head.m_message_type);
break;
}
return ret;
}
int32_t IRpc::AddOnRequestFunction(const std::string& name, const OnRpcRequest& on_request) {
if (name.empty() || !on_request) {
PLOG_ERROR("param invalid: name = %s, !on_request = %u", name.c_str(), !on_request);
return kRPC_INVALID_PARAM;
}
if (false == m_service_map.insert({name, on_request}).second) {
PLOG_ERROR("the %s is existed", name.c_str());
return kRPC_FUNCTION_NAME_EXISTED;
}
if (m_event_handler) {
m_event_handler->AddNameToStat(name);
}
return kRPC_SUCCESS;
}
int32_t IRpc::RemoveOnRequestFunction(const std::string& name) {
if (m_event_handler) {
m_event_handler->RemoveNameFromStat(name);
}
return m_service_map.erase(name) == 1 ? kRPC_SUCCESS : kRPC_FUNCTION_NAME_UNEXISTED;
}
void IRpc::GetResourceUsed(cxx::unordered_map<std::string, int64_t>* resource_info) {
if (!resource_info) {
return;
}
std::ostringstream timer;
timer << "Rpc(" << this << "):timer";
(*resource_info)[timer.str()] = m_timer->GetTimerNum();
std::ostringstream session;
session << "Rpc(" << this << "):session";
(*resource_info)[session.str()] = m_session_map.size();
return;
}
int32_t IRpc::SendRequest(int64_t handle,
const RpcHead& rpc_head,
const uint8_t* buff,
uint32_t buff_len,
const OnRpcResponse& on_rsp,
int32_t timeout_ms) {
// buff允许为空,长度非0时做非空检查
if (buff_len != 0 && NULL == buff) {
PLOG_ERROR_N_EVERY_SECOND(1, "param invalid: buff = %p, buff_len = %u", buff, buff_len);
return kRPC_INVALID_PARAM;
}
// 发送请求
int32_t ret = SendMessage(handle, rpc_head, buff, buff_len);
if (ret != kRPC_SUCCESS) {
ResponseProcComplete(rpc_head.m_function_name, kRPC_SEND_FAILED, 0);
return ret;
}
// ONEWAY请求
if (!on_rsp) {
ResponseProcComplete(rpc_head.m_function_name, kRPC_SUCCESS, 0);
return kRPC_SUCCESS;
}
// 保持会话
cxx::shared_ptr<RpcSession> session(new RpcSession());
session->m_session_id = rpc_head.m_session_id;
session->m_handle = handle;
session->m_rsp = on_rsp;
session->m_rpc_head = rpc_head;
session->m_server_side = false;
TimeoutCallback cb = cxx::bind(&IRpc::OnTimeout, this, session->m_session_id);
if (timeout_ms <= 0) {
timeout_ms = 10 * 1000;
}
session->m_timerid = m_timer->StartTimer(timeout_ms, cb);
session->m_start_time = TimeUtility::GetCurrentMS();
m_session_map[session->m_session_id] = session;
return kRPC_SUCCESS;
}
int32_t IRpc::BroadcastRequest(const std::string& name,
const RpcHead& rpc_head,
const uint8_t* buff,
uint32_t buff_len) {
// buff允许为空,长度非0时做非空检查
if (buff_len != 0 && NULL == buff) {
PLOG_ERROR_N_EVERY_SECOND(1, "param invalid: buff = %p, buff_len = %u", buff, buff_len);
return kRPC_INVALID_PARAM;
}
// 发送请求
int32_t head_len = HeadEncode(rpc_head, m_rpc_head_buff, sizeof(m_rpc_head_buff));
if (head_len < 0) {
PLOG_ERROR_N_EVERY_SECOND(1, "encode head failed(%d)", head_len);
return kRPC_ENCODE_FAILED;
}
const uint8_t* msg_frag[] = { m_rpc_head_buff, buff };
uint32_t msg_frag_len[] = { (uint32_t)head_len, buff_len };
int32_t num = m_broadcastv(name, sizeof(msg_frag) / sizeof(*msg_frag), msg_frag, msg_frag_len);
return num >= 0 ? kRPC_SUCCESS : kPRC_BROADCAST_FAILED;
}
int32_t IRpc::SendResponse(uint64_t session_id, int32_t ret,
const uint8_t* buff, uint32_t buff_len) {
cxx::unordered_map< uint64_t, cxx::shared_ptr<RpcSession> >::iterator it =
m_session_map.find(session_id);
if (m_session_map.end() == it) {
PLOG_ERROR("session %lu not found", session_id);
return kRPC_SESSION_NOT_FOUND;
}
m_timer->StopTimer(it->second->m_timerid);
int32_t result = kRPC_SUCCESS;
int32_t error_code = kRPC_SUCCESS;
if (kRPC_SUCCESS == ret) {
// 业务处理成功,构造响应消息返回
it->second->m_rpc_head.m_message_type = kRPC_REPLY;
result = SendMessage(it->second->m_handle, it->second->m_rpc_head, buff, buff_len);
error_code = result;
} else {
// 业务处理失败,构造异常消息携带错误信息返回
result = ResponseException(it->second->m_handle, ret, it->second->m_rpc_head, buff, buff_len);
error_code = ret;
}
RequestProcComplete(it->second->m_rpc_head.m_function_name,
error_code, TimeUtility::GetCurrentMS() - it->second->m_start_time);
m_session_map.erase(it);
return result;
}
int32_t IRpc::SendMessage(int64_t handle, const RpcHead& rpc_head,
const uint8_t* buff, uint32_t buff_len) {
int32_t head_len = HeadEncode(rpc_head, m_rpc_head_buff, sizeof(m_rpc_head_buff));
if (head_len < 0) {
PLOG_ERROR_N_EVERY_SECOND(1, "encode head failed(%d)", head_len);
return kRPC_ENCODE_FAILED;
}
const uint8_t* msg_frag[] = { m_rpc_head_buff, buff };
uint32_t msg_frag_len[] = { (uint32_t)head_len, buff_len };
// 消息原路返回,如果有消息来源,则返回到来源点,如果无消息来源,走默认发送流程
int32_t send_ret = 0;
if (rpc_head.m_dst) {
send_ret = rpc_head.m_dst->SendV(handle, sizeof(msg_frag)/sizeof(*msg_frag), msg_frag, msg_frag_len, 0);
} else {
send_ret = SendV(handle, sizeof(msg_frag)/sizeof(*msg_frag), msg_frag, msg_frag_len, 0);
}
if (send_ret != 0) {
PLOG_ERROR_N_EVERY_SECOND(1, "send failed %d", send_ret);
}
return send_ret;
}
int32_t IRpc::OnTimeout(uint64_t session_id) {
cxx::unordered_map< uint64_t, cxx::shared_ptr<RpcSession> >::iterator it =
m_session_map.find(session_id);
if (m_session_map.end() == it) {
PLOG_ERROR("session %lu not found", session_id);
return kTIMER_BE_REMOVED;
}
cxx::shared_ptr<RpcSession> session = it->second;
// request timeout
if (session->m_rsp) {
session->m_rsp(kRPC_REQUEST_TIMEOUT, NULL, 0);
ReportTransportQuality(session->m_handle, kRPC_REQUEST_TIMEOUT, 0);
}
if (session->m_server_side) {
RequestProcComplete(session->m_rpc_head.m_function_name,
kRPC_PROCESS_TIMEOUT, TimeUtility::GetCurrentMS() - session->m_start_time);
} else {
ResponseProcComplete(session->m_rpc_head.m_function_name,
kRPC_REQUEST_TIMEOUT, TimeUtility::GetCurrentMS() - session->m_start_time);
}
m_session_map.erase(session_id);
return kTIMER_BE_REMOVED;
}
int32_t IRpc::ProcessRequest(int64_t handle, const RpcHead& rpc_head,
const uint8_t* buff, uint32_t buff_len) {
return ProcessRequestImp(handle, rpc_head, buff, buff_len);
}
int32_t IRpc::ProcessRequestImp(int64_t handle, const RpcHead& rpc_head,
const uint8_t* buff, uint32_t buff_len) {
cxx::unordered_map<std::string, OnRpcRequest>::iterator it =
m_service_map.find(rpc_head.m_function_name);
if (m_service_map.end() == it) {
PLOG_ERROR_N_EVERY_SECOND(1, "%s's request proc func not found", rpc_head.m_function_name.c_str());
ResponseException(handle, kRPC_UNSUPPORT_FUNCTION_NAME, rpc_head);
RequestProcComplete(rpc_head.m_function_name, kRPC_UNSUPPORT_FUNCTION_NAME,
rpc_head.m_arrived_ms > 0 ? TimeUtility::GetCurrentMS() - rpc_head.m_arrived_ms : 0);
return kRPC_UNSUPPORT_FUNCTION_NAME;
}
if (kRPC_ONEWAY == rpc_head.m_message_type) {
cxx::function<int32_t(int32_t, const uint8_t*, uint32_t)> rsp; // NOLINT
int32_t ret = (it->second)(buff, buff_len, rsp);
RequestProcComplete(rpc_head.m_function_name, ret,
rpc_head.m_arrived_ms > 0 ? TimeUtility::GetCurrentMS() - rpc_head.m_arrived_ms : 0);
return ret;
}
// 请求处理也保持会话,方便扩展
cxx::shared_ptr<RpcSession> session(new RpcSession());
session->m_session_id = GenSessionId();
session->m_handle = handle;
session->m_rpc_head = rpc_head;
session->m_server_side = true;
TimeoutCallback cb = cxx::bind(&IRpc::OnTimeout, this, session->m_session_id);
session->m_timerid = m_timer->StartTimer(m_proc_req_timeout_ms, cb);
session->m_start_time = rpc_head.m_arrived_ms > 0 ? rpc_head.m_arrived_ms : TimeUtility::GetCurrentMS();
m_session_map[session->m_session_id] = session;
cxx::function<int32_t(int32_t, const uint8_t*, uint32_t)> rsp = cxx::bind( // NOLINT
&IRpc::SendResponse, this, session->m_session_id,
cxx::placeholders::_1, cxx::placeholders::_2, cxx::placeholders::_3);
return (it->second)(buff, buff_len, rsp);
}
int32_t IRpc::ProcessResponse(const RpcHead& rpc_head,
const uint8_t* buff, uint32_t buff_len) {
cxx::unordered_map< uint64_t, cxx::shared_ptr<RpcSession> >::iterator it =
m_session_map.find(rpc_head.m_session_id);
if (m_session_map.end() == it) {
PLOG_ERROR_N_EVERY_SECOND(1, "session(%lu) not found, function_name(%s)",
rpc_head.m_session_id, rpc_head.m_function_name.c_str());
return kRPC_SESSION_NOT_FOUND;
}
cxx::shared_ptr<RpcSession> session = it->second;
m_timer->StopTimer(session->m_timerid);
int ret = kRPC_SUCCESS;
const uint8_t* real_buff = buff;
uint32_t real_buff_len = buff_len;
RpcException exception;
if (kRPC_EXCEPTION == rpc_head.m_message_type) {
int32_t len = ExceptionDecode(buff, buff_len, &exception);
if (len < 0) {
PLOG_ERROR_N_EVERY_SECOND(1, "ExceptionDecode failed(%d), exception data len = %u", len, buff_len);
ret = kRPC_RECV_EXCEPTION_MSG;
real_buff = NULL;
real_buff_len = 0;
} else {
ret = exception.m_error_code;
real_buff = (const uint8_t*)exception.m_message.data();
real_buff_len = exception.m_message.size();
}
}
if (session->m_rsp) {
ret = session->m_rsp(ret, real_buff, real_buff_len);
}
int64_t time_cost = TimeUtility::GetCurrentMS() - session->m_start_time;
ReportTransportQuality(session->m_handle, ret, time_cost);
ResponseProcComplete(session->m_rpc_head.m_function_name, ret, time_cost);
m_session_map.erase(rpc_head.m_session_id);
return ret;
}
int32_t IRpc::ResponseException(int64_t handle, int32_t ret, const RpcHead& rpc_head,
const uint8_t* buff, uint32_t buff_len) {
(const_cast<RpcHead&>(rpc_head)).m_message_type = kRPC_EXCEPTION;
RpcException exception;
exception.m_error_code = ret;
if (buff_len < sizeof(m_rpc_exception_buff)) {
exception.m_message.assign((const char*)buff, buff_len);
} else {
// 业务处理失败,业务数据超长时只返回业务错误码
PLOG_ERROR_N_EVERY_SECOND(1, "exception msg too long %u", buff_len);
}
int32_t result = ExceptionEncode(exception, m_rpc_exception_buff, sizeof(m_rpc_exception_buff));
if (result < 0) {
PLOG_ERROR_N_EVERY_SECOND(1, "ExceptionEncode failed, ret = %d, exception data len = %u", result, buff_len);
result = 0;
}
return SendMessage(handle, rpc_head, m_rpc_exception_buff, result);
}
void IRpc::ReportTransportQuality(int64_t handle, int32_t ret_code,
int64_t time_cost_ms) {
if (m_event_handler) {
m_event_handler->ReportTransportQuality(handle, ret_code, time_cost_ms);
}
}
void IRpc::RequestProcComplete(const std::string& name,
int32_t result, int32_t time_cost_ms) {
if (m_event_handler) {
m_event_handler->RequestProcComplete(name, result, time_cost_ms);
}
}
void IRpc::ResponseProcComplete(const std::string& name,
int32_t result, int32_t time_cost_ms) {
if (m_event_handler) {
m_event_handler->ResponseProcComplete(name, result, time_cost_ms);
}
}
} // namespace pebble