raft_handlers.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/coreos/go-raft"
  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. r.serverStats.RecvAppendReq(aereq.LeaderName, int(req.ContentLength))
  39. if resp := r.AppendEntries(aereq); resp != nil {
  40. w.WriteHeader(http.StatusOK)
  41. json.NewEncoder(w).Encode(resp)
  42. if !resp.Success {
  43. debugf("[Append Entry] Step back")
  44. }
  45. return
  46. }
  47. }
  48. warnf("[Append Entry] ERROR: %v", err)
  49. w.WriteHeader(http.StatusInternalServerError)
  50. }
  51. // Response to recover from snapshot request
  52. func SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) {
  53. aereq := &raft.SnapshotRequest{}
  54. err := decodeJsonRequest(req, aereq)
  55. if err == nil {
  56. debugf("[recv] POST %s/snapshot/ ", r.url)
  57. if resp := r.RequestSnapshot(aereq); resp != nil {
  58. w.WriteHeader(http.StatusOK)
  59. json.NewEncoder(w).Encode(resp)
  60. return
  61. }
  62. }
  63. warnf("[Snapshot] ERROR: %v", err)
  64. w.WriteHeader(http.StatusInternalServerError)
  65. }
  66. // Response to recover from snapshot request
  67. func SnapshotRecoveryHttpHandler(w http.ResponseWriter, req *http.Request) {
  68. aereq := &raft.SnapshotRecoveryRequest{}
  69. err := decodeJsonRequest(req, aereq)
  70. if err == nil {
  71. debugf("[recv] POST %s/snapshotRecovery/ ", r.url)
  72. if resp := r.SnapshotRecoveryRequest(aereq); resp != nil {
  73. w.WriteHeader(http.StatusOK)
  74. json.NewEncoder(w).Encode(resp)
  75. return
  76. }
  77. }
  78. warnf("[Snapshot] ERROR: %v", err)
  79. w.WriteHeader(http.StatusInternalServerError)
  80. }
  81. // Get the port that listening for etcd connecting of the server
  82. func EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) {
  83. debugf("[recv] Get %s/etcdURL/ ", r.url)
  84. w.WriteHeader(http.StatusOK)
  85. w.Write([]byte(argInfo.EtcdURL))
  86. }
  87. // Response to the join request
  88. func JoinHttpHandler(w http.ResponseWriter, req *http.Request) error {
  89. command := &JoinCommand{}
  90. if err := decodeJsonRequest(req, command); err == nil {
  91. debugf("Receive Join Request from %s", command.Name)
  92. return dispatch(command, w, req, false)
  93. } else {
  94. w.WriteHeader(http.StatusInternalServerError)
  95. return nil
  96. }
  97. }
  98. // Response to remove request
  99. func RemoveHttpHandler(w http.ResponseWriter, req *http.Request) {
  100. if req.Method != "DELETE" {
  101. w.WriteHeader(http.StatusMethodNotAllowed)
  102. return
  103. }
  104. nodeName := req.URL.Path[len("/remove/"):]
  105. command := &RemoveCommand{
  106. Name: nodeName,
  107. }
  108. debugf("[recv] Remove Request [%s]", command.Name)
  109. dispatch(command, w, req, false)
  110. }
  111. // Response to the name request
  112. func NameHttpHandler(w http.ResponseWriter, req *http.Request) {
  113. debugf("[recv] Get %s/name/ ", r.url)
  114. w.WriteHeader(http.StatusOK)
  115. w.Write([]byte(r.name))
  116. }
  117. // Response to the name request
  118. func RaftVersionHttpHandler(w http.ResponseWriter, req *http.Request) {
  119. debugf("[recv] Get %s/version/ ", r.url)
  120. w.WriteHeader(http.StatusOK)
  121. w.Write([]byte(r.version))
  122. }