forked from vulcand/vulcand
/
backend.go
633 lines (530 loc) · 17.2 KB
/
backend.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// Package backend defines interfaces and structures controlling the proxy configuration and changes.
package backend
import (
"crypto/subtle"
"crypto/tls"
"fmt"
"net/http"
"regexp"
"strings"
"time"
"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/vulcan/location/httploc"
"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/vulcan/metrics"
"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/vulcan/netutils"
"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/vulcan/threshold"
"github.com/mailgun/vulcand/plugin"
)
type NewBackendFn func() (Backend, error)
type Backend interface {
GetHosts() ([]*Host, error)
AddHost(*Host) (*Host, error)
DeleteHost(name string) error
UpdateHostKeyPair(hostname string, keyPair *KeyPair) (*Host, error)
GetHost(name string) (*Host, error)
AddHostListener(hostname string, listener *Listener) (*Listener, error)
DeleteHostListener(hostname string, listenerId string) error
AddLocation(*Location) (*Location, error)
GetLocation(hostname, id string) (*Location, error)
UpdateLocationUpstream(hostname, id string, upstream string) (*Location, error)
UpdateLocationOptions(hostname, locationId string, o LocationOptions) (*Location, error)
DeleteLocation(hostname, id string) error
AddLocationMiddleware(hostname, locationId string, m *MiddlewareInstance) (*MiddlewareInstance, error)
GetLocationMiddleware(hostname, locationId string, mType, id string) (*MiddlewareInstance, error)
UpdateLocationMiddleware(hostname, locationId string, m *MiddlewareInstance) (*MiddlewareInstance, error)
DeleteLocationMiddleware(hostname, locationId, mType, id string) error
GetUpstreams() ([]*Upstream, error)
AddUpstream(*Upstream) (*Upstream, error)
UpdateUpstreamOptions(upId string, o UpstreamOptions) (*Upstream, error)
GetUpstream(id string) (*Upstream, error)
DeleteUpstream(id string) error
AddEndpoint(*Endpoint) (*Endpoint, error)
GetEndpoint(upstreamId, id string) (*Endpoint, error)
DeleteEndpoint(upstreamId, id string) error
// WatchChanges is an entry point for getting the configuration changes as well as the initial configuration.
// It should be a blocking function generating events from change.go to the changes channel.
WatchChanges(changes chan interface{}, cancel chan bool) error
// GetRegistry returns registry with the supported plugins.
GetRegistry() *plugin.Registry
Close()
}
// StatsProvider provides realtime stats abount endpoints, upstreams and locations
type StatsProvider interface {
GetLocationStats(l *Location) (*RoundTripStats, error)
GetEndpointStats(e *Endpoint) (*RoundTripStats, error)
GetUpstreamStats(u *Upstream) (*RoundTripStats, error)
// GetTopLocations returns locations sorted by criteria (faulty, slow, most used)
// if hostname or upstreamId is present, will filter out locations for that host or upstreamId
GetTopLocations(hostname, upstreamId string) ([]*Location, error)
// GetTopEndpoints returns endpoints sorted by criteria (faulty, slow, mos used)
// if upsrtreamId is not empty, will filter out endpoints for that upstreamId
GetTopEndpoints(upstreamId string) ([]*Endpoint, error)
}
type KeyPair struct {
Key []byte
Cert []byte
}
func NewKeyPair(cert, key []byte) (*KeyPair, error) {
if len(cert) == 0 || len(key) == 0 {
return nil, fmt.Errorf("Provide non-empty certificate and a private key")
}
if _, err := tls.X509KeyPair(cert, key); err != nil {
return nil, err
}
return &KeyPair{Cert: cert, Key: key}, nil
}
func (c *KeyPair) Equals(o *KeyPair) bool {
return (len(c.Cert) == len(o.Cert)) &&
(len(c.Key) == len(o.Key)) &&
subtle.ConstantTimeCompare(c.Cert, o.Cert) == 1 &&
subtle.ConstantTimeCompare(c.Key, o.Key) == 1
}
type Address struct {
Network string
Address string
}
// Listener specifies the listening point - the network and interface for each host. Host can have multiple interfaces.
type Listener struct {
Id string
// HTTP or HTTPS
Protocol string
// Adddress specifies network (tcp or unix) and address (ip:port or path to unix socket)
Address Address
}
func (l *Listener) String() string {
return fmt.Sprintf("Listener(%s, %s://%s)", l.Protocol, l.Address.Network, l.Address.Address)
}
func (a *Address) Equals(o Address) bool {
return a.Network == o.Network && a.Address == o.Address
}
type HostOptions struct {
Default bool
}
// Incoming requests are matched by their hostname first. Hostname is defined by incoming 'Host' header.
// E.g. curl http://example.com/alice will be matched by the host example.com first.
type Host struct {
Name string
Locations []*Location
KeyPair *KeyPair
Listeners []*Listener
Options HostOptions
}
func NewHost(name string) (*Host, error) {
if name == "" {
return nil, fmt.Errorf("Hostname can not be empty")
}
return &Host{
Name: name,
Locations: []*Location{},
}, nil
}
func (h *Host) String() string {
return fmt.Sprintf("Host(%s)", h.Name)
}
func (h *Host) GetId() string {
return h.Name
}
// Hosts contain one or several locations. Each location defines a path - simply a regular expression that will be matched against request's url.
// Location contains link to an upstream and vulcand will use the endpoints from this upstream to serve the request.
// E.g. location loc1 will serve the request curl http://example.com/alice because it matches the path /alice:
type Location struct {
Hostname string
Path string
Id string
Upstream *Upstream
Middlewares []*MiddlewareInstance
Options LocationOptions
// Combined stats from all endpoints in the location
Stats RoundTripStats
}
// Limits contains various limits one can supply for a location.
type LocationLimits struct {
MaxMemBodyBytes int64 // Maximum size to keep in memory before buffering to disk
MaxBodyBytes int64 // Maximum size of a request body in bytes
}
// Additional options to control this location, such as timeouts
type LocationOptions struct {
// Limits contains various limits one can supply for a location.
Limits LocationLimits
// Predicate that defines when requests are allowed to failover
FailoverPredicate string
// Used in forwarding headers
Hostname string
// In this case appends new forward info to the existing header
TrustForwardHeader bool
}
func NewAddress(network, address string) (*Address, error) {
if len(address) == 0 {
return nil, fmt.Errorf("supply a non empty address")
}
network = strings.ToLower(network)
if network != TCP && network != UNIX {
return nil, fmt.Errorf("unsupported network '%s', supported networks are tcp and unix", network)
}
return &Address{Network: network, Address: address}, nil
}
func NewListener(id, protocol, network, address string) (*Listener, error) {
protocol = strings.ToLower(protocol)
if protocol != HTTP && protocol != HTTPS {
return nil, fmt.Errorf("unsupported protocol '%s', supported protocols are http and https", protocol)
}
a, err := NewAddress(network, address)
if err != nil {
return nil, err
}
return &Listener{
Id: id,
Address: *a,
Protocol: protocol,
}, nil
}
func NewLocation(hostname, id, path, upstreamId string) (*Location, error) {
return NewLocationWithOptions(hostname, id, path, upstreamId, LocationOptions{})
}
func NewLocationWithOptions(hostname, id, path, upstreamId string, options LocationOptions) (*Location, error) {
if len(path) == 0 || len(hostname) == 0 || len(upstreamId) == 0 {
return nil, fmt.Errorf("supply valid hostname, path and upstream id")
}
// Make sure location path is a valid regular expression
if _, err := regexp.Compile(path); err != nil {
return nil, fmt.Errorf("path should be a valid Golang regular expression")
}
if _, err := parseLocationOptions(options); err != nil {
return nil, err
}
return &Location{
Hostname: hostname,
Path: path,
Id: id,
Upstream: &Upstream{Id: upstreamId, Endpoints: []*Endpoint{}},
Middlewares: []*MiddlewareInstance{},
Options: options,
}, nil
}
func parseLocationOptions(l LocationOptions) (*httploc.Options, error) {
o := &httploc.Options{}
var err error
// Location-specific limits
o.Limits.MaxMemBodyBytes = l.Limits.MaxMemBodyBytes
o.Limits.MaxBodyBytes = l.Limits.MaxBodyBytes
// Failover predicate
if len(l.FailoverPredicate) != 0 {
if o.FailoverPredicate, err = threshold.ParseExpression(l.FailoverPredicate); err != nil {
return nil, err
}
}
o.Hostname = l.Hostname
o.TrustForwardHeader = l.TrustForwardHeader
return o, nil
}
func (l *Location) GetOptions() (*httploc.Options, error) {
return parseLocationOptions(l.Options)
}
func (l *Location) String() string {
return fmt.Sprintf("Location(%s/%s, %s, %s)", l.Hostname, l.Id, l.Path, l.Upstream)
}
func (l *Location) GetId() string {
return l.Id
}
func (l *Location) GetUniqueId() LocationKey {
return LocationKey{Hostname: l.Hostname, Id: l.Id}
}
type UpstreamTimeouts struct {
// Socket read timeout (before we receive the first reply header)
Read string
// Socket connect timeout
Dial string
// TLS handshake timeout
TlsHandshake string
}
type UpstreamKeepAlive struct {
// Keepalive period
Period string
// How many idle connections will be kept per host
MaxIdleConnsPerHost int
}
// Additional options to control this location, such as timeouts
type UpstreamOptions struct {
Timeouts UpstreamTimeouts
// Controls KeepAlive settins for backend servers
KeepAlive UpstreamKeepAlive
}
func (u *UpstreamOptions) Equals(o UpstreamOptions) bool {
return (u.Timeouts.Read == o.Timeouts.Read &&
u.Timeouts.Dial == o.Timeouts.Dial &&
u.Timeouts.TlsHandshake == o.Timeouts.TlsHandshake &&
u.KeepAlive.Period == o.KeepAlive.Period &&
u.KeepAlive.MaxIdleConnsPerHost == o.KeepAlive.MaxIdleConnsPerHost)
}
// Wrapper that contains information about this middleware backend-specific data used for serialization/deserialization
type MiddlewareInstance struct {
Id string
Priority int
Type string
Middleware plugin.Middleware
}
// Upstream is a collection of endpoints. Each location is assigned an upstream. Changing assigned upstream
// of the location gracefully redirects the traffic to the new endpoints of the upstream.
type Upstream struct {
Id string
Endpoints []*Endpoint
Options UpstreamOptions
}
// NewUpstreamWithOptions creates a new instance of the upstream object
func NewUpstreamWithOptions(id string, o UpstreamOptions) (*Upstream, error) {
if _, err := parseUpstreamOptions(o); err != nil {
return nil, err
}
return &Upstream{
Id: id,
Endpoints: []*Endpoint{},
Options: o,
}, nil
}
// NewUpstream creates a new instance of the upstream object with default options applied
func NewUpstream(id string) (*Upstream, error) {
return NewUpstreamWithOptions(id, UpstreamOptions{})
}
func (u *Upstream) String() string {
return fmt.Sprintf("Upstream(id=%s)", u.Id)
}
func (u *Upstream) GetId() string {
return u.Id
}
func (u *Upstream) GetUniqueId() UpstreamKey {
return UpstreamKey{Id: u.Id}
}
func (u *Upstream) GetTransportOptions() (*httploc.TransportOptions, error) {
return parseUpstreamOptions(u.Options)
}
func parseUpstreamOptions(o UpstreamOptions) (*httploc.TransportOptions, error) {
t := &httploc.TransportOptions{}
var err error
// Connection timeouts
if len(o.Timeouts.Read) != 0 {
if t.Timeouts.Read, err = time.ParseDuration(o.Timeouts.Read); err != nil {
return nil, fmt.Errorf("invalid read timeout: %s", err)
}
}
if len(o.Timeouts.Dial) != 0 {
if t.Timeouts.Dial, err = time.ParseDuration(o.Timeouts.Dial); err != nil {
return nil, fmt.Errorf("invalid dial timeout: %s", err)
}
}
if len(o.Timeouts.TlsHandshake) != 0 {
if t.Timeouts.TlsHandshake, err = time.ParseDuration(o.Timeouts.TlsHandshake); err != nil {
return nil, fmt.Errorf("invalid tls handshake timeout: %s", err)
}
}
// Keep Alive parameters
if len(o.KeepAlive.Period) != 0 {
if t.KeepAlive.Period, err = time.ParseDuration(o.KeepAlive.Period); err != nil {
return nil, fmt.Errorf("invalid tls handshake timeout: %s", err)
}
}
t.KeepAlive.MaxIdleConnsPerHost = o.KeepAlive.MaxIdleConnsPerHost
return t, nil
}
// Endpoint is a final destination of the request
type Endpoint struct {
Id string
Url string
UpstreamId string
Stats RoundTripStats
}
func NewEndpoint(upstreamId, id, url string) (*Endpoint, error) {
if upstreamId == "" {
return nil, fmt.Errorf("upstream id can not be empty")
}
if _, err := netutils.ParseUrl(url); err != nil {
return nil, fmt.Errorf("endpoint url '%s' is not valid", url)
}
return &Endpoint{
UpstreamId: upstreamId,
Id: id,
Url: url,
}, nil
}
func (e *Endpoint) String() string {
return fmt.Sprintf("Endpoint(%s, %s, %s, %s)", e.Id, e.UpstreamId, e.Url, e.Stats)
}
func (e *Endpoint) GetId() string {
return e.Id
}
func (e *Endpoint) GetUniqueId() EndpointKey {
return EndpointKey{UpstreamId: e.UpstreamId, Id: e.Id}
}
type LatencyBrackets []Bracket
func (l LatencyBrackets) GetQuantile(q float64) (*Bracket, error) {
if len(l) == 0 {
return nil, fmt.Errorf("quantile %f not found", q)
}
for _, b := range l {
if b.Quantile == q {
return &b, nil
}
}
return nil, fmt.Errorf("quantile %f not found", q)
}
// RoundTrip stats contain real time statistics about performance of Endpoint or Location
// such as latency, processed and failed requests.
type RoundTripStats struct {
Verdict Verdict
Counters Counters
LatencyBrackets LatencyBrackets
}
func NewRoundTripStats(m *metrics.RoundTripMetrics) (*RoundTripStats, error) {
codes := m.GetStatusCodesCounts()
sc := make([]StatusCode, 0, len(codes))
for k, v := range codes {
if v != 0 {
sc = append(sc, StatusCode{Code: k, Count: v})
}
}
h, err := m.GetLatencyHistogram()
if err != nil {
return nil, err
}
return &RoundTripStats{
Counters: Counters{
NetErrors: m.GetNetworkErrorCount(),
Total: m.GetTotalCount(),
Period: m.GetOptions().CounterResolution * time.Duration(m.GetOptions().CounterBuckets),
StatusCodes: sc,
},
LatencyBrackets: NewBrackets(h),
}, nil
}
// NetErroRate calculates the amont of ntwork errors such as time outs and dropped connection
// that occured in the given time window
func (e *RoundTripStats) NetErrorRatio() float64 {
if e.Counters.Total == 0 {
return 0
}
return (float64(e.Counters.NetErrors) / float64(e.Counters.Total))
}
// AppErrorRate calculates the ratio of 500 responses that designate internal server errors
// to success responses - 2xx, it specifically not counts 4xx or any other than 500 error to avoid noisy results.
func (e *RoundTripStats) AppErrorRatio() float64 {
return e.ResponseCodeRatio(http.StatusInternalServerError, http.StatusInternalServerError+1, 200, 300)
}
// ResponseCodeRatio calculates ratio of count(startA to endA) / count(startB to endB)
func (e *RoundTripStats) ResponseCodeRatio(startA, endA, startB, endB int) float64 {
a := int64(0)
b := int64(0)
for _, status := range e.Counters.StatusCodes {
if status.Code < endA && status.Code >= startA {
a += status.Count
}
if status.Code < endB && status.Code >= startB {
b += status.Count
}
}
if b != 0 {
return float64(a) / float64(b)
}
return 0
}
func (e *RoundTripStats) RequestsPerSecond() float64 {
if e.Counters.Period == 0 {
return 0
}
return float64(e.Counters.Total) / float64(e.Counters.Period/time.Second)
}
func (e *RoundTripStats) String() string {
return fmt.Sprintf("%.2f requests/sec, %.2f failures/sec", e.RequestsPerSecond(), e.NetErrorRatio())
}
type Verdict struct {
IsBad bool
Anomalies []Anomaly
}
func (v Verdict) String() string {
return fmt.Sprintf("verdict[bad=%t, anomalies=%v]", v.IsBad, v.Anomalies)
}
type Anomaly struct {
Code int
Message string
}
func (a Anomaly) String() string {
return fmt.Sprintf("(%d) %s", a.Code, a.Message)
}
type NotFoundError struct {
Message string
}
func (n *NotFoundError) Error() string {
if n.Message != "" {
return n.Message
} else {
return "Object not found"
}
}
type AlreadyExistsError struct {
Message string
}
func (n *AlreadyExistsError) Error() string {
return n.Message
}
type Counters struct {
Period time.Duration
NetErrors int64
Total int64
StatusCodes []StatusCode
}
type StatusCode struct {
Code int
Count int64
}
type Bracket struct {
Quantile float64
Value time.Duration
}
func NewBrackets(h metrics.Histogram) []Bracket {
quantiles := []float64{50, 75, 95, 99, 99.9}
brackets := make([]Bracket, len(quantiles))
for i, v := range quantiles {
brackets[i] = Bracket{
Quantile: v,
Value: time.Duration(h.ValueAtQuantile(v)) * time.Microsecond,
}
}
return brackets
}
type LocationKey struct {
Hostname string
Id string
}
func (l LocationKey) String() string {
return fmt.Sprintf("%s.%s", l.Hostname, l.Id)
}
type EndpointKey struct {
UpstreamId string
Id string
}
func (e EndpointKey) String() string {
return fmt.Sprintf("%s.%s", e.UpstreamId, e.Id)
}
func ParseEndpointKey(v string) (*EndpointKey, error) {
out := strings.SplitN(v, ".", 2)
if len(out) != 2 {
return nil, fmt.Errorf("invalid id: '%s'", v)
}
return &EndpointKey{UpstreamId: out[0], Id: out[1]}, nil
}
func MustParseEndpointKey(v string) EndpointKey {
k, err := ParseEndpointKey(v)
if err != nil {
panic(err)
}
return *k
}
type UpstreamKey struct {
Id string
}
func (u UpstreamKey) String() string {
return u.Id
}
const (
HTTP = "http"
HTTPS = "https"
TCP = "tcp"
UNIX = "unix"
)