Commit 938f2e85 authored by zhujiashun's avatar zhujiashun

health_check_using_rpc: refine code

parent 413db42c
...@@ -68,7 +68,12 @@ public: ...@@ -68,7 +68,12 @@ public:
class HealthCheckManager { class HealthCheckManager {
public: public:
static void StartCheck(SocketId id, int64_t check_interval_s) { static void StartCheck(SocketId id, int64_t check_interval_s);
static void* AppCheck(void* arg);
static void RunAppCheck(void* arg);
};
void HealthCheckManager::StartCheck(SocketId id, int64_t check_interval_s) {
SocketUniquePtr ptr; SocketUniquePtr ptr;
const int rc = Socket::AddressFailedAsWell(id, &ptr); const int rc = Socket::AddressFailedAsWell(id, &ptr);
if (rc < 0) { if (rc < 0) {
...@@ -93,9 +98,9 @@ public: ...@@ -93,9 +98,9 @@ public:
return; return;
} }
AppCheck(done); AppCheck(done);
} }
static void* AppCheck(void* arg) { void* HealthCheckManager::AppCheck(void* arg) {
OnAppHealthCheckDone* done = static_cast<OnAppHealthCheckDone*>(arg); OnAppHealthCheckDone* done = static_cast<OnAppHealthCheckDone*>(arg);
done->cntl.Reset(); done->cntl.Reset();
done->cntl.http_request().uri() = FLAGS_health_check_path; done->cntl.http_request().uri() = FLAGS_health_check_path;
...@@ -103,9 +108,9 @@ public: ...@@ -103,9 +108,9 @@ public:
done->last_check_time_ms = butil::gettimeofday_ms(); done->last_check_time_ms = butil::gettimeofday_ms();
done->channel.CallMethod(NULL, &done->cntl, NULL, NULL, done); done->channel.CallMethod(NULL, &done->cntl, NULL, NULL, done);
return NULL; return NULL;
} }
static void RunAppCheck(void* arg) { void HealthCheckManager::RunAppCheck(void* arg) {
bthread_t th = 0; bthread_t th = 0;
int rc = bthread_start_background( int rc = bthread_start_background(
&th, &BTHREAD_ATTR_NORMAL, AppCheck, arg); &th, &BTHREAD_ATTR_NORMAL, AppCheck, arg);
...@@ -114,8 +119,7 @@ public: ...@@ -114,8 +119,7 @@ public:
AppCheck(arg); AppCheck(arg);
return; return;
} }
} }
};
void OnAppHealthCheckDone::Run() { void OnAppHealthCheckDone::Run() {
std::unique_ptr<OnAppHealthCheckDone> self_guard(this); std::unique_ptr<OnAppHealthCheckDone> self_guard(this);
...@@ -129,6 +133,8 @@ void OnAppHealthCheckDone::Run() { ...@@ -129,6 +133,8 @@ void OnAppHealthCheckDone::Run() {
if (!cntl.Failed() || ptr->Failed()) { if (!cntl.Failed() || ptr->Failed()) {
LOG_IF(INFO, !cntl.Failed()) << "Succeeded to call " LOG_IF(INFO, !cntl.Failed()) << "Succeeded to call "
<< ptr->remote_side() << FLAGS_health_check_path; << ptr->remote_side() << FLAGS_health_check_path;
// if ptr->Failed(), previous SetFailed would trigger next round
// of hc, just return here.
ptr->_ninflight_app_health_check.fetch_sub( ptr->_ninflight_app_health_check.fetch_sub(
1, butil::memory_order_relaxed); 1, butil::memory_order_relaxed);
return; return;
...@@ -145,6 +151,9 @@ void OnAppHealthCheckDone::Run() { ...@@ -145,6 +151,9 @@ void OnAppHealthCheckDone::Run() {
&timer_id, abstime, HealthCheckManager::RunAppCheck, this); &timer_id, abstime, HealthCheckManager::RunAppCheck, this);
if (rc != 0) { if (rc != 0) {
LOG(ERROR) << "Fail to add timer for RunAppCheck"; LOG(ERROR) << "Fail to add timer for RunAppCheck";
// TODO(zhujiashun): we need to handle the case when timer fails.
// In most situations, the possibility of this case is quite small,
// so currently we just keep sending the hc call.
HealthCheckManager::AppCheck(this); HealthCheckManager::AppCheck(this);
return; return;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment