forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picker.go
330 lines (269 loc) · 7.91 KB
/
picker.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
package picker
import (
"fmt"
"math"
"math/rand"
"sort"
"sync"
"github.com/docker/swarmkit/api"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/transport"
)
var errRemotesUnavailable = fmt.Errorf("no remote hosts provided")
// Remotes keeps track of remote addresses by weight, informed by
// observations.
type Remotes interface {
// Weight returns the remotes with their current weights.
Weights() map[api.Peer]int
// Select a remote from the set of available remotes with optionally
// excluding ID or address.
Select(...string) (api.Peer, error)
// Observe records an experience with a particular remote. A positive weight
// indicates a good experience and a negative weight a bad experience.
//
// The observation will be used to calculate a moving weight, which is
// implementation dependent. This method will be called such that repeated
// observations of the same master in each session request are favored.
Observe(peer api.Peer, weight int)
// ObserveIfExists records an experience with a particular remote if when a
// remote exists.
ObserveIfExists(peer api.Peer, weight int)
// Remove the remote from the list completely.
Remove(addrs ...api.Peer)
}
// NewRemotes returns a Remotes instance with the provided set of addresses.
// Entries provided are heavily weighted initially.
func NewRemotes(peers ...api.Peer) Remotes {
mwr := &remotesWeightedRandom{
remotes: make(map[api.Peer]int),
}
for _, peer := range peers {
mwr.Observe(peer, 1)
}
return mwr
}
type remotesWeightedRandom struct {
remotes map[api.Peer]int
mu sync.Mutex
// workspace to avoid reallocation. these get lazily allocated when
// selecting values.
cdf []float64
peers []api.Peer
}
func (mwr *remotesWeightedRandom) Weights() map[api.Peer]int {
mwr.mu.Lock()
defer mwr.mu.Unlock()
ms := make(map[api.Peer]int, len(mwr.remotes))
for addr, weight := range mwr.remotes {
ms[addr] = weight
}
return ms
}
func (mwr *remotesWeightedRandom) Select(excludes ...string) (api.Peer, error) {
mwr.mu.Lock()
defer mwr.mu.Unlock()
// NOTE(stevvooe): We then use a weighted random selection algorithm
// (http://stackoverflow.com/questions/4463561/weighted-random-selection-from-array)
// to choose the master to connect to.
//
// It is possible that this is insufficient. The following may inform a
// better solution:
// https://github.com/LK4D4/sample
//
// The first link applies exponential distribution weight choice reservior
// sampling. This may be relevant if we view the master selection as a
// distributed reservior sampling problem.
// bias to zero-weighted remotes have same probability. otherwise, we
// always select first entry when all are zero.
const bias = 0.1
// clear out workspace
mwr.cdf = mwr.cdf[:0]
mwr.peers = mwr.peers[:0]
cum := 0.0
// calculate CDF over weights
for peer, weight := range mwr.remotes {
for _, exclude := range excludes {
if peer.NodeID == exclude || peer.Addr == exclude {
continue
}
}
if weight < 0 {
// treat these as zero, to keep there selection unlikely.
weight = 0
}
cum += float64(weight) + bias
mwr.cdf = append(mwr.cdf, cum)
mwr.peers = append(mwr.peers, peer)
}
if len(mwr.peers) == 0 {
return api.Peer{}, errRemotesUnavailable
}
r := mwr.cdf[len(mwr.cdf)-1] * rand.Float64()
i := sort.SearchFloat64s(mwr.cdf, r)
return mwr.peers[i], nil
}
func (mwr *remotesWeightedRandom) Observe(peer api.Peer, weight int) {
mwr.mu.Lock()
defer mwr.mu.Unlock()
mwr.observe(peer, float64(weight))
}
func (mwr *remotesWeightedRandom) ObserveIfExists(peer api.Peer, weight int) {
mwr.mu.Lock()
defer mwr.mu.Unlock()
if _, ok := mwr.remotes[peer]; !ok {
return
}
mwr.observe(peer, float64(weight))
}
func (mwr *remotesWeightedRandom) Remove(addrs ...api.Peer) {
mwr.mu.Lock()
defer mwr.mu.Unlock()
for _, addr := range addrs {
delete(mwr.remotes, addr)
}
}
const (
// remoteWeightSmoothingFactor for exponential smoothing. This adjusts how
// much of the // observation and old value we are using to calculate the new value.
// See
// https://en.wikipedia.org/wiki/Exponential_smoothing#Basic_exponential_smoothing
// for details.
remoteWeightSmoothingFactor = 0.7
remoteWeightMax = 1 << 8
)
func clip(x float64) float64 {
if math.IsNaN(x) {
// treat garbage as such
// acts like a no-op for us.
return 0
}
return math.Max(math.Min(remoteWeightMax, x), -remoteWeightMax)
}
func (mwr *remotesWeightedRandom) observe(peer api.Peer, weight float64) {
// While we have a decent, ad-hoc approach here to weight subsequent
// observerations, we may want to look into applying forward decay:
//
// http://dimacs.rutgers.edu/~graham/pubs/papers/fwddecay.pdf
//
// We need to get better data from behavior in a cluster.
// makes the math easier to read below
var (
w0 = float64(mwr.remotes[peer])
w1 = clip(weight)
)
const α = remoteWeightSmoothingFactor
// Multiply the new value to current value, and appy smoothing against the old
// value.
wn := clip(α*w1 + (1-α)*w0)
mwr.remotes[peer] = int(math.Ceil(wn))
}
// Picker implements a grpc Picker
type Picker struct {
r Remotes
peer api.Peer // currently selected remote peer
conn *grpc.Conn
mu sync.Mutex
}
var _ grpc.Picker = &Picker{}
// NewPicker returns a Picker
func NewPicker(r Remotes, initial ...string) *Picker {
var peer api.Peer
if len(initial) == 0 {
peer, _ = r.Select() // empty in case of error
} else {
peer = api.Peer{Addr: initial[0]}
}
return &Picker{r: r, peer: peer}
}
// Init does initial processing for the Picker, e.g., initiate some connections.
func (p *Picker) Init(cc *grpc.ClientConn) error {
p.mu.Lock()
peer := p.peer
p.mu.Unlock()
p.r.ObserveIfExists(peer, 1)
c, err := grpc.NewConn(cc)
if err != nil {
return err
}
p.mu.Lock()
p.conn = c
p.mu.Unlock()
return nil
}
// Pick blocks until either a transport.ClientTransport is ready for the upcoming RPC
// or some error happens.
func (p *Picker) Pick(ctx context.Context) (transport.ClientTransport, error) {
p.mu.Lock()
peer := p.peer
p.mu.Unlock()
transport, err := p.conn.Wait(ctx)
if err != nil {
p.r.ObserveIfExists(peer, -1)
}
return transport, err
}
// PickAddr picks a peer address for connecting. This will be called repeated for
// connecting/reconnecting.
func (p *Picker) PickAddr() (string, error) {
p.mu.Lock()
peer := p.peer
p.mu.Unlock()
p.r.ObserveIfExists(peer, -1) // downweight the current addr
var err error
peer, err = p.r.Select("")
if err != nil {
return "", err
}
p.mu.Lock()
p.peer = peer
p.mu.Unlock()
return p.peer.Addr, err
}
// State returns the connectivity state of the underlying connections.
func (p *Picker) State() (grpc.ConnectivityState, error) {
return p.conn.State(), nil
}
// WaitForStateChange blocks until the state changes to something other than
// the sourceState. It returns the new state or error.
func (p *Picker) WaitForStateChange(ctx context.Context, sourceState grpc.ConnectivityState) (grpc.ConnectivityState, error) {
p.mu.Lock()
conn := p.conn
peer := p.peer
p.mu.Unlock()
state, err := conn.WaitForStateChange(ctx, sourceState)
if err != nil {
return state, err
}
// TODO(stevvooe): We may want to actually score the transition by checking
// sourceState.
// TODO(stevvooe): This is questionable, but we'll see how it works.
switch state {
case grpc.Idle:
p.r.ObserveIfExists(peer, 1)
case grpc.Connecting:
p.r.ObserveIfExists(peer, 1)
case grpc.Ready:
p.r.ObserveIfExists(peer, 1)
case grpc.TransientFailure:
p.r.ObserveIfExists(peer, -1)
case grpc.Shutdown:
p.r.ObserveIfExists(peer, -1)
}
return state, err
}
// Reset the current connection and force a reconnect to another address.
func (p *Picker) Reset() error {
p.mu.Lock()
conn := p.conn
p.mu.Unlock()
conn.NotifyReset()
return nil
}
// Close closes all the Conn's owned by this Picker.
func (p *Picker) Close() error {
p.mu.Lock()
conn := p.conn
p.mu.Unlock()
return conn.Close()
}