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

Method verifyTokens

ring/lifecycler.go:807–845  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
807func (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
847func (i *Lifecycler) compareTokens(fromRing Tokens) bool {
848 sort.Sort(fromRing)

Callers 1

loopMethod · 0.95

Calls 12

compareTokensMethod · 0.95
GetReadOnlyStateMethod · 0.95
GetStateMethod · 0.95
getRegisteredAtMethod · 0.95
setTokensMethod · 0.95
GetOrCreateRingDescFunction · 0.85
TokensForMethod · 0.80
AddIngesterMethod · 0.80
CASMethod · 0.65
GenerateTokensMethod · 0.65
LogMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected