raft_handlers.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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", raftTransporter.scheme+raftServer.Name())
  13. w.Header().Set("Content-Type", "application/json")
  14. w.WriteHeader(http.StatusOK)
  15. json.NewEncoder(w).Encode(raftServer.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]", raftTransporter.scheme+raftServer.Name(), rvreq.CandidateName)
  23. if resp := raftServer.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]", raftTransporter.scheme+raftServer.Name(), len(aereq.Entries))
  38. if resp := raftServer.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/ ", raftTransporter.scheme+raftServer.Name())
  56. if resp := raftServer.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/ ", raftTransporter.scheme+raftServer.Name())
  71. if resp := raftServer.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 client connecting of the server
  81. func ClientHttpHandler(w http.ResponseWriter, req *http.Request) {
  82. debugf("[recv] Get %s/client/ ", raftTransporter.scheme+raftServer.Name())
  83. w.WriteHeader(http.StatusOK)
  84. client := argInfo.ClientURL
  85. w.Write([]byte(client))
  86. }
  87. // Response to the join request
  88. func JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
  89. command := &JoinCommand{}
  90. if err := decodeJsonRequest(req, command); err == nil {
  91. debugf("Receive Join Request from %s", command.Name)
  92. dispatch(command, &w, req, false)
  93. } else {
  94. w.WriteHeader(http.StatusInternalServerError)
  95. return
  96. }
  97. }
  98. // Response to the name request
  99. func NameHttpHandler(w http.ResponseWriter, req *http.Request) {
  100. debugf("[recv] Get %s/name/ ", raftTransporter.scheme+raftServer.Name())
  101. w.WriteHeader(http.StatusOK)
  102. w.Write([]byte(raftServer.Name()))
  103. }