-
Notifications
You must be signed in to change notification settings - Fork 245
/
hashring.go
420 lines (349 loc) · 13.5 KB
/
hashring.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
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
package balancer
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"sync"
"time"
"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/spicedb/pkg/consistent"
)
// This is based off of the example implementation in grpc-go:
// https://github.com/grpc/grpc-go/blob/afcbdc9ace7b4af94d014620727ea331cc3047fe/balancer/base/balancer.go
// The original work is copyright gRPC authors and licensed under the Apache License, Version 2.0.
// Note that there is little locking in this file. In grpc it is wrapped
// with `ccBalancerWrapper` which serializes calls to these callbacks.
// See: https://github.com/grpc/grpc-go/blob/417d4b6895679bd9378cb37c2afecf6a292eb267/balancer_conn_wrappers.go#L35-L48
// The hashring also locks internally.a
type ctxKey string
const (
// BalancerName is the name of consistent-hashring balancer.
BalancerName = "consistent-hashring"
// CtxKey is the key for the grpc request's context.Context which points to
// the key to hash for the request. The value it points to must be []byte
CtxKey ctxKey = "requestKey"
defaultReplicationFactor = 100
defaultSpread = 1
)
var defaultBalancerServiceConfig = &ConsistentHashringBalancerConfig{
ReplicationFactor: defaultReplicationFactor,
Spread: defaultSpread,
}
// DefaultBalancerServiceConfigJSON is a grpc Service Config JSON with the
// defaults for the ConsstentHashringBalancer configured.
var DefaultBalancerServiceConfigJSON = defaultBalancerServiceConfig.MustToServiceConfigJSON()
// ConsistentHashringBalancerConfig supports common settings for the balancer.
// It should be converted to json with ToServiceConfigJSON or
// MustToServiceConfigJSON and passed to the grpc.WithDefaultServiceConfig
// option on grpc.Dial.
type ConsistentHashringBalancerConfig struct {
serviceconfig.LoadBalancingConfig `json:"-"`
ReplicationFactor uint16 `json:"replicationFactor,omitempty"`
Spread uint8 `json:"spread,omitempty"`
}
// ToServiceConfigJSON converts the config into the standard grpc Service Config
// json format.
func (c *ConsistentHashringBalancerConfig) ToServiceConfigJSON() (string, error) {
type wrapper struct {
Config []map[string]*ConsistentHashringBalancerConfig `json:"loadBalancingConfig"`
}
out := wrapper{Config: []map[string]*ConsistentHashringBalancerConfig{{
BalancerName: c,
}}}
j, err := json.Marshal(out)
if err != nil {
return "", err
}
return string(j), nil
}
// MustToServiceConfigJSON calls ToServiceConfigJSON but panics if there is an
// error.
func (c *ConsistentHashringBalancerConfig) MustToServiceConfigJSON() string {
o, err := c.ToServiceConfigJSON()
if err != nil {
panic(err)
}
return o
}
var logger = grpclog.Component("consistenthashring")
// NewConsistentHashringBuilder creates a new balancer.Builder that
// will create a consistent hashring balancer.
// Before making a connection, register it with grpc with:
// `balancer.Register(consistent.NewConsistentHashringBuilder(hasher))`
func NewConsistentHashringBuilder(hasher consistent.HasherFunc) *ConsistentHashringBuilder {
return &ConsistentHashringBuilder{
hasher: hasher,
}
}
type subConnMember struct {
balancer.SubConn
key string
}
// Key implements consistent.Member
// This value is what will be hashed for placement on the consistent hash ring.
func (s subConnMember) Key() string {
return s.key
}
var _ consistent.Member = &subConnMember{}
// ConsistentHashringBuilder stamps out new ConsistentHashringBalancer
// when requested by grpc.
type ConsistentHashringBuilder struct {
sync.Mutex
hasher consistent.HasherFunc
config ConsistentHashringBalancerConfig
}
// Build satisfies balancer.Builder and returns a new ConsistentHashringBalancer.
func (b *ConsistentHashringBuilder) Build(cc balancer.ClientConn, _ balancer.BuildOptions) balancer.Balancer {
bal := &ConsistentHashringBalancer{
cc: cc,
subConns: resolver.NewAddressMap(),
scStates: make(map[balancer.SubConn]connectivity.State),
csEvltr: &balancer.ConnectivityStateEvaluator{},
state: connectivity.Connecting,
hasher: b.hasher,
picker: base.NewErrPicker(balancer.ErrNoSubConnAvailable),
}
return bal
}
// Name satisfies balancer.Builder and returns the name of the balancer for
// use in Service Config files.
func (b *ConsistentHashringBuilder) Name() string {
return BalancerName
}
// ParseConfig satisfies balancer.ConfigParser and is used to parse new
// Service Config json. The results are stored on the builder so that
// subsequently built Balancers use the config.
func (b *ConsistentHashringBuilder) ParseConfig(js json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
var lbCfg ConsistentHashringBalancerConfig
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
}
b.Lock()
b.config = lbCfg
b.Unlock()
return &lbCfg, nil
}
// ConsistentHashringBalancer implements balancer.Balancer and uses a
// consistent hashring to pick a backend for a request.
type ConsistentHashringBalancer struct {
state connectivity.State
cc balancer.ClientConn
picker balancer.Picker
csEvltr *balancer.ConnectivityStateEvaluator
subConns *resolver.AddressMap
scStates map[balancer.SubConn]connectivity.State
config *ConsistentHashringBalancerConfig
hashring *consistent.Hashring
hasher consistent.HasherFunc
resolverErr error // the last error reported by the resolver; cleared on successful resolution
connErr error // the last connection error; cleared upon leaving TransientFailure
}
// ResolverError satisfies balancer.Balancer and is called when there is a
// an error in the resolver.
func (b *ConsistentHashringBalancer) 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 satisfies balancer.Balancer and 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 *ConsistentHashringBalancer) 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.(*ConsistentHashringBalancerConfig)
if b.config == nil || svcConfig.ReplicationFactor != b.config.ReplicationFactor {
b.hashring = consistent.MustNewHashring(b.hasher, svcConfig.ReplicationFactor)
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.NewAddressMap()
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).
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.csEvltr.RecordTransition(connectivity.Shutdown, connectivity.Idle)
sc.Connect()
if err := b.hashring.Add(subConnMember{
SubConn: sc,
key: addr.ServerName + addr.Addr,
}); err != nil {
return fmt.Errorf("couldn't add to hashring")
}
}
}
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 {
b.cc.RemoveSubConn(sc)
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.hashring.Remove(subConnMember{
SubConn: sc,
key: addr.ServerName + addr.Addr,
}); err != nil {
return fmt.Errorf("couldn't add to hashring")
}
}
}
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
}
// If the overall connection state is not in transient failure, we return
// addr new picker with addr reference to the hashring (otherwise an error picker)
if b.state == connectivity.TransientFailure {
b.picker = base.NewErrPicker(errors.Join(b.connErr, b.resolverErr))
} else {
b.picker = &consistentHashringPicker{
hashring: b.hashring,
spread: b.config.Spread,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
// update the ClientConn with the current hashring picker picker
b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker})
return nil
}
// UpdateSubConnState satisfies balancer.Balancer and is called when there is
// 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 *ConsistentHashringBalancer) 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
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)
case connectivity.TransientFailure:
// Save error to be reported via picker.
b.connErr = state.ConnectionError
}
b.state = b.csEvltr.RecordTransition(oldS, s)
b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker})
}
// Close is a no-op because base balancer doesn't have internal state to clean up,
// and it doesn't need to call RemoveSubConn for the SubConns.
func (b *ConsistentHashringBalancer) Close() {
}
type consistentHashringPicker struct {
sync.Mutex
hashring *consistent.Hashring
spread uint8
rand *rand.Rand
}
// Pick satisfies balancer.Picker and 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. Note that
// there is no fallback behavior if the subconnection is unavailable; this
// prevents the request from going to a node that doesn't expect to receive it.
// Spread can be increased to be robust against single node availability
// problems.
// For dispatch we generally use a resolver that removes connections from the
// list when they are observably unavailable, so in practice this is not a
// huge problem.
// If spread is greater than 1, a random selection is made from the set of
// subconns matching the hash.
func (p *consistentHashringPicker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
key := info.Ctx.Value(CtxKey).([]byte)
members, err := p.hashring.FindN(key, p.spread)
if err != nil {
return balancer.PickResult{}, err
}
index := 0
if p.spread > 1 {
// TODO: should look into other options for this to avoid locking; we mostly use spread 1 so it's not urgent
// rand is not safe for concurrent use
p.Lock()
index = p.rand.Intn(int(p.spread))
p.Unlock()
}
chosen := members[index].(subConnMember)
return balancer.PickResult{
SubConn: chosen.SubConn,
}, nil
}