-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrequest.go
More file actions
167 lines (154 loc) · 4.77 KB
/
request.go
File metadata and controls
167 lines (154 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package dinghy
import (
"bytes"
"context"
"encoding/json"
"net/http"
"strings"
"sync"
"time"
)
// AppendEntriesRequest represents AppendEntries requests. Replication logging is ignored.
type AppendEntriesRequest struct {
Term int `json:"term"`
LeaderID int `json:"leader_id"`
}
// appendEntriesResponse represents the response to an appendEntries. In
// dinghy this always returns success.
type appendEntriesResponse struct {
Term int `json:"term"`
Success bool `json:"success"`
Reason string `json:"reason,omitempty"`
}
// requestVoteRequest represents a requestVote sent by a candidate after an
// election timeout.
type requestVoteRequest struct {
Term int `json:"term"`
CandidateID int `json:"candidate_id"`
}
// requestVoteResponse represents the response to a requestVote.
type requestVoteResponse struct {
Term int `json:"term"`
VoteGranted bool `json:"vote_granted"`
Reason string `json:"reason,omitempty"`
}
// RequestVoteRequest will broadcast a request for votes in order to update dinghy to
// either a follower or leader. If this candidate becomes leader error
// will return nil. The latest known term is always
// returned (this could be a newer term from another peer).
func (d *Dinghy) RequestVoteRequest() (int, error) {
if d.State.State() == StateLeader {
// no op
d.logger.Println("already leader")
return d.State.Term(), nil
}
rvr := requestVoteRequest{
Term: d.State.Term(),
CandidateID: d.State.ID(),
}
method := "POST"
route := d.routePrefix + RouteRequestVote
body, err := json.Marshal(rvr)
if err != nil {
d.logger.Errorln("could not create payload", method, route, string(body))
return d.State.Term(), err
}
responses := d.BroadcastRequest(d.Nodes, method, route, body, 0)
votes := 0
LOOP:
for i, resp := range responses {
if resp == nil {
// peer failed
continue LOOP
}
defer resp.Body.Close()
var rvr requestVoteResponse
if err := json.NewDecoder(resp.Body).Decode(&rvr); err != nil {
d.logger.Errorln("peer request returned invalid", d.Nodes[i], err)
continue LOOP
}
if rvr.Term > d.State.Term() {
// step down to a follower with the newer term
d.logger.Printf("newer election term found %+v %s", rvr, d.State)
return rvr.Term, ErrNewElectionTerm
}
if rvr.VoteGranted {
d.logger.Printf("vote granted from %s %+v", d.Nodes[i], rvr)
votes++
}
}
if votes < (len(d.Nodes))/2 {
d.logger.Printf("too few votes %d but need more than %d", votes, (len(d.Nodes))/2)
return d.State.Term(), ErrTooFewVotes
}
d.logger.Printf("election won with %d votes, becoming leader %s", votes, d.State)
return d.State.Term(), nil
}
// AppendEntriesRequest will broadcast an AppendEntries request to peers.
// In the raft protocol this deals with appending and processing the
// replication log, however for leader election this is unused.
// It returns the current term with any errors.
func (d *Dinghy) AppendEntriesRequest() (int, error) {
aer := AppendEntriesRequest{
Term: d.State.Term(),
LeaderID: d.State.ID(),
}
method := "POST"
route := d.routePrefix + RouteAppendEntries
body, err := json.Marshal(aer)
if err != nil {
d.logger.Errorln("could not create payload", method, route, string(body))
return d.State.Term(), err
}
responses := d.BroadcastRequest(d.Nodes, method, route, body, d.State.heartbeatTimeoutMS/2)
for i, resp := range responses {
if resp == nil {
// peer failed
continue
}
defer resp.Body.Close()
var aer appendEntriesResponse
if err := json.NewDecoder(resp.Body).Decode(&aer); err != nil {
d.logger.Errorln("peer request returned invalid", d.Nodes[i], err)
continue
}
if aer.Term > d.State.Term() {
d.logger.Printf("newer election term found %+v", aer)
return aer.Term, ErrNewElectionTerm
}
}
return d.State.Term(), nil
}
// BroadcastRequest will send a request to all other nodes in the system.
func (d *Dinghy) BroadcastRequest(peers []string, method, route string, body []byte, timeoutMS int) []*http.Response {
responses := make([]*http.Response, len(peers))
wg := &sync.WaitGroup{}
for ind, peer := range peers {
wg.Add(1)
go func(i int, p string) {
defer wg.Done()
url := p
if !strings.HasPrefix(url, "http") {
url = "http://" + url + route
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
d.logger.Errorln("could not create request", method, url, string(body))
return
}
if timeoutMS > 0 {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutMS)*time.Millisecond)
defer cancel()
req = req.WithContext(ctx)
}
resp, err := d.client.Do(req)
if err != nil {
d.logger.Errorln("failed request", err, method, url, string(body))
return
}
responses[i] = resp
}(ind, peer)
}
wg.Wait()
return responses
}