Skip to content

Commit 553aa4f

Browse files
committed
Bugfix: fixed the following bugs
- Resolved RPC call would hang in fiber UDP IO pool when exceeding max_conn_num threshold.​ - Adjust the heartbeat reporting logic to after port listening, to avoid service failures from premature traffic during RegisterService-to-port-binding latency.
1 parent b7c7de8 commit 553aa4f

6 files changed

Lines changed: 43 additions & 17 deletions

File tree

trpc/naming/domain/selector_domain_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ TEST(SelectorDomainTest, select_test) {
7878
result.context = MakeRefCounted<ClientContext>(trpc_codec);
7979
result.context->SetCallerName("test_service");
8080

81-
result.context->SetAddr("192.168.0.1", 1001);
81+
result.context->SetAddr("127.0.0.1", 1001);
8282
int ret = ptr->ReportInvokeResult(&result);
8383
EXPECT_EQ(0, ret);
8484

trpc/server/trpc_server.cc

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ bool TrpcServer::Start() {
167167
if (!iter.second->Listen()) {
168168
return false;
169169
}
170+
171+
RegisterServiceHeartBeatInfo(iter.first, iter.second);
170172
} else {
171173
TRPC_FMT_DEBUG("Service {} is not auto-started.", iter.first);
172174
}
@@ -310,18 +312,6 @@ TrpcServer::RegisterRetCode TrpcServer::RegisterService(const std::string& servi
310312
}
311313
}
312314

313-
if (TrpcConfig::GetInstance()->GetGlobalConfig().heartbeat_config.enable_heartbeat) {
314-
TRPC_FMT_DEBUG("service_name:{} report heartbeat info", service_name);
315-
316-
ServiceHeartBeatInfo heartbeat_info;
317-
auto const& option = service_adapter_it->second->GetServiceAdapterOption();
318-
heartbeat_info.service_name = service_name;
319-
heartbeat_info.host = option.ip;
320-
heartbeat_info.port = option.port;
321-
heartbeat_info.group_name = option.threadmodel_instance_name;
322-
HeartBeatReport::GetInstance()->RegisterServiceHeartBeatInfo(std::move(heartbeat_info));
323-
}
324-
325315
return RegisterRetCode::kOk;
326316
}
327317

@@ -351,6 +341,7 @@ TrpcServer::RegisterRetCode TrpcServer::RegisterService(const ServiceConfig& con
351341
}
352342
}
353343

344+
RegisterServiceHeartBeatInfo(config.service_name, service_adapter);
354345
return RegisterRetCode::kOk;
355346
}
356347

@@ -374,6 +365,7 @@ bool TrpcServer::StartService(const std::string& service_name) {
374365
return false;
375366
}
376367

368+
RegisterServiceHeartBeatInfo(service_name, service_adapter_it->second);
377369
return true;
378370
}
379371

@@ -472,4 +464,25 @@ std::shared_ptr<TrpcServer> GetTrpcServer() {
472464
return server;
473465
}
474466

467+
void TrpcServer::RegisterServiceHeartBeatInfo(const std::string& service_name,
468+
const ServiceAdapterPtr& service_adapter) {
469+
if (TrpcConfig::GetInstance()->GetGlobalConfig().heartbeat_config.enable_heartbeat) {
470+
if (service_name == admin_service_name_) {
471+
// Admin service does not need to report heartbeat
472+
return;
473+
}
474+
475+
TRPC_FMT_DEBUG("service_name:{} report heartbeat info", service_name);
476+
477+
// Register service heartbeat info to heartbeat thread
478+
ServiceHeartBeatInfo heartbeat_info;
479+
auto const& option = service_adapter->GetServiceAdapterOption();
480+
heartbeat_info.service_name = service_name;
481+
heartbeat_info.host = option.ip;
482+
heartbeat_info.port = option.port;
483+
heartbeat_info.group_name = option.threadmodel_instance_name;
484+
HeartBeatReport::GetInstance()->RegisterServiceHeartBeatInfo(std::move(heartbeat_info));
485+
}
486+
}
487+
475488
} // namespace trpc

trpc/server/trpc_server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class TrpcServer {
149149
void BuildTrpcRegistryInfo(const ServiceAdapterOption& option, TrpcRegistryInfo& registry_info);
150150
bool CheckSharedTransportConfig(const ServiceConfig& first_service_conf, const ServiceConfig& service_conf);
151151
std::string GetSharedKey(const ServiceConfig& config);
152+
void RegisterServiceHeartBeatInfo(const std::string& service_name, const ServiceAdapterPtr& service_adapter);
152153

153154
private:
154155
// stop running

trpc/transport/client/fiber/conn_pool/fiber_udp_io_pool_connector.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ void FiberUdpIoPoolConnector::Destroy() {
6161
}
6262
}
6363

64+
void FiberUdpIoPoolConnector::DoClose() {
65+
// We need to start a fiber for reclaim here to avoid having both reclaim (which depends on read_events being 0 to
66+
// prevent hanging) and message handling occur in the same fiber, which would cause a deadlock.
67+
bool start_fiber = StartFiberDetached([this, ref = RefPtr(ref_ptr, this)]() mutable {
68+
Stop();
69+
Destroy();
70+
});
71+
72+
TRPC_ASSERT(start_fiber && "StartFiberDetached failed when CloseConnection");
73+
}
74+
6475
bool FiberUdpIoPoolConnector::CreateFiberUdpTransceiver(uint64_t conn_id) {
6576
auto socket = Socket::CreateUdpSocket(options_.is_ipv6);
6677
if (!socket.IsValid()) {

trpc/transport/client/fiber/conn_pool/fiber_udp_io_pool_connector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class FiberUdpIoPoolConnector final : public RefCounted<FiberUdpIoPoolConnector>
5252

5353
void Destroy();
5454

55+
void DoClose();
56+
5557
void SaveCallContext(CTransportReqMsg* req_msg, CTransportRspMsg* rsp_msg, OnCompletionFunction&& cb);
5658

5759
void SendReqMsg(CTransportReqMsg* req_msg);

trpc/transport/client/fiber/conn_pool/fiber_udp_io_pool_connector_group.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,16 @@ void FiberUdpIoPoolConnectorGroup::Reclaim(uint64_t id) {
180180

181181
void FiberUdpIoPoolConnectorGroup::Reclaim(int ret, RefPtr<FiberUdpIoPoolConnector>& connector) {
182182
if (connector->GetConnId() == 0) {
183-
connector->Stop();
184-
connector->Destroy();
183+
connector->DoClose();
185184
return;
186185
}
187186

188187
if (ret == 0) {
189188
Reclaim(connector->GetConnId());
190189
} else {
191-
connector->Stop();
192-
connector->Destroy();
190+
connector->DoClose();
193191
udp_pool_[connector->GetConnId()] = nullptr;
192+
Reclaim(connector->GetConnId());
194193
}
195194
}
196195

0 commit comments

Comments
 (0)