2019-03-07 04:10:45 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2023-06-10 00:42:48 +08:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
2019-03-07 04:10:45 +08:00
|
|
|
"net/http"
|
2023-06-10 00:42:48 +08:00
|
|
|
"time"
|
2019-03-07 04:10:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ClusterStatusResult struct {
|
2021-09-13 13:47:52 +08:00
|
|
|
IsLeader bool `json:"IsLeader,omitempty"`
|
|
|
|
Leader pb.ServerAddress `json:"Leader,omitempty"`
|
|
|
|
Peers []string `json:"Peers,omitempty"`
|
|
|
|
MaxVolumeId needle.VolumeId `json:"MaxVolumeId,omitempty"`
|
2019-03-07 04:10:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RaftServer) StatusHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ret := ClusterStatusResult{
|
2020-10-05 00:56:17 +08:00
|
|
|
IsLeader: s.topo.IsLeader(),
|
|
|
|
Peers: s.Peers(),
|
|
|
|
MaxVolumeId: s.topo.GetMaxVolumeId(),
|
2019-03-07 04:10:45 +08:00
|
|
|
}
|
2020-10-05 00:56:17 +08:00
|
|
|
|
2019-03-07 04:10:45 +08:00
|
|
|
if leader, e := s.topo.Leader(); e == nil {
|
|
|
|
ret.Leader = leader
|
|
|
|
}
|
|
|
|
writeJsonQuiet(w, r, http.StatusOK, ret)
|
|
|
|
}
|
2022-04-04 22:16:06 +08:00
|
|
|
|
2022-07-31 01:26:56 +08:00
|
|
|
func (s *RaftServer) HealthzHandler(w http.ResponseWriter, r *http.Request) {
|
2023-06-10 00:42:48 +08:00
|
|
|
leader, err := s.topo.Leader()
|
2022-07-31 01:26:56 +08:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
2023-06-10 00:42:48 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.serverAddr == leader {
|
|
|
|
expBackoff := backoff.NewExponentialBackOff()
|
|
|
|
expBackoff.InitialInterval = 20 * time.Millisecond
|
|
|
|
expBackoff.MaxInterval = 1 * time.Second
|
|
|
|
expBackoff.MaxElapsedTime = 5 * time.Second
|
|
|
|
isLocked, err := backoff.RetryWithData(s.topo.IsChildLocked, expBackoff)
|
2023-07-11 14:38:52 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("HealthzHandler: %+v", err)
|
|
|
|
}
|
2023-06-10 00:42:48 +08:00
|
|
|
if isLocked {
|
|
|
|
w.WriteHeader(http.StatusLocked)
|
|
|
|
return
|
|
|
|
}
|
2022-07-31 01:26:56 +08:00
|
|
|
}
|
2023-06-10 00:42:48 +08:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2022-07-31 01:26:56 +08:00
|
|
|
}
|
|
|
|
|
2022-04-04 22:16:06 +08:00
|
|
|
func (s *RaftServer) StatsRaftHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if s.RaftHashicorp == nil {
|
|
|
|
writeJsonQuiet(w, r, http.StatusNotFound, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writeJsonQuiet(w, r, http.StatusOK, s.RaftHashicorp.Stats())
|
|
|
|
}
|