zyearn commented on a change in pull request #694: Health check by rpc call
URL: https://github.com/apache/incubator-brpc/pull/694#discussion_r268419752
 
 

 ##########
 File path: src/brpc/socket.cpp
 ##########
 @@ -979,6 +995,89 @@ void HealthCheckTask::OnDestroyingTask() {
     delete this;
 }
 
+class HealthCheckChannel : public brpc::Channel {
+public:
+    HealthCheckChannel() {}
+    ~HealthCheckChannel() {}
+
+    int Init(SocketId id, const ChannelOptions* options);
+};
+
+int HealthCheckChannel::Init(SocketId id, const ChannelOptions* options) {
+    brpc::GlobalInitializeOrDie();
+    if (InitChannelOptions(options) != 0) {
+        return -1;
+    }
+    _server_id = id;
+    return 0;
+}
+
+class OnHealthCheckRPCDone : public google::protobuf::Closure {
+public:
+    void Run() {
+        std::unique_ptr<OnHealthCheckRPCDone> self_guard(this);
+        SocketUniquePtr ptr;
+        const int rc = Socket::AddressFailedAsWell(id, &ptr);
+        if (rc < 0) {
+            RPC_VLOG << "SocketId=" << id
+                     << " was abandoned during health checking";
+            return;
+        }
+        if (!cntl.Failed()) {
+            ptr->ResetHealthCheckingUsingRPC();
+            return;
+        }
+
+        // Socket::SetFailed() will trigger next round of hc, just
+        // return here.
+        if (cntl.Failed() && ptr->Failed()) {
+            return;
+        }
+        // the left case is cntl.Failed() && !ptr->Failed(),
+        // in which we should retry hc rpc.
+        RPC_VLOG << "Fail to health check using rpc, error="
+            << cntl.ErrorText();
+        bthread_usleep(interval_s * 1000000);
+        cntl.Reset();
+        cntl.http_request().uri() = FLAGS_health_check_path;
+        cntl.set_health_check_call(true);
+        channel.CallMethod(NULL, &cntl, NULL, NULL, self_guard.release());
+    }
+
+    HealthCheckChannel channel;
+    brpc::Controller cntl;
+    SocketId id;
+    int64_t interval_s;
+};
+
+class HealthCheckManager {
+public:
+    static void StartCheck(SocketId id, int64_t check_interval_s) {
+        SocketUniquePtr ptr;
+        const int rc = Socket::AddressFailedAsWell(id, &ptr);
+        if (rc < 0) {
+            RPC_VLOG << "SocketId=" << id
+                     << " was abandoned during health checking";
+            return;
+        }
+        OnHealthCheckRPCDone* done = new OnHealthCheckRPCDone;
+        done->id = id;
+        done->interval_s = check_interval_s;
+        brpc::ChannelOptions options;
+        options.protocol = "http";
+        options.max_retry = 0;
+        options.timeout_ms = FLAGS_health_check_timeout_ms;
+        if (done->channel.Init(id, &options) != 0) {
+            LOG(WARNING) << "Fail to init health check channel to SocketId=" 
<< id;
+            ptr->ResetHealthCheckingUsingRPC();
+            return;
 
 Review comment:
   å—¯Fixed

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to