| 993 | } |
| 994 | |
| 995 | func (r *Ring) shuffleShard(identifier string, size int, lookbackPeriod time.Duration, now time.Time) *Ring { |
| 996 | lookbackUntil := now.Add(-lookbackPeriod).Unix() |
| 997 | |
| 998 | r.mtx.RLock() |
| 999 | defer r.mtx.RUnlock() |
| 1000 | |
| 1001 | // If all instances have RegisteredTimestamp within the lookback period, |
| 1002 | // then all instances would be included in the resulting ring, so we can |
| 1003 | // simply return this ring. |
| 1004 | // |
| 1005 | // If any instance had RegisteredTimestamp equal to 0 (it would not cause additional lookup of next instance), |
| 1006 | // then r.oldestRegisteredTimestamp is zero too, and we skip this optimization. |
| 1007 | // |
| 1008 | // Even if some instances are read-only, they must have changed their read-only status within lookback window |
| 1009 | // (because they were all registered within lookback window), so they would be included in the result. |
| 1010 | if lookbackPeriod > 0 && r.oldestRegisteredTimestamp > 0 && r.oldestRegisteredTimestamp >= lookbackUntil { |
| 1011 | return r |
| 1012 | } |
| 1013 | |
| 1014 | var numInstancesPerZone int |
| 1015 | var actualZones []string |
| 1016 | |
| 1017 | if r.cfg.ZoneAwarenessEnabled { |
| 1018 | numInstancesPerZone = shardUtil.ShuffleShardExpectedInstancesPerZone(size, len(r.ringZones)) |
| 1019 | actualZones = r.ringZones |
| 1020 | } else { |
| 1021 | numInstancesPerZone = size |
| 1022 | actualZones = []string{""} |
| 1023 | } |
| 1024 | |
| 1025 | shard := make(map[string]InstanceDesc, min(len(r.ringDesc.Ingesters), size)) |
| 1026 | |
| 1027 | // We need to iterate zones always in the same order to guarantee stability. |
| 1028 | for _, zone := range actualZones { |
| 1029 | var tokens []uint32 |
| 1030 | |
| 1031 | if r.cfg.ZoneAwarenessEnabled { |
| 1032 | // If we're going to include all instances from this zone, we can simply filter out |
| 1033 | // unwanted instances, and avoid iterating through tokens. |
| 1034 | if numInstancesPerZone >= r.instancesCountPerZone[zone] { |
| 1035 | for id, inst := range r.ringDesc.Ingesters { |
| 1036 | if inst.Zone == zone && shouldIncludeReadonlyInstanceInTheShard(inst, lookbackPeriod, lookbackUntil) { |
| 1037 | shard[id] = inst |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | // We can go to the next zone, no need to iterate tokens. |
| 1042 | continue |
| 1043 | } |
| 1044 | |
| 1045 | tokens = r.ringTokensByZone[zone] |
| 1046 | } else { |
| 1047 | // When zone-awareness is disabled, we just iterate over 1 single fake zone |
| 1048 | // and use all tokens in the ring. |
| 1049 | tokens = r.ringTokens |
| 1050 | } |
| 1051 | |
| 1052 | // Initialise the random generator used to select instances in the ring. |