• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19705

20 Feb 2025 04:25AM UTC coverage: 88.616% (+0.03%) from 88.589%
#19705

push

github

web-flow
xds: explicitly set request hash key for the ring hash LB policy

Implements [gRFC A76: explicitly setting the request hash key for the
ring hash LB policy][A76]
* Explictly setting the request hash key is guarded by the
  `GRPC_EXPERIMENTAL_RING_HASH_SET_REQUEST_HASH_KEY` environment
  variable until API stabilized. 

Tested:
* Verified end-to-end by spinning up multiple gRPC servers and a gRPC
  client that injects a custom service (load balancing) config with
  `ring_hash_experimental` and a custom `request_hash_header` (with
  NO associated value in the metadata headers) which generates a random
  hash for each request to the ring hash LB. Verified picks/RPCs are
  split evenly/uniformly across all backends.
* Ran affected unit tests with thread sanitizer and 1000 iterations to
  prevent data races.

[A76]: https://github.com/grpc/proposal/blob/master/A76-ring-hash-improvements.md#explicitly-setting-the-request-hash-key

34305 of 38712 relevant lines covered (88.62%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.8
/../xds/src/main/java/io/grpc/xds/RingHashLoadBalancer.java
1
/*
2
 * Copyright 2021 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.xds;
18

19
import static com.google.common.base.Preconditions.checkArgument;
20
import static com.google.common.base.Preconditions.checkNotNull;
21
import static com.google.common.base.Preconditions.checkState;
22
import static io.grpc.ConnectivityState.CONNECTING;
23
import static io.grpc.ConnectivityState.IDLE;
24
import static io.grpc.ConnectivityState.READY;
25
import static io.grpc.ConnectivityState.SHUTDOWN;
26
import static io.grpc.ConnectivityState.TRANSIENT_FAILURE;
27

28
import com.google.common.annotations.VisibleForTesting;
29
import com.google.common.base.Joiner;
30
import com.google.common.base.MoreObjects;
31
import com.google.common.collect.HashMultiset;
32
import com.google.common.collect.Multiset;
33
import com.google.common.primitives.UnsignedInteger;
34
import io.grpc.Attributes;
35
import io.grpc.ConnectivityState;
36
import io.grpc.EquivalentAddressGroup;
37
import io.grpc.InternalLogId;
38
import io.grpc.LoadBalancer;
39
import io.grpc.Metadata;
40
import io.grpc.Status;
41
import io.grpc.SynchronizationContext;
42
import io.grpc.util.MultiChildLoadBalancer;
43
import io.grpc.xds.ThreadSafeRandom.ThreadSafeRandomImpl;
44
import io.grpc.xds.client.XdsLogger;
45
import io.grpc.xds.client.XdsLogger.XdsLogLevel;
46
import java.net.SocketAddress;
47
import java.util.ArrayList;
48
import java.util.Collection;
49
import java.util.Collections;
50
import java.util.HashMap;
51
import java.util.HashSet;
52
import java.util.List;
53
import java.util.Map;
54
import java.util.Set;
55
import java.util.stream.Collectors;
56
import javax.annotation.Nullable;
57

58
/**
59
 * A {@link LoadBalancer} that provides consistent hashing based load balancing to upstream hosts.
60
 * It implements the "Ketama" hashing that maps hosts onto a circle (the "ring") by hashing its
61
 * addresses. Each request is routed to a host by hashing some property of the request and finding
62
 * the nearest corresponding host clockwise around the ring. Each host is placed on the ring some
63
 * number of times proportional to its weight. With the ring partitioned appropriately, the
64
 * addition or removal of one host from a set of N hosts will affect only 1/N requests.
65
 */
66
final class RingHashLoadBalancer extends MultiChildLoadBalancer {
67
  private static final Status RPC_HASH_NOT_FOUND =
1✔
68
      Status.INTERNAL.withDescription("RPC hash not found. Probably a bug because xds resolver"
1✔
69
          + " config selector always generates a hash.");
70
  private static final XxHash64 hashFunc = XxHash64.INSTANCE;
1✔
71

72
  private final LoadBalancer.Factory lazyLbFactory =
1✔
73
      new LazyLoadBalancer.Factory(pickFirstLbProvider);
74
  private final XdsLogger logger;
75
  private final SynchronizationContext syncContext;
76
  private final ThreadSafeRandom random;
77
  private List<RingEntry> ring;
78
  @Nullable private Metadata.Key<String> requestHashHeaderKey;
79

80
  RingHashLoadBalancer(Helper helper) {
81
    this(helper, ThreadSafeRandomImpl.instance);
1✔
82
  }
1✔
83

84
  @VisibleForTesting
85
  RingHashLoadBalancer(Helper helper, ThreadSafeRandom random) {
86
    super(helper);
1✔
87
    syncContext = checkNotNull(helper.getSynchronizationContext(), "syncContext");
1✔
88
    logger = XdsLogger.withLogId(InternalLogId.allocate("ring_hash_lb", helper.getAuthority()));
1✔
89
    logger.log(XdsLogLevel.INFO, "Created");
1✔
90
    this.random = checkNotNull(random, "random");
1✔
91
  }
1✔
92

93
  @Override
94
  public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
95
    logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
1✔
96
    List<EquivalentAddressGroup> addrList = resolvedAddresses.getAddresses();
1✔
97
    Status addressValidityStatus = validateAddrList(addrList);
1✔
98
    if (!addressValidityStatus.isOk()) {
1✔
99
      return addressValidityStatus;
1✔
100
    }
101

102
    // Now do the ringhash specific logic with weights and building the ring
103
    RingHashConfig config = (RingHashConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
1✔
104
    if (config == null) {
1✔
105
      throw new IllegalArgumentException("Missing RingHash configuration");
×
106
    }
107
    requestHashHeaderKey =
1✔
108
        config.requestHashHeader.isEmpty()
1✔
109
            ? null
1✔
110
            : Metadata.Key.of(config.requestHashHeader, Metadata.ASCII_STRING_MARSHALLER);
1✔
111
    Map<EquivalentAddressGroup, Long> serverWeights = new HashMap<>();
1✔
112
    long totalWeight = 0L;
1✔
113
    for (EquivalentAddressGroup eag : addrList) {
1✔
114
      Long weight = eag.getAttributes().get(XdsAttributes.ATTR_SERVER_WEIGHT);
1✔
115
      // Support two ways of server weighing: either multiple instances of the same address
116
      // or each address contains a per-address weight attribute. If a weight is not provided,
117
      // each occurrence of the address will be counted a weight value of one.
118
      if (weight == null) {
1✔
119
        weight = 1L;
×
120
      }
121
      totalWeight += weight;
1✔
122
      EquivalentAddressGroup addrKey = stripAttrs(eag);
1✔
123
      if (serverWeights.containsKey(addrKey)) {
1✔
124
        serverWeights.put(addrKey, serverWeights.get(addrKey) + weight);
×
125
      } else {
126
        serverWeights.put(addrKey, weight);
1✔
127
      }
128
    }
1✔
129
    // Calculate scale
130
    long minWeight = Collections.min(serverWeights.values());
1✔
131
    double normalizedMinWeight = (double) minWeight / totalWeight;
1✔
132
    // Scale up the number of hashes per host such that the least-weighted host gets a whole
133
    // number of hashes on the the ring. Other hosts might not end up with whole numbers, and
134
    // that's fine (the ring-building algorithm can handle this). This preserves the original
135
    // implementation's behavior: when weights aren't provided, all hosts should get an equal
136
    // number of hashes. In the case where this number exceeds the max_ring_size, it's scaled
137
    // back down to fit.
138
    double scale = Math.min(
1✔
139
        Math.ceil(normalizedMinWeight * config.minRingSize) / normalizedMinWeight,
1✔
140
        (double) config.maxRingSize);
141

142
    // Build the ring
143
    ring = buildRing(serverWeights, totalWeight, scale);
1✔
144

145
    return super.acceptResolvedAddresses(resolvedAddresses);
1✔
146
  }
147

148

149
  /**
150
   * Updates the overall balancing state by aggregating the connectivity states of all subchannels.
151
   *
152
   * <p>Aggregation rules (in order of dominance):
153
   * <ol>
154
   *   <li>If there is at least one subchannel in READY state, overall state is READY</li>
155
   *   <li>If there are <em>2 or more</em> subchannels in TRANSIENT_FAILURE, overall state is
156
   *   TRANSIENT_FAILURE (to allow timely failover to another policy)</li>
157
   *   <li>If there is at least one subchannel in CONNECTING state, overall state is
158
   *   CONNECTING</li>
159
   *   <li> If there is one subchannel in TRANSIENT_FAILURE state and there is
160
   *    more than one subchannel, report CONNECTING </li>
161
   *   <li>If there is at least one subchannel in IDLE state, overall state is IDLE</li>
162
   *   <li>Otherwise, overall state is TRANSIENT_FAILURE</li>
163
   * </ol>
164
   */
165
  @Override
166
  protected void updateOverallBalancingState() {
167
    checkState(!getChildLbStates().isEmpty(), "no subchannel has been created");
1✔
168
    if (this.currentConnectivityState == SHUTDOWN) {
1✔
169
      // Ignore changes that happen after shutdown is called
170
      logger.log(XdsLogLevel.DEBUG, "UpdateOverallBalancingState called after shutdown");
×
171
      return;
×
172
    }
173

174
    // Calculate the current overall state to report
175
    int numIdle = 0;
1✔
176
    int numReady = 0;
1✔
177
    int numConnecting = 0;
1✔
178
    int numTF = 0;
1✔
179

180
    forloop:
181
    for (ChildLbState childLbState : getChildLbStates()) {
1✔
182
      ConnectivityState state = childLbState.getCurrentState();
1✔
183
      switch (state) {
1✔
184
        case READY:
185
          numReady++;
1✔
186
          break forloop;
1✔
187
        case CONNECTING:
188
          numConnecting++;
1✔
189
          break;
1✔
190
        case IDLE:
191
          numIdle++;
1✔
192
          break;
1✔
193
        case TRANSIENT_FAILURE:
194
          numTF++;
1✔
195
          break;
1✔
196
        default:
197
          // ignore it
198
      }
199
    }
1✔
200

201
    ConnectivityState overallState;
202
    if (numReady > 0) {
1✔
203
      overallState = READY;
1✔
204
    } else if (numTF >= 2) {
1✔
205
      overallState = TRANSIENT_FAILURE;
1✔
206
    } else if (numConnecting > 0) {
1✔
207
      overallState = CONNECTING;
1✔
208
    } else if (numTF == 1 && getChildLbStates().size() > 1) {
1✔
209
      overallState = CONNECTING;
1✔
210
    } else if (numIdle > 0) {
1✔
211
      overallState = IDLE;
1✔
212
    } else {
213
      overallState = TRANSIENT_FAILURE;
×
214
    }
215

216
    RingHashPicker picker =
1✔
217
        new RingHashPicker(syncContext, ring, getChildLbStates(), requestHashHeaderKey, random);
1✔
218
    getHelper().updateBalancingState(overallState, picker);
1✔
219
    this.currentConnectivityState = overallState;
1✔
220
  }
1✔
221

222
  @Override
223
  protected ChildLbState createChildLbState(Object key) {
224
    return new ChildLbState(key, lazyLbFactory);
1✔
225
  }
226

227
  private Status validateAddrList(List<EquivalentAddressGroup> addrList) {
228
    if (addrList.isEmpty()) {
1✔
229
      Status unavailableStatus = Status.UNAVAILABLE.withDescription("Ring hash lb error: EDS "
×
230
              + "resolution was successful, but returned server addresses are empty.");
231
      handleNameResolutionError(unavailableStatus);
×
232
      return unavailableStatus;
×
233
    }
234

235
    String dupAddrString = validateNoDuplicateAddresses(addrList);
1✔
236
    if (dupAddrString != null) {
1✔
237
      Status unavailableStatus = Status.UNAVAILABLE.withDescription("Ring hash lb error: EDS "
1✔
238
              + "resolution was successful, but there were duplicate addresses: " + dupAddrString);
239
      handleNameResolutionError(unavailableStatus);
1✔
240
      return unavailableStatus;
1✔
241
    }
242

243
    long totalWeight = 0;
1✔
244
    for (EquivalentAddressGroup eag : addrList) {
1✔
245
      Long weight = eag.getAttributes().get(XdsAttributes.ATTR_SERVER_WEIGHT);
1✔
246

247
      if (weight == null) {
1✔
248
        weight = 1L;
×
249
      }
250

251
      if (weight < 0) {
1✔
252
        Status unavailableStatus = Status.UNAVAILABLE.withDescription(
1✔
253
            String.format("Ring hash lb error: EDS resolution was successful, but returned a "
1✔
254
                        + "negative weight for %s.", stripAttrs(eag)));
1✔
255
        handleNameResolutionError(unavailableStatus);
1✔
256
        return unavailableStatus;
1✔
257
      }
258
      if (weight > UnsignedInteger.MAX_VALUE.longValue()) {
1✔
259
        Status unavailableStatus = Status.UNAVAILABLE.withDescription(
1✔
260
            String.format("Ring hash lb error: EDS resolution was successful, but returned a weight"
1✔
261
                + " too large to fit in an unsigned int for %s.", stripAttrs(eag)));
1✔
262
        handleNameResolutionError(unavailableStatus);
1✔
263
        return unavailableStatus;
1✔
264
      }
265
      totalWeight += weight;
1✔
266
    }
1✔
267

268
    if (totalWeight > UnsignedInteger.MAX_VALUE.longValue()) {
1✔
269
      Status unavailableStatus = Status.UNAVAILABLE.withDescription(
1✔
270
          String.format(
1✔
271
              "Ring hash lb error: EDS resolution was successful, but returned a sum of weights too"
272
                  + " large to fit in an unsigned int (%d).", totalWeight));
1✔
273
      handleNameResolutionError(unavailableStatus);
1✔
274
      return unavailableStatus;
1✔
275
    }
276

277
    return Status.OK;
1✔
278
  }
279

280
  @Nullable
281
  private String validateNoDuplicateAddresses(List<EquivalentAddressGroup> addrList) {
282
    Set<SocketAddress> addresses = new HashSet<>();
1✔
283
    Multiset<String> dups = HashMultiset.create();
1✔
284
    for (EquivalentAddressGroup eag : addrList) {
1✔
285
      for (SocketAddress address : eag.getAddresses()) {
1✔
286
        if (!addresses.add(address)) {
1✔
287
          dups.add(address.toString());
1✔
288
        }
289
      }
1✔
290
    }
1✔
291

292
    if (!dups.isEmpty()) {
1✔
293
      return dups.entrySet().stream()
1✔
294
          .map((dup) ->
1✔
295
              String.format("Address: %s, count: %d", dup.getElement(), dup.getCount() + 1))
1✔
296
          .collect(Collectors.joining("; "));
1✔
297
    }
298

299
    return null;
1✔
300
  }
301

302
  private static List<RingEntry> buildRing(
303
      Map<EquivalentAddressGroup, Long> serverWeights, long totalWeight, double scale) {
304
    List<RingEntry> ring = new ArrayList<>();
1✔
305
    double currentHashes = 0.0;
1✔
306
    double targetHashes = 0.0;
1✔
307
    for (Map.Entry<EquivalentAddressGroup, Long> entry : serverWeights.entrySet()) {
1✔
308
      Endpoint endpoint = new Endpoint(entry.getKey());
1✔
309
      double normalizedWeight = (double) entry.getValue() / totalWeight;
1✔
310
      // Per GRFC A61 use the first address for the hash
311
      StringBuilder sb = new StringBuilder(entry.getKey().getAddresses().get(0).toString());
1✔
312
      sb.append('_');
1✔
313
      int lengthWithoutCounter = sb.length();
1✔
314
      targetHashes += scale * normalizedWeight;
1✔
315
      long i = 0L;
1✔
316
      while (currentHashes < targetHashes) {
1✔
317
        sb.append(i);
1✔
318
        long hash = hashFunc.hashAsciiString(sb.toString());
1✔
319
        ring.add(new RingEntry(hash, endpoint));
1✔
320
        i++;
1✔
321
        currentHashes++;
1✔
322
        sb.setLength(lengthWithoutCounter);
1✔
323
      }
1✔
324
    }
1✔
325
    Collections.sort(ring);
1✔
326
    return Collections.unmodifiableList(ring);
1✔
327
  }
328

329
  @SuppressWarnings("ReferenceEquality")
330
  public static EquivalentAddressGroup stripAttrs(EquivalentAddressGroup eag) {
331
    if (eag.getAttributes() == Attributes.EMPTY) {
1✔
332
      return eag;
×
333
    }
334
    return new EquivalentAddressGroup(eag.getAddresses());
1✔
335
  }
336

337
  private static final class RingHashPicker extends SubchannelPicker {
338
    private final SynchronizationContext syncContext;
339
    private final List<RingEntry> ring;
340
    // Avoid synchronization between pickSubchannel and subchannel's connectivity state change,
341
    // freeze picker's view of subchannel's connectivity state.
342
    // TODO(chengyuanzhang): can be more performance-friendly with
343
    //  IdentityHashMap<Subchannel, ConnectivityStateInfo> and RingEntry contains Subchannel.
344
    private final Map<Endpoint, SubchannelView> pickableSubchannels;  // read-only
345
    @Nullable private final Metadata.Key<String> requestHashHeaderKey;
346
    private final ThreadSafeRandom random;
347
    private final boolean hasEndpointInConnectingState;
348

349
    private RingHashPicker(
350
        SynchronizationContext syncContext, List<RingEntry> ring,
351
        Collection<ChildLbState> children, Metadata.Key<String> requestHashHeaderKey,
352
        ThreadSafeRandom random) {
1✔
353
      this.syncContext = syncContext;
1✔
354
      this.ring = ring;
1✔
355
      this.requestHashHeaderKey = requestHashHeaderKey;
1✔
356
      this.random = random;
1✔
357
      pickableSubchannels = new HashMap<>(children.size());
1✔
358
      boolean hasConnectingState = false;
1✔
359
      for (ChildLbState childLbState : children) {
1✔
360
        pickableSubchannels.put((Endpoint)childLbState.getKey(),
1✔
361
            new SubchannelView(childLbState, childLbState.getCurrentState()));
1✔
362
        if (childLbState.getCurrentState() == CONNECTING) {
1✔
363
          hasConnectingState = true;
1✔
364
        }
365
      }
1✔
366
      this.hasEndpointInConnectingState = hasConnectingState;
1✔
367
    }
1✔
368

369
    // Find the ring entry with hash next to (clockwise) the RPC's hash (binary search).
370
    private int getTargetIndex(long requestHash) {
371
      if (ring.size() <= 1) {
1✔
372
        return 0;
×
373
      }
374

375
      int low = 0;
1✔
376
      int high = ring.size() - 1;
1✔
377
      int mid = (low + high) / 2;
1✔
378
      do {
379
        long midVal = ring.get(mid).hash;
1✔
380
        long midValL = mid == 0 ? 0 : ring.get(mid - 1).hash;
1✔
381
        if (requestHash <= midVal && requestHash > midValL) {
1✔
382
          break;
1✔
383
        }
384
        if (midVal < requestHash) {
1✔
385
          low = mid + 1;
1✔
386
        } else {
387
          high =  mid - 1;
1✔
388
        }
389
        mid = (low + high) / 2;
1✔
390
      } while (mid < ring.size() && low <= high);
1✔
391
      return mid;
1✔
392
    }
393

394
    @Override
395
    public PickResult pickSubchannel(PickSubchannelArgs args) {
396
      // Determine request hash.
397
      boolean usingRandomHash = false;
1✔
398
      long requestHash;
399
      if (requestHashHeaderKey == null) {
1✔
400
        // Set by the xDS config selector.
401
        Long rpcHashFromCallOptions = args.getCallOptions().getOption(XdsNameResolver.RPC_HASH_KEY);
1✔
402
        if (rpcHashFromCallOptions == null) {
1✔
403
          return PickResult.withError(RPC_HASH_NOT_FOUND);
×
404
        }
405
        requestHash = rpcHashFromCallOptions;
1✔
406
      } else {
1✔
407
        Iterable<String> headerValues = args.getHeaders().getAll(requestHashHeaderKey);
1✔
408
        if (headerValues != null) {
1✔
409
          requestHash = hashFunc.hashAsciiString(Joiner.on(",").join(headerValues));
1✔
410
        } else {
411
          requestHash = random.nextLong();
1✔
412
          usingRandomHash = true;
1✔
413
        }
414
      }
415

416
      int targetIndex = getTargetIndex(requestHash);
1✔
417

418
      if (!usingRandomHash) {
1✔
419
        // Per gRFC A61, because of sticky-TF with PickFirst's auto reconnect on TF, we ignore
420
        // all TF subchannels and find the first ring entry in READY, CONNECTING or IDLE.  If
421
        // CONNECTING or IDLE we return a pick with no results.  Additionally, if that entry is in
422
        // IDLE, we initiate a connection.
423
        for (int i = 0; i < ring.size(); i++) {
1✔
424
          int index = (targetIndex + i) % ring.size();
1✔
425
          SubchannelView subchannelView = pickableSubchannels.get(ring.get(index).addrKey);
1✔
426
          ChildLbState childLbState = subchannelView.childLbState;
1✔
427

428
          if (subchannelView.connectivityState  == READY) {
1✔
429
            return childLbState.getCurrentPicker().pickSubchannel(args);
1✔
430
          }
431

432
          // RPCs can be buffered if the next subchannel is pending (per A62). Otherwise, RPCs
433
          // are failed unless there is a READY connection.
434
          if (subchannelView.connectivityState == CONNECTING) {
1✔
435
            return PickResult.withNoResult();
1✔
436
          }
437

438
          if (subchannelView.connectivityState == IDLE) {
1✔
439
            syncContext.execute(() -> {
1✔
440
              childLbState.getLb().requestConnection();
1✔
441
            });
1✔
442

443
            return PickResult.withNoResult(); // Indicates that this should be retried after backoff
1✔
444
          }
445
        }
446
      } else {
447
        // Using a random hash. Find and use the first READY ring entry, triggering at most one
448
        // entry to attempt connection.
449
        boolean requestedConnection = hasEndpointInConnectingState;
1✔
450
        for (int i = 0; i < ring.size(); i++) {
1✔
451
          int index = (targetIndex + i) % ring.size();
1✔
452
          SubchannelView subchannelView = pickableSubchannels.get(ring.get(index).addrKey);
1✔
453
          ChildLbState childLbState = subchannelView.childLbState;
1✔
454
          if (subchannelView.connectivityState == READY) {
1✔
455
            return childLbState.getCurrentPicker().pickSubchannel(args);
1✔
456
          }
457
          if (!requestedConnection && subchannelView.connectivityState == IDLE) {
1✔
458
            syncContext.execute(
1✔
459
                () -> {
460
                  childLbState.getLb().requestConnection();
1✔
461
                });
1✔
462
            requestedConnection = true;
1✔
463
          }
464
        }
465
        if (requestedConnection) {
1✔
466
          return PickResult.withNoResult();
1✔
467
        }
468
      }
469

470
      // return the pick from the original subchannel hit by hash, which is probably an error
471
      ChildLbState originalSubchannel =
1✔
472
          pickableSubchannels.get(ring.get(targetIndex).addrKey).childLbState;
1✔
473
      return originalSubchannel.getCurrentPicker().pickSubchannel(args);
1✔
474
    }
475

476
  }
477

478
  /**
479
   * An unmodifiable view of a subchannel with state not subject to its real connectivity
480
   * state changes.
481
   */
482
  private static final class SubchannelView {
483
    private final ChildLbState childLbState;
484
    private final ConnectivityState connectivityState;
485

486
    private SubchannelView(ChildLbState childLbState, ConnectivityState state) {
1✔
487
      this.childLbState = childLbState;
1✔
488
      this.connectivityState = state;
1✔
489
    }
1✔
490
  }
491

492
  private static final class RingEntry implements Comparable<RingEntry> {
493
    private final long hash;
494
    private final Endpoint addrKey;
495

496
    private RingEntry(long hash, Endpoint addrKey) {
1✔
497
      this.hash = hash;
1✔
498
      this.addrKey = addrKey;
1✔
499
    }
1✔
500

501
    @Override
502
    public int compareTo(RingEntry entry) {
503
      return Long.compare(hash, entry.hash);
1✔
504
    }
505
  }
506

507
  /**
508
   * Configures the ring property. The larger the ring is (that is, the more hashes there are
509
   * for each provided host) the better the request distribution will reflect the desired weights.
510
   */
511
  static final class RingHashConfig {
512
    final long minRingSize;
513
    final long maxRingSize;
514
    final String requestHashHeader;
515

516
    RingHashConfig(long minRingSize, long maxRingSize, String requestHashHeader) {
1✔
517
      checkArgument(minRingSize > 0, "minRingSize <= 0");
1✔
518
      checkArgument(maxRingSize > 0, "maxRingSize <= 0");
1✔
519
      checkArgument(minRingSize <= maxRingSize, "minRingSize > maxRingSize");
1✔
520
      checkNotNull(requestHashHeader);
1✔
521
      this.minRingSize = minRingSize;
1✔
522
      this.maxRingSize = maxRingSize;
1✔
523
      this.requestHashHeader = requestHashHeader;
1✔
524
    }
1✔
525

526
    @Override
527
    public String toString() {
528
      return MoreObjects.toStringHelper(this)
1✔
529
          .add("minRingSize", minRingSize)
1✔
530
          .add("maxRingSize", maxRingSize)
1✔
531
          .add("requestHashHeader", requestHashHeader)
1✔
532
          .toString();
1✔
533
    }
534
  }
535
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc