/
algorithm.go
202 lines (168 loc) · 4.76 KB
/
algorithm.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
package loadbalancer
import (
"errors"
"hash/fnv"
"math/rand"
"net/url"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/net"
"github.com/zalando/skipper/routing"
)
// Algorithm indicates the used load balancing algorithm.
type Algorithm int
const (
// None is the default non-specified algorithm.
None Algorithm = iota
// RoundRobin indicates round-robin load balancing between the backend endpoints.
RoundRobin
// Random indicates random choice between the backend endpoints.
Random
// ConsistentHash indicates choice between the backends based on their hashed address.
ConsistentHash
)
var (
algorithms = map[Algorithm]initializeAgorithm{
RoundRobin: newRoundRobin,
Random: newRandom,
ConsistentHash: newConsistentHash,
}
defaultAlgorithm = newRoundRobin
)
func newRoundRobin(endpoints []string) routing.LBAlgorithm {
i := time.Now().UnixNano()
rand.Seed(i)
return &roundRobin{
index: rand.Intn(len(endpoints)),
}
}
type roundRobin struct {
mx sync.Mutex
index int
}
// Apply implements routing.LBAlgorithm with a roundrobin algorithm.
func (r *roundRobin) Apply(ctx *routing.LBContext) routing.LBEndpoint {
r.mx.Lock()
defer r.mx.Unlock()
r.index = (r.index + 1) % len(ctx.Route.LBEndpoints)
return ctx.Route.LBEndpoints[r.index]
}
type random struct {
rand *rand.Rand
}
func newRandom(endpoints []string) routing.LBAlgorithm {
t := time.Now().UnixNano()
return &random{rand: rand.New(rand.NewSource(t))}
}
// Apply implements routing.LBAlgorithm with a stateless random algorithm.
func (r *random) Apply(ctx *routing.LBContext) routing.LBEndpoint {
return ctx.Route.LBEndpoints[r.rand.Intn(len(ctx.Route.LBEndpoints))]
}
type consistentHash struct{}
func newConsistentHash(endpoints []string) routing.LBAlgorithm {
return &consistentHash{}
}
// Apply implements routing.LBAlgorithm with a consistent hash algorithm.
func (*consistentHash) Apply(ctx *routing.LBContext) routing.LBEndpoint {
var sum uint32
h := fnv.New32()
key := net.RemoteHost(ctx.Request).String()
if _, err := h.Write([]byte(key)); err != nil {
log.Errorf("Failed to write '%s' into hash: %v", key, err)
return ctx.Route.LBEndpoints[rand.Intn(len(ctx.Route.LBEndpoints))]
}
sum = h.Sum32()
choice := int(sum) % len(ctx.Route.LBEndpoints)
if choice < 0 {
choice = len(ctx.Route.LBEndpoints) + choice
}
return ctx.Route.LBEndpoints[choice]
}
type (
algorithmProvider struct{}
initializeAgorithm func(endpoints []string) routing.LBAlgorithm
)
// NewAlgorithmProvider creates a routing.PostProcessor used to initialize
// the algorithm of load balancing routes.
func NewAlgorithmProvider() routing.PostProcessor {
return &algorithmProvider{}
}
// AlgorithmFromString parses the string representation of the algorithm definition.
func AlgorithmFromString(a string) (Algorithm, error) {
switch a {
case "":
// This means that the user didn't explicitly specify which
// algorithm should be used, and we will use a default one.
return None, nil
case "roundRobin":
return RoundRobin, nil
case "random":
return Random, nil
case "consistentHash":
return ConsistentHash, nil
default:
return None, errors.New("unsupported algorithm")
}
}
// String returns the string representation of an algorithm definition.
func (a Algorithm) String() string {
switch a {
case RoundRobin:
return "roundRobin"
case Random:
return "random"
case ConsistentHash:
return "consistentHash"
default:
return ""
}
}
func parseEndpoints(r *routing.Route) error {
r.LBEndpoints = make([]routing.LBEndpoint, len(r.Route.LBEndpoints))
for i, e := range r.Route.LBEndpoints {
eu, err := url.ParseRequestURI(e)
if err != nil {
return err
}
r.LBEndpoints[i] = routing.LBEndpoint{Scheme: eu.Scheme, Host: eu.Host}
}
return nil
}
func setAlgorithm(r *routing.Route) error {
t, err := AlgorithmFromString(r.Route.LBAlgorithm)
if err != nil {
return err
}
initialize := defaultAlgorithm
if t != None {
initialize = algorithms[t]
}
r.LBAlgorithm = initialize(r.Route.LBEndpoints)
return nil
}
// Do implements routing.PostProcessor
func (p *algorithmProvider) Do(r []*routing.Route) []*routing.Route {
rr := make([]*routing.Route, 0, len(r))
for _, ri := range r {
if ri.Route.BackendType != eskip.LBBackend {
rr = append(rr, ri)
continue
}
if len(ri.Route.LBEndpoints) == 0 {
log.Errorf("failed to post-process LB route: %s, no endpoints defined", ri.Id)
continue
}
if err := parseEndpoints(ri); err != nil {
log.Errorf("failed to parse LB endpoints for route %s: %v", ri.Id, err)
continue
}
if err := setAlgorithm(ri); err != nil {
log.Errorf("failed to set LB algorithm implementation for route %s: %v", ri.Id, err)
continue
}
rr = append(rr, ri)
}
return rr
}