request_vote.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package raft
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "github.com/coreos/etcd/third_party/code.google.com/p/gogoprotobuf/proto"
  6. "github.com/coreos/etcd/third_party/github.com/goraft/raft/protobuf"
  7. )
  8. // The request sent to a server to vote for a candidate to become a leader.
  9. type RequestVoteRequest struct {
  10. peer *Peer
  11. Term uint64
  12. LastLogIndex uint64
  13. LastLogTerm uint64
  14. CandidateName string
  15. }
  16. // The response returned from a server after a vote for a candidate to become a leader.
  17. type RequestVoteResponse struct {
  18. peer *Peer
  19. Term uint64
  20. VoteGranted bool
  21. }
  22. // Creates a new RequestVote request.
  23. func newRequestVoteRequest(term uint64, candidateName string, lastLogIndex uint64, lastLogTerm uint64) *RequestVoteRequest {
  24. return &RequestVoteRequest{
  25. Term: term,
  26. LastLogIndex: lastLogIndex,
  27. LastLogTerm: lastLogTerm,
  28. CandidateName: candidateName,
  29. }
  30. }
  31. // Encodes the RequestVoteRequest to a buffer. Returns the number of bytes
  32. // written and any error that may have occurred.
  33. func (req *RequestVoteRequest) Encode(w io.Writer) (int, error) {
  34. pb := &protobuf.RequestVoteRequest{
  35. Term: proto.Uint64(req.Term),
  36. LastLogIndex: proto.Uint64(req.LastLogIndex),
  37. LastLogTerm: proto.Uint64(req.LastLogTerm),
  38. CandidateName: proto.String(req.CandidateName),
  39. }
  40. p, err := proto.Marshal(pb)
  41. if err != nil {
  42. return -1, err
  43. }
  44. return w.Write(p)
  45. }
  46. // Decodes the RequestVoteRequest from a buffer. Returns the number of bytes read and
  47. // any error that occurs.
  48. func (req *RequestVoteRequest) Decode(r io.Reader) (int, error) {
  49. data, err := ioutil.ReadAll(r)
  50. if err != nil {
  51. return -1, err
  52. }
  53. totalBytes := len(data)
  54. pb := &protobuf.RequestVoteRequest{}
  55. if err = proto.Unmarshal(data, pb); err != nil {
  56. return -1, err
  57. }
  58. req.Term = pb.GetTerm()
  59. req.LastLogIndex = pb.GetLastLogIndex()
  60. req.LastLogTerm = pb.GetLastLogTerm()
  61. req.CandidateName = pb.GetCandidateName()
  62. return totalBytes, nil
  63. }
  64. // Creates a new RequestVote response.
  65. func newRequestVoteResponse(term uint64, voteGranted bool) *RequestVoteResponse {
  66. return &RequestVoteResponse{
  67. Term: term,
  68. VoteGranted: voteGranted,
  69. }
  70. }
  71. // Encodes the RequestVoteResponse to a buffer. Returns the number of bytes
  72. // written and any error that may have occurred.
  73. func (resp *RequestVoteResponse) Encode(w io.Writer) (int, error) {
  74. pb := &protobuf.RequestVoteResponse{
  75. Term: proto.Uint64(resp.Term),
  76. VoteGranted: proto.Bool(resp.VoteGranted),
  77. }
  78. p, err := proto.Marshal(pb)
  79. if err != nil {
  80. return -1, err
  81. }
  82. return w.Write(p)
  83. }
  84. // Decodes the RequestVoteResponse from a buffer. Returns the number of bytes read and
  85. // any error that occurs.
  86. func (resp *RequestVoteResponse) Decode(r io.Reader) (int, error) {
  87. data, err := ioutil.ReadAll(r)
  88. if err != nil {
  89. return 0, err
  90. }
  91. totalBytes := len(data)
  92. pb := &protobuf.RequestVoteResponse{}
  93. if err = proto.Unmarshal(data, pb); err != nil {
  94. return -1, err
  95. }
  96. resp.Term = pb.GetTerm()
  97. resp.VoteGranted = pb.GetVoteGranted()
  98. return totalBytes, nil
  99. }