MCPcopy
hub / github.com/grafana/dskit / verifyTokens

Method verifyTokens

ring/basic_lifecycler.go:406–438  ·  view source on GitHub ↗

Verifies that tokens that this instance has registered to the ring still belong to it. Gossiping ring may change the ownership of tokens in case of conflicts. If instance doesn't own its tokens anymore, this method generates new tokens and stores them to the ring.

(ctx context.Context)

Source from the content-addressed store, hash-verified

404// Gossiping ring may change the ownership of tokens in case of conflicts.
405// If instance doesn't own its tokens anymore, this method generates new tokens and stores them to the ring.
406func (l *BasicLifecycler) verifyTokens(ctx context.Context) bool {
407 result := false
408
409 err := l.updateInstance(ctx, func(r *Desc, i *InstanceDesc) bool {
410 // At this point, we should have the same tokens as we have registered before.
411 actualTokens, takenTokens := r.TokensFor(l.cfg.ID)
412
413 if actualTokens.Equals(l.GetTokens()) {
414 // Tokens have been verified. No need to change them.
415 result = true
416 return false
417 }
418
419 // uh, oh... our tokens are not our anymore. Let's try new ones.
420 needTokens := l.cfg.NumTokens - len(actualTokens)
421
422 level.Info(l.logger).Log("msg", "generating new tokens", "count", needTokens, "ring", l.ringName)
423 newTokens := l.tokenGenerator.GenerateTokens(needTokens, takenTokens)
424
425 actualTokens = append(actualTokens, newTokens...)
426 sort.Sort(actualTokens)
427
428 i.Tokens = actualTokens
429 return true
430 })
431
432 if err != nil {
433 level.Error(l.logger).Log("msg", "failed to verify tokens", "ring", l.ringName, "err", err)
434 return false
435 }
436
437 return result
438}
439
440// unregister removes our entry from the store.
441func (l *BasicLifecycler) unregisterInstance(ctx context.Context) error {

Callers 1

waitStableTokensMethod · 0.95

Calls 7

updateInstanceMethod · 0.95
GetTokensMethod · 0.95
TokensForMethod · 0.80
EqualsMethod · 0.80
GenerateTokensMethod · 0.65
LogMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected