From 1a4a36d510dceae8b9f79ff77c326795fe560707 Mon Sep 17 00:00:00 2001 From: Patrick Schmidt Date: Sat, 30 Jul 2022 19:26:56 +0200 Subject: [PATCH] Add healthy indicator for raft status --- weed/command/master.go | 7 +++++-- weed/server/raft_server_handlers.go | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/weed/command/master.go b/weed/command/master.go index 93c2bb57d..bbae29cc2 100644 --- a/weed/command/master.go +++ b/weed/command/master.go @@ -2,19 +2,21 @@ package command import ( "fmt" - "golang.org/x/exp/slices" "net/http" "os" "path" "strings" "time" + "golang.org/x/exp/slices" + "github.com/gorilla/mux" "github.com/seaweedfs/raft/protobuf" - stats_collect "github.com/seaweedfs/seaweedfs/weed/stats" "github.com/spf13/viper" "google.golang.org/grpc/reflection" + stats_collect "github.com/seaweedfs/seaweedfs/weed/stats" + "github.com/seaweedfs/seaweedfs/weed/util/grace" "github.com/seaweedfs/seaweedfs/weed/glog" @@ -179,6 +181,7 @@ func startMaster(masterOption MasterOptions, masterWhiteList []string) { } ms.SetRaftServer(raftServer) r.HandleFunc("/cluster/status", raftServer.StatusHandler).Methods("GET") + r.HandleFunc("/cluster/healthz", raftServer.HealthzHandler).Methods("GET", "HEAD") if *m.raftHashicorp { r.HandleFunc("/raft/stats", raftServer.StatsRaftHandler).Methods("GET") } diff --git a/weed/server/raft_server_handlers.go b/weed/server/raft_server_handlers.go index e9feb7050..9ed9a0c90 100644 --- a/weed/server/raft_server_handlers.go +++ b/weed/server/raft_server_handlers.go @@ -26,6 +26,15 @@ func (s *RaftServer) StatusHandler(w http.ResponseWriter, r *http.Request) { writeJsonQuiet(w, r, http.StatusOK, ret) } +func (s *RaftServer) HealthzHandler(w http.ResponseWriter, r *http.Request) { + _, err := s.topo.Leader() + if err != nil { + w.WriteHeader(http.StatusServiceUnavailable) + } else { + w.WriteHeader(http.StatusOK) + } +} + func (s *RaftServer) StatsRaftHandler(w http.ResponseWriter, r *http.Request) { if s.RaftHashicorp == nil { writeJsonQuiet(w, r, http.StatusNotFound, nil)