-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrafthttp.go
307 lines (264 loc) · 9.4 KB
/
rafthttp.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Package rafthttp provides a HTTP/JSON-based raft transport.
package rafthttp
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"time"
"github.com/armon/go-metrics"
"github.com/hashicorp/raft"
)
// Doer provides the Do() method, as found in net/http.Client.
//
// Using this interface instead of net/http.Client directly is useful so that
// users of the HTTPTransport can wrap requests to, for example, call
// req.SetBasicAuth.
type Doer interface {
Do(*http.Request) (*http.Response, error)
}
// HTTPTransport provides a HTTP-based transport that can be used to
// communicate with Raft on remote machines. It is convenient to use if your
// application is an HTTP server already and you do not want to use multiple
// different transports (if not, you can use raft.NetworkTransport).
type HTTPTransport struct {
logger *log.Logger
consumer chan raft.RPC
addr raft.ServerAddress
client Doer
urlFmt string
}
// NewHTTPTransport creates a new HTTP transport on the given addr.
//
// client must implement the Doer interface, but you can use e.g.
// net/http.DefaultClient if you do not need to wrap the Do() method.
//
// logger defaults to log.New(os.Stderr, "", log.LstdFlags) if nil.
//
// urlFmt defaults to "https://%v/raft/" and will be used in
// fmt.Sprintf(urlFmt+"/method", target) where method is the raft RPC method
// (e.g. appendEntries).
func NewHTTPTransport(addr raft.ServerAddress, client Doer, logger *log.Logger, urlFmt string) *HTTPTransport {
if client == nil {
client = http.DefaultClient
}
if logger == nil {
logger = log.New(os.Stderr, "", log.LstdFlags)
}
if urlFmt == "" {
urlFmt = "https://%v/raft/"
}
return &HTTPTransport{
logger: logger,
consumer: make(chan raft.RPC),
addr: addr,
client: client,
urlFmt: urlFmt,
}
}
type installSnapshotRequest struct {
Args *raft.InstallSnapshotRequest
Data []byte
}
func (t *HTTPTransport) send(url string, in, out interface{}) error {
defer metrics.MeasureSince([]string{"raft", "httptransport", "latency"}, time.Now())
buf, err := json.Marshal(in)
if err != nil {
return fmt.Errorf("could not serialize request: %v", err)
}
req, err := http.NewRequest("POST", url, bytes.NewReader(buf))
if err != nil {
return err
}
res, err := t.client.Do(req)
if err != nil {
return fmt.Errorf("could not send request: %v", err)
}
defer func() {
// Make sure to read the entire body and close the connection,
// otherwise net/http cannot re-use the connection.
ioutil.ReadAll(res.Body)
res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected HTTP status code: %v", res.Status)
}
return json.NewDecoder(res.Body).Decode(out)
}
// Consumer implements the raft.Transport interface.
func (t *HTTPTransport) Consumer() <-chan raft.RPC {
return t.consumer
}
// LocalAddr implements the raft.Transport interface.
func (t *HTTPTransport) LocalAddr() raft.ServerAddress {
return t.addr
}
// AppendEntriesPipeline implements the raft.Transport interface.
func (t *HTTPTransport) AppendEntriesPipeline(_ raft.ServerID, target raft.ServerAddress) (raft.AppendPipeline, error) {
// This transport does not support pipelining in the hashicorp/raft sense.
// The underlying net/http reuses connections (keep-alive) and that is good
// enough. We are talking about differences in the microsecond range, which
// becomes irrelevant as soon as the raft nodes run on different computers.
return nil, raft.ErrPipelineReplicationNotSupported
}
// AppendEntries implements the raft.Transport interface.
func (t *HTTPTransport) AppendEntries(_ raft.ServerID, target raft.ServerAddress, args *raft.AppendEntriesRequest, resp *raft.AppendEntriesResponse) error {
return t.send(fmt.Sprintf(t.urlFmt+"AppendEntries", target), args, resp)
}
// RequestVote implements the raft.Transport interface.
func (t *HTTPTransport) RequestVote(_ raft.ServerID, target raft.ServerAddress, args *raft.RequestVoteRequest, resp *raft.RequestVoteResponse) error {
return t.send(fmt.Sprintf(t.urlFmt+"RequestVote", target), args, resp)
}
// InstallSnapshot implements the raft.Transport interface.
func (t *HTTPTransport) InstallSnapshot(_ raft.ServerID, target raft.ServerAddress, args *raft.InstallSnapshotRequest, resp *raft.InstallSnapshotResponse, data io.Reader) error {
defer metrics.MeasureSince([]string{"raft", "httptransport", "latency"}, time.Now())
// Send a dummy request to see if the remote host supports
// InstallSnapshotStreaming after all. We need to know whether we can use
// InstallSnapshotStreaming or whether we need to fall back to
// InstallSnapshot beforehand, because we cannot seek in |data|.
url := fmt.Sprintf(t.urlFmt+"InstallSnapshotStreaming", target)
probeReq, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
probeRes, err := t.client.Do(probeReq)
if err != nil {
return err
}
ioutil.ReadAll(probeRes.Body)
probeRes.Body.Close()
if probeRes.StatusCode == http.StatusNotFound {
// Possibly the remote host runs an older version of the code
// without the InstallSnapshotStreaming handler. Try the old
// version.
buf := make([]byte, 0, args.Size+bytes.MinRead)
b := bytes.NewBuffer(buf)
if _, err := io.CopyN(b, data, args.Size); err != nil {
return fmt.Errorf("could not read data: %v", err)
}
buf = b.Bytes()
return t.send(fmt.Sprintf(t.urlFmt+"InstallSnapshot", target), installSnapshotRequest{args, buf}, resp)
}
req, err := http.NewRequest("POST", url, data)
if err != nil {
return err
}
buf, err := json.Marshal(args)
if err != nil {
return err
}
req.Header.Set("X-InstallSnapshotRequest", string(buf))
res, err := t.client.Do(req)
if err != nil {
return fmt.Errorf("could not send request: %v", err)
}
defer func() {
// Make sure to read the entire body and close the connection,
// otherwise net/http cannot re-use the connection.
ioutil.ReadAll(res.Body)
res.Body.Close()
}()
if res.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(res.Body)
return fmt.Errorf("unexpected HTTP status code: %v (body: %s)", res.Status, strings.TrimSpace(string(b)))
}
return json.NewDecoder(res.Body).Decode(resp)
}
// EncodePeer implements the raft.Transport interface.
func (t *HTTPTransport) EncodePeer(_ raft.ServerID, a raft.ServerAddress) []byte {
return []byte(a)
}
// DecodePeer implements the raft.Transport interface.
func (t *HTTPTransport) DecodePeer(b []byte) raft.ServerAddress {
return raft.ServerAddress(string(b))
}
func (t *HTTPTransport) handle(res http.ResponseWriter, req *http.Request, rpc raft.RPC) error {
if err := json.NewDecoder(req.Body).Decode(&rpc.Command); err != nil {
err := fmt.Errorf("Could not parse request: %v", err)
http.Error(res, err.Error(), http.StatusBadRequest)
return err
}
if r, ok := rpc.Command.(*installSnapshotRequest); ok {
rpc.Command = r.Args
rpc.Reader = bytes.NewReader(r.Data)
}
respChan := make(chan raft.RPCResponse)
rpc.RespChan = respChan
t.consumer <- rpc
resp := <-respChan
if resp.Error != nil {
err := fmt.Errorf("Could not run RPC: %v", resp.Error)
http.Error(res, err.Error(), http.StatusBadRequest)
return err
}
res.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(res).Encode(resp.Response); err != nil {
err := fmt.Errorf("Could not encode response: %v", err)
http.Error(res, err.Error(), http.StatusInternalServerError)
return err
}
return nil
}
// ServeHTTP implements the net/http.Handler interface, so that you can use
//
// http.Handle("/raft/", transport)
func (t *HTTPTransport) ServeHTTP(res http.ResponseWriter, req *http.Request) {
cmd := path.Base(req.URL.Path)
var rpc raft.RPC
switch cmd {
case "InstallSnapshot":
rpc.Command = &installSnapshotRequest{}
case "InstallSnapshotStreaming":
var isr raft.InstallSnapshotRequest
if err := json.Unmarshal([]byte(req.Header.Get("X-InstallSnapshotRequest")), &isr); err != nil {
err := fmt.Errorf("Could not parse request: %v", err)
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
rpc.Command = &isr
rpc.Reader = req.Body
respChan := make(chan raft.RPCResponse)
rpc.RespChan = respChan
t.consumer <- rpc
resp := <-respChan
if resp.Error != nil {
err := fmt.Errorf("Could not run RPC: %v", resp.Error)
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
res.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(res).Encode(resp.Response); err != nil {
err := fmt.Errorf("Could not encode response: %v", err)
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
return
case "RequestVote":
rpc.Command = &raft.RequestVoteRequest{}
case "AppendEntries":
rpc.Command = &raft.AppendEntriesRequest{}
case "TimeoutNow":
rpc.Command = &raft.TimeoutNowRequest{}
default:
http.Error(res, fmt.Sprintf("No RPC %q", cmd), 404)
return
}
if err := t.handle(res, req, rpc); err != nil {
t.logger.Printf("[%s, %s] %v\n", req.RemoteAddr, cmd, err)
}
metrics.IncrCounter([]string{"raft", "httptransport", "handled"}, 1)
}
// SetHeartbeatHandler implements the raft.Transport interface.
func (t *HTTPTransport) SetHeartbeatHandler(cb func(rpc raft.RPC)) {
// Not supported
}
// TimeoutNow implements the raft.Transport interface.
func (t *HTTPTransport) TimeoutNow(_ raft.ServerID, target raft.ServerAddress, args *raft.TimeoutNowRequest, resp *raft.TimeoutNowResponse) error {
return t.send(fmt.Sprintf(t.urlFmt+"TimeoutNow", target), args, resp)
}