-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
lb_policy.go
114 lines (101 loc) · 3.36 KB
/
lb_policy.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
/*
Copyright 2020 The Knative Authors
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.
*/
// This file contains the load load balancing policies for Activator load balancing.
package net
import (
"context"
"math/rand"
"sync"
)
// lbPolicy is a functor that selects a target pod from the list, or (noop, nil) if
// no such target can be currently acquired.
// Policies will presume that `targets` list is appropriately guarded by the caller,
// that is while podTrackers themselves can change during this call, the list
// and pointers therein are immutable.
type lbPolicy func(ctx context.Context, targets []*podTracker) (func(), *podTracker)
// randomLBPolicy is a load balancer policy that picks a random target.
// This approximates the LB policy done by K8s Service (IPTables based).
//
// nolint // This is currently unused but kept here for posterity.
func randomLBPolicy(_ context.Context, targets []*podTracker) (func(), *podTracker) {
return noop, targets[rand.Intn(len(targets))]
}
// randomChoice2Policy implements the Power of 2 choices LB algorithm
func randomChoice2Policy(_ context.Context, targets []*podTracker) (func(), *podTracker) {
// Avoid random if possible.
l := len(targets)
// One tracker = no choice.
if l == 1 {
pick := targets[0]
pick.increaseWeight()
return pick.decreaseWeight, pick
}
r1, r2 := 0, 1
// Two trackers - we know both contestants,
// otherwise pick 2 random unequal integers.
if l > 2 {
// nolint:gosec // We don't need cryptographic randomness here.
r1, r2 = rand.Intn(l), rand.Intn(l-1)
// shift second half of second rand.Intn down so we're picking
// from range of numbers other than r1.
// i.e. rand.Intn(l-1) range is now from range [0,r1),[r1+1,l).
if r2 >= r1 {
r2++
}
}
pick, alt := targets[r1], targets[r2]
// Possible race here, but this policy is for CC=0,
// so fine.
if pick.getWeight() > alt.getWeight() {
pick = alt
}
pick.increaseWeight()
return pick.decreaseWeight, pick
}
// firstAvailableLBPolicy is a load balancer policy, that picks the first target
// that has capacity to serve the request right now.
func firstAvailableLBPolicy(ctx context.Context, targets []*podTracker) (func(), *podTracker) {
for _, t := range targets {
if cb, ok := t.Reserve(ctx); ok {
return cb, t
}
}
return noop, nil
}
func newRoundRobinPolicy() lbPolicy {
var (
mu sync.Mutex
idx int
)
return func(ctx context.Context, targets []*podTracker) (func(), *podTracker) {
mu.Lock()
defer mu.Unlock()
// The number of trackers might have shrunk, so reset to 0.
l := len(targets)
if idx >= l {
idx = 0
}
// Now for |targets| elements and check every next one in
// round robin fashion.
for i := 0; i < l; i++ {
p := (idx + i) % l
if cb, ok := targets[p].Reserve(ctx); ok {
// We want to start with the next index.
idx = p + 1
return cb, targets[p]
}
}
// We exhausted all the options...
return noop, nil
}
}