raft_server_handlers.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package weed_server
  2. import (
  3. "github.com/cenkalti/backoff/v4"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  7. "net/http"
  8. "time"
  9. )
  10. type ClusterStatusResult struct {
  11. IsLeader bool `json:"IsLeader,omitempty"`
  12. Leader pb.ServerAddress `json:"Leader,omitempty"`
  13. Peers []string `json:"Peers,omitempty"`
  14. MaxVolumeId needle.VolumeId `json:"MaxVolumeId,omitempty"`
  15. }
  16. func (s *RaftServer) StatusHandler(w http.ResponseWriter, r *http.Request) {
  17. ret := ClusterStatusResult{
  18. IsLeader: s.topo.IsLeader(),
  19. Peers: s.Peers(),
  20. MaxVolumeId: s.topo.GetMaxVolumeId(),
  21. }
  22. if leader, e := s.topo.Leader(); e == nil {
  23. ret.Leader = leader
  24. }
  25. writeJsonQuiet(w, r, http.StatusOK, ret)
  26. }
  27. func (s *RaftServer) HealthzHandler(w http.ResponseWriter, r *http.Request) {
  28. leader, err := s.topo.Leader()
  29. if err != nil {
  30. w.WriteHeader(http.StatusServiceUnavailable)
  31. return
  32. }
  33. if s.serverAddr == leader {
  34. expBackoff := backoff.NewExponentialBackOff()
  35. expBackoff.InitialInterval = 20 * time.Millisecond
  36. expBackoff.MaxInterval = 1 * time.Second
  37. expBackoff.MaxElapsedTime = 5 * time.Second
  38. isLocked, err := backoff.RetryWithData(s.topo.IsChildLocked, expBackoff)
  39. if err != nil {
  40. glog.Errorf("HealthzHandler: %+v", err)
  41. }
  42. if isLocked {
  43. w.WriteHeader(http.StatusLocked)
  44. return
  45. }
  46. }
  47. w.WriteHeader(http.StatusOK)
  48. }
  49. func (s *RaftServer) StatsRaftHandler(w http.ResponseWriter, r *http.Request) {
  50. if s.RaftHashicorp == nil {
  51. writeJsonQuiet(w, r, http.StatusNotFound, nil)
  52. return
  53. }
  54. writeJsonQuiet(w, r, http.StatusOK, s.RaftHashicorp.Stats())
  55. }