-
Notifications
You must be signed in to change notification settings - Fork 492
/
loadbalance.go
301 lines (260 loc) · 8.34 KB
/
loadbalance.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
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package proxies
import (
"fmt"
"hash/fnv"
"math/rand"
"sync/atomic"
"time"
"github.com/megaease/easegress/v2/pkg/logger"
"github.com/megaease/easegress/v2/pkg/protocols"
)
const (
// LoadBalancePolicyRoundRobin is the load balance policy of round robin.
LoadBalancePolicyRoundRobin = "roundRobin"
// LoadBalancePolicyRandom is the load balance policy of random.
LoadBalancePolicyRandom = "random"
// LoadBalancePolicyWeightedRandom is the load balance policy of weighted random.
LoadBalancePolicyWeightedRandom = "weightedRandom"
// LoadBalancePolicyIPHash is the load balance policy of IP hash.
LoadBalancePolicyIPHash = "ipHash"
// LoadBalancePolicyHeaderHash is the load balance policy of HTTP header hash.
LoadBalancePolicyHeaderHash = "headerHash"
)
// LoadBalancer is the interface of a load balancer.
type LoadBalancer interface {
ChooseServer(req protocols.Request) *Server
ReturnServer(server *Server, req protocols.Request, resp protocols.Response)
Close()
}
// LoadBalanceSpec is the spec to create a load balancer.
//
// TODO: this spec currently include all options for all load balance policies,
// this is not good as new policies could be added in the future, we should
// convert it to a map later.
type LoadBalanceSpec struct {
Policy string `json:"policy" jsonschema:"omitempty"`
HeaderHashKey string `json:"headerHashKey" jsonschema:"omitempty"`
ForwardKey string `json:"forwardKey" jsonschema:"omitempty"`
StickySession *StickySessionSpec `json:"stickySession" jsonschema:"omitempty"`
HealthCheck *HealthCheckSpec `json:"healthCheck" jsonschema:"omitempty"`
}
// LoadBalancePolicy is the interface of a load balance policy.
type LoadBalancePolicy interface {
ChooseServer(req protocols.Request, sg *ServerGroup) *Server
}
// GeneralLoadBalancer implements a general purpose load balancer.
type GeneralLoadBalancer struct {
spec *LoadBalanceSpec
servers []*Server
healthyServers atomic.Pointer[ServerGroup]
done chan struct{}
lbp LoadBalancePolicy
ss SessionSticker
hc HealthChecker
}
// NewGeneralLoadBalancer creates a new GeneralLoadBalancer.
func NewGeneralLoadBalancer(spec *LoadBalanceSpec, servers []*Server) *GeneralLoadBalancer {
lb := &GeneralLoadBalancer{
spec: spec,
servers: servers,
}
lb.healthyServers.Store(newServerGroup(servers))
return lb
}
// Init initializes the load balancer.
func (glb *GeneralLoadBalancer) Init(
fnNewSessionSticker func(*StickySessionSpec) SessionSticker,
fnNewHealthChecker func(*HealthCheckSpec) HealthChecker,
lbp LoadBalancePolicy,
) {
// load balance policy
if lbp == nil {
switch glb.spec.Policy {
case LoadBalancePolicyRoundRobin, "":
lbp = &RoundRobinLoadBalancePolicy{}
case LoadBalancePolicyRandom:
lbp = &RandomLoadBalancePolicy{}
case LoadBalancePolicyWeightedRandom:
lbp = &WeightedRandomLoadBalancePolicy{}
case LoadBalancePolicyIPHash:
lbp = &IPHashLoadBalancePolicy{}
case LoadBalancePolicyHeaderHash:
lbp = &HeaderHashLoadBalancePolicy{spec: glb.spec}
default:
logger.Errorf("unsupported load balancing policy: %s", glb.spec.Policy)
lbp = &RoundRobinLoadBalancePolicy{}
}
}
glb.lbp = lbp
// sticky session
if glb.spec.StickySession != nil {
ss := fnNewSessionSticker(glb.spec.StickySession)
ss.UpdateServers(glb.servers)
glb.ss = ss
}
// health check
if glb.spec.HealthCheck == nil {
return
}
if glb.spec.HealthCheck.Fails <= 0 {
glb.spec.HealthCheck.Fails = 1
}
if glb.spec.HealthCheck.Passes <= 0 {
glb.spec.HealthCheck.Passes = 1
}
glb.hc = fnNewHealthChecker(glb.spec.HealthCheck)
interval, _ := time.ParseDuration(glb.spec.HealthCheck.Interval)
if interval <= 0 {
interval = time.Minute
}
ticker := time.NewTicker(interval)
glb.done = make(chan struct{})
go func() {
for {
select {
case <-glb.done:
ticker.Stop()
return
case <-ticker.C:
glb.checkServers()
}
}
}()
}
func (glb *GeneralLoadBalancer) checkServers() {
changed := false
servers := make([]*Server, 0, len(glb.servers))
for _, svr := range glb.servers {
succ := glb.hc.Check(svr)
if succ {
if svr.HealthCounter < 0 {
svr.HealthCounter = 0
}
svr.HealthCounter++
if svr.Unhealth && svr.HealthCounter >= glb.spec.HealthCheck.Passes {
logger.Warnf("server:%v becomes healthy.", svr.ID())
svr.Unhealth = false
changed = true
}
} else {
if svr.HealthCounter > 0 {
svr.HealthCounter = 0
}
svr.HealthCounter--
if svr.Healthy() && svr.HealthCounter <= -glb.spec.HealthCheck.Fails {
logger.Warnf("server:%v becomes unhealthy.", svr.ID())
svr.Unhealth = true
changed = true
}
}
if svr.Healthy() {
servers = append(servers, svr)
}
}
if !changed {
return
}
glb.healthyServers.Store(newServerGroup(servers))
if glb.ss != nil {
glb.ss.UpdateServers(servers)
}
}
// ChooseServer chooses a server according to the load balancing spec.
func (glb *GeneralLoadBalancer) ChooseServer(req protocols.Request) *Server {
sg := glb.healthyServers.Load()
if sg == nil || len(sg.Servers) == 0 {
return nil
}
if glb.ss != nil {
if svr := glb.ss.GetServer(req, sg); svr != nil {
return svr
}
}
return glb.lbp.ChooseServer(req, sg)
}
// ReturnServer returns a server to the load balancer.
func (glb *GeneralLoadBalancer) ReturnServer(server *Server, req protocols.Request, resp protocols.Response) {
if glb.ss != nil {
glb.ss.ReturnServer(server, req, resp)
}
}
// Close closes the load balancer
func (glb *GeneralLoadBalancer) Close() {
if glb.hc != nil {
close(glb.done)
glb.hc.Close()
}
if glb.ss != nil {
glb.ss.Close()
}
}
// RandomLoadBalancePolicy is a load balance policy that chooses a server randomly.
type RandomLoadBalancePolicy struct {
}
// ChooseServer chooses a server randomly.
func (lbp *RandomLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server {
return sg.Servers[rand.Intn(len(sg.Servers))]
}
// RoundRobinLoadBalancePolicy is a load balance policy that chooses a server by round robin.
type RoundRobinLoadBalancePolicy struct {
counter uint64
}
// ChooseServer chooses a server by round robin.
func (lbp *RoundRobinLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server {
counter := atomic.AddUint64(&lbp.counter, 1) - 1
return sg.Servers[int(counter)%len(sg.Servers)]
}
// WeightedRandomLoadBalancePolicy is a load balance policy that chooses a server randomly by weight.
type WeightedRandomLoadBalancePolicy struct {
}
// ChooseServer chooses a server randomly by weight.
func (lbp *WeightedRandomLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server {
w := rand.Intn(sg.TotalWeight)
for _, svr := range sg.Servers {
w -= svr.Weight
if w < 0 {
return svr
}
}
panic(fmt.Errorf("BUG: should not run to here, total weight=%d", sg.TotalWeight))
}
// IPHashLoadBalancePolicy is a load balance policy that chooses a server by ip hash.
type IPHashLoadBalancePolicy struct {
}
// ChooseServer chooses a server by ip hash.
func (lbp *IPHashLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server {
ip := req.RealIP()
hash := fnv.New32()
hash.Write([]byte(ip))
return sg.Servers[hash.Sum32()%uint32(len(sg.Servers))]
}
// HeaderHashLoadBalancePolicy is a load balance policy that chooses a server by header hash.
type HeaderHashLoadBalancePolicy struct {
spec *LoadBalanceSpec
}
// ChooseServer chooses a server by header hash.
func (lbp *HeaderHashLoadBalancePolicy) ChooseServer(req protocols.Request, sg *ServerGroup) *Server {
v, ok := req.Header().Get(lbp.spec.HeaderHashKey).(string)
if !ok {
panic("HeaderHashLoadBalancePolicy only support headers with string values")
}
hash := fnv.New32()
hash.Write([]byte(v))
return sg.Servers[hash.Sum32()%uint32(len(sg.Servers))]
}