Verifies that tokens that this ingester has registered to the ring still belong to it. Gossiping ring may change the ownership of tokens in case of conflicts. If ingester doesn't own its tokens anymore, this method generates new tokens and puts them to the ring.
(ctx context.Context)
| 805 | // Gossiping ring may change the ownership of tokens in case of conflicts. |
| 806 | // If ingester doesn't own its tokens anymore, this method generates new tokens and puts them to the ring. |
| 807 | func (i *Lifecycler) verifyTokens(ctx context.Context) bool { |
| 808 | result := false |
| 809 | |
| 810 | err := i.KVStore.CAS(ctx, i.RingKey, func(in interface{}) (out interface{}, retry bool, err error) { |
| 811 | ringDesc := GetOrCreateRingDesc(in) |
| 812 | |
| 813 | // At this point, we should have the same tokens as we have registered before |
| 814 | ringTokens, takenTokens := ringDesc.TokensFor(i.ID) |
| 815 | |
| 816 | if !i.compareTokens(ringTokens) { |
| 817 | // uh, oh... our tokens are not ours anymore. Let's try new ones. |
| 818 | needTokens := i.cfg.NumTokens - len(ringTokens) |
| 819 | |
| 820 | level.Info(i.logger).Log("msg", "generating new tokens", "count", needTokens, "ring", i.RingName) |
| 821 | newTokens := i.tokenGenerator.GenerateTokens(needTokens, takenTokens) |
| 822 | |
| 823 | ringTokens = append(ringTokens, newTokens...) |
| 824 | sort.Sort(ringTokens) |
| 825 | |
| 826 | ro, rots := i.GetReadOnlyState() |
| 827 | ringDesc.AddIngester(i.ID, i.Addr, i.Zone, ringTokens, i.GetState(), i.getRegisteredAt(), ro, rots, nil) |
| 828 | |
| 829 | i.setTokens(ringTokens) |
| 830 | |
| 831 | return ringDesc, true, nil |
| 832 | } |
| 833 | |
| 834 | // all is good, this ingester owns its tokens |
| 835 | result = true |
| 836 | return nil, true, nil |
| 837 | }) |
| 838 | |
| 839 | if err != nil { |
| 840 | level.Error(i.logger).Log("msg", "failed to verify tokens", "ring", i.RingName, "err", err) |
| 841 | return false |
| 842 | } |
| 843 | |
| 844 | return result |
| 845 | } |
| 846 | |
| 847 | func (i *Lifecycler) compareTokens(fromRing Tokens) bool { |
| 848 | sort.Sort(fromRing) |
no test coverage detected