-
Notifications
You must be signed in to change notification settings - Fork 786
/
lifecycler.go
511 lines (431 loc) · 16.5 KB
/
lifecycler.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
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
package ring
import (
"context"
"flag"
"fmt"
"os"
"sort"
"sync"
"time"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/cortexproject/cortex/pkg/ring/kv"
"github.com/cortexproject/cortex/pkg/util"
"github.com/cortexproject/cortex/pkg/util/flagext"
)
var (
consulHeartbeats = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "cortex_member_consul_heartbeats_total",
Help: "The total number of heartbeats sent to consul.",
}, []string{"name"})
tokensOwned = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "cortex_member_ring_tokens_owned",
Help: "The number of tokens owned in the ring.",
}, []string{"name"})
tokensToOwn = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "cortex_member_ring_tokens_to_own",
Help: "The number of tokens to own in the ring.",
}, []string{"name"})
shutdownDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "cortex_shutdown_duration_seconds",
Help: "Duration (in seconds) of cortex shutdown procedure (ie transfer or flush).",
Buckets: prometheus.ExponentialBuckets(10, 2, 8), // Biggest bucket is 10*2^(9-1) = 2560, or 42 mins.
}, []string{"op", "status", "name"})
)
// LifecyclerConfig is the config to build a Lifecycler.
type LifecyclerConfig struct {
RingConfig Config `yaml:"ring,omitempty"`
// Config for the ingester lifecycle control
ListenPort *int
NumTokens int `yaml:"num_tokens,omitempty"`
HeartbeatPeriod time.Duration `yaml:"heartbeat_period,omitempty"`
JoinAfter time.Duration `yaml:"join_after,omitempty"`
MinReadyDuration time.Duration `yaml:"min_ready_duration,omitempty"`
ClaimOnRollout bool `yaml:"claim_on_rollout,omitempty"`
NormaliseTokens bool `yaml:"normalise_tokens,omitempty"`
InfNames []string `yaml:"interface_names"`
FinalSleep time.Duration `yaml:"final_sleep"`
// For testing, you can override the address and ID of this ingester
Addr string `yaml:"address"`
Port int
ID string
SkipUnregister bool
}
// RegisterFlags adds the flags required to config this to the given FlagSet
func (cfg *LifecyclerConfig) RegisterFlags(f *flag.FlagSet) {
cfg.RegisterFlagsWithPrefix("", f)
}
// RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet.
func (cfg *LifecyclerConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
cfg.RingConfig.RegisterFlagsWithPrefix(prefix, f)
// In order to keep backwards compatibility all of these need to be prefixed
// with "ingester."
if prefix == "" {
prefix = "ingester."
}
f.IntVar(&cfg.NumTokens, prefix+"num-tokens", 128, "Number of tokens for each ingester.")
f.DurationVar(&cfg.HeartbeatPeriod, prefix+"heartbeat-period", 5*time.Second, "Period at which to heartbeat to consul.")
f.DurationVar(&cfg.JoinAfter, prefix+"join-after", 0*time.Second, "Period to wait for a claim from another member; will join automatically after this.")
f.DurationVar(&cfg.MinReadyDuration, prefix+"min-ready-duration", 1*time.Minute, "Minimum duration to wait before becoming ready. This is to work around race conditions with ingesters exiting and updating the ring.")
f.BoolVar(&cfg.ClaimOnRollout, prefix+"claim-on-rollout", false, "Send chunks to PENDING ingesters on exit.")
f.BoolVar(&cfg.NormaliseTokens, prefix+"normalise-tokens", false, "Store tokens in a normalised fashion to reduce allocations.")
f.DurationVar(&cfg.FinalSleep, prefix+"final-sleep", 30*time.Second, "Duration to sleep for before exiting, to ensure metrics are scraped.")
hostname, err := os.Hostname()
if err != nil {
level.Error(util.Logger).Log("msg", "failed to get hostname", "err", err)
os.Exit(1)
}
cfg.InfNames = []string{"eth0", "en0"}
f.Var((*flagext.Strings)(&cfg.InfNames), prefix+"lifecycler.interface", "Name of network interface to read address from.")
f.StringVar(&cfg.Addr, prefix+"lifecycler.addr", "", "IP address to advertise in consul.")
f.IntVar(&cfg.Port, prefix+"lifecycler.port", 0, "port to advertise in consul (defaults to server.grpc-listen-port).")
f.StringVar(&cfg.ID, prefix+"lifecycler.ID", hostname, "ID to register into consul.")
}
// FlushTransferer controls the shutdown of an ingester.
type FlushTransferer interface {
StopIncomingRequests()
Flush()
TransferOut(ctx context.Context) error
}
// Lifecycler is responsible for managing the lifecycle of entries in the ring.
type Lifecycler struct {
cfg LifecyclerConfig
flushTransferer FlushTransferer
KVStore kv.Client
// Controls the lifecycle of the ingester
quit chan struct{}
done sync.WaitGroup
actorChan chan func()
// These values are initialised at startup, and never change
ID string
Addr string
RingName string
// We need to remember the ingester state just in case consul goes away and comes
// back empty. And it changes during lifecycle of ingester.
stateMtx sync.Mutex
state IngesterState
tokens []uint32
// Controls the ready-reporting
readyLock sync.Mutex
startTime time.Time
ready bool
}
// NewLifecycler makes and starts a new Lifecycler.
func NewLifecycler(cfg LifecyclerConfig, flushTransferer FlushTransferer, name string) (*Lifecycler, error) {
addr := cfg.Addr
if addr == "" {
var err error
addr, err = util.GetFirstAddressOf(cfg.InfNames)
if err != nil {
return nil, err
}
}
port := cfg.Port
if port == 0 {
port = *cfg.ListenPort
}
codec := GetCodec()
store, err := kv.NewClient(cfg.RingConfig.KVStore, codec)
if err != nil {
return nil, err
}
l := &Lifecycler{
cfg: cfg,
flushTransferer: flushTransferer,
KVStore: store,
Addr: fmt.Sprintf("%s:%d", addr, port),
ID: cfg.ID,
RingName: name,
quit: make(chan struct{}),
actorChan: make(chan func()),
state: PENDING,
startTime: time.Now(),
}
tokensToOwn.WithLabelValues(l.RingName).Set(float64(cfg.NumTokens))
l.done.Add(1)
go l.loop()
return l, nil
}
// CheckReady is used to rate limit the number of ingesters that can be coming or
// going at any one time, by only returning true if all ingesters are active.
// The state latches: once we have gone ready we don't go un-ready
func (i *Lifecycler) CheckReady(ctx context.Context) error {
i.readyLock.Lock()
defer i.readyLock.Unlock()
if i.ready {
return nil
}
// Ingester always take at least minReadyDuration to become ready to work
// around race conditions with ingesters exiting and updating the ring
if time.Now().Sub(i.startTime) < i.cfg.MinReadyDuration {
return fmt.Errorf("waiting for %v after startup", i.cfg.MinReadyDuration)
}
ringDesc, err := i.KVStore.Get(ctx, ConsulKey)
if err != nil {
level.Error(util.Logger).Log("msg", "error talking to consul", "err", err)
return fmt.Errorf("error talking to consul: %s", err)
}
if len(i.getTokens()) == 0 {
return fmt.Errorf("this ingester owns no tokens")
}
if err := ringDesc.(*Desc).Ready(i.cfg.RingConfig.HeartbeatTimeout); err != nil {
return err
}
i.ready = true
return nil
}
// GetState returns the state of this ingester.
func (i *Lifecycler) GetState() IngesterState {
i.stateMtx.Lock()
defer i.stateMtx.Unlock()
return i.state
}
func (i *Lifecycler) setState(state IngesterState) {
i.stateMtx.Lock()
defer i.stateMtx.Unlock()
i.state = state
}
// ChangeState of the ingester, for use off of the loop() goroutine.
func (i *Lifecycler) ChangeState(ctx context.Context, state IngesterState) error {
err := make(chan error)
i.actorChan <- func() {
err <- i.changeState(ctx, state)
}
return <-err
}
func (i *Lifecycler) getTokens() []uint32 {
i.stateMtx.Lock()
defer i.stateMtx.Unlock()
return i.tokens
}
func (i *Lifecycler) setTokens(tokens []uint32) {
tokensOwned.WithLabelValues(i.RingName).Set(float64(len(tokens)))
i.stateMtx.Lock()
defer i.stateMtx.Unlock()
i.tokens = tokens
}
// ClaimTokensFor takes all the tokens for the supplied ingester and assigns them to this ingester.
func (i *Lifecycler) ClaimTokensFor(ctx context.Context, ingesterID string) error {
err := make(chan error)
i.actorChan <- func() {
var tokens []uint32
claimTokens := func(in interface{}) (out interface{}, retry bool, err error) {
ringDesc, ok := in.(*Desc)
if !ok || ringDesc == nil {
return nil, false, fmt.Errorf("Cannot claim tokens in an empty ring")
}
tokens = ringDesc.ClaimTokens(ingesterID, i.ID, i.cfg.NormaliseTokens)
return ringDesc, true, nil
}
if err := i.KVStore.CAS(ctx, ConsulKey, claimTokens); err != nil {
level.Error(util.Logger).Log("msg", "Failed to write to consul", "err", err)
}
i.setTokens(tokens)
err <- nil
}
return <-err
}
// Shutdown the lifecycle. It will:
// - send chunks to another ingester, if it can.
// - otherwise, flush chunks to the chunk store.
// - remove config from Consul.
// - block until we've successfully shutdown.
func (i *Lifecycler) Shutdown() {
// This will prevent us accepting any more samples
i.flushTransferer.StopIncomingRequests()
// closing i.quit triggers loop() to exit, which in turn will trigger
// the removal of our tokens etc
close(i.quit)
i.done.Wait()
}
func (i *Lifecycler) loop() {
defer func() {
level.Info(util.Logger).Log("msg", "member.loop() exited gracefully")
i.done.Done()
}()
// First, see if we exist in the cluster, update our state to match if we do,
// and add ourselves (without tokens) if we don't.
if err := i.initRing(context.Background()); err != nil {
level.Error(util.Logger).Log("msg", "failed to join consul", "err", err)
os.Exit(1)
}
// We do various period tasks
autoJoinAfter := time.After(i.cfg.JoinAfter)
heartbeatTicker := time.NewTicker(i.cfg.HeartbeatPeriod)
defer heartbeatTicker.Stop()
loop:
for {
select {
case <-autoJoinAfter:
level.Debug(util.Logger).Log("msg", "JoinAfter expired")
// Will only fire once, after auto join timeout. If we haven't entered "JOINING" state,
// then pick some tokens and enter ACTIVE state.
if i.GetState() == PENDING {
level.Info(util.Logger).Log("msg", "auto-joining cluster after timeout")
if err := i.autoJoin(context.Background()); err != nil {
level.Error(util.Logger).Log("msg", "failed to pick tokens in consul", "err", err)
os.Exit(1)
}
}
case <-heartbeatTicker.C:
consulHeartbeats.WithLabelValues(i.RingName).Inc()
if err := i.updateConsul(context.Background()); err != nil {
level.Error(util.Logger).Log("msg", "failed to write to consul, sleeping", "err", err)
}
case f := <-i.actorChan:
f()
case <-i.quit:
break loop
}
}
// Mark ourselved as Leaving so no more samples are send to us.
i.changeState(context.Background(), LEAVING)
// Do the transferring / flushing on a background goroutine so we can continue
// to heartbeat to consul.
done := make(chan struct{})
go func() {
i.processShutdown(context.Background())
close(done)
}()
heartbeatLoop:
for {
select {
case <-heartbeatTicker.C:
consulHeartbeats.WithLabelValues(i.RingName).Inc()
if err := i.updateConsul(context.Background()); err != nil {
level.Error(util.Logger).Log("msg", "failed to write to consul, sleeping", "err", err)
}
case <-done:
break heartbeatLoop
}
}
if !i.cfg.SkipUnregister {
if err := i.unregister(context.Background()); err != nil {
level.Error(util.Logger).Log("msg", "Failed to unregister from consul", "err", err)
os.Exit(1)
}
level.Info(util.Logger).Log("msg", "ingester removed from consul")
}
}
// initRing is the first thing we do when we start. It:
// - add an ingester entry to the ring
// - copies out our state and tokens if they exist
func (i *Lifecycler) initRing(ctx context.Context) error {
return i.KVStore.CAS(ctx, ConsulKey, func(in interface{}) (out interface{}, retry bool, err error) {
var ringDesc *Desc
if in == nil {
ringDesc = NewDesc()
} else {
ringDesc = in.(*Desc)
}
ingesterDesc, ok := ringDesc.Ingesters[i.ID]
if !ok {
// Either we are a new ingester, or consul must have restarted
level.Info(util.Logger).Log("msg", "entry not found in ring, adding with no tokens")
ringDesc.AddIngester(i.ID, i.Addr, []uint32{}, i.GetState(), i.cfg.NormaliseTokens)
return ringDesc, true, nil
}
// We exist in the ring, so assume the ring is right and copy out tokens & state out of there.
i.setState(ingesterDesc.State)
tokens, _ := ringDesc.TokensFor(i.ID)
i.setTokens(tokens)
level.Info(util.Logger).Log("msg", "existing entry found in ring", "state", i.GetState(), "tokens", len(tokens))
return ringDesc, true, nil
})
}
// autoJoin selects random tokens & moves state to ACTIVE
func (i *Lifecycler) autoJoin(ctx context.Context) error {
return i.KVStore.CAS(ctx, ConsulKey, func(in interface{}) (out interface{}, retry bool, err error) {
var ringDesc *Desc
if in == nil {
ringDesc = NewDesc()
} else {
ringDesc = in.(*Desc)
}
// At this point, we should not have any tokens, and we should be in PENDING state.
myTokens, takenTokens := ringDesc.TokensFor(i.ID)
if len(myTokens) > 0 {
level.Error(util.Logger).Log("msg", "tokens already exist for this ingester - wasn't expecting any!", "num_tokens", len(myTokens))
}
newTokens := GenerateTokens(i.cfg.NumTokens-len(myTokens), takenTokens)
i.setState(ACTIVE)
ringDesc.AddIngester(i.ID, i.Addr, newTokens, i.GetState(), i.cfg.NormaliseTokens)
tokens := append(myTokens, newTokens...)
sort.Sort(sortableUint32(tokens))
i.setTokens(tokens)
return ringDesc, true, nil
})
}
// updateConsul updates our entries in consul, heartbeating and dealing with
// consul restarts.
func (i *Lifecycler) updateConsul(ctx context.Context) error {
return i.KVStore.CAS(ctx, ConsulKey, func(in interface{}) (out interface{}, retry bool, err error) {
var ringDesc *Desc
if in == nil {
ringDesc = NewDesc()
} else {
ringDesc = in.(*Desc)
}
ingesterDesc, ok := ringDesc.Ingesters[i.ID]
if !ok {
// consul must have restarted
level.Info(util.Logger).Log("msg", "found empty ring, inserting tokens")
ringDesc.AddIngester(i.ID, i.Addr, i.getTokens(), i.GetState(), i.cfg.NormaliseTokens)
} else {
ingesterDesc.Timestamp = time.Now().Unix()
ingesterDesc.State = i.GetState()
ingesterDesc.Addr = i.Addr
ringDesc.Ingesters[i.ID] = ingesterDesc
}
return ringDesc, true, nil
})
}
// changeState updates consul with state transitions for us. NB this must be
// called from loop()! Use ChangeState for calls from outside of loop().
func (i *Lifecycler) changeState(ctx context.Context, state IngesterState) error {
currState := i.GetState()
// Only the following state transitions can be triggered externally
if !((currState == PENDING && state == JOINING) || // triggered by TransferChunks at the beginning
(currState == JOINING && state == PENDING) || // triggered by TransferChunks on failure
(currState == JOINING && state == ACTIVE) || // triggered by TransferChunks on success
(currState == PENDING && state == ACTIVE) || // triggered by autoJoin
(currState == ACTIVE && state == LEAVING)) { // triggered by shutdown
return fmt.Errorf("Changing ingester state from %v -> %v is disallowed", currState, state)
}
level.Info(util.Logger).Log("msg", "changing ingester state from", "old_state", currState, "new_state", state)
i.setState(state)
return i.updateConsul(ctx)
}
func (i *Lifecycler) processShutdown(ctx context.Context) {
flushRequired := true
if i.cfg.ClaimOnRollout {
transferStart := time.Now()
if err := i.flushTransferer.TransferOut(ctx); err != nil {
level.Error(util.Logger).Log("msg", "Failed to transfer chunks to another ingester", "err", err)
shutdownDuration.WithLabelValues("transfer", "fail", i.RingName).Observe(time.Since(transferStart).Seconds())
} else {
flushRequired = false
shutdownDuration.WithLabelValues("transfer", "success", i.RingName).Observe(time.Since(transferStart).Seconds())
}
}
if flushRequired {
flushStart := time.Now()
i.flushTransferer.Flush()
shutdownDuration.WithLabelValues("flush", "success", i.RingName).Observe(time.Since(flushStart).Seconds())
}
// Sleep so the shutdownDuration metric can be collected.
time.Sleep(i.cfg.FinalSleep)
}
// unregister removes our entry from consul.
func (i *Lifecycler) unregister(ctx context.Context) error {
level.Debug(util.Logger).Log("msg", "unregistering member from ring")
return i.KVStore.CAS(ctx, ConsulKey, func(in interface{}) (out interface{}, retry bool, err error) {
if in == nil {
return nil, false, fmt.Errorf("found empty ring when trying to unregister")
}
ringDesc := in.(*Desc)
ringDesc.RemoveIngester(i.ID)
return ringDesc, true, nil
})
}