autoJoin selects random tokens & moves state to targetState
(ctx context.Context, targetState InstanceState)
| 910 | |
| 911 | // autoJoin selects random tokens & moves state to targetState |
| 912 | func (i *Lifecycler) autoJoin(ctx context.Context, targetState InstanceState) error { |
| 913 | err := i.waitBeforeJoining(ctx) |
| 914 | if err != nil { |
| 915 | return err |
| 916 | } |
| 917 | |
| 918 | var ringDesc *Desc |
| 919 | err = i.KVStore.CAS(ctx, i.RingKey, func(in interface{}) (out interface{}, retry bool, err error) { |
| 920 | ringDesc = GetOrCreateRingDesc(in) |
| 921 | |
| 922 | // At this point, we should not have any tokens, and we should be in PENDING state. |
| 923 | myTokens, takenTokens := ringDesc.TokensFor(i.ID) |
| 924 | if len(myTokens) > 0 { |
| 925 | level.Error(i.logger).Log("msg", "tokens already exist for this instance - wasn't expecting any!", "num_tokens", len(myTokens), "ring", i.RingName) |
| 926 | } |
| 927 | |
| 928 | newTokens := i.tokenGenerator.GenerateTokens(i.cfg.NumTokens-len(myTokens), takenTokens) |
| 929 | i.setState(targetState) |
| 930 | |
| 931 | myTokens = append(myTokens, newTokens...) |
| 932 | sort.Sort(myTokens) |
| 933 | i.setTokens(myTokens) |
| 934 | |
| 935 | ro, rots := i.GetReadOnlyState() |
| 936 | ringDesc.AddIngester(i.ID, i.Addr, i.Zone, i.getTokens(), i.GetState(), i.getRegisteredAt(), ro, rots, nil) |
| 937 | return ringDesc, true, nil |
| 938 | }) |
| 939 | |
| 940 | // Update counters |
| 941 | if err == nil { |
| 942 | i.updateCounters(ringDesc) |
| 943 | } |
| 944 | |
| 945 | return err |
| 946 | } |
| 947 | |
| 948 | // updateConsul updates our entries in consul, heartbeating and dealing with |
| 949 | // consul restarts. |
no test coverage detected