| 863 | } |
| 864 | |
| 865 | func (i *Lifecycler) waitBeforeJoining(ctx context.Context) error { |
| 866 | if !i.tokenGenerator.CanJoinEnabled() { |
| 867 | return nil |
| 868 | } |
| 869 | |
| 870 | level.Info(i.logger).Log("msg", "waiting to be able to join the ring", "ring", i.RingName, "id", i.cfg.ID, "timeout", i.canJoinTimeout) |
| 871 | |
| 872 | ctxWithTimeout, cancel := context.WithTimeout(ctx, i.canJoinTimeout) |
| 873 | defer cancel() |
| 874 | retries := backoff.New(ctxWithTimeout, backoff.Config{ |
| 875 | MinBackoff: 1 * time.Second, |
| 876 | MaxBackoff: 1 * time.Second, |
| 877 | MaxRetries: 0, |
| 878 | }) |
| 879 | |
| 880 | var lastError error |
| 881 | for ; retries.Ongoing(); retries.Wait() { |
| 882 | var desc interface{} |
| 883 | desc, lastError = i.KVStore.Get(ctxWithTimeout, i.RingKey) |
| 884 | if lastError != nil { |
| 885 | lastError = errors.Wrap(lastError, "error getting the ring from the KV store") |
| 886 | continue |
| 887 | } |
| 888 | |
| 889 | ringDesc, ok := desc.(*Desc) |
| 890 | if !ok || ringDesc == nil { |
| 891 | lastError = fmt.Errorf("no ring returned from the KV store") |
| 892 | continue |
| 893 | } |
| 894 | lastError = i.tokenGenerator.CanJoin(ringDesc.GetIngesters()) |
| 895 | if lastError == nil { |
| 896 | level.Info(i.logger).Log("msg", "it is now possible to join the ring", "ring", i.RingName, "id", i.cfg.ID, "retries", retries.NumRetries()) |
| 897 | return nil |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | if lastError == nil { |
| 902 | lastError = retries.Err() |
| 903 | } |
| 904 | level.Warn(i.logger).Log("msg", "there was a problem while checking whether this instance could join the ring - will continue anyway", "ring", i.RingName, "id", i.cfg.ID, "err", lastError) |
| 905 | |
| 906 | // Return error only in case the parent context has been cancelled. |
| 907 | // In all other cases, we just want to swallow the error and move on. |
| 908 | return ctx.Err() |
| 909 | } |
| 910 | |
| 911 | // autoJoin selects random tokens & moves state to targetState |
| 912 | func (i *Lifecycler) autoJoin(ctx context.Context, targetState InstanceState) error { |