forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
218 lines (187 loc) · 6.06 KB
/
handler.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package raft
import (
"encoding/json"
"io"
"net/http"
"net/url"
"path"
"strconv"
)
// Handler represents an HTTP endpoint for Raft to communicate over.
type Handler struct {
Log interface {
AddPeer(u *url.URL) (uint64, *Config, error)
RemovePeer(id uint64) error
Heartbeat(term, commitIndex, leaderID uint64) (currentIndex, currentTerm uint64, err error)
WriteEntriesTo(w io.Writer, id, term, index uint64) error
RequestVote(term, candidateID, lastLogIndex, lastLogTerm uint64) (uint64, error)
}
}
// ServeHTTP handles all incoming HTTP requests.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch path.Base(r.URL.Path) {
case "join":
h.serveJoin(w, r)
case "leave":
h.serveLeave(w, r)
case "heartbeat":
h.serveHeartbeat(w, r)
case "stream":
h.serveStream(w, r)
case "vote":
h.serveRequestVote(w, r)
case "ping":
w.WriteHeader(http.StatusOK)
default:
http.NotFound(w, r)
}
}
// serveJoin serves a Raft membership addition to the underlying log.
func (h *Handler) serveJoin(w http.ResponseWriter, r *http.Request) {
// TODO(benbjohnson): Redirect to leader.
// Parse argument.
if r.FormValue("url") == "" {
w.Header().Set("X-Raft-Error", "url required")
w.WriteHeader(http.StatusBadRequest)
return
}
// Parse URL.
u, err := url.Parse(r.FormValue("url"))
if err != nil {
w.Header().Set("X-Raft-Error", "invalid url")
w.WriteHeader(http.StatusBadRequest)
return
}
// Add peer to the log.
id, config, err := h.Log.AddPeer(u)
if err != nil {
w.Header().Set("X-Raft-Error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
// Return member's id in the cluster.
w.Header().Set("X-Raft-ID", strconv.FormatUint(id, 10))
w.WriteHeader(http.StatusOK)
// Write config to the body.
_ = json.NewEncoder(w).Encode(config)
}
// serveLeave removes a member from the cluster.
func (h *Handler) serveLeave(w http.ResponseWriter, r *http.Request) {
// TODO(benbjohnson): Redirect to leader.
// Parse arguments.
id, err := strconv.ParseUint(r.FormValue("id"), 10, 64)
if err != nil {
w.Header().Set("X-Raft-Error", "invalid raft id")
w.WriteHeader(http.StatusBadRequest)
return
}
// Remove a peer from the log.
if err := h.Log.RemovePeer(id); err != nil {
w.Header().Set("X-Raft-Error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
// serveHeartbeat serves a Raft heartbeat to the underlying log.
func (h *Handler) serveHeartbeat(w http.ResponseWriter, r *http.Request) {
var err error
var term, commitIndex, leaderID uint64
// Parse arguments.
if term, err = strconv.ParseUint(r.FormValue("term"), 10, 64); err != nil {
w.Header().Set("X-Raft-Error", "invalid term")
w.WriteHeader(http.StatusBadRequest)
return
}
if commitIndex, err = strconv.ParseUint(r.FormValue("commitIndex"), 10, 64); err != nil {
w.Header().Set("X-Raft-Error", "invalid commit index")
w.WriteHeader(http.StatusBadRequest)
return
}
if leaderID, err = strconv.ParseUint(r.FormValue("leaderID"), 10, 64); err != nil {
w.Header().Set("X-Raft-Error", "invalid leader id")
w.WriteHeader(http.StatusBadRequest)
return
}
// Execute heartbeat on the log.
currentIndex, currentTerm, err := h.Log.Heartbeat(term, commitIndex, leaderID)
// Return current term and index.
w.Header().Set("X-Raft-Index", strconv.FormatUint(currentIndex, 10))
w.Header().Set("X-Raft-Term", strconv.FormatUint(currentTerm, 10))
// Write error, if applicable.
if err != nil {
w.Header().Set("X-Raft-Error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
// serveStream provides a streaming log endpoint.
func (h *Handler) serveStream(w http.ResponseWriter, r *http.Request) {
var err error
var id, index, term uint64
// Parse client's id.
if id, err = strconv.ParseUint(r.FormValue("id"), 10, 64); err != nil {
w.Header().Set("X-Raft-Error", "invalid id")
w.WriteHeader(http.StatusBadRequest)
return
}
// Parse client's current term.
if term, err = strconv.ParseUint(r.FormValue("term"), 10, 64); err != nil {
w.Header().Set("X-Raft-Error", "invalid term")
w.WriteHeader(http.StatusBadRequest)
return
}
// Parse starting index.
if s := r.FormValue("index"); s != "" {
if index, err = strconv.ParseUint(r.FormValue("index"), 10, 64); err != nil {
w.Header().Set("X-Raft-Error", "invalid index")
w.WriteHeader(http.StatusBadRequest)
return
}
}
// TODO(benbjohnson): Redirect to leader.
// Write to the response.
if err := h.Log.WriteEntriesTo(w, id, term, index); err != nil && err != io.EOF {
w.Header().Set("X-Raft-Error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
}
// serveRequestVote serves a vote request to the underlying log.
func (h *Handler) serveRequestVote(w http.ResponseWriter, r *http.Request) {
var err error
var term, candidateID, lastLogIndex, lastLogTerm uint64
// Parse arguments.
if term, err = strconv.ParseUint(r.FormValue("term"), 10, 64); err != nil {
w.Header().Set("X-Raft-Error", "invalid term")
w.WriteHeader(http.StatusBadRequest)
return
}
if candidateID, err = strconv.ParseUint(r.FormValue("candidateID"), 10, 64); err != nil {
w.Header().Set("X-Raft-Error", "invalid candidate id")
w.WriteHeader(http.StatusBadRequest)
return
}
if lastLogIndex, err = strconv.ParseUint(r.FormValue("lastLogIndex"), 10, 64); err != nil {
w.Header().Set("X-Raft-Error", "invalid last log index")
w.WriteHeader(http.StatusBadRequest)
return
}
if lastLogTerm, err = strconv.ParseUint(r.FormValue("lastLogTerm"), 10, 64); err != nil {
w.Header().Set("X-Raft-Error", "invalid last log term")
w.WriteHeader(http.StatusBadRequest)
return
}
// Execute heartbeat on the log.
currentTerm, err := h.Log.RequestVote(term, candidateID, lastLogIndex, lastLogTerm)
// Return current term and index.
w.Header().Set("X-Raft-Term", strconv.FormatUint(currentTerm, 10))
// Write error, if applicable.
if err != nil {
w.Header().Set("X-Raft-Error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}