forked from baidu/sofa-pbrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_message_stream.h
787 lines (696 loc) · 25 KB
/
rpc_message_stream.h
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
// Copyright (c) 2014 Baidu.com, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: qinzuoyan01@baidu.com (Qin Zuoyan)
#ifndef _SOFA_PBRPC_RPC_MESSAGE_STREAM_H_
#define _SOFA_PBRPC_RPC_MESSAGE_STREAM_H_
#include <deque>
#include <sofa/pbrpc/rpc_byte_stream.h>
#include <sofa/pbrpc/buffer.h>
#include <sofa/pbrpc/rpc_error_code.h>
#include <sofa/pbrpc/rpc_message_header.h>
#include <sofa/pbrpc/tran_buf_pool.h>
#include <sofa/pbrpc/flow_controller.h>
namespace sofa {
namespace pbrpc {
// The "SendCookie" type should has default constructor, and
// should be copyable.
template <typename SendCookie>
class RpcMessageStream : public RpcByteStream
{
public:
enum RoleType {
ROLE_TYPE_SERVER = 0,
ROLE_TYPE_CLIENT = 1,
};
public:
RpcMessageStream(RoleType role_type, IOService& io_service, const RpcEndpoint& endpoint)
: RpcByteStream(io_service, endpoint)
, _role_type(role_type)
, _pending_message_count(0)
, _pending_data_size(0)
, _pending_buffer_size(0)
, _swapped_message_count(0)
, _swapped_data_size(0)
, _swapped_buffer_size(0)
, _max_pending_buffer_size(0)
, _read_quota_token(1)
, _write_quota_token(1)
, _total_sent_count(0)
, _total_sent_size(0)
, _total_received_count(0)
, _total_received_size(0)
, _sent_size(0)
, _sending_data(NULL)
, _sending_size(0)
, _received_message_size(0)
, _received_header_size(0)
, _receiving_header_identified(false)
, _tran_buf(NULL)
, _receiving_data(NULL)
, _receiving_size(0)
, _send_token(TOKEN_FREE)
, _receive_token(TOKEN_FREE)
{}
virtual ~RpcMessageStream()
{
if (_tran_buf != NULL)
{
TranBufPool::free(_tran_buf);
_tran_buf = NULL;
}
}
// Async send message to the remote endpoint. The message may be first
// put into a pending queue if the channel is busy at that time.
//
// Before starting sending data, the hook function "on_sending" will
// be called to check if the message still need to be sent.
//
// If send succeed, callback "on_sent" will be called;
// else, callback "on_send_failed" will be called.
//
// @param message the message to send, including header, meta and data.
// @param cookie a cookie affiliated to the message, which can carry some
// user's customized info. It will not be sent, but would
// be passed to the callback function.
void async_send_message(
const ReadBufferPtr& message,
const SendCookie& cookie)
{
SOFA_PBRPC_FUNCTION_TRACE;
if (is_closed())
{
on_send_failed(RPC_ERROR_CONNECTION_CLOSED, message, cookie);
return;
}
put_into_pending_queue(message, cookie);
try_start_send();
}
// Set the flow controller.
void set_flow_controller(const FlowControllerPtr& flow_controller)
{
_flow_controller = flow_controller;
}
// Set the max size of pending buffer for send.
void set_max_pending_buffer_size(int64 max_pending_buffer_size)
{
_max_pending_buffer_size = max_pending_buffer_size;
}
// Get the max size of pending buffer for send.
int64 max_pending_buffer_size() const
{
return _max_pending_buffer_size;
}
// Get the current count of messages in the pending queue.
int pending_message_count() const
{
return _pending_message_count + _swapped_message_count;
}
// Get the current data size of messages in the pending queue.
int64 pending_data_size() const
{
return _pending_data_size + _swapped_data_size;
}
// Get the current buffer size occupied by the pending queue.
// This size may be larger than "pending_data_size".
int64 pending_buffer_size() const
{
return _pending_buffer_size + _swapped_buffer_size;
}
// Trigger receiving operator.
// @return true if suceessfully triggered
virtual bool trigger_receive()
{
_read_quota_token = 1;
return try_start_receive();
}
// Trigger sending operator.
// @return true if suceessfully triggered
virtual bool trigger_send()
{
_write_quota_token = 1;
return try_start_send();
}
// Get read quota token used for sorting the trigger order.
// Always no more than 0.
int read_quota_token() const
{
return _read_quota_token;
}
// Get write quota token used for sorting the trigger order.
// Always no more than 0.
int write_quota_token() const
{
return _write_quota_token;
}
protected:
// Hook function on the point of starting the sending.
//
// If return true, go ahead with the sending;
// else, cancel the sending.
//
// This hook may be useful when the message stayed in a sending waiting
// queue for some time, but when dequed, it is already timeout.
virtual bool on_sending(
const ReadBufferPtr& message,
const SendCookie& cookie) = 0;
// Callback function when send message succeed.
virtual void on_sent(
const ReadBufferPtr& message,
const SendCookie& cookie) = 0;
// Callback function when send message failed.
virtual void on_send_failed(
RpcErrorCode error_code,
const ReadBufferPtr& message,
const SendCookie& cookie) = 0;
// Hook function when received message.
// @param message the rough received message, including meta and data.
// @param meta_size the size of meta.
// @param data_size the size of data.
virtual void on_received(
const ReadBufferPtr& message,
int meta_size,
int64 data_size) = 0;
private:
virtual bool on_connected()
{
SOFA_PBRPC_FUNCTION_TRACE;
// prepare receiving
reset_receiving_env();
if (!reset_tran_buf())
{
close("out of memory");
return false;
}
return true;
}
// Callback of "async_read_some()".
virtual void on_read_some(
const boost::system::error_code& error,
std::size_t bytes_transferred)
{
SOFA_PBRPC_FUNCTION_TRACE;
if (error)
{
if (error != boost::asio::error::eof)
{
#if defined( LOG )
LOG(ERROR) << "on_read_some(): " << RpcEndpointToString(_remote_endpoint)
<< ": read error: " << error.message();
#else
SLOG(ERROR, "on_read_some(): %s: read error: %s",
RpcEndpointToString(_remote_endpoint).c_str(),
error.message().c_str());
#endif
}
close(error.message());
return;
}
_last_rw_ticks = _ticks;
++_total_received_count;
_total_received_size += bytes_transferred;
std::deque<ReceivedItem> received_messages;
if (!split_and_process_message(_receiving_data,
static_cast<int>(bytes_transferred), &received_messages))
{
close("broken stream");
return;
}
_receiving_data += bytes_transferred;
_receiving_size -= bytes_transferred;
// check if current tran buf is used out
if (_receiving_size == 0 && !reset_tran_buf())
{
close("out of memory");
return;
}
// release token
atomic_comp_swap(&_receive_token, TOKEN_FREE, TOKEN_LOCK);
// trigger next receive
try_start_receive();
// process messages
while (!is_closed() && !received_messages.empty())
{
const ReceivedItem& item = received_messages.front();
on_received(item.message, item.meta_size, item.data_size);
received_messages.pop_front();
}
}
// Callback of "async_write_some()".
virtual void on_write_some(
const boost::system::error_code& error,
std::size_t bytes_transferred)
{
SOFA_PBRPC_FUNCTION_TRACE;
if (error)
{
#if defined( LOG )
LOG(ERROR) << "on_write_some(): " << RpcEndpointToString(_remote_endpoint)
<< ": write error: " << error.message();
#else
SLOG(ERROR, "on_write_some(): %s: write error: %s",
RpcEndpointToString(_remote_endpoint).c_str(),
error.message().c_str());
#endif
on_send_failed(RPC_ERROR_CHANNEL_BROKEN, _sending_message, _sending_cookie);
clear_sending_env();
close(error.message());
return;
}
_last_rw_ticks = _ticks;
_sent_size += bytes_transferred;
++_total_sent_count;
_total_sent_size += bytes_transferred;
if (static_cast<int>(bytes_transferred) == _sending_size)
{
// seek to the next buf handle
if (_sending_message->Next(
reinterpret_cast<const void**>(&_sending_data),
&_sending_size))
{
// more buf to send
async_write_some(_sending_data, _sending_size);
}
else
{
// current message is completely sent
SCHECK_EQ(_sent_size, _sending_message->ByteCount());
// call hook function
on_sent(_sending_message, _sending_cookie);
clear_sending_env();
// release token
atomic_comp_swap(&_send_token, TOKEN_FREE, TOKEN_LOCK);
// trigger next send
try_start_send();
// trigger next receive if it is server, because receiving operator
// in server may be hanged for pending buffer full.
if (_role_type == ROLE_TYPE_SERVER)
try_start_receive();
}
}
else
{
// only sent part of the data
SCHECK_LT(static_cast<int>(bytes_transferred), _sending_size);
// start sending the remaining data
_sending_data += bytes_transferred;
_sending_size -= bytes_transferred;
async_write_some(_sending_data, _sending_size);
}
}
// Put an item into back of the pending queue.
//
// @return false if the pending queue is full.
void put_into_pending_queue(
const ReadBufferPtr& message,
const SendCookie& cookie)
{
SOFA_PBRPC_FUNCTION_TRACE;
ScopedLocker<FastLock> _(_pending_lock);
_pending_calls.push_back(PendingItem(message, cookie));
++_pending_message_count;
_pending_data_size += message->TotalCount();
_pending_buffer_size += message->BlockCount() * TranBufPool::block_size();
}
// Insert an item into front of the pending queue.
//
// @return false if the pending queue is full.
void insert_into_pending_queue(
const ReadBufferPtr& message,
const SendCookie& cookie)
{
SOFA_PBRPC_FUNCTION_TRACE;
_swapped_calls.push_back(PendingItem(message, cookie));
++_swapped_message_count;
_swapped_data_size += message->TotalCount();
_swapped_buffer_size += message->BlockCount() * TranBufPool::block_size();
}
// Get an item from the pending queue.
//
// @return false if the pending queue is empty.
bool get_from_pending_queue(
ReadBufferPtr* message,
SendCookie* cookie)
{
SOFA_PBRPC_FUNCTION_TRACE;
if (_swapped_calls.empty() && _pending_message_count > 0)
{
// swap from _pending_calls
ScopedLocker<FastLock> _(_pending_lock);
if (!_pending_calls.empty())
{
_swapped_calls.swap(_pending_calls);
_swapped_message_count = _pending_message_count;
_swapped_data_size = _pending_data_size;
_swapped_buffer_size = _pending_buffer_size;
_pending_message_count = 0;
_pending_data_size = 0;
_pending_buffer_size = 0;
}
}
if (!_swapped_calls.empty())
{
// fetch the top one
*message = _swapped_calls.front().message;
*cookie = _swapped_calls.front().cookie;
_swapped_calls.pop_front();
// update stats
--_swapped_message_count;
_swapped_data_size -= (*message)->TotalCount();
_swapped_buffer_size -= (*message)->BlockCount() * TranBufPool::block_size();
return true;
}
// no message found
return false;
}
// Try to start receiving messages. It will check that receiving is allowed,
// and try to acquire the reveive token.
//
// If failed, the receiving is ignored.
// If succeed, the token must be acquired.
bool try_start_receive()
{
SOFA_PBRPC_FUNCTION_TRACE;
if (_receive_token == TOKEN_LOCK)
{
// no token
return false;
}
if (_read_quota_token <= 0)
{
// last acquire quota failed
return false;
}
if (_role_type == ROLE_TYPE_SERVER
&& _pending_buffer_size > _max_pending_buffer_size)
{
// sending buffer full, should suspend receiving to wait
return false;
}
bool started = false;
if (is_connected()
&& atomic_comp_swap(&_receive_token, TOKEN_LOCK, TOKEN_FREE) == TOKEN_FREE)
{
SCHECK(_receiving_data != NULL);
SCHECK(_receiving_size > 0);
if((_read_quota_token = _flow_controller->acquire_read_quota(_receiving_size)) <= 0)
{
// no network quota
atomic_comp_swap(&_receive_token, TOKEN_FREE, TOKEN_LOCK);
}
else
{
// now start receive
async_read_some(_receiving_data, _receiving_size);
started = true;
}
}
return started;
}
// Try to start sending a message. It will check that sending is allowed,
// and try to acquire the send token.
//
// If failed, the sending is ignored.
// If succeed, the token must be acquired.
bool try_start_send()
{
SOFA_PBRPC_FUNCTION_TRACE;
if (_send_token == TOKEN_LOCK)
{
// no token
return false;
}
if (_write_quota_token <= 0)
{
// last acquire quota failed
return false;
}
bool started = false;
while (is_connected()
&& atomic_comp_swap(&_send_token, TOKEN_LOCK, TOKEN_FREE) == TOKEN_FREE)
{
if (get_from_pending_queue(&_sending_message, &_sending_cookie))
{
if (!on_sending(_sending_message, _sending_cookie))
{
// need not send
on_send_failed(RPC_ERROR_REQUEST_CANCELED, _sending_message, _sending_cookie);
clear_sending_env();
atomic_comp_swap(&_send_token, TOKEN_FREE, TOKEN_LOCK);
}
else if ((_write_quota_token =_flow_controller->acquire_write_quota(
_sending_message->TotalCount())) <= 0)
{
// no network quota
insert_into_pending_queue(_sending_message, _sending_cookie);
clear_sending_env();
atomic_comp_swap(&_send_token, TOKEN_FREE, TOKEN_LOCK);
break;
}
else
{
// now send
_sent_size = 0;
bool ret = _sending_message->Next(
reinterpret_cast<const void**>(&_sending_data), &_sending_size);
SCHECK(ret);
async_write_some(_sending_data, _sending_size);
started = true;
break;
}
}
else
{
// empty pending queue
atomic_comp_swap(&_send_token, TOKEN_FREE, TOKEN_LOCK);
break;
}
}
return started;
}
// Process "data", split message, and store found messages.
//
// @return true if no error occured, and found messages are stored
// in "received_messages", may be empty.
// @return false if error occured, for example bad message stream.
class ReceivedItem;
bool split_and_process_message(char* data, int size,
std::deque<ReceivedItem>* received_messages)
{
SOFA_PBRPC_FUNCTION_TRACE;
while (size > 0)
{
if (!_receiving_header_identified)
{
int consumed;
int identify_result = identify_message_header(data, size, &consumed);
if (identify_result > 0)
{
_receiving_header_identified = true;
if (consumed == size)
{
// all the data is consumed by header
return true;
}
else
{
data += consumed;
size -= consumed;
}
}
else if (identify_result == 0)
{
// in-complete header
return true;
}
else // identify_result < 0
{
return false;
}
}
int64 message_size = _receiving_header.message_size;
if (_received_message_size + size < message_size)
{
// all the remaining data belongs to the in-complete message
_receiving_message->Append(BufHandle(_tran_buf, size, data - _tran_buf));
_received_message_size += size;
return true;
}
int64 consume_size = message_size - _received_message_size;
_receiving_message->Append(BufHandle(_tran_buf, consume_size, data - _tran_buf));
received_messages->push_back(ReceivedItem(_receiving_message,
_receiving_header.meta_size, _receiving_header.data_size));
reset_receiving_env();
data += consume_size;
size -= consume_size;
}
return true;
}
// Identify a message header.
//
// @return 1 a header identified. The header is stored in "_receiving_header",
// @return 0 no header identified for incomplete data reason. All the data is
// consumed and copied into "_receiving_header".
// @return -1 the message stream is broken.
int identify_message_header(char* data, int size, int* consumed)
{
// copy the data into the correct position of the header
int header_size = static_cast<int>(sizeof(_receiving_header));
int copy_size = std::min(size, header_size - _received_header_size);
memcpy(reinterpret_cast<char*>(&_receiving_header) + _received_header_size,
data, copy_size);
_received_header_size += copy_size;
*consumed = copy_size;
if (_received_header_size < header_size)
{
// in-complete header
return 0;
}
else // complete header
{
// check header magic str
if (!_receiving_header.CheckMagicString())
{
#if defined( LOG )
LOG(ERROR) << "identify_message_header(): " << RpcEndpointToString(_remote_endpoint)
<< ": check magic string failed: " << _receiving_header.magic_str_value;
#else
SLOG(ERROR, "identify_message_header(): %s: check magic string failed: 0x%8x",
RpcEndpointToString(_remote_endpoint).c_str(), _receiving_header.magic_str_value);
#endif
return -1;
}
if (_receiving_header.meta_size + _receiving_header.data_size != _receiving_header.message_size)
{
#if defined( LOG )
LOG(ERROR) << "identify_message_header(): " << RpcEndpointToString(_remote_endpoint)
<< ": check size in header failed"
<< ": meta_size=" << _receiving_header.meta_size
<< ", data_size=" << _receiving_header.data_size
<< ", message_size=" << _receiving_header.message_size;
#else
SLOG(ERROR, "identify_message_header(): %s: "
"check size in header failed: meta_size=%d, data_size=%lld, message_size=%lld",
RpcEndpointToString(_remote_endpoint).c_str(),
_receiving_header.meta_size,
_receiving_header.data_size,
_receiving_header.message_size);
#endif
return -1;
}
return 1;
}
}
// Clear temp variables for sending message.
void clear_sending_env()
{
_sending_message.reset();
_sending_cookie = SendCookie();
_sent_size = 0;
_sending_data = NULL;
_sending_size = 0;
}
// Reset temp variables for receiving message.
void reset_receiving_env()
{
_receiving_message.reset(new ReadBuffer());
_received_message_size = 0;
_received_header_size = 0;
_receiving_header_identified = false;
}
// Reset tran buf variables for receiving message.
bool reset_tran_buf()
{
if (_tran_buf != NULL)
{
// free old buf, in fact just decrease ref count
TranBufPool::free(_tran_buf);
_tran_buf = NULL;
}
_tran_buf = reinterpret_cast<char*>(TranBufPool::malloc());
if(_tran_buf == NULL)
{
#if defined( LOG )
LOG(ERROR) << "reset_tran_buf(): " << RpcEndpointToString(_remote_endpoint)
<< ": malloc buffer failed: out of memory";
#else
SLOG(ERROR, "reset_tran_buf(): %s: malloc buffer failed: out of memory",
RpcEndpointToString(_remote_endpoint).c_str());
#endif
return false;
}
_receiving_data = reinterpret_cast<char*>(_tran_buf);
_receiving_size = TranBufPool::block_size();
return true;
}
protected:
RoleType _role_type;
private:
struct PendingItem
{
ReadBufferPtr message;
SendCookie cookie;
PendingItem(const ReadBufferPtr& _message,
const SendCookie& _cookie)
: message(_message)
, cookie(_cookie) {}
};
struct ReceivedItem
{
ReadBufferPtr message;
int meta_size;
int64 data_size;
ReceivedItem(const ReadBufferPtr& _message,
int _meta_size,
int64 _data_size)
: message(_message)
, meta_size(_meta_size)
, data_size(_data_size) {}
};
// TODO improve sync queue performance
volatile int _pending_message_count;
volatile int64 _pending_data_size;
volatile int64 _pending_buffer_size;
std::deque<PendingItem> _pending_calls;
FastLock _pending_lock;
std::deque<PendingItem> _swapped_calls;
volatile int _swapped_message_count;
volatile int64 _swapped_data_size;
volatile int64 _swapped_buffer_size;
// flow control
FlowControllerPtr _flow_controller;
int64 _max_pending_buffer_size;
volatile int32 _read_quota_token; // <=0 means no quota
volatile int32 _write_quota_token; // <=0 means no quota
// statistics
int64 _total_sent_count;
int64 _total_sent_size;
int64 _total_received_count;
int64 _total_received_size;
// temp variables for sending message
ReadBufferPtr _sending_message; //current sending message
SendCookie _sending_cookie; // cookie of the message
int64 _sent_size; // current sent size of the message
const char* _sending_data; // current sending data, weak ptr
int _sending_size; // size of the current sending data
// temp variables for receiving message
ReadBufferPtr _receiving_message; // current receiving message
int64 _received_message_size; // current received size of the message
RpcMessageHeader _receiving_header;
int _received_header_size;
bool _receiving_header_identified;
// tran buf for reading data
char* _tran_buf; // strong ptr
char* _receiving_data; // weak ptr
int64 _receiving_size;
// send and receive token
static const int TOKEN_FREE = 0;
static const int TOKEN_LOCK = 1;
volatile int _send_token;
volatile int _receive_token;
}; // class RpcMessageStream
} // namespace pbrpc
} // namespace sofa
#endif // _SOFA_PBRPC_RPC_MESSAGE_STREAM_H_
/* vim: set ts=4 sw=4 sts=4 tw=100 */