raft_handlers.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package main
  2. import (
  3. "encoding/json"
  4. "github.com/coreos/go-raft"
  5. "net/http"
  6. "strconv"
  7. )
  8. //-------------------------------------------------------------
  9. // Handlers to handle raft related request via raft server port
  10. //-------------------------------------------------------------
  11. // Get all the current logs
  12. func GetLogHttpHandler(w http.ResponseWriter, req *http.Request) {
  13. debug("[recv] GET http://%v/log", raftServer.Name())
  14. w.Header().Set("Content-Type", "application/json")
  15. w.WriteHeader(http.StatusOK)
  16. json.NewEncoder(w).Encode(raftServer.LogEntries())
  17. }
  18. // Response to vote request
  19. func VoteHttpHandler(w http.ResponseWriter, req *http.Request) {
  20. rvreq := &raft.RequestVoteRequest{}
  21. err := decodeJsonRequest(req, rvreq)
  22. if err == nil {
  23. debug("[recv] POST http://%v/vote [%s]", raftServer.Name(), rvreq.CandidateName)
  24. if resp := raftServer.RequestVote(rvreq); resp != nil {
  25. w.WriteHeader(http.StatusOK)
  26. json.NewEncoder(w).Encode(resp)
  27. return
  28. }
  29. }
  30. warn("[vote] ERROR: %v", err)
  31. w.WriteHeader(http.StatusInternalServerError)
  32. }
  33. // Response to append entries request
  34. func AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) {
  35. aereq := &raft.AppendEntriesRequest{}
  36. err := decodeJsonRequest(req, aereq)
  37. if err == nil {
  38. debug("[recv] POST http://%s/log/append [%d]", raftServer.Name(), len(aereq.Entries))
  39. if resp := raftServer.AppendEntries(aereq); resp != nil {
  40. w.WriteHeader(http.StatusOK)
  41. json.NewEncoder(w).Encode(resp)
  42. if !resp.Success {
  43. debug("[Append Entry] Step back")
  44. }
  45. return
  46. }
  47. }
  48. warn("[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. debug("[recv] POST http://%s/snapshot/ ", raftServer.Name())
  57. if resp, _ := raftServer.SnapshotRecovery(aereq); resp != nil {
  58. w.WriteHeader(http.StatusOK)
  59. json.NewEncoder(w).Encode(resp)
  60. return
  61. }
  62. }
  63. warn("[Snapshot] ERROR: %v", err)
  64. w.WriteHeader(http.StatusInternalServerError)
  65. }
  66. // Get the port that listening for client connecting of the server
  67. func ClientHttpHandler(w http.ResponseWriter, req *http.Request) {
  68. debug("[recv] Get http://%v/client/ ", raftServer.Name())
  69. w.WriteHeader(http.StatusOK)
  70. client := address + ":" + strconv.Itoa(clientPort)
  71. w.Write([]byte(client))
  72. }
  73. // Response to the join request
  74. func JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
  75. command := &JoinCommand{}
  76. if err := decodeJsonRequest(req, command); err == nil {
  77. debug("Receive Join Request from %s", command.Name)
  78. dispatch(command, &w, req, false)
  79. } else {
  80. w.WriteHeader(http.StatusInternalServerError)
  81. return
  82. }
  83. }