@@ -29,17 +29,29 @@ namespace rocketmq {
29
29
30
30
// <!************************************************************************
31
31
TcpRemotingClient::TcpRemotingClient (int pullThreadNum, uint64_t tcpConnectTimeout, uint64_t tcpTransportTryLockTimeout)
32
- : m_pullThreadNum(pullThreadNum),
32
+ : m_dispatchThreadNum(1 ),
33
+ m_pullThreadNum (pullThreadNum),
33
34
m_tcpConnectTimeout(tcpConnectTimeout),
34
35
m_tcpTransportTryLockTimeout(tcpTransportTryLockTimeout),
35
36
m_namesrvIndex(0 ),
36
- m_ioServiceWork(m_ioService) {
37
+ m_dispatchServiceWork(m_dispatchService),
38
+ m_handleServiceWork(m_handleService) {
37
39
#if !defined(WIN32) && !defined(__APPLE__)
38
40
string taskName = UtilAll::getProcessName ();
41
+ prctl (PR_SET_NAME, " DispatchTP" , 0 , 0 , 0 );
42
+ #endif
43
+ for (int i = 0 ; i != m_dispatchThreadNum; ++i) {
44
+ m_dispatchThreadPool.create_thread (boost::bind (&boost::asio::io_service::run, &m_dispatchService));
45
+ }
46
+ #if !defined(WIN32) && !defined(__APPLE__)
47
+ prctl (PR_SET_NAME, taskName.c_str (), 0 , 0 , 0 );
48
+ #endif
49
+
50
+ #if !defined(WIN32) && !defined(__APPLE__)
39
51
prctl (PR_SET_NAME, " NetworkTP" , 0 , 0 , 0 );
40
52
#endif
41
53
for (int i = 0 ; i != m_pullThreadNum; ++i) {
42
- m_threadpool .create_thread (boost::bind (&boost::asio::io_service::run, &m_ioService ));
54
+ m_handleThreadPool .create_thread (boost::bind (&boost::asio::io_service::run, &m_handleService ));
43
55
}
44
56
#if !defined(WIN32) && !defined(__APPLE__)
45
57
prctl (PR_SET_NAME, taskName.c_str (), 0 , 0 , 0 );
@@ -48,7 +60,7 @@ TcpRemotingClient::TcpRemotingClient(int pullThreadNum, uint64_t tcpConnectTimeo
48
60
LOG_INFO (" m_tcpConnectTimeout:%ju, m_tcpTransportTryLockTimeout:%ju, m_pullThreadNum:%d" , m_tcpConnectTimeout,
49
61
m_tcpTransportTryLockTimeout, m_pullThreadNum);
50
62
51
- m_async_service_thread .reset (new boost::thread (boost::bind (&TcpRemotingClient::boost_asio_work, this )));
63
+ m_timerServiceThread .reset (new boost::thread (boost::bind (&TcpRemotingClient::boost_asio_work, this )));
52
64
}
53
65
54
66
void TcpRemotingClient::boost_asio_work () {
@@ -59,9 +71,9 @@ void TcpRemotingClient::boost_asio_work() {
59
71
#endif
60
72
61
73
// avoid async io service stops after first timer timeout callback
62
- boost::asio::io_service::work work (m_async_ioService );
74
+ boost::asio::io_service::work work (m_timerService );
63
75
64
- m_async_ioService .run ();
76
+ m_timerService .run ();
65
77
}
66
78
67
79
TcpRemotingClient::~TcpRemotingClient () {
@@ -75,20 +87,24 @@ TcpRemotingClient::~TcpRemotingClient() {
75
87
void TcpRemotingClient::stopAllTcpTransportThread () {
76
88
LOG_DEBUG (" TcpRemotingClient::stopAllTcpTransportThread Begin" );
77
89
78
- m_async_ioService .stop ();
79
- m_async_service_thread ->interrupt ();
80
- m_async_service_thread ->join ();
90
+ m_timerService .stop ();
91
+ m_timerServiceThread ->interrupt ();
92
+ m_timerServiceThread ->join ();
81
93
removeAllTimerCallback ();
82
94
83
95
{
96
+ std::lock_guard<std::timed_mutex> lock (m_tcpTableLock);
84
97
for (const auto & trans : m_tcpTable) {
85
98
trans.second ->disconnect (trans.first );
86
99
}
87
100
m_tcpTable.clear ();
88
101
}
89
102
90
- m_ioService.stop ();
91
- m_threadpool.join_all ();
103
+ m_handleService.stop ();
104
+ m_handleThreadPool.join_all ();
105
+
106
+ m_dispatchService.stop ();
107
+ m_dispatchThreadPool.join_all ();
92
108
93
109
{
94
110
std::lock_guard<std::mutex> lock (m_futureTableLock);
@@ -98,7 +114,7 @@ void TcpRemotingClient::stopAllTcpTransportThread() {
98
114
}
99
115
}
100
116
101
- LOG_DEBUG (" TcpRemotingClient::stopAllTcpTransportThread End" );
117
+ LOG_ERROR (" TcpRemotingClient::stopAllTcpTransportThread End, m_tcpTable:%lu " , m_tcpTable. size () );
102
118
}
103
119
104
120
void TcpRemotingClient::updateNameServerAddressList (const string& addrs) {
@@ -226,13 +242,13 @@ bool TcpRemotingClient::invokeAsync(const string& addr,
226
242
227
243
if (callback) {
228
244
boost::asio::deadline_timer* t =
229
- new boost::asio::deadline_timer (m_async_ioService , boost::posix_time::milliseconds (timeoutMillis));
245
+ new boost::asio::deadline_timer (m_timerService , boost::posix_time::milliseconds (timeoutMillis));
230
246
addTimerCallback (t, opaque);
231
247
t->async_wait (
232
248
boost::bind (&TcpRemotingClient::handleAsyncRequestTimeout, this , boost::asio::placeholders::error, opaque));
233
249
}
234
250
235
- // Even if send failed, asyncTimerThread will trigger next pull request or report send msg failed
251
+ // even if send failed, asyncTimerThread will trigger next pull request or report send msg failed
236
252
if (SendCommand (pTcp, request)) {
237
253
LOG_DEBUG (" invokeAsync success, addr:%s, code:%d, opaque:%d" , addr.c_str (), code, opaque);
238
254
responseFuture->setSendRequestOK (true );
@@ -453,7 +469,7 @@ void TcpRemotingClient::static_messageReceived(void* context, const MemoryBlock&
453
469
}
454
470
455
471
void TcpRemotingClient::messageReceived (const MemoryBlock& mem, const string& addr) {
456
- m_ioService .post (boost::bind (&TcpRemotingClient::ProcessData, this , mem, addr));
472
+ m_dispatchService .post (boost::bind (&TcpRemotingClient::ProcessData, this , mem, addr));
457
473
}
458
474
459
475
void TcpRemotingClient::ProcessData (const MemoryBlock& mem, const string& addr) {
@@ -482,7 +498,7 @@ void TcpRemotingClient::ProcessData(const MemoryBlock& mem, const string& addr)
482
498
LOG_DEBUG (" find_response opaque:%d" , opaque);
483
499
processResponseCommand (pRespondCmd, pFuture);
484
500
} else {
485
- processRequestCommand ( pRespondCmd, addr);
501
+ m_handleService. post ( boost::bind (&TcpRemotingClient::processRequestCommand, this , pRespondCmd, addr) );
486
502
}
487
503
}
488
504
@@ -503,7 +519,8 @@ void TcpRemotingClient::processResponseCommand(RemotingCommand* pCmd, std::share
503
519
504
520
if (pFuture->getAsyncFlag ()) {
505
521
cancelTimerCallback (opaque);
506
- pFuture->invokeCompleteCallback ();
522
+
523
+ m_handleService.post (boost::bind (&ResponseFuture::invokeCompleteCallback, pFuture));
507
524
}
508
525
}
509
526
@@ -520,7 +537,7 @@ void TcpRemotingClient::handleAsyncRequestTimeout(const boost::system::error_cod
520
537
LOG_ERROR (" no response got for opaque:%d" , opaque);
521
538
eraseTimerCallback (opaque);
522
539
if (pFuture->getAsyncCallbackWrap ()) {
523
- pFuture-> invokeExceptionCallback ( );
540
+ m_handleService. post ( boost::bind (&ResponseFuture::invokeExceptionCallback, pFuture) );
524
541
}
525
542
}
526
543
}
0 commit comments