-
Notifications
You must be signed in to change notification settings - Fork 7
/
policy.go
212 lines (184 loc) · 4.47 KB
/
policy.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
package transport
import (
"sort"
"github.com/scylladb/scylla-go-driver/log"
)
// HostSelectionPolicy decides which node the query should be routed to.
type HostSelectionPolicy interface {
// Returns i-th node of the host selection plan, returns nil after going through the whole plan.
Node(QueryInfo, int) *Node
}
type TokenAwarePolicy struct {
localDC string
}
func NewTokenAwarePolicy(localDC string) *TokenAwarePolicy {
return &TokenAwarePolicy{localDC: localDC}
}
func (p *TokenAwarePolicy) Node(qi QueryInfo, offset int) *Node {
if p.localDC == "" {
var replicas []*Node
pi := qi.topology.policyInfo
if qi.tokenAware {
pos := pi.ring.tokenLowerBound(qi.token)
replicas = pi.ring[pos].localReplicas
} else {
// Fallback to round robin on all nodes.
replicas = pi.localNodes
}
if offset >= len(replicas) {
return nil
}
idx := (qi.offset + uint64(offset)) % uint64(len(replicas))
return replicas[idx]
}
var local, remote []*Node
pi := qi.topology.policyInfo
if qi.tokenAware {
pos := pi.ring.tokenLowerBound(qi.token)
local = pi.ring[pos].localReplicas
remote = pi.ring[pos].remoteReplicas
} else {
// Fallback to DC aware round robin on all nodes.
local = pi.localNodes
remote = pi.remoteNodes
}
if offset < len(local) {
idx := (qi.offset + uint64(offset)) % uint64(len(local))
return local[idx]
} else if offset < len(local)+len(remote) {
idx := (qi.offset + uint64(offset) - uint64(len(local))) % uint64(len(remote))
return remote[idx]
}
return nil
}
type policyInfo struct {
ring Ring
localNodes []*Node
remoteNodes []*Node
}
func (pi *policyInfo) Preprocess(t *topology, ks keyspace, logger log.Logger) {
switch ks.strategy.class {
case simpleStrategy, localStrategy:
pi.preprocessSimpleStrategy(t, ks.strategy)
case networkTopologyStrategy:
pi.preprocessNetworkTopologyStrategy(t, ks.strategy)
default:
logger.Warnf("policyInfo: keyspace %v has unknown strategy, defaulting to round robin")
if t.localDC == "" {
pi.preprocessRoundRobinStrategy(t)
} else {
pi.preprocessDCAwareRoundRobinStrategy(t)
}
}
}
func (pi *policyInfo) preprocessSimpleStrategy(t *topology, stg strategy) {
pi.localNodes = t.Nodes
sort.Sort(pi.ring)
trie := trieRoot()
for i := range pi.ring {
rit := replicaIter{
ring: pi.ring,
offset: i,
fetched: 0,
}
filter := func(n *Node, res []*Node) bool {
for _, v := range res {
if n.hostID == v.hostID {
return false
}
}
return true
}
cur := &trie
for len(cur.Path()) < int(stg.rf) {
n := rit.Next()
if n == nil {
break
}
if filter(n, cur.Path()) {
cur = cur.Next(n)
}
}
pi.ring[i].localReplicas = cur.Path()
}
}
func (pi *policyInfo) preprocessRoundRobinStrategy(t *topology) {
pi.localNodes = t.Nodes
pi.remoteNodes = nil
}
func (pi *policyInfo) preprocessDCAwareRoundRobinStrategy(t *topology) {
pi.localNodes = make([]*Node, 0)
pi.remoteNodes = make([]*Node, 0)
for _, v := range t.Nodes {
if v.datacenter == t.localDC {
pi.localNodes = append(pi.localNodes, v)
} else {
pi.remoteNodes = append(pi.remoteNodes, v)
}
}
}
func (pi *policyInfo) preprocessNetworkTopologyStrategy(t *topology, stg strategy) {
sort.Sort(pi.ring)
trie := trieRoot()
for i := range pi.ring {
rit := replicaIter{
ring: pi.ring,
offset: i,
fetched: 0,
}
desiredCnt := 0
// repeats store the amount of nodes from the same rack that we can take in given DC.
repeats := make(map[string]int, len(stg.dcRF))
for k, v := range stg.dcRF {
desiredCnt += int(v)
repeats[k] = int(v) - t.dcRacks[k]
}
filter := func(n *Node, res []*Node) bool {
rf := stg.dcRF[n.datacenter]
fromDC := 0
fromRack := 0
for _, v := range res {
if n.addr == v.addr {
return false
}
if n.datacenter == v.datacenter {
fromDC++
if n.rack == v.rack {
fromRack++
}
}
}
if fromDC < int(rf) {
if fromRack == 0 {
return true
}
if repeats[n.datacenter] > 0 {
repeats[n.datacenter]--
return true
}
}
return false
}
plan := make([]*Node, 0, desiredCnt)
for len(plan) < desiredCnt {
n := rit.Next()
if n == nil {
break
}
if filter(n, plan) {
plan = append(plan, n)
}
}
local := &trie
remote := &trie
for _, n := range plan {
if n.datacenter == t.localDC {
local = local.Next(n)
} else {
remote = remote.Next(n)
}
}
pi.ring[i].localReplicas = local.Path()
pi.ring[i].remoteReplicas = remote.Path()
}
}