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)
| 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. |
| 406 | func (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. |
| 441 | func (l *BasicLifecycler) unregisterInstance(ctx context.Context) error { |
no test coverage detected