Repository navigation
Expand file tree
/
Copy pathbalancer.go
More file actions
556 lines (468 loc) · 17.1 KB
/
Copy pathbalancer.go
File metadata and controls
556 lines (468 loc) · 17.1 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Package consistent implements a gRPC Balancer that routes requests based
// upon a consistent hashring.
//
// The hashing algorithm is customizable, but xxhash is recommended.
//
// A large portion of the structure of this library is based off of the example
// implementation in grpc-go. That original work is copyrighted by the gRPC
// authors and licensed under the Apache License, Version 2.0.
//
// This package relies on the synchronization guarantees provided by
// `ccBalancerWrapper`, an upstream type that serializes calls to the balancer.
package consistent
import (
"encoding/json"
"errors"
"fmt"
"hash/maphash"
"sync"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/base"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/serviceconfig"
"github.com/authzed/consistent/hashring"
)
type ctxKey string
const (
// BalancerName is the name used to identify this implementation of a
// consistent-hashring balancer to gRPC.
BalancerName = "consistent-hashring"
// CtxKey is the key that will be present in each gRPC request's context
// that points to the value that will be hashed in order to map the request
// to the hashring.
//
// The value stored at this key must be []byte.
CtxKey ctxKey = "requestKey"
// DefaultReplicationFactor is the value that will be used when parsing a
// service config provides an invalid value.
DefaultReplicationFactor = 100
// DefaultSpread is the value that will be used when parsing a service
// config provides an invalid value.
DefaultSpread = 1
)
// DefaultServiceConfigJSON is a helper to easily leverage the defaults.
//
// Here's an example:
// ```go
// grpc.Dial(addr, grpc.WithDefaultServiceConfig(consistent.DefaultServiceConfigJSON))
// ```
var DefaultServiceConfigJSON = (&BalancerConfig{
ReplicationFactor: DefaultReplicationFactor,
Spread: DefaultSpread,
}).MustServiceConfigJSON()
// BalancerConfig exposes the configurable aspects of the balancer.
//
// This type is meant to be used with `grpc.WithDefaultServiceConfig()` through
// the `ServiceConfigJSON()` or `MustServiceConfigJSON()` methods.
//
// If you're unsure of what values to use in this configuration, use
// `DefaultBalancerConfig`.
type BalancerConfig struct {
serviceconfig.LoadBalancingConfig `json:"-"`
ReplicationFactor uint16 `json:"replicationFactor,omitempty"`
Spread uint8 `json:"spread,omitempty"`
}
// ServiceConfigJSON encodes the current config into the gRPC Service Config
// JSON format.
func (c *BalancerConfig) ServiceConfigJSON() (string, error) {
type wrapper struct {
Config []map[string]*BalancerConfig `json:"loadBalancingConfig"`
}
out := wrapper{Config: []map[string]*BalancerConfig{{BalancerName: c}}}
j, err := json.Marshal(out)
if err != nil {
return "", err
}
return string(j), nil
}
// MustServiceConfigJSON calls ServiceConfigJSON, but panics if there is an
// error.
func (c *BalancerConfig) MustServiceConfigJSON() string {
o, err := c.ServiceConfigJSON()
if err != nil {
panic(err)
}
return o
}
var logger = grpclog.Component("consistenthashring")
// NewBuilder allocates a new gRPC balancer.Builder that will route traffic
// according to a hashring configured with the provided hash function.
//
// The following is an example usage:
// ```go
// balancer.Register(consistent.NewBuilder(xxhash.Sum64))
// ```
func NewBuilder(hashfn hashring.HashFunc) Builder {
return &builder{
hashfn: hashfn,
slots: make(map[string]*ringSlot),
}
}
type subConnMember struct {
balancer.SubConn
key string
}
// Key implements hashring.Member.
// This value is what will be hashed for placement on the consistent hash ring.
func (s subConnMember) Key() string { return s.key }
var _ hashring.Member = (*subConnMember)(nil)
type builder struct {
hashfn hashring.HashFunc
mu sync.Mutex
slots map[string]*ringSlot
}
// Builder combines gRPC's `balancer.Builder` and `balancer.ConfigParser`
// interfaces. It also exposes read-only views of the hashrings of the
// balancers it builds.
type Builder interface {
balancer.Builder
balancer.ConfigParser
// RingFor returns a read-only view of the live hashring that routes
// requests to the given target. The target is the string form of the
// dial target, for example "kubernetes:///spicedb.default:50053".
//
// The returned view is always non-nil, and callers can get it before any
// balancer for the target exists. Its methods return ErrNoRing (or no
// members) until the balancer produces a ring. ClientConns that share a
// target string share one view, and the last writer wins.
RingFor(target string) RingView
}
var _ Builder = (*builder)(nil)
func (b *builder) Name() string { return BalancerName }
// slotFor returns the ringSlot for the given target. It creates the slot if
// none exists.
func (b *builder) slotFor(target string) *ringSlot {
b.mu.Lock()
defer b.mu.Unlock()
slot, ok := b.slots[target]
if !ok {
slot = &ringSlot{}
b.slots[target] = slot
}
return slot
}
func (b *builder) RingFor(target string) RingView {
return b.slotFor(target)
}
func (b *builder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
bal := &ringBalancer{
cc: cc,
subConns: resolver.NewAddressMapV2[any](),
scStates: make(map[balancer.SubConn]connectivity.State),
scKeys: make(map[balancer.SubConn]string),
ringMembers: make(map[balancer.SubConn]struct{}),
csEvltr: &balancer.ConnectivityStateEvaluator{},
state: connectivity.Connecting,
hasher: b.hashfn,
picker: base.NewErrPicker(balancer.ErrNoSubConnAvailable),
slot: b.slotFor(opts.Target.String()),
}
return bal
}
func (b *builder) ParseConfig(js json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
var lbCfg BalancerConfig
if err := json.Unmarshal(js, &lbCfg); err != nil {
return nil, fmt.Errorf("wrr: unable to unmarshal LB policy config: %s, error: %w", string(js), err)
}
logger.Infof("parsed balancer config %s", js)
if lbCfg.ReplicationFactor == 0 {
lbCfg.ReplicationFactor = DefaultReplicationFactor
}
if lbCfg.Spread == 0 {
lbCfg.Spread = DefaultSpread
}
return &lbCfg, nil
}
type ringBalancer struct {
state connectivity.State
cc balancer.ClientConn
picker balancer.Picker
csEvltr *balancer.ConnectivityStateEvaluator
subConns *resolver.AddressMapV2[any]
scStates map[balancer.SubConn]connectivity.State
// scKeys holds the hashring key of every SubConn the resolver gave us.
scKeys map[balancer.SubConn]string
// ringMembers is the set of SubConns on the hashring: every SubConn the
// resolver gave us except those in TRANSIENT_FAILURE.
ringMembers map[balancer.SubConn]struct{}
config *BalancerConfig
hashring *hashring.Ring
hasher hashring.HashFunc
// slot is where the balancer publishes its live ring for RingView
// readers.
slot *ringSlot
resolverErr error // the last error reported by the resolver; cleared on successful resolution
connErr error // the last connection error; cleared upon leaving TransientFailure
}
var _ balancer.Balancer = (*ringBalancer)(nil)
func (b *ringBalancer) ResolverError(err error) {
b.resolverErr = err
if b.subConns.Len() == 0 {
b.state = connectivity.TransientFailure
b.picker = base.NewErrPicker(errors.Join(b.connErr, b.resolverErr))
}
if b.state != connectivity.TransientFailure {
// The picker will not change since the balancer does not currently
// report an error.
return
}
b.cc.UpdateState(balancer.State{
ConnectivityState: b.state,
Picker: b.picker,
})
}
// UpdateClientConnState is called when there are changes in the Address set or
// Service Config that the balancer may want to react to.
//
// In this case, the hashring is updated and a new picker using that hashring
// is generated.
func (b *ringBalancer) UpdateClientConnState(s balancer.ClientConnState) error {
if logger.V(2) {
logger.Info("got new ClientConn state: ", s)
}
// Successful resolution: clear resolver error and ensure we return nil.
b.resolverErr = nil
// update the service config if it has changed
if s.BalancerConfig != nil {
svcConfig := s.BalancerConfig.(*BalancerConfig)
if b.config == nil || svcConfig.ReplicationFactor != b.config.ReplicationFactor {
b.hashring = hashring.MustNew(b.hasher, svcConfig.ReplicationFactor)
b.slot.ring.Store(b.hashring)
// The new ring starts empty: put every SubConn that has not
// failed back on it.
b.ringMembers = make(map[balancer.SubConn]struct{})
for sc, st := range b.scStates {
if st == connectivity.TransientFailure {
continue
}
if err := b.addToRing(sc); err != nil {
return err
}
}
}
b.config = svcConfig
}
// if there's no hashring yet, the balancer hasn't yet parsed an initial
// service config with settings
if b.hashring == nil {
b.picker = base.NewErrPicker(errors.Join(b.connErr, b.resolverErr))
b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker})
return fmt.Errorf("no hashring configured")
}
// Look through the set of addresses the resolver has passed to the balancer
// if any new targets have been added, they are added to the hashring, and
// any that have been removed since the last update are removed from the
// hashring.
addrsSet := resolver.NewAddressMapV2[any]()
for _, addr := range s.ResolverState.Addresses {
addrsSet.Set(addr, nil)
if _, ok := b.subConns.Get(addr); !ok {
// addr is addr new address (not existing in b.subConns).
// NewSubConn is deprecated only to warn that a SubConn will soon
// hold a single address. We already pass exactly one address, and
// grpc offers no replacement API yet.
//nolint:staticcheck // SA1019: single-address usage is the future-proof form
sc, err := b.cc.NewSubConn([]resolver.Address{addr}, balancer.NewSubConnOptions{HealthCheckEnabled: false})
if err != nil {
logger.Warningf("base.baseBalancer: failed to create new SubConn: %v", err)
continue
}
b.subConns.Set(addr, sc)
b.scStates[sc] = connectivity.Idle
b.scKeys[sc] = addr.ServerName + addr.Addr
b.csEvltr.RecordTransition(connectivity.Shutdown, connectivity.Idle)
sc.Connect()
// The backend owns its keys from the start: its connection is
// already on the way, and RPCs picked before READY wait for it.
if err := b.addToRing(sc); err != nil {
return err
}
}
}
for _, addr := range b.subConns.Keys() {
sci, _ := b.subConns.Get(addr)
sc := sci.(balancer.SubConn)
// addr was removed by resolver.
if _, ok := addrsSet.Get(addr); !ok {
sc.Shutdown()
b.subConns.Delete(addr)
// Keep the state of this sc in b.scStates until sc's state becomes Shutdown.
// The entry will be deleted in UpdateSubConnState.
if err := b.removeFromRing(sc); err != nil {
return err
}
}
}
if logger.V(2) {
logger.Infof("%d hashring members found", len(b.hashring.Members()))
for _, m := range b.hashring.Members() {
logger.Infof("hashring member %s", m.Key())
}
}
// If resolver state contains no addresses, return an error so ClientConn
// will trigger re-resolve. Also records this as addr resolver error, so when
// the overall state turns transient failure, the error message will have
// the zero address information.
if len(s.ResolverState.Addresses) == 0 {
b.ResolverError(errors.New("produced zero addresses"))
return balancer.ErrBadResolverState
}
b.regeneratePicker()
b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker})
return nil
}
// regeneratePicker installs an error picker while the balancer is in
// TRANSIENT_FAILURE and a hashring picker otherwise.
func (b *ringBalancer) regeneratePicker() {
if b.state == connectivity.TransientFailure {
b.picker = base.NewErrPicker(errors.Join(b.connErr, b.resolverErr))
return
}
b.picker = &picker{
hashring: b.hashring,
spread: b.config.Spread,
}
}
// addToRing places sc on the hashring if it is not already there.
func (b *ringBalancer) addToRing(sc balancer.SubConn) error {
if _, ok := b.ringMembers[sc]; ok {
return nil
}
if err := b.hashring.Add(subConnMember{SubConn: sc, key: b.scKeys[sc]}); err != nil {
return fmt.Errorf("couldn't add to hashring: %w", err)
}
b.ringMembers[sc] = struct{}{}
return nil
}
// removeFromRing takes sc off the hashring if it is there.
func (b *ringBalancer) removeFromRing(sc balancer.SubConn) error {
if _, ok := b.ringMembers[sc]; !ok {
return nil
}
if err := b.hashring.Remove(subConnMember{SubConn: sc, key: b.scKeys[sc]}); err != nil {
return fmt.Errorf("couldn't remove from hashring: %w", err)
}
delete(b.ringMembers, sc)
return nil
}
// UpdateSubConnState is called when there's a change in a subconnection state.
// Subconnection state can affect the overall state of the balancer.
// This also attempts to reconnect any idle connections.
func (b *ringBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
s := state.ConnectivityState
if logger.V(2) {
logger.Infof("base.baseBalancer: handle SubConn state change: %p, %v", sc, s)
}
oldS, ok := b.scStates[sc]
if !ok {
if logger.V(2) {
logger.Infof("base.baseBalancer: got state changes for an unknown SubConn: %p, %v", sc, s)
}
return
}
if oldS == connectivity.TransientFailure &&
(s == connectivity.Connecting || s == connectivity.Idle) {
// Once a subconn enters TRANSIENT_FAILURE, ignore subsequent IDLE or
// CONNECTING transitions to prevent the aggregated state from being
// always CONNECTING when many backends exist but are all down.
if s == connectivity.Idle {
sc.Connect()
}
return
}
b.scStates[sc] = s
// Ring membership changes on failure, not on every state change. IDLE
// and CONNECTING backends keep their keys: those states normally resolve
// quickly, and a remap on each one would move keys across the fleet on
// every connection recycle. A backend whose connection attempt failed
// leaves the ring, and its keys move to the closest remaining member.
var ringErr error
switch s {
case connectivity.Ready:
ringErr = b.addToRing(sc)
case connectivity.TransientFailure, connectivity.Shutdown:
ringErr = b.removeFromRing(sc)
}
if ringErr != nil {
logger.Warningf("consistent-hashring: %v", ringErr)
}
switch s {
case connectivity.Idle:
sc.Connect()
case connectivity.Shutdown:
// When an address was removed by resolver, b called RemoveSubConn but
// kept the sc's state in scStates. Remove state for this sc here.
delete(b.scStates, sc)
delete(b.scKeys, sc)
case connectivity.TransientFailure:
// Save error to be reported via picker.
b.connErr = state.ConnectionError
}
b.state = b.csEvltr.RecordTransition(oldS, s)
b.regeneratePicker()
b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker})
}
func (b *ringBalancer) Close() {
// Withdraw the published ring so that RingView readers stop routing to a
// balancer that no longer exists. The CompareAndSwap clears only this
// balancer's own ring, so it cannot remove the ring of a second balancer
// that shares the target's slot.
b.slot.ring.CompareAndSwap(b.hashring, nil)
}
func (b *ringBalancer) ExitIdle() {
// No-op as we already reconnect SubConns on demand when they report
// connectivity.Idle in UpdateSubConnState. This is here to satisfy
// the balancer.Balancer interface >v1.74.0
}
type picker struct {
hashring *hashring.Ring
spread uint8
}
var _ balancer.Picker = (*picker)(nil)
// Pick returns a subconnection to use for a request based on the request info.
//
// The value stored in CtxKey is hashed into the hashring, and the resulting
// subconnection is used.
//
// The hashring contains every resolver-provided backend except those in
// TRANSIENT_FAILURE. The next closest member serves the keys of a failed
// backend. A key whose backend is IDLE or CONNECTING waits for that
// connection. The dial timeout (MinConnectTimeout) bounds the wait: when
// the dial fails, the backend leaves the ring and its keys move.
//
// Spread can be increased to be robust against single node availability
// problems. If spread is greater than 1, a random selection is made from the
// set of subconns matching the hash.
func (p *picker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
key := info.Ctx.Value(CtxKey).([]byte)
// FindN only fails with hashring.ErrNotEnoughMembers.
members, err := p.hashring.FindN(key, p.spread)
if err != nil {
// Fewer ready backends than the configured spread: use those that are.
members, err = p.hashring.FindN(key, 1)
if err != nil {
// No ready backends at all: queue the RPC until one is ready.
return balancer.PickResult{}, balancer.ErrNoSubConnAvailable
}
}
index := 0
if len(members) > 1 {
index = intn(uint8(len(members)))
}
chosen := members[index].(subConnMember)
return balancer.PickResult{SubConn: chosen.SubConn}, nil
}
// intn returns, as an int, a non-negative pseudo-random number in the
// half-open interval [0,n).
//
// Under the hood, it's taking advantage of maphash's use of runtime.fastrand
// for an extremely fast, thread-safe PRNG.
var intn = func(n uint8) int {
out := int(new(maphash.Hash).Sum64())
if out < 0 {
out = -out
}
return out % int(n)
}