From ad3d2132cd5856d1c5ce4885c6f02d45da3429a9 Mon Sep 17 00:00:00 2001 From: GeekXu Date: Fri, 18 Mar 2022 11:09:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20API=20response=20of=20api?= =?UTF-8?q?/status?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit API response of api/status to a follower node in raft mode returns RaftHealthyMembers field of null ✅ Closes: #14333 --- go/process/health.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/go/process/health.go b/go/process/health.go index deaef2ca8..fea7fde08 100644 --- a/go/process/health.go +++ b/go/process/health.go @@ -17,6 +17,8 @@ package process import ( + "encoding/json" + "fmt" "sync" "sync/atomic" "time" @@ -25,7 +27,7 @@ import ( "github.com/openark/orchestrator/go/util" "github.com/openark/golib/log" - "github.com/openark/orchestrator/go/raft" + orcraft "github.com/openark/orchestrator/go/raft" "github.com/patrickmn/go-cache" ) @@ -121,6 +123,8 @@ func HealthTest() (health *HealthStatus, err error) { health.Healthy = healthy } + orcraft.IsLeader() + if orcraft.IsRaftEnabled() { health.ActiveNode.Hostname = orcraft.GetLeader() health.IsActiveNode = orcraft.IsLeader() @@ -137,6 +141,32 @@ func HealthTest() (health *HealthStatus, err error) { } health.AvailableNodes, err = ReadAvailableNodes(true) + if !orcraft.IsLeader() { + resBytes, err := orcraft.HttpGetLeader("status") + if err != nil { + health.Error = err + return health, log.Errore(err) + } + type LeaderResp struct { + Code string + Message string + Details interface{} + } + var apiResp LeaderResp + if err = json.Unmarshal(resBytes, &apiResp); err != nil { + health.Error = err + return health, log.Errore(err) + } + healthOfLeader, ok := apiResp.Details.(map[string]interface{}) + if ok { + var rhm []string + for _, v := range healthOfLeader["RaftHealthyMembers"].([]interface{}) { + rhm = append(rhm, fmt.Sprint(v)) + } + health.RaftHealthyMembers = rhm + } + } + return health, nil }