-
Notifications
You must be signed in to change notification settings - Fork 15
/
mapping.go
385 lines (344 loc) · 9.63 KB
/
mapping.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
package rediscluster
import (
"fmt"
"sync/atomic"
"unsafe"
"github.com/joomcode/errorx"
"github.com/joomcode/redispipe/redis"
"github.com/joomcode/redispipe/redisconn"
)
// storeConfig atomically stores config
func (c *Cluster) storeConfig(cfg *clusterConfig) {
p := (*unsafe.Pointer)(unsafe.Pointer(&c.config))
atomic.StorePointer(p, unsafe.Pointer(cfg))
}
// getConfig loads config atomically
func (c *Cluster) getConfig() *clusterConfig {
p := (*unsafe.Pointer)(unsafe.Pointer(&c.config))
return (*clusterConfig)(atomic.LoadPointer(p))
}
// ClusterHandle is used to wrap cluster's handle and set it as connection's handle.
// You can use it in connection's logging.
type ClusterHandle struct {
Handle interface{}
Address string
N int
}
// newNode creates handle for a connection, that will be established in a future.
func (c *Cluster) newNode(addr string, initial bool) (*node, error) {
node := &node{
opts: c.opts.HostOpts,
addr: addr,
refcnt: 1,
}
node.opts.AsyncDial = true
node.conns = make([]*redisconn.Connection, c.opts.ConnsPerHost)
for i := range node.conns {
node.opts.Handle = ClusterHandle{c.opts.Handle, addr, i}
var err error
node.conns[i], err = redisconn.Connect(c.ctx, addr, node.opts)
if err != nil {
if initial {
return nil, err
}
// Since we are connected in async mode, there are should no be
// errors. If there is error, it is configuration error.
// There could no be configuration error after start.
panic(err)
}
}
return node, nil
}
type connThen func(conn *redisconn.Connection, err error)
// Call callback with connection to specified address.
// If connection is already established, callback will be called immediately.
// Otherwise, callback will be called after connection established.
func (c *Cluster) ensureConnForAddress(addr string, then connThen) {
node := c.getConfig().nodes[addr]
if node != nil {
// there is node for address, so call callback now.
conn := node.getConn(c.opts.ConnHostPolicy, preferConnected, nil)
if conn != nil {
then(conn, nil)
} else {
err := c.err(ErrNoAliveConnection).WithProperty(redis.EKAddress, addr)
then(nil, err)
}
return
}
c.nodeWait.Lock()
defer c.nodeWait.Unlock()
if future, ok := c.nodeWait.promises[addr]; ok {
// there are already queued callback.
// It means, goroutine with connection establishing is already run.
// Add our callback to queue, and exit.
*future = append(*future, then)
return
}
// initiate queue for this address
future := &[]connThen{then}
promises := c.nodeWait.promises
promises[addr] = future
go func() {
node := c.addNode(addr)
var err error
conn := node.getConn(c.opts.ConnHostPolicy, mayBeConnected, nil)
if conn == nil {
err = c.err(ErrNoAliveConnection).WithProperty(redis.EKAddress, addr)
}
c.nodeWait.Lock()
delete(promises, addr)
c.nodeWait.Unlock()
// since we deleted from promises under lock, no one could append to *future any more.
// lets run callbacks.
for _, cb := range *future {
cb(conn, err)
}
}()
}
// addNode creates host handle and adds it to cluster configuration.
func (c *Cluster) addNode(addr string) *node {
DebugEvent("addNode")
var node *node
var ok bool
if node, ok = c.getConfig().nodes[addr]; ok {
return node
}
c.m.Lock()
defer c.m.Unlock()
oldConf := c.getConfig()
if node, ok = oldConf.nodes[addr]; ok {
// someone could already create same node
return node
}
// we could not update configuration in-place (threadsafety, bla-bla-bla).
// So we have to copy configuration and node map.
newConf := *oldConf
newConf.nodes = make(nodeMap, len(oldConf.nodes)+1)
for a, node := range oldConf.nodes {
newConf.nodes[a] = node
}
if node, ok = c.prevNodes[addr]; ok {
atomic.AddUint32(&node.refcnt, 1)
} else {
node, _ = c.newNode(addr, false)
}
newConf.nodes[addr] = node
c.storeConfig(&newConf)
return node
}
func (cfg *clusterConfig) slot2shardno(slot uint16) uint16 {
pos, off := slot/2, 16*(slot&1)
sh32 := atomic.LoadUint32(&cfg.slots[pos])
sh16 := uint16((sh32 >> off) & 0x3fff)
return sh16
}
// slotSetShard sets slot2shard mapping
func (cfg *clusterConfig) slotSetShard(slot, shard uint16) {
pos, off := slot/2, 16*(slot&1)
sh32 := atomic.LoadUint32(&cfg.slots[pos])
if uint16((sh32>>off)&0x3fff) == shard {
return
}
sh32 &^= 0xffff << off
sh32 |= uint32(shard) << off
// yep, we doesn't do any synchronization here.
// If we lost update now, it will be naturally retried with other MOVED redis response.
atomic.StoreUint32(&cfg.slots[pos], sh32)
}
func (cfg *clusterConfig) slotMarkAsking(slot uint16) {
pos, off := slot/2, 16*(slot&1)
sh32 := atomic.LoadUint32(&cfg.slots[pos])
flag := uint32(masterOnlyFlag << off)
if sh32&flag == 0 {
sh32 |= flag
// Again: no synchronization, because any updates will be retried with redis responses.
atomic.StoreUint32(&cfg.slots[pos], sh32)
}
}
func (cfg *clusterConfig) slotIsAsking(slot uint16) bool {
pos, off := slot/2, 16*(slot&1)
sh32 := atomic.LoadUint32(&cfg.slots[pos])
flag := uint32(masterOnlyFlag << off)
return sh32&flag != 0
}
func (cfg *clusterConfig) slot2shard(slot uint16) *shard {
sh16 := cfg.slot2shardno(slot)
shard := cfg.shards[sh16]
return shard
}
var rr, rs = func() ([32]uint32, [32]uint32) {
var rr [32]uint32 // {1, 1, 1, ...}
var rs [32]uint32 // {1, 3, 3, ...}
for i := range rr[:] {
rr[i] = 1
rs[i] = 3
}
rs[0] = 1
return rr, rs
}()
// connForSlot returns established connection for slot, if it exists.
func (c *Cluster) connForSlot(slot uint16, policy ReplicaPolicyEnum, seen []*redisconn.Connection) (*redisconn.Connection, *errorx.Error) {
var conn *redisconn.Connection
cfg := c.getConfig()
shard := cfg.slot2shard(slot)
nodes := cfg.nodes
if shard == nil {
return nil, c.err(ErrClusterConfigEmpty).WithProperty(redis.EKSlot, slot)
}
var addr string
switch policy {
case MasterOnly:
addr = shard.addr[0]
node := nodes[addr]
if node == nil {
break /*switch*/
}
conn = node.getConn(c.opts.ConnHostPolicy, preferConnected, seen)
case MasterAndSlaves, PreferSlaves:
var ws [32]uint32
if atomic.LoadUint32(&c.latencyAwareness) == 0 {
ws = rr
if policy == PreferSlaves {
ws = rs
}
} else {
for i := range shard.weights {
ws[i] = atomic.LoadUint32(&shard.weights[i])
}
}
weights := ws[:len(shard.weights)]
health := atomic.LoadUint32(&shard.good) // load health information
healthWeight := uint32(0)
for i, w := range weights {
if health&(1<<uint(i)) == 0 {
continue
}
healthWeight += w
}
off := c.opts.RoundRobinSeed.Current()
// First, we try already established connections.
// If no one found, then connections thar are connecting at the moment are tried.
for _, needState := range []int{needConnected, mayBeConnected} {
mask, maskWeight := health, healthWeight
// a bit of quadratic algorithms
for mask != 0 && conn == nil {
r := nextRng(&off, maskWeight)
k := uint(0)
for i, w := range weights {
if mask&(1<<uint(i)) == 0 {
continue
}
if r < w {
k = uint(i)
break
}
r -= w
}
mask &^= 1 << k
maskWeight -= weights[k]
addr = shard.addr[k]
node := nodes[addr]
if node == nil {
// it is strange a bit, but lets ignore
continue
}
conn = node.getConn(c.opts.ConnHostPolicy, needState, seen)
}
if conn != nil {
break
}
}
default:
panic("unknown policy")
}
if conn == nil {
c.ForceReloading()
return nil, c.err(ErrNoAliveConnection).WithProperty(redis.EKSlot, slot).WithProperty(EKPolicy, policy)
}
return conn, nil
}
func (c *Cluster) connForAddress(addr string) *redisconn.Connection {
node := c.getConfig().nodes[addr]
if node == nil {
return nil
}
return node.getConn(c.opts.ConnHostPolicy, preferConnected, nil)
}
func connHealthy(c *redisconn.Connection, needState int) bool {
switch needState {
case needConnected:
return c.ConnectedNow()
case mayBeConnected:
return c.MayBeConnected()
default:
panic(fmt.Sprintf("unknown needState: %d", needState))
}
}
func isSeen(conn *redisconn.Connection, seen []*redisconn.Connection) bool {
for _, p := range seen {
if conn == p {
return true
}
}
return false
}
// getConn returns connection with desired "health", but without already seen(used) connections.
func (n *node) getConn(policy ConnHostPolicyEnum, liveness int, seen []*redisconn.Connection) *redisconn.Connection {
if liveness == preferConnected {
conn := n.getConnConcreteNeed(policy, needConnected, seen)
if conn == nil {
conn = n.getConnConcreteNeed(policy, mayBeConnected, seen)
}
return conn
}
return n.getConnConcreteNeed(policy, liveness, seen)
}
func (n *node) getConnConcreteNeed(policy ConnHostPolicyEnum, liveness int, seen []*redisconn.Connection) *redisconn.Connection {
switch policy {
case ConnHostPreferFirst:
for _, conn := range n.conns {
if isSeen(conn, seen) {
continue
}
if connHealthy(conn, liveness) {
return conn
}
}
case ConnHostRoundRobin:
off := atomic.AddUint32(&n.rr, 1)
l := uint32(len(n.conns))
mask := uint32(1)<<l - 1
for mask != 0 {
k := nextRng(&off, l)
if mask&(1<<k) == 0 {
continue
}
mask &^= 1 << k
conn := n.conns[k]
if isSeen(conn, seen) {
continue
}
if connHealthy(conn, liveness) {
return conn
}
}
default:
panic("unknown ConnHostPolicy")
}
return nil
}
func (n *node) updatePingLatency() {
latency := redisconn.PingMaxLatency
for _, conn := range n.conns {
if l := conn.PingLatency(); l < latency {
latency = l
}
}
n.ping = uint32(latency / redisconn.PingLatencyGranularity)
}
func nextRng(state *uint32, mod uint32) uint32 {
v := *state
*state = v*0x12345 + 1
return (v ^ v>>16) % mod
}