forked from vulcand/vulcand
-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
712 lines (605 loc) · 17.4 KB
/
model.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
// Package model defines interfaces and structures controlling the proxy configuration.
package engine
import (
"crypto/subtle"
"crypto/tls"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/oxy/memmetrics"
"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/oxy/stream"
"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/route"
"github.com/mailgun/vulcand/plugin"
)
// StatsProvider provides realtime stats abount endpoints, backends and locations
type StatsProvider interface {
FrontendStats(FrontendKey) (*RoundTripStats, error)
ServerStats(ServerKey) (*RoundTripStats, error)
BackendStats(BackendKey) (*RoundTripStats, error)
// TopFrontends returns locations sorted by criteria (faulty, slow, most used)
// if hostname or backendId is present, will filter out locations for that host or backendId
TopFrontends(*BackendKey) ([]Frontend, error)
// TopServers returns endpoints sorted by criteria (faulty, slow, mos used)
// if backendId is not empty, will filter out endpoints for that backendId
TopServers(*BackendKey) ([]Server, 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
}
type ListenerKey struct {
Id string
}
func (l ListenerKey) String() string {
return l.Id
}
// 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
// Scope is optional expression that limits the operational scope of this listener
Scope string
// Settings provides listener-type specific settings, e.g. TLS settings for HTTPS listener
Settings *HTTPSListenerSettings `json:",omitempty"`
}
func (l *Listener) TLSConfig() (*tls.Config, error) {
if l.Protocol != HTTPS {
return nil, fmt.Errorf("wrong listener proto: %v", l.Protocol)
}
if l.Settings == nil {
return NewTLSConfig(&TLSSettings{})
}
return NewTLSConfig(&l.Settings.TLS)
}
func (l *Listener) String() string {
return fmt.Sprintf("Listener(%s, %s://%s, scope=%s)", l.Protocol, l.Address.Network, l.Address.Address, l.Scope)
}
func (a *Address) Equals(o Address) bool {
return a.Network == o.Network && a.Address == o.Address
}
func (l *Listener) SettingsEquals(o *Listener) bool {
if l.Settings == nil && o.Settings == nil {
return true
}
ls, os := l.Settings, o.Settings
if (ls == nil && os != nil) || (ls != nil && os == nil) {
return false
}
return (&os.TLS).Equals(&ls.TLS)
}
type HTTPSListenerSettings struct {
TLS TLSSettings
}
// Sets up OCSP stapling, see http://en.wikipedia.org/wiki/OCSP_stapling
type OCSPSettings struct {
Enabled bool
Period string
// Optional responders. Responder is the CA-operated HTTP server that responds with revocation status
// If set, this field will override
Responders []string
SkipSignatureCheck bool
}
func (o *OCSPSettings) RefreshPeriod() (time.Duration, error) {
if o.Period == "" {
return time.Hour, nil
}
return time.ParseDuration(o.Period)
}
func (o *OCSPSettings) Equals(other *OCSPSettings) bool {
if o.Enabled != other.Enabled {
return false
}
p, err := o.RefreshPeriod()
if err != nil {
return false
}
p2, err := other.RefreshPeriod()
if err != nil {
return false
}
if p != p2 {
return false
}
if len(o.Responders) != len(other.Responders) {
return false
}
if len(o.Responders) == 0 || len(other.Responders) == 0 {
return true
}
for i := range o.Responders {
if o.Responders[i] != other.Responders[i] {
return false
}
}
return true
}
type HostSettings struct {
Default bool
KeyPair *KeyPair
OCSP OCSPSettings
}
type HostKey struct {
Name string
}
func (h HostKey) String() string {
return h.Name
}
// 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
Settings HostSettings
}
func NewHost(name string, settings HostSettings) (*Host, error) {
if name == "" {
return nil, fmt.Errorf("Hostname can not be empty")
}
return &Host{
Name: name,
Settings: settings,
}, nil
}
func (h *Host) String() string {
return fmt.Sprintf("Host(%s, keyPair=%t, ocsp=%t)", h.Name, h.Settings.KeyPair != nil, h.Settings.OCSP.Enabled)
}
func (h *Host) GetId() string {
return h.Name
}
// Frontend is connected to a backend and vulcand will use the servers from this backend.
type Frontend struct {
Id string
Route string
Type string
BackendId string
Stats *RoundTripStats `json:",omitempty"`
Settings interface{} `json:",omitempty"`
}
// Limits contains various limits one can supply for a location.
type HTTPFrontendLimits 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 HTTPFrontendSettings struct {
// Limits contains various limits one can supply for a location.
Limits HTTPFrontendLimits
// 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, scope string, settings *HTTPSListenerSettings) (*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)
}
if scope != "" {
if !route.IsValid(scope) {
return nil, fmt.Errorf("Scope should be a valid route expression")
}
}
a, err := NewAddress(network, address)
if err != nil {
return nil, err
}
return &Listener{
Scope: scope,
Id: id,
Address: *a,
Protocol: protocol,
Settings: settings,
}, nil
}
func NewHTTPFrontend(id, backendId string, routeExpr string, settings HTTPFrontendSettings) (*Frontend, error) {
if len(id) == 0 || len(backendId) == 0 {
return nil, fmt.Errorf("supply valid route, id, and backendId")
}
// Make sure location path is a valid route expression
if !route.IsValid(routeExpr) {
return nil, fmt.Errorf("route should be a valid route expression")
}
if settings.FailoverPredicate != "" && !stream.IsValidExpression(settings.FailoverPredicate) {
return nil, fmt.Errorf("invalid failover predicate")
}
return &Frontend{
Id: id,
BackendId: backendId,
Route: routeExpr,
Type: HTTP,
Settings: settings,
}, nil
}
func (f *Frontend) HTTPSettings() HTTPFrontendSettings {
return (f.Settings).(HTTPFrontendSettings)
}
func (l HTTPFrontendSettings) Equals(o HTTPFrontendSettings) bool {
return (l.Limits.MaxMemBodyBytes == o.Limits.MaxMemBodyBytes &&
l.Limits.MaxBodyBytes == o.Limits.MaxBodyBytes &&
l.FailoverPredicate == o.FailoverPredicate &&
l.Hostname == o.Hostname &&
l.TrustForwardHeader == o.TrustForwardHeader)
}
func (f *Frontend) String() string {
return fmt.Sprintf("Frontend(%v, %v, %v)", f.Type, f.Id, f.BackendId)
}
func (l *Frontend) GetId() string {
return l.Id
}
func (l *Frontend) GetKey() FrontendKey {
return FrontendKey{Id: l.Id}
}
type HTTPBackendTimeouts struct {
// Socket read timeout (before we receive the first reply header)
Read string
// Socket connect timeout
Dial string
// TLS handshake timeout
TLSHandshake string
}
type HTTPBackendKeepAlive struct {
// Keepalive period
Period string
// How many idle connections will be kept per host
MaxIdleConnsPerHost int
}
type HTTPBackendSettings struct {
// Timeouts provides timeout settings for backend servers
Timeouts HTTPBackendTimeouts
// KeepAlive controls keep-alive settings for backend servers
KeepAlive HTTPBackendKeepAlive
// TLS provides optional TLS settings for HTTP backend
TLS *TLSSettings `json:",omitempty"`
}
func (s *HTTPBackendSettings) Equals(o HTTPBackendSettings) bool {
return (s.Timeouts.Read == o.Timeouts.Read &&
s.Timeouts.Dial == o.Timeouts.Dial &&
s.Timeouts.TLSHandshake == o.Timeouts.TLSHandshake &&
s.KeepAlive.Period == o.KeepAlive.Period &&
s.KeepAlive.MaxIdleConnsPerHost == o.KeepAlive.MaxIdleConnsPerHost &&
((s.TLS == nil && o.TLS == nil) ||
((s.TLS != nil && o.TLS != nil) && s.TLS.Equals(o.TLS))))
}
type MiddlewareKey struct {
FrontendKey FrontendKey
Id string
}
func (m MiddlewareKey) String() string {
return fmt.Sprintf("%v.%v", m.FrontendKey, m.Id)
}
// Middleware contains information about this middleware backend-specific data used for serialization/deserialization
type Middleware struct {
Id string
Priority int
Type string
Middleware plugin.Middleware
}
// Backend is a collection of endpoints. Each location is assigned an backend. Changing assigned backend
// of the location gracefully redirects the traffic to the new endpoints of the backend.
type Backend struct {
Id string
Type string
Stats *RoundTripStats `json:",omitempty"`
Settings interface{}
}
// NewBackend creates a new instance of the backend object
func NewHTTPBackend(id string, s HTTPBackendSettings) (*Backend, error) {
if _, err := transportSettings(s); err != nil {
return nil, err
}
return &Backend{
Id: id,
Type: HTTP,
Settings: s,
}, nil
}
func (b *Backend) HTTPSettings() HTTPBackendSettings {
return b.Settings.(HTTPBackendSettings)
}
func (b *Backend) String() string {
return fmt.Sprintf("Backend(id=%s)", b.Id)
}
func (b *Backend) GetId() string {
return b.Id
}
func (b *Backend) GetUniqueId() BackendKey {
return BackendKey{Id: b.Id}
}
func (b *Backend) TransportSettings() (*TransportSettings, error) {
return transportSettings(b.Settings.(HTTPBackendSettings))
}
func transportSettings(s HTTPBackendSettings) (*TransportSettings, error) {
t := &TransportSettings{}
var err error
// Connection timeouts
if len(s.Timeouts.Read) != 0 {
if t.Timeouts.Read, err = time.ParseDuration(s.Timeouts.Read); err != nil {
return nil, fmt.Errorf("invalid read timeout: %s", err)
}
}
if len(s.Timeouts.Dial) != 0 {
if t.Timeouts.Dial, err = time.ParseDuration(s.Timeouts.Dial); err != nil {
return nil, fmt.Errorf("invalid dial timeout: %s", err)
}
}
if len(s.Timeouts.TLSHandshake) != 0 {
if t.Timeouts.TLSHandshake, err = time.ParseDuration(s.Timeouts.TLSHandshake); err != nil {
return nil, fmt.Errorf("invalid tls handshake timeout: %s", err)
}
}
// Keep Alive parameters
if len(s.KeepAlive.Period) != 0 {
if t.KeepAlive.Period, err = time.ParseDuration(s.KeepAlive.Period); err != nil {
return nil, fmt.Errorf("invalid tls handshake timeout: %s", err)
}
}
t.KeepAlive.MaxIdleConnsPerHost = s.KeepAlive.MaxIdleConnsPerHost
if s.TLS != nil {
config, err := NewTLSConfig(s.TLS)
if err != nil {
return nil, err
}
t.TLS = config
}
return t, nil
}
// Server is a final destination of the request
type Server struct {
Id string
URL string
Stats *RoundTripStats `json:",omitempty"`
}
func NewServer(id, u string) (*Server, error) {
if _, err := url.ParseRequestURI(u); err != nil {
return nil, fmt.Errorf("endpoint url '%s' is not valid", u)
}
return &Server{
Id: id,
URL: u,
}, nil
}
func (e *Server) String() string {
return fmt.Sprintf("HTTPServer(%s, %s, %s)", e.Id, e.URL, e.Stats)
}
func (e *Server) GetId() string {
return 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 in %v", q, l)
}
for _, b := range l {
if b.Quantile == q {
return &b, nil
}
}
return nil, fmt.Errorf("quantile %f not found", q)
}
// RoundTripStats contain real time statistics about performance of Server or Frontend
// such as latency, processed and failed requests.
type RoundTripStats struct {
Verdict Verdict
Counters Counters
LatencyBrackets LatencyBrackets
}
func NewRoundTripStats(m *memmetrics.RTMetrics) (*RoundTripStats, error) {
codes := m.StatusCodesCounts()
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.LatencyHistogram()
if err != nil {
return nil, err
}
return &RoundTripStats{
Counters: Counters{
NetErrors: m.NetworkErrorCount(),
Total: m.TotalCount(),
Period: m.CounterWindowSize(),
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 InvalidFormatError struct {
Message string
}
func (n *InvalidFormatError) Error() string {
if n.Message != "" {
return n.Message
} else {
return "Invalid format"
}
}
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 *memmetrics.HDRHistogram) []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 FrontendKey struct {
Id string
}
func (f FrontendKey) String() string {
return f.Id
}
type ServerKey struct {
BackendKey BackendKey
Id string
}
func (e ServerKey) String() string {
return fmt.Sprintf("%v.%v", e.BackendKey, e.Id)
}
func ParseServerKey(v string) (*ServerKey, error) {
out := strings.SplitN(v, ".", 2)
if len(out) != 2 {
return nil, fmt.Errorf("invalid id: '%s'", v)
}
return &ServerKey{BackendKey: BackendKey{Id: out[0]}, Id: out[1]}, nil
}
func MustParseServerKey(v string) ServerKey {
k, err := ParseServerKey(v)
if err != nil {
panic(err)
}
return *k
}
type BackendKey struct {
Id string
}
func (u BackendKey) String() string {
return u.Id
}
const (
HTTP = "http"
HTTPS = "https"
TCP = "tcp"
UNIX = "unix"
NoTTL = 0
)
type TransportTimeouts struct {
// Socket read timeout (before we receive the first reply header)
Read time.Duration
// Socket connect timeout
Dial time.Duration
// TLS handshake timeout
TLSHandshake time.Duration
}
type TransportKeepAlive struct {
// Keepalive period
Period time.Duration
// How many idle connections will be kept per host
MaxIdleConnsPerHost int
}
type TransportSettings struct {
Timeouts TransportTimeouts
KeepAlive TransportKeepAlive
TLS *tls.Config
}