transporter.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/coreos/go-raft"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. )
  11. // Transporter layer for communication between raft nodes
  12. type transporter struct {
  13. client *http.Client
  14. // scheme
  15. scheme string
  16. }
  17. // Sends AppendEntries RPCs to a peer when the server is the leader.
  18. func (t transporter) SendAppendEntriesRequest(server *raft.Server, peer *raft.Peer, req *raft.AppendEntriesRequest) *raft.AppendEntriesResponse {
  19. var aersp *raft.AppendEntriesResponse
  20. var b bytes.Buffer
  21. json.NewEncoder(&b).Encode(req)
  22. u, _ := nameToRaftURL(peer.Name())
  23. debugf("Send LogEntries to %s ", u)
  24. resp, err := t.Post(fmt.Sprintf("%s/log/append", u), &b)
  25. if err != nil {
  26. debugf("Cannot send AppendEntriesRequest to %s: %s", u, err)
  27. }
  28. if resp != nil {
  29. defer resp.Body.Close()
  30. aersp = &raft.AppendEntriesResponse{}
  31. if err := json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
  32. return aersp
  33. }
  34. }
  35. return aersp
  36. }
  37. // Sends RequestVote RPCs to a peer when the server is the candidate.
  38. func (t transporter) SendVoteRequest(server *raft.Server, peer *raft.Peer, req *raft.RequestVoteRequest) *raft.RequestVoteResponse {
  39. var rvrsp *raft.RequestVoteResponse
  40. var b bytes.Buffer
  41. json.NewEncoder(&b).Encode(req)
  42. u, _ := nameToRaftURL(peer.Name())
  43. debugf("Send Vote to %s", u)
  44. resp, err := t.Post(fmt.Sprintf("%s/vote", u), &b)
  45. if err != nil {
  46. debugf("Cannot send VoteRequest to %s : %s", u, err)
  47. }
  48. if resp != nil {
  49. defer resp.Body.Close()
  50. rvrsp := &raft.RequestVoteResponse{}
  51. if err := json.NewDecoder(resp.Body).Decode(&rvrsp); err == nil || err == io.EOF {
  52. return rvrsp
  53. }
  54. }
  55. return rvrsp
  56. }
  57. // Sends SnapshotRequest RPCs to a peer when the server is the candidate.
  58. func (t transporter) SendSnapshotRequest(server *raft.Server, peer *raft.Peer, req *raft.SnapshotRequest) *raft.SnapshotResponse {
  59. var aersp *raft.SnapshotResponse
  60. var b bytes.Buffer
  61. json.NewEncoder(&b).Encode(req)
  62. u, _ := nameToRaftURL(peer.Name())
  63. debugf("Send Snapshot to %s [Last Term: %d, LastIndex %d]", u,
  64. req.LastTerm, req.LastIndex)
  65. resp, err := t.Post(fmt.Sprintf("%s/snapshot", u), &b)
  66. if resp != nil {
  67. defer resp.Body.Close()
  68. aersp = &raft.SnapshotResponse{}
  69. if err = json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
  70. return aersp
  71. }
  72. }
  73. return aersp
  74. }
  75. // Sends SnapshotRecoveryRequest RPCs to a peer when the server is the candidate.
  76. func (t transporter) SendSnapshotRecoveryRequest(server *raft.Server, peer *raft.Peer, req *raft.SnapshotRecoveryRequest) *raft.SnapshotRecoveryResponse {
  77. var aersp *raft.SnapshotRecoveryResponse
  78. var b bytes.Buffer
  79. json.NewEncoder(&b).Encode(req)
  80. u, _ := nameToRaftURL(peer.Name())
  81. debugf("Send SnapshotRecovery to %s [Last Term: %d, LastIndex %d]", u,
  82. req.LastTerm, req.LastIndex)
  83. resp, err := t.Post(fmt.Sprintf("%s/snapshotRecovery", u), &b)
  84. if resp != nil {
  85. defer resp.Body.Close()
  86. aersp = &raft.SnapshotRecoveryResponse{}
  87. if err = json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
  88. return aersp
  89. }
  90. }
  91. return aersp
  92. }
  93. // Get the client address of the leader in the cluster
  94. func (t transporter) GetLeaderClientAddress() string {
  95. u, _ := nameToRaftURL(raftServer.Leader())
  96. resp, _ := t.Get(fmt.Sprintf("%s/client", u))
  97. if resp != nil {
  98. body, _ := ioutil.ReadAll(resp.Body)
  99. resp.Body.Close()
  100. return string(body)
  101. }
  102. return ""
  103. }
  104. // Send server side POST request
  105. func (t transporter) Post(path string, body io.Reader) (*http.Response, error) {
  106. resp, err := t.client.Post(path, "application/json", body)
  107. return resp, err
  108. }
  109. // Send server side GET request
  110. func (t transporter) Get(path string) (*http.Response, error) {
  111. resp, err := t.client.Get(path)
  112. return resp, err
  113. }