raft_handlers.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "encoding/json"
  4. "github.com/coreos/go-raft"
  5. "net/http"
  6. )
  7. //-------------------------------------------------------------
  8. // Handlers to handle raft related request via raft server port
  9. //-------------------------------------------------------------
  10. // Get all the current logs
  11. func GetLogHttpHandler(w http.ResponseWriter, req *http.Request) {
  12. debugf("[recv] GET %s/log", r.url)
  13. w.Header().Set("Content-Type", "application/json")
  14. w.WriteHeader(http.StatusOK)
  15. json.NewEncoder(w).Encode(r.LogEntries())
  16. }
  17. // Response to vote request
  18. func VoteHttpHandler(w http.ResponseWriter, req *http.Request) {
  19. rvreq := &raft.RequestVoteRequest{}
  20. err := decodeJsonRequest(req, rvreq)
  21. if err == nil {
  22. debugf("[recv] POST %s/vote [%s]", r.url, rvreq.CandidateName)
  23. if resp := r.RequestVote(rvreq); resp != nil {
  24. w.WriteHeader(http.StatusOK)
  25. json.NewEncoder(w).Encode(resp)
  26. return
  27. }
  28. }
  29. warnf("[vote] ERROR: %v", err)
  30. w.WriteHeader(http.StatusInternalServerError)
  31. }
  32. // Response to append entries request
  33. func AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) {
  34. aereq := &raft.AppendEntriesRequest{}
  35. err := decodeJsonRequest(req, aereq)
  36. if err == nil {
  37. debugf("[recv] POST %s/log/append [%d]", r.url, len(aereq.Entries))
  38. if resp := r.AppendEntries(aereq); resp != nil {
  39. w.WriteHeader(http.StatusOK)
  40. json.NewEncoder(w).Encode(resp)
  41. if !resp.Success {
  42. debugf("[Append Entry] Step back")
  43. }
  44. return
  45. }
  46. }
  47. warnf("[Append Entry] ERROR: %v", err)
  48. w.WriteHeader(http.StatusInternalServerError)
  49. }
  50. // Response to recover from snapshot request
  51. func SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) {
  52. aereq := &raft.SnapshotRequest{}
  53. err := decodeJsonRequest(req, aereq)
  54. if err == nil {
  55. debugf("[recv] POST %s/snapshot/ ", r.url)
  56. if resp := r.RequestSnapshot(aereq); resp != nil {
  57. w.WriteHeader(http.StatusOK)
  58. json.NewEncoder(w).Encode(resp)
  59. return
  60. }
  61. }
  62. warnf("[Snapshot] ERROR: %v", err)
  63. w.WriteHeader(http.StatusInternalServerError)
  64. }
  65. // Response to recover from snapshot request
  66. func SnapshotRecoveryHttpHandler(w http.ResponseWriter, req *http.Request) {
  67. aereq := &raft.SnapshotRecoveryRequest{}
  68. err := decodeJsonRequest(req, aereq)
  69. if err == nil {
  70. debugf("[recv] POST %s/snapshotRecovery/ ", r.url)
  71. if resp := r.SnapshotRecoveryRequest(aereq); resp != nil {
  72. w.WriteHeader(http.StatusOK)
  73. json.NewEncoder(w).Encode(resp)
  74. return
  75. }
  76. }
  77. warnf("[Snapshot] ERROR: %v", err)
  78. w.WriteHeader(http.StatusInternalServerError)
  79. }
  80. // Get the port that listening for etcd connecting of the server
  81. func EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) {
  82. debugf("[recv] Get %s/etcdURL/ ", r.url)
  83. w.WriteHeader(http.StatusOK)
  84. w.Write([]byte(argInfo.EtcdURL))
  85. }
  86. // Response to the join request
  87. func JoinHttpHandler(w http.ResponseWriter, req *http.Request) error {
  88. command := &JoinCommand{}
  89. if err := decodeJsonRequest(req, command); err == nil {
  90. debugf("Receive Join Request from %s", command.Name)
  91. return dispatch(command, w, req, false)
  92. } else {
  93. w.WriteHeader(http.StatusInternalServerError)
  94. return nil
  95. }
  96. }
  97. // Response to the name request
  98. func NameHttpHandler(w http.ResponseWriter, req *http.Request) {
  99. debugf("[recv] Get %s/name/ ", r.url)
  100. w.WriteHeader(http.StatusOK)
  101. w.Write([]byte(r.name))
  102. }
  103. // Response to the name request
  104. func RaftVersionHttpHandler(w http.ResponseWriter, req *http.Request) {
  105. debugf("[recv] Get %s/version/ ", r.url)
  106. w.WriteHeader(http.StatusOK)
  107. w.Write([]byte(r.version))
  108. }