Skip to content

Commit a14580f

Browse files
authored
[ISSUE #137] split TcpRemotingClient::m_ioService into m_dispatchService and m_handleService
2 parents 94d97a0 + df4d218 commit a14580f

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

src/transport/TcpRemotingClient.cpp

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,29 @@ namespace rocketmq {
2929

3030
//<!************************************************************************
3131
TcpRemotingClient::TcpRemotingClient(int pullThreadNum, uint64_t tcpConnectTimeout, uint64_t tcpTransportTryLockTimeout)
32-
: m_pullThreadNum(pullThreadNum),
32+
: m_dispatchThreadNum(1),
33+
m_pullThreadNum(pullThreadNum),
3334
m_tcpConnectTimeout(tcpConnectTimeout),
3435
m_tcpTransportTryLockTimeout(tcpTransportTryLockTimeout),
3536
m_namesrvIndex(0),
36-
m_ioServiceWork(m_ioService) {
37+
m_dispatchServiceWork(m_dispatchService),
38+
m_handleServiceWork(m_handleService) {
3739
#if !defined(WIN32) && !defined(__APPLE__)
3840
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__)
3951
prctl(PR_SET_NAME, "NetworkTP", 0, 0, 0);
4052
#endif
4153
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));
4355
}
4456
#if !defined(WIN32) && !defined(__APPLE__)
4557
prctl(PR_SET_NAME, taskName.c_str(), 0, 0, 0);
@@ -48,7 +60,7 @@ TcpRemotingClient::TcpRemotingClient(int pullThreadNum, uint64_t tcpConnectTimeo
4860
LOG_INFO("m_tcpConnectTimeout:%ju, m_tcpTransportTryLockTimeout:%ju, m_pullThreadNum:%d", m_tcpConnectTimeout,
4961
m_tcpTransportTryLockTimeout, m_pullThreadNum);
5062

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)));
5264
}
5365

5466
void TcpRemotingClient::boost_asio_work() {
@@ -59,9 +71,9 @@ void TcpRemotingClient::boost_asio_work() {
5971
#endif
6072

6173
// 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);
6375

64-
m_async_ioService.run();
76+
m_timerService.run();
6577
}
6678

6779
TcpRemotingClient::~TcpRemotingClient() {
@@ -75,20 +87,24 @@ TcpRemotingClient::~TcpRemotingClient() {
7587
void TcpRemotingClient::stopAllTcpTransportThread() {
7688
LOG_DEBUG("TcpRemotingClient::stopAllTcpTransportThread Begin");
7789

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();
8193
removeAllTimerCallback();
8294

8395
{
96+
std::lock_guard<std::timed_mutex> lock(m_tcpTableLock);
8497
for (const auto& trans : m_tcpTable) {
8598
trans.second->disconnect(trans.first);
8699
}
87100
m_tcpTable.clear();
88101
}
89102

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();
92108

93109
{
94110
std::lock_guard<std::mutex> lock(m_futureTableLock);
@@ -98,7 +114,7 @@ void TcpRemotingClient::stopAllTcpTransportThread() {
98114
}
99115
}
100116

101-
LOG_DEBUG("TcpRemotingClient::stopAllTcpTransportThread End");
117+
LOG_ERROR("TcpRemotingClient::stopAllTcpTransportThread End, m_tcpTable:%lu", m_tcpTable.size());
102118
}
103119

104120
void TcpRemotingClient::updateNameServerAddressList(const string& addrs) {
@@ -226,13 +242,13 @@ bool TcpRemotingClient::invokeAsync(const string& addr,
226242

227243
if (callback) {
228244
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));
230246
addTimerCallback(t, opaque);
231247
t->async_wait(
232248
boost::bind(&TcpRemotingClient::handleAsyncRequestTimeout, this, boost::asio::placeholders::error, opaque));
233249
}
234250

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
236252
if (SendCommand(pTcp, request)) {
237253
LOG_DEBUG("invokeAsync success, addr:%s, code:%d, opaque:%d", addr.c_str(), code, opaque);
238254
responseFuture->setSendRequestOK(true);
@@ -453,7 +469,7 @@ void TcpRemotingClient::static_messageReceived(void* context, const MemoryBlock&
453469
}
454470

455471
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));
457473
}
458474

459475
void TcpRemotingClient::ProcessData(const MemoryBlock& mem, const string& addr) {
@@ -482,7 +498,7 @@ void TcpRemotingClient::ProcessData(const MemoryBlock& mem, const string& addr)
482498
LOG_DEBUG("find_response opaque:%d", opaque);
483499
processResponseCommand(pRespondCmd, pFuture);
484500
} else {
485-
processRequestCommand(pRespondCmd, addr);
501+
m_handleService.post(boost::bind(&TcpRemotingClient::processRequestCommand, this, pRespondCmd, addr));
486502
}
487503
}
488504

@@ -503,7 +519,8 @@ void TcpRemotingClient::processResponseCommand(RemotingCommand* pCmd, std::share
503519

504520
if (pFuture->getAsyncFlag()) {
505521
cancelTimerCallback(opaque);
506-
pFuture->invokeCompleteCallback();
522+
523+
m_handleService.post(boost::bind(&ResponseFuture::invokeCompleteCallback, pFuture));
507524
}
508525
}
509526

@@ -520,7 +537,7 @@ void TcpRemotingClient::handleAsyncRequestTimeout(const boost::system::error_cod
520537
LOG_ERROR("no response got for opaque:%d", opaque);
521538
eraseTimerCallback(opaque);
522539
if (pFuture->getAsyncCallbackWrap()) {
523-
pFuture->invokeExceptionCallback();
540+
m_handleService.post(boost::bind(&ResponseFuture::invokeExceptionCallback, pFuture));
524541
}
525542
}
526543
}

src/transport/TcpRemotingClient.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class TcpRemotingClient {
109109
AsyncTimerMap m_asyncTimerTable;
110110
std::mutex m_asyncTimerTableLock;
111111

112+
int m_dispatchThreadNum;
112113
int m_pullThreadNum;
113114
uint64_t m_tcpConnectTimeout; // ms
114115
uint64_t m_tcpTransportTryLockTimeout; // s
@@ -119,12 +120,16 @@ class TcpRemotingClient {
119120
string m_namesrvAddrChoosed;
120121
unsigned int m_namesrvIndex;
121122

122-
boost::asio::io_service m_ioService;
123-
boost::asio::io_service::work m_ioServiceWork;
124-
boost::thread_group m_threadpool;
123+
boost::asio::io_service m_dispatchService;
124+
boost::asio::io_service::work m_dispatchServiceWork;
125+
boost::thread_group m_dispatchThreadPool;
125126

126-
boost::asio::io_service m_async_ioService;
127-
unique_ptr<boost::thread> m_async_service_thread;
127+
boost::asio::io_service m_handleService;
128+
boost::asio::io_service::work m_handleServiceWork;
129+
boost::thread_group m_handleThreadPool;
130+
131+
boost::asio::io_service m_timerService;
132+
unique_ptr<boost::thread> m_timerServiceThread;
128133
};
129134

130135
//<!************************************************************************

0 commit comments

Comments
 (0)