-
Notifications
You must be signed in to change notification settings - Fork 795
/
model.go
397 lines (336 loc) · 10.6 KB
/
model.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
package ring
import (
"fmt"
"sort"
"time"
"github.com/golang/protobuf/proto"
"github.com/cortexproject/cortex/pkg/ring/kv/codec"
"github.com/cortexproject/cortex/pkg/ring/kv/memberlist"
)
// ByToken is a sortable list of TokenDescs
type ByToken []TokenDesc
func (ts ByToken) Len() int { return len(ts) }
func (ts ByToken) Swap(i, j int) { ts[i], ts[j] = ts[j], ts[i] }
func (ts ByToken) Less(i, j int) bool { return ts[i].Token < ts[j].Token }
// ProtoDescFactory makes new Descs
func ProtoDescFactory() proto.Message {
return NewDesc()
}
// GetCodec returns the codec used to encode and decode data being put by ring.
func GetCodec() codec.Codec {
return codec.NewProtoCodec("ringDesc", ProtoDescFactory)
}
// NewDesc returns an empty ring.Desc
func NewDesc() *Desc {
return &Desc{
Ingesters: map[string]IngesterDesc{},
}
}
// AddIngester adds the given ingester to the ring. Ingester will only use supplied tokens,
// any other tokens are removed.
func (d *Desc) AddIngester(id, addr string, tokens []uint32, state IngesterState) {
if d.Ingesters == nil {
d.Ingesters = map[string]IngesterDesc{}
}
ingester := IngesterDesc{
Addr: addr,
Timestamp: time.Now().Unix(),
State: state,
Tokens: tokens,
}
d.Ingesters[id] = ingester
}
// RemoveIngester removes the given ingester and all its tokens.
func (d *Desc) RemoveIngester(id string) {
delete(d.Ingesters, id)
}
// ClaimTokens transfers all the tokens from one ingester to another,
// returning the claimed token.
// This method assumes that Ring is in the correct state, 'to' ingester has no tokens anywhere.
// Tokens list must be sorted properly. If all of this is true, everything will be fine.
func (d *Desc) ClaimTokens(from, to string) Tokens {
var result Tokens
if fromDesc, found := d.Ingesters[from]; found {
result = fromDesc.Tokens
fromDesc.Tokens = nil
d.Ingesters[from] = fromDesc
}
ing := d.Ingesters[to]
ing.Tokens = result
d.Ingesters[to] = ing
return result
}
// FindIngestersByState returns the list of ingesters in the given state
func (d *Desc) FindIngestersByState(state IngesterState) []IngesterDesc {
var result []IngesterDesc
for _, ing := range d.Ingesters {
if ing.State == state {
result = append(result, ing)
}
}
return result
}
// Ready returns no error when all ingesters are active and healthy.
func (d *Desc) Ready(now time.Time, heartbeatTimeout time.Duration) error {
numTokens := 0
for id, ingester := range d.Ingesters {
if now.Sub(time.Unix(ingester.Timestamp, 0)) > heartbeatTimeout {
return fmt.Errorf("ingester %s past heartbeat timeout", id)
} else if ingester.State != ACTIVE {
return fmt.Errorf("ingester %s in state %v", id, ingester.State)
}
numTokens += len(ingester.Tokens)
}
if numTokens == 0 {
return fmt.Errorf("Not ready: no tokens in ring")
}
return nil
}
// TokensFor partitions the tokens into those for the given ID, and those for others.
func (d *Desc) TokensFor(id string) (tokens, other Tokens) {
takenTokens, myTokens := Tokens{}, Tokens{}
for _, token := range d.getTokens() {
takenTokens = append(takenTokens, token.Token)
if token.Ingester == id {
myTokens = append(myTokens, token.Token)
}
}
return myTokens, takenTokens
}
// IsHealthy checks whether the ingester appears to be alive and heartbeating
func (i *IngesterDesc) IsHealthy(op Operation, heartbeatTimeout time.Duration) bool {
healthy := false
switch op {
case Write:
healthy = (i.State == ACTIVE)
case Read:
healthy = (i.State == ACTIVE) || (i.State == LEAVING) || (i.State == PENDING)
case Reporting:
healthy = true
}
return healthy && time.Since(time.Unix(i.Timestamp, 0)) <= heartbeatTimeout
}
// Merge merges other ring into this one. Returns sub-ring that represents the change,
// and can be sent out to other clients.
//
// This merge function depends on the timestamp of the ingester. For each ingester,
// it will choose more recent state from the two rings, and put that into this ring.
// There is one exception: we accept LEFT state even if Timestamp hasn't changed.
//
// localCAS flag tells the merge that it can use incoming ring as a full state, and detect
// missing ingesters based on it. Ingesters from incoming ring will cause ingester
// to be marked as LEFT and gossiped about.
//
// If multiple ingesters end up owning the same tokens, Merge will do token conflict resolution
// (see resolveConflicts).
//
// This method is part of memberlist.Mergeable interface, and is only used by gossiping ring.
func (d *Desc) Merge(mergeable memberlist.Mergeable, localCAS bool) (memberlist.Mergeable, error) {
if mergeable == nil {
return nil, nil
}
other, ok := mergeable.(*Desc)
if !ok {
// This method only deals with non-nil rings.
return nil, fmt.Errorf("expected *ring.Desc, got %T", mergeable)
}
if other == nil {
return nil, nil
}
thisIngesterMap := buildNormalizedIngestersMap(d)
otherIngesterMap := buildNormalizedIngestersMap(other)
var updated []string
for name, oing := range otherIngesterMap {
ting := thisIngesterMap[name]
// firstIng.Timestamp will be 0, if there was no such ingester in our version
if oing.Timestamp > ting.Timestamp {
oing.Tokens = append([]uint32(nil), oing.Tokens...) // make a copy of tokens
thisIngesterMap[name] = oing
updated = append(updated, name)
} else if oing.Timestamp == ting.Timestamp && ting.State != LEFT && oing.State == LEFT {
// we accept LEFT even if timestamp hasn't changed
thisIngesterMap[name] = oing // has no tokens already
updated = append(updated, name)
}
}
if localCAS {
// This breaks commutativity! But we only do it locally, not when gossiping with others.
for name, ting := range thisIngesterMap {
if _, ok := otherIngesterMap[name]; !ok && ting.State != LEFT {
// missing, let's mark our ingester as LEFT
ting.State = LEFT
ting.Tokens = nil
thisIngesterMap[name] = ting
updated = append(updated, name)
}
}
}
// No updated ingesters
if len(updated) == 0 {
return nil, nil
}
// resolveConflicts allocates lot of memory, so if we can avoid it, do that.
if conflictingTokensExist(thisIngesterMap) {
resolveConflicts(thisIngesterMap)
}
// Let's build a "change" for returning
out := NewDesc()
for _, u := range updated {
ing := thisIngesterMap[u]
out.Ingesters[u] = ing
}
d.Ingesters = thisIngesterMap
return out, nil
}
// MergeContent describes content of this Mergeable.
// Ring simply returns list of ingesters that it includes.
func (d *Desc) MergeContent() []string {
result := []string(nil)
for k := range d.Ingesters {
result = append(result, k)
}
return result
}
// buildNormalizedIngestersMap will do the following:
// - sorts tokens and removes duplicates (only within single ingester)
// - it doesn't modify input ring
func buildNormalizedIngestersMap(inputRing *Desc) map[string]IngesterDesc {
out := map[string]IngesterDesc{}
// Make sure LEFT ingesters have no tokens
for n, ing := range inputRing.Ingesters {
if ing.State == LEFT {
ing.Tokens = nil
}
out[n] = ing
}
// Sort tokens, and remove duplicates
for name, ing := range out {
if len(ing.Tokens) == 0 {
continue
}
if !sort.IsSorted(Tokens(ing.Tokens)) {
sort.Sort(Tokens(ing.Tokens))
}
// tokens are sorted now, we can easily remove duplicates.
prev := ing.Tokens[0]
for ix := 1; ix < len(ing.Tokens); {
if ing.Tokens[ix] == prev {
ing.Tokens = append(ing.Tokens[:ix], ing.Tokens[ix+1:]...)
} else {
prev = ing.Tokens[ix]
ix++
}
}
// write updated value back to map
out[name] = ing
}
return out
}
func conflictingTokensExist(normalizedIngesters map[string]IngesterDesc) bool {
count := 0
for _, ing := range normalizedIngesters {
count += len(ing.Tokens)
}
tokensMap := make(map[uint32]bool, count)
for _, ing := range normalizedIngesters {
for _, t := range ing.Tokens {
if tokensMap[t] {
return true
}
tokensMap[t] = true
}
}
return false
}
// This function resolves token conflicts, if there are any.
//
// We deal with two possibilities:
// 1) if one node is LEAVING or LEFT and the other node is not, LEVING/LEFT one loses the token
// 2) otherwise node names are compared, and node with "lower" name wins the token
//
// Modifies ingesters map with updated tokens.
func resolveConflicts(normalizedIngesters map[string]IngesterDesc) {
size := 0
for _, ing := range normalizedIngesters {
size += len(ing.Tokens)
}
tokens := make([]uint32, 0, size)
tokenToIngester := make(map[uint32]string, size)
for ingKey, ing := range normalizedIngesters {
if ing.State == LEFT {
// LEFT ingesters don't use tokens anymore
continue
}
for _, token := range ing.Tokens {
prevKey, found := tokenToIngester[token]
if !found {
tokens = append(tokens, token)
tokenToIngester[token] = ingKey
} else {
// there is already ingester for this token, let's do conflict resolution
prevIng := normalizedIngesters[prevKey]
winnerKey := ingKey
switch {
case ing.State == LEAVING && prevIng.State != LEAVING:
winnerKey = prevKey
case prevIng.State == LEAVING && ing.State != LEAVING:
winnerKey = ingKey
case ingKey < prevKey:
winnerKey = ingKey
case prevKey < ingKey:
winnerKey = prevKey
}
tokenToIngester[token] = winnerKey
}
}
}
sort.Sort(Tokens(tokens))
// let's store the resolved result back
newTokenLists := map[string][]uint32{}
for key := range normalizedIngesters {
// make sure that all ingesters start with empty list
// especially ones that will no longer have any tokens
newTokenLists[key] = nil
}
// build list of tokens for each ingester
for _, token := range tokens {
key := tokenToIngester[token]
newTokenLists[key] = append(newTokenLists[key], token)
}
// write tokens back
for key, tokens := range newTokenLists {
ing := normalizedIngesters[key]
ing.Tokens = tokens
normalizedIngesters[key] = ing
}
}
// RemoveTombstones removes LEFT ingesters older than given time limit. If time limit is zero, remove all LEFT ingesters.
func (d *Desc) RemoveTombstones(limit time.Time) {
removed := 0
for n, ing := range d.Ingesters {
if ing.State == LEFT && (limit.IsZero() || time.Unix(ing.Timestamp, 0).Before(limit)) {
// remove it
delete(d.Ingesters, n)
removed++
}
}
}
type TokenDesc struct {
Token uint32
Ingester string
}
// Returns sorted list of tokens with ingester names.
func (d *Desc) getTokens() []TokenDesc {
numTokens := 0
for _, ing := range d.Ingesters {
numTokens += len(ing.Tokens)
}
tokens := make([]TokenDesc, 0, numTokens)
for key, ing := range d.Ingesters {
for _, token := range ing.Tokens {
tokens = append(tokens, TokenDesc{Token: token, Ingester: key})
}
}
sort.Sort(ByToken(tokens))
return tokens
}