Skip to content

Commit 899736f

Browse files
committed
feat(gax-grpc): optimize ChannelPool resize and add thread safety comment
1 parent 827b22d commit 899736f

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

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

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,13 @@ class ChannelPool extends ManagedChannel {
8484
private final AtomicInteger indexTicker = new AtomicInteger();
8585
private final String authority;
8686

87-
// The number of consecutive resize cycles to wait before logging a warning about repeated
88-
// resizing.
89-
// This is an arbitrary value chosen to detect repeated requests for changes (multiple continuous
90-
// increase or decrease attempts) without being too sensitive.
87+
// The number of consecutive resize cycles to wait before logging a warning about repeated resizing.
88+
// This is an arbitrary value chosen to detect repeated requests for changes (multiple continuous increase or decrease attempts) without being too sensitive.
9189
private static final int CONSECUTIVE_RESIZE_THRESHOLD = 5;
9290

93-
// Tracks the number of consecutive resize cycles where a resize actually occurred (either expand
94-
// or shrink).
91+
// Tracks the number of consecutive resize cycles where a resize actually occurred (either expand or shrink).
9592
// Used to detect repeated resizing activity and log a warning.
93+
// Note: This field is only accessed within the synchronized resize() method, so it does not need to be atomic.
9694
private int consecutiveResizes = 0;
9795

9896
static ChannelPool create(
@@ -331,7 +329,7 @@ void resize() {
331329
// We only count as "resized" if the thresholds are crossed and we actually attempt to scale.
332330
// Checking (dampenedTarget != currentSize) would cause false positives when the pool is within
333331
// bounds but not at the target, because the target aims for the middle of the bounds.
334-
boolean resized = (localEntries.size() < minChannels || localEntries.size() > maxChannels);
332+
boolean resized = (currentSize < minChannels || currentSize > maxChannels);
335333
if (resized) {
336334
consecutiveResizes++;
337335
} else {
@@ -341,9 +339,12 @@ void resize() {
341339
// Log warning only once when the threshold is reached to avoid spamming logs.
342340
// Using == instead of >= ensures we don't log on every subsequent resize cycle.
343341
if (consecutiveResizes == CONSECUTIVE_RESIZE_THRESHOLD) {
344-
LOG.warning(
345-
"Channel pool is repeatedly resizing. Consider adjusting `initialChannelCount` or `maxResizeDelta` to a more reasonable value. "
346-
+ "See https://docs.cloud.google.com/java/docs/troubleshooting to enable logging and set `com.google.api.gax.grpc.ChannelPool.level=FINEST` to log the channel pool resize behavior.");
342+
StringBuilder sb = new StringBuilder();
343+
sb.append("Channel pool is repeatedly resizing. ");
344+
sb.append("Consider adjusting `initialChannelCount` or `maxResizeDelta` to a more reasonable value. ");
345+
sb.append("See https://docs.cloud.google.com/java/docs/troubleshooting to enable logging ");
346+
sb.append("and set `com.google.api.gax.grpc.ChannelPool.level=FINEST` to log the channel pool resize behavior.");
347+
LOG.warning(sb.toString());
347348
}
348349

349350
// Only resize the pool when thresholds are crossed

0 commit comments

Comments
 (0)