(set ring.ReplicationSet, exclude []string, balancingStrategy loadBalancingStrategy, zoneAwarenessEnabled bool, attemptedZones map[string]int)
| 159 | } |
| 160 | |
| 161 | func getNonExcludedInstance(set ring.ReplicationSet, exclude []string, balancingStrategy loadBalancingStrategy, zoneAwarenessEnabled bool, attemptedZones map[string]int) ring.InstanceDesc { |
| 162 | if balancingStrategy == randomLoadBalancing { |
| 163 | // Randomize the list of instances to not always query the same one. |
| 164 | rand.Shuffle(len(set.Instances), func(i, j int) { |
| 165 | set.Instances[i], set.Instances[j] = set.Instances[j], set.Instances[i] |
| 166 | }) |
| 167 | } |
| 168 | |
| 169 | minAttempt := math.MaxInt |
| 170 | numOfZone := set.GetNumOfZones() |
| 171 | // There are still unattempted zones so we know min is 0. |
| 172 | if len(attemptedZones) < numOfZone { |
| 173 | minAttempt = 0 |
| 174 | } else { |
| 175 | // Iterate over attempted zones and find the min attempts. |
| 176 | for _, c := range attemptedZones { |
| 177 | if c < minAttempt { |
| 178 | minAttempt = c |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | for _, instance := range set.Instances { |
| 183 | if slices.Contains(exclude, instance.Addr) { |
| 184 | continue |
| 185 | } |
| 186 | // If zone awareness is not enabled, pick first non-excluded instance. |
| 187 | // Otherwise, keep iterating until we find an instance in a zone where |
| 188 | // we have the least retries. |
| 189 | if !zoneAwarenessEnabled || attemptedZones[instance.Zone] == minAttempt { |
| 190 | return instance |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return ring.InstanceDesc{} |
| 195 | } |
no test coverage detected