-
Notifications
You must be signed in to change notification settings - Fork 10
/
p2c.go
219 lines (193 loc) · 3.93 KB
/
p2c.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
package ydb
import (
"math/rand"
"sync"
"time"
)
type P2CConfig struct {
// PreferLocal reports whether p2c balancer should prefer local endpoint
// when all other runtime indicators are the same (such as error rate or
// average response time).
PreferLocal bool
// OpTimeThreshold specifies such difference between endpoint average
// operation time when it becomes significant to be used during comparison.
OpTimeThreshold time.Duration
}
type criterion interface {
Best(a, b *connListElement) *connListElement
}
type connRuntimeCriterion struct {
PreferLocal bool
OpTimeThreshold time.Duration
}
func (c connRuntimeCriterion) chooseByState(c1, c2 *connListElement, s1, s2 ConnState) *connListElement {
if s1 == s2 {
return nil
}
switch s1 {
case ConnStateUnknown:
case ConnOnline:
case ConnOffline:
case ConnBanned:
}
if s1 > s2 {
return c1
} else if s2 > s1 {
return c2
} else {
return nil
}
}
func (c connRuntimeCriterion) Best(c1, c2 *connListElement) *connListElement {
s1 := c1.conn.runtime.stats()
s2 := c2.conn.runtime.stats()
if choise := c.chooseByState(c1, c2, s1.State, s2.State); choise != nil {
return choise
}
var (
f1 float64
f2 float64
)
if s1.OpPerMinute > 0 {
f1 = s1.ErrPerMinute / s1.OpPerMinute
}
if s2.OpPerMinute > 0 {
f2 = s2.ErrPerMinute / s2.OpPerMinute
}
if f1 == f2 {
t := s1.AvgOpTime - s2.AvgOpTime
switch {
case absDuration(t) > c.OpTimeThreshold:
if t < 0 {
f1 = 0
f2 = 1
} else {
f1 = 1
f2 = 0
}
case c.PreferLocal && c1.info.local && !c2.info.local:
f1 = 0
f2 = 1
case c.PreferLocal && c2.info.local && !c1.info.local:
f1 = 1
f2 = 0
default:
f1 = float64(s1.OpPending())
f2 = float64(s2.OpPending())
}
}
if f1 < f2 {
return c1
}
return c2
}
// p2c implements the "power of two choices" balancing algorithm.
// See https://www.eecs.harvard.edu/~michaelm/postscripts/mythesis.pdf
type p2c struct {
Source rand.Source64
Criterion criterion
once sync.Once
rand *rand.Rand
conns connList
}
func (p *p2c) init() {
p.once.Do(func() {
if p.Criterion == nil {
p.Criterion = &connRuntimeCriterion{
OpTimeThreshold: time.Second,
}
}
if p.Source == nil {
p.Source = rand.NewSource(0).(rand.Source64)
}
p.rand = rand.New(&lockedSource{src: p.Source})
})
}
func (p *p2c) Next() *conn {
p.init()
n := len(p.conns)
switch n {
case 0:
return nil
case 1:
return p.conns[0].conn
}
var (
r1 uint64
r2 uint64
)
const (
maxRetries = 2
)
for i := 0; i <= maxRetries; i++ {
rnd := p.rand.Uint64()
r1 = (rnd >> 32) % uint64(n)
r2 = (rnd & 0xffffffff) % uint64(n)
if r1 != r2 {
break
}
}
b := p.Criterion.Best(p.conns[r1], p.conns[r2])
return b.conn
}
func (p *p2c) Insert(conn *conn, info connInfo) balancerElement {
el := p.conns.Insert(conn, info)
return el
}
func (p *p2c) Update(x balancerElement, info connInfo) {
el := x.(*connListElement)
el.info = info
}
func (p *p2c) Remove(x balancerElement) {
p.conns.Remove(x.(*connListElement))
}
func (p *p2c) Pessimize(x balancerElement) error {
if x == nil {
return ErrNilBalancerElement
}
el, ok := x.(*connListElement)
if !ok {
return ErrUnknownTypeOfBalancerElement
}
if !p.conns.Contains(el) {
return ErrUnknownBalancerElement
}
el.conn.runtime.setState(ConnBanned)
return nil
}
func (p *p2c) Contains(x balancerElement) bool {
if x == nil {
return false
}
el, ok := x.(*connListElement)
if !ok {
return false
}
return p.conns.Contains(el)
}
type lockedSource struct {
mu sync.Mutex
src rand.Source64
}
func (s *lockedSource) Int63() int64 {
s.mu.Lock()
x := s.src.Int63()
s.mu.Unlock()
return x
}
func (s *lockedSource) Uint64() uint64 {
s.mu.Lock()
x := s.src.Uint64()
s.mu.Unlock()
return x
}
func (s *lockedSource) Seed(seed int64) {
s.mu.Lock()
s.src.Seed(seed)
s.mu.Unlock()
}
func absDuration(d time.Duration) time.Duration {
x := int64(d)
m := x >> 63
return time.Duration(x ^ m - m)
}