Skip to content

Commit 3a2046f

Browse files
committed
Refine ChannelPool Javadoc and restore validation
1 parent 019ca96 commit 3a2046f

3 files changed

Lines changed: 52 additions & 13 deletions

File tree

sdk-platform-java/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/ChannelPool.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ class ChannelPool extends ManagedChannel {
9696

9797
// Tracks the number of consecutive resize cycles where a resize actually occurred (either expand
9898
// or shrink). Used to detect repeated resizing activity and log a warning.
99-
// Note: This field is only accessed safely within resizeSafely() and does not need to be atomic.
100-
private int consecutiveResizes = 0;
99+
private final AtomicInteger consecutiveResizes = new AtomicInteger(0);
101100

102101
static ChannelPool create(
103102
ChannelPoolSettings settings,
@@ -328,13 +327,17 @@ void resize() {
328327
maxChannels = settings.getMinChannelCount();
329328
}
330329

331-
// If the pool were to be resized, try to aim for the middle of the bound, but limit rate of
332-
// change.
330+
// If the pool were to be resized, try to aim for the middle of the bound,
331+
// but limit rate of change.
333332
int tentativeTarget = (maxChannels + minChannels) / 2;
334333
int currentSize = localEntries.size();
334+
// Calculate the desired change in pool size.
335335
int delta = tentativeTarget - currentSize;
336336
int dampenedTarget = tentativeTarget;
337+
// Dampen the rate of change if the desired delta exceeds the maximum allowed step size.
337338
if (Math.abs(delta) > settings.getMaxResizeDelta()) {
339+
// Limit the change to maxResizeDelta, maintaining the correct direction (positive or
340+
// negative).
338341
dampenedTarget = currentSize + (int) Math.copySign(settings.getMaxResizeDelta(), delta);
339342
}
340343

@@ -343,15 +346,15 @@ void resize() {
343346
// bounds but not at the target (target aims for the middle of the bounds)
344347
boolean resized = (currentSize < minChannels || currentSize > maxChannels);
345348
if (resized) {
346-
consecutiveResizes++;
349+
consecutiveResizes.incrementAndGet();
347350
} else {
348-
consecutiveResizes = 0;
351+
consecutiveResizes.set(0);
349352
}
350353

351354
// Log warning only once when the consecutive threshold is reached to avoid spamming logs. Log
352355
// message will repeat if the number of consecutive resizes resets (e.g. stabilizes for a bit).
353356
// However, aim to log once to ensure that this does not incur log spam.
354-
if (consecutiveResizes == CONSECUTIVE_RESIZE_THRESHOLD) {
357+
if (consecutiveResizes.get() == CONSECUTIVE_RESIZE_THRESHOLD) {
355358
LOG.warning(CHANNEL_POOL_CONSECUTIVE_RESIZING_WARNING);
356359
}
357360

sdk-platform-java/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/ChannelPoolSettings.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,57 @@ public static Builder builder() {
158158

159159
@AutoValue.Builder
160160
public abstract static class Builder {
161+
/**
162+
* Sets the minimum desired number of concurrent RPCs per channel.
163+
*
164+
* <p>This ensures channels are adequately utilized. If the average load per channel falls below
165+
* this value, the pool attempts to shrink. The resulting target channel count is a dynamic
166+
* value determined by load and is bounded by {@link #setMinChannelCount} and {@link
167+
* #setMaxChannelCount}.
168+
*/
161169
public abstract Builder setMinRpcsPerChannel(int count);
162170

171+
/**
172+
* Sets the maximum desired number of concurrent RPCs per channel.
173+
*
174+
* <p>This ensures channels do not become overloaded. If the average load per channel exceeds
175+
* this value, the pool attempts to expand. The resulting target channel count is a dynamic
176+
* value determined by load and is bounded by {@link #setMinChannelCount} and {@link
177+
* #setMaxChannelCount}.
178+
*/
163179
public abstract Builder setMaxRpcsPerChannel(int count);
164180

181+
/**
182+
* Sets the minimum number of channels the pool can shrink to.
183+
*
184+
* <p>When resizing, if the calculated resize bounds fall below this minimum configuration, the
185+
* bounds will be clamped to this value. This ensures the pool never shrinks below this absolute
186+
* minimum, even under very low load.
187+
*/
165188
public abstract Builder setMinChannelCount(int count);
166189

190+
/**
191+
* Sets the maximum number of channels the pool can expand to.
192+
*
193+
* <p>When resizing, if the calculated resize bounds exceed this maximum configuration, the
194+
* bounds will be clamped to this value. This ensures the pool never expands above this absolute
195+
* maximum, even under very high load.
196+
*/
167197
public abstract Builder setMaxChannelCount(int count);
168198

199+
/** Sets the initial number of channels in the pool. */
169200
public abstract Builder setInitialChannelCount(int count);
170201

202+
/**
203+
* Sets whether preemptive channel refresh is enabled to prevent channels from becoming idle.
204+
*/
171205
public abstract Builder setPreemptiveRefreshEnabled(boolean enabled);
172206

207+
/**
208+
* Sets the maximum number of channels that can be added or removed in a single resize cycle.
209+
* This acts as a rate limiter to prevent wild fluctuations. The pool resizes periodically
210+
* according to {@link #RESIZE_INTERVAL} (default 1 minute).
211+
*/
173212
public abstract Builder setMaxResizeDelta(int count);
174213

175214
abstract ChannelPoolSettings autoBuild();
@@ -193,9 +232,6 @@ public ChannelPoolSettings build() {
193232
s.getInitialChannelCount() > 0, "Initial channel count must be greater than 0");
194233
Preconditions.checkState(
195234
s.getMaxResizeDelta() > 0, "Max resize delta must be greater than 0");
196-
Preconditions.checkState(
197-
s.getMaxResizeDelta() <= s.getMaxChannelCount(),
198-
"Max resize delta cannot be greater than max channel count");
199235
return s;
200236
}
201237
}

sdk-platform-java/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/ChannelPoolTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,9 @@ void testDoubleRelease() throws Exception {
852852

853853
@Test
854854
void settingsValidationFailsWhenMinChannelsExceedsMaxChannels() {
855-
Assertions.assertThrows(
856-
IllegalStateException.class,
857-
() -> ChannelPoolSettings.builder().setMinChannelCount(2).setMaxChannelCount(1).build());
855+
ChannelPoolSettings.Builder builder =
856+
ChannelPoolSettings.builder().setMinChannelCount(2).setMaxChannelCount(1);
857+
Assertions.assertThrows(IllegalStateException.class, () -> builder.build());
858858
}
859859

860860
@Test

0 commit comments

Comments
 (0)