From 24d65742d97b7cd016b0634981b536ec1955e7b3 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Mon, 16 Nov 2026 15:11:56 +0000 Subject: [PATCH 1/7] fix(bigtable): cap PoolSizer desiredCapacity at maxIdleSessions --- .../data/v2/internal/session/PoolSizer.java | 2 +- .../v2/internal/session/PoolSizerTest.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java index 3e9d166d9ae0..3aea1e6b7e70 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java @@ -84,7 +84,7 @@ public int getScaleDelta() { int desiredIdleSessions = Math.max(Math.min(unboundedDesiredIdleSessions, maxIdleSessions), minIdleSessions); - int desiredCapacity = sessionsInUse + desiredIdleSessions; + int desiredCapacity = Math.min(sessionsInUse + desiredIdleSessions, maxIdleSessions); int eventualCapacity = stats.getExpectedCapacity(); int immediateCapacity = eventualCapacity - stats.getStartingCount(); diff --git a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java index a343c7973ce8..897079f2244c 100644 --- a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java +++ b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java @@ -249,4 +249,26 @@ void testEmptyConfig() { assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isFalse(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isFalse(); } + + @Test + void testDesiredCapacityCappedAtMaxIdleSessions() { + PoolStats stats = new PoolStats(); + AtomicInteger pendingCount = new AtomicInteger(0); + PoolSizer sizer = new PoolSizer(stats, pendingCount::get, DEFAULT_CONFIG); + + // MAX_IDLE_SESSIONS in use + 2 ready -> immediateCapacity = MAX_IDLE_SESSIONS + 2. + // Total desiredCapacity is capped at MAX_IDLE_SESSIONS, so scaleDelta is -2. + stats.getTestHelper().setInUseCount(MAX_IDLE_SESSIONS); + stats.getTestHelper().setReadyCount(2); + stats.getTestHelper().setExpectedCapacity(MAX_IDLE_SESSIONS + 2); + assertThat(sizer.getScaleDelta()).isEqualTo(-2); + + // When MAX_IDLE_SESSIONS are in use and 50 RPCs are queued, scaleDelta must be 0 + // rather than scaling above MAX_IDLE_SESSIONS. + stats.getTestHelper().setReadyCount(0); + stats.getTestHelper().setExpectedCapacity(MAX_IDLE_SESSIONS); + pendingCount.set(50); + assertThat(sizer.getScaleDelta()).isEqualTo(0); + assertThat(sizer.handleNewCall()).isFalse(); + } } From c5f3794bbdabc084040fa84c5209438ac6a91c45 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Mon, 16 Nov 2026 16:29:33 +0000 Subject: [PATCH 2/7] refactor(bigtable): rename PoolSizer.maxIdleSessions to maxSessions --- .../data/v2/internal/session/PoolSizer.java | 10 ++++----- .../v2/internal/session/PoolSizerTest.java | 22 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java index 3aea1e6b7e70..9c6740e9a339 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java @@ -29,7 +29,7 @@ class PoolSizer { // 50% increase in latency (since that also increases requests-in-flight). private volatile float idlesSessionHeadRoom; private volatile int minIdleSessions; - private volatile int maxIdleSessions; + private volatile int maxSessions; private final PoolStats stats; private final Sized pendingRpcs; @@ -60,12 +60,12 @@ interface Sized { void updateConfig(SessionClientConfiguration.SessionPoolConfiguration poolConfiguration) { this.idlesSessionHeadRoom = poolConfiguration.getHeadroom(); this.minIdleSessions = poolConfiguration.getMinSessionCount(); - this.maxIdleSessions = poolConfiguration.getMaxSessionCount(); + this.maxSessions = poolConfiguration.getMaxSessionCount(); this.pendingVRpcsPerSession = poolConfiguration.getNewSessionQueueLength(); this.disabled = poolConfiguration.equals( SessionClientConfiguration.SessionPoolConfiguration.getDefaultInstance()) - || this.maxIdleSessions <= 0; + || this.maxSessions <= 0; } boolean isDisabled() { @@ -82,9 +82,9 @@ public int getScaleDelta() { int sessionsInUse = effectivePending + stats.getInUseCount(); int unboundedDesiredIdleSessions = (int) Math.ceil(sessionsInUse * idlesSessionHeadRoom); int desiredIdleSessions = - Math.max(Math.min(unboundedDesiredIdleSessions, maxIdleSessions), minIdleSessions); + Math.max(Math.min(unboundedDesiredIdleSessions, maxSessions), minIdleSessions); - int desiredCapacity = Math.min(sessionsInUse + desiredIdleSessions, maxIdleSessions); + int desiredCapacity = Math.min(sessionsInUse + desiredIdleSessions, maxSessions); int eventualCapacity = stats.getExpectedCapacity(); int immediateCapacity = eventualCapacity - stats.getStartingCount(); diff --git a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java index 897079f2244c..f7a6df9a4440 100644 --- a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java +++ b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java @@ -31,14 +31,14 @@ class PoolSizerTest { private static final float IDLE_SESSION_HEADROOM_RATIO = 0.5f; private static final int MIN_IDLE_SESSIONS = 5; - private static final int MAX_IDLE_SESSIONS = 400; + private static final int MAX_SESSIONS = 400; private static final int PENDING_RPCS_PER_SESSION = 10; private static final SessionClientConfiguration.SessionPoolConfiguration DEFAULT_CONFIG = SessionClientConfiguration.SessionPoolConfiguration.newBuilder() .setHeadroom(IDLE_SESSION_HEADROOM_RATIO) .setMinSessionCount(MIN_IDLE_SESSIONS) - .setMaxSessionCount(MAX_IDLE_SESSIONS) + .setMaxSessionCount(MAX_SESSIONS) .setNewSessionQueueLength(PENDING_RPCS_PER_SESSION) .build(); @@ -193,7 +193,7 @@ void testEquilibriumWhenStartingWithLoad() { int sessionsNeeded = forPending + stats.getInUseCount(); int desiredIdleSessions = (int) Math.ceil(sessionsNeeded * IDLE_SESSION_HEADROOM_RATIO); desiredIdleSessions = - Math.max(Math.min(desiredIdleSessions, MAX_IDLE_SESSIONS), MIN_IDLE_SESSIONS); + Math.max(Math.min(desiredIdleSessions, MAX_SESSIONS), MIN_IDLE_SESSIONS); int desiredCapacity = sessionsNeeded + desiredIdleSessions; assertThat(sizer.getScaleDelta()) @@ -251,22 +251,22 @@ void testEmptyConfig() { } @Test - void testDesiredCapacityCappedAtMaxIdleSessions() { + void testDesiredCapacityCappedAtMaxSessions() { PoolStats stats = new PoolStats(); AtomicInteger pendingCount = new AtomicInteger(0); PoolSizer sizer = new PoolSizer(stats, pendingCount::get, DEFAULT_CONFIG); - // MAX_IDLE_SESSIONS in use + 2 ready -> immediateCapacity = MAX_IDLE_SESSIONS + 2. - // Total desiredCapacity is capped at MAX_IDLE_SESSIONS, so scaleDelta is -2. - stats.getTestHelper().setInUseCount(MAX_IDLE_SESSIONS); + // MAX_SESSIONS in use + 2 ready -> immediateCapacity = MAX_SESSIONS + 2. + // Total desiredCapacity is capped at MAX_SESSIONS, so scaleDelta is -2. + stats.getTestHelper().setInUseCount(MAX_SESSIONS); stats.getTestHelper().setReadyCount(2); - stats.getTestHelper().setExpectedCapacity(MAX_IDLE_SESSIONS + 2); + stats.getTestHelper().setExpectedCapacity(MAX_SESSIONS + 2); assertThat(sizer.getScaleDelta()).isEqualTo(-2); - // When MAX_IDLE_SESSIONS are in use and 50 RPCs are queued, scaleDelta must be 0 - // rather than scaling above MAX_IDLE_SESSIONS. + // When MAX_SESSIONS are in use and 50 RPCs are queued, scaleDelta must be 0 + // rather than scaling above MAX_SESSIONS. stats.getTestHelper().setReadyCount(0); - stats.getTestHelper().setExpectedCapacity(MAX_IDLE_SESSIONS); + stats.getTestHelper().setExpectedCapacity(MAX_SESSIONS); pendingCount.set(50); assertThat(sizer.getScaleDelta()).isEqualTo(0); assertThat(sizer.handleNewCall()).isFalse(); From dd0dfd710516bae89c0b9fc2f171b3187c3bf60a Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Mon, 16 Nov 2026 17:05:43 +0000 Subject: [PATCH 3/7] refactor(bigtable): rename minIdleSessions to minSessions and cap test desiredCapacity --- .../data/v2/internal/session/PoolSizer.java | 6 +-- .../v2/internal/session/PoolSizerTest.java | 53 +++++++++---------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java index 9c6740e9a339..34f11e2d47b6 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java @@ -28,7 +28,7 @@ class PoolSizer { // example, a headroom of 50% will keep enough sessions to deal with a 50% increase in QPS or a // 50% increase in latency (since that also increases requests-in-flight). private volatile float idlesSessionHeadRoom; - private volatile int minIdleSessions; + private volatile int minSessions; private volatile int maxSessions; private final PoolStats stats; @@ -59,7 +59,7 @@ interface Sized { void updateConfig(SessionClientConfiguration.SessionPoolConfiguration poolConfiguration) { this.idlesSessionHeadRoom = poolConfiguration.getHeadroom(); - this.minIdleSessions = poolConfiguration.getMinSessionCount(); + this.minSessions = poolConfiguration.getMinSessionCount(); this.maxSessions = poolConfiguration.getMaxSessionCount(); this.pendingVRpcsPerSession = poolConfiguration.getNewSessionQueueLength(); this.disabled = @@ -82,7 +82,7 @@ public int getScaleDelta() { int sessionsInUse = effectivePending + stats.getInUseCount(); int unboundedDesiredIdleSessions = (int) Math.ceil(sessionsInUse * idlesSessionHeadRoom); int desiredIdleSessions = - Math.max(Math.min(unboundedDesiredIdleSessions, maxSessions), minIdleSessions); + Math.max(Math.min(unboundedDesiredIdleSessions, maxSessions), minSessions); int desiredCapacity = Math.min(sessionsInUse + desiredIdleSessions, maxSessions); int eventualCapacity = stats.getExpectedCapacity(); diff --git a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java index f7a6df9a4440..e38cd4904941 100644 --- a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java +++ b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java @@ -30,14 +30,14 @@ class PoolSizerTest { private static final float IDLE_SESSION_HEADROOM_RATIO = 0.5f; - private static final int MIN_IDLE_SESSIONS = 5; + private static final int MIN_SESSIONS = 5; private static final int MAX_SESSIONS = 400; private static final int PENDING_RPCS_PER_SESSION = 10; private static final SessionClientConfiguration.SessionPoolConfiguration DEFAULT_CONFIG = SessionClientConfiguration.SessionPoolConfiguration.newBuilder() .setHeadroom(IDLE_SESSION_HEADROOM_RATIO) - .setMinSessionCount(MIN_IDLE_SESSIONS) + .setMinSessionCount(MIN_SESSIONS) .setMaxSessionCount(MAX_SESSIONS) .setNewSessionQueueLength(PENDING_RPCS_PER_SESSION) .build(); @@ -48,7 +48,7 @@ void testInitial() { Sized pendingRpcsCount = () -> 0; PoolSizer sizer = new PoolSizer(stats, pendingRpcsCount, DEFAULT_CONFIG); - assertThat(sizer.getScaleDelta()).isEqualTo(MIN_IDLE_SESSIONS); + assertThat(sizer.getScaleDelta()).isEqualTo(MIN_SESSIONS); assertThat(sizer.handleNewCall()).isTrue(); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isTrue(); @@ -60,17 +60,17 @@ void testTestEquilibrium() { Sized pendingRpcsCount = () -> 0; PoolSizer sizer = new PoolSizer(stats, pendingRpcsCount, DEFAULT_CONFIG); - stats.getTestHelper().setReadyCount(MIN_IDLE_SESSIONS); - stats.getTestHelper().setExpectedCapacity(MIN_IDLE_SESSIONS); + stats.getTestHelper().setReadyCount(MIN_SESSIONS); + stats.getTestHelper().setExpectedCapacity(MIN_SESSIONS); assertThat(sizer.getScaleDelta()).isEqualTo(0); assertThat(sizer.handleNewCall()).isFalse(); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isFalse(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isFalse(); - // After onSessionClosing / onSessionClosed decrements expectedCapacity below MIN_IDLE_SESSIONS: - stats.getTestHelper().setReadyCount(MIN_IDLE_SESSIONS - 1); - stats.getTestHelper().setExpectedCapacity(MIN_IDLE_SESSIONS - 1); + // After onSessionClosing / onSessionClosed decrements expectedCapacity below MIN_SESSIONS: + stats.getTestHelper().setReadyCount(MIN_SESSIONS - 1); + stats.getTestHelper().setExpectedCapacity(MIN_SESSIONS - 1); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isTrue(); } @@ -82,10 +82,10 @@ void testPendingRpcs() { PoolSizer sizer = new PoolSizer(stats, pendingRpcsCount, DEFAULT_CONFIG); Preconditions.checkState( - pendingRpcsCount.getSize() > MIN_IDLE_SESSIONS, + pendingRpcsCount.getSize() > MIN_SESSIONS, "Test requires the pendingCount to be larger than ready count"); - int numReadySessions = MIN_IDLE_SESSIONS; + int numReadySessions = MIN_SESSIONS; stats.getTestHelper().setReadyCount(numReadySessions); stats.getTestHelper().setInUseCount(0); @@ -94,7 +94,7 @@ void testPendingRpcs() { .isEqualTo( (int) Math.ceil((float) pendingRpcsCount.getSize() / PENDING_RPCS_PER_SESSION) - numReadySessions - + MIN_IDLE_SESSIONS); + + MIN_SESSIONS); assertThat(sizer.handleNewCall()).isTrue(); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isTrue(); @@ -110,10 +110,10 @@ void testInUseWithNewCall() { stats.getTestHelper().setInUseCount(1); stats.getTestHelper().setExpectedCapacity(1); - // Ensure that we always have at least MIN_IDLE_SESSIONS sessions available + // Ensure that we always have at least MIN_SESSIONS sessions available // Separately we want to have IDLE_SESSION_HEADROOM_RATIO available sessions, but in this case - // MIN_IDLE_SESSIONS is the primary indicator - assertThat(sizer.getScaleDelta()).isEqualTo(MIN_IDLE_SESSIONS); + // MIN_SESSIONS is the primary indicator + assertThat(sizer.getScaleDelta()).isEqualTo(MIN_SESSIONS); // Remain steady in band assertThat(sizer.handleNewCall()).isTrue(); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); @@ -131,7 +131,7 @@ void testInUseWithNewCall2() { stats.getTestHelper().setExpectedCapacity(20); // Separately we want to have IDLE_SESSION_HEADROOM_RATIO available sessions - // Separately we want to ensure that we always have at least MIN_IDLE_SESSIONS sessions + // Separately we want to ensure that we always have at least MIN_SESSIONS sessions // but in this case the ratio is the primary indicator available assertThat(sizer.getScaleDelta()).isEqualTo(10); // Remain steady in band @@ -146,8 +146,8 @@ void testEquilibriumWhenStarting() { Sized pendingRpcsCount = () -> 0; PoolSizer sizer = new PoolSizer(stats, pendingRpcsCount, DEFAULT_CONFIG); - stats.getTestHelper().setStartingCount(MIN_IDLE_SESSIONS); - stats.getTestHelper().setExpectedCapacity(MIN_IDLE_SESSIONS); + stats.getTestHelper().setStartingCount(MIN_SESSIONS); + stats.getTestHelper().setExpectedCapacity(MIN_SESSIONS); assertThat(sizer.getScaleDelta()).isEqualTo(0); // Remain steady in band @@ -155,9 +155,9 @@ void testEquilibriumWhenStarting() { assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isFalse(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isFalse(); - // After onSessionClosing / onSessionClosed decrements expectedCapacity below MIN_IDLE_SESSIONS: - stats.getTestHelper().setStartingCount(MIN_IDLE_SESSIONS - 1); - stats.getTestHelper().setExpectedCapacity(MIN_IDLE_SESSIONS - 1); + // After onSessionClosing / onSessionClosed decrements expectedCapacity below MIN_SESSIONS: + stats.getTestHelper().setStartingCount(MIN_SESSIONS - 1); + stats.getTestHelper().setExpectedCapacity(MIN_SESSIONS - 1); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isTrue(); } @@ -169,11 +169,11 @@ void testEquilibriumWhenStartingWithLoad() { Sized pendingRpcsCount = () -> pendingCount.get(); PoolSizer sizer = new PoolSizer(stats, pendingRpcsCount, DEFAULT_CONFIG); - assertThat(sizer.getScaleDelta()).isEqualTo(MIN_IDLE_SESSIONS); + assertThat(sizer.getScaleDelta()).isEqualTo(MIN_SESSIONS); - // Started 5 sessions on startup to satisfy PoolSizer.MIN_IDLE_SESSIONS. - stats.getTestHelper().setStartingCount(MIN_IDLE_SESSIONS); - stats.getTestHelper().setExpectedCapacity(MIN_IDLE_SESSIONS); + // Started 5 sessions on startup to satisfy PoolSizer.MIN_SESSIONS. + stats.getTestHelper().setStartingCount(MIN_SESSIONS); + stats.getTestHelper().setExpectedCapacity(MIN_SESSIONS); // Requests start to pile up while we still have our first 5 sessions starting. int incomingRequests = 100; @@ -192,9 +192,8 @@ void testEquilibriumWhenStartingWithLoad() { int forPending = (int) Math.ceil((float) pendingCount.get() / PENDING_RPCS_PER_SESSION); int sessionsNeeded = forPending + stats.getInUseCount(); int desiredIdleSessions = (int) Math.ceil(sessionsNeeded * IDLE_SESSION_HEADROOM_RATIO); - desiredIdleSessions = - Math.max(Math.min(desiredIdleSessions, MAX_SESSIONS), MIN_IDLE_SESSIONS); - int desiredCapacity = sessionsNeeded + desiredIdleSessions; + desiredIdleSessions = Math.max(Math.min(desiredIdleSessions, MAX_SESSIONS), MIN_SESSIONS); + int desiredCapacity = Math.min(sessionsNeeded + desiredIdleSessions, MAX_SESSIONS); assertThat(sizer.getScaleDelta()) .isEqualTo(Math.max(0, desiredCapacity - stats.getExpectedCapacity())); From 7c9c230616e46f8cd8bb58bc3beffe06e4387474 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Mon, 16 Nov 2026 17:35:14 +0000 Subject: [PATCH 4/7] fix(bigtable): clamp total desiredCapacity to [minSessions, maxSessions] --- .../data/v2/internal/session/PoolSizer.java | 7 ++-- .../v2/internal/session/PoolSizerTest.java | 32 ++++++++----------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java index 34f11e2d47b6..9d1d017f4213 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java @@ -80,11 +80,10 @@ public int getScaleDelta() { ? (int) Math.ceil((float) pendingRpcs.getSize() / pendingVRpcsPerSession) : pendingRpcs.getSize(); int sessionsInUse = effectivePending + stats.getInUseCount(); - int unboundedDesiredIdleSessions = (int) Math.ceil(sessionsInUse * idlesSessionHeadRoom); - int desiredIdleSessions = - Math.max(Math.min(unboundedDesiredIdleSessions, maxSessions), minSessions); + int desiredIdleSessions = (int) Math.ceil(sessionsInUse * idlesSessionHeadRoom); - int desiredCapacity = Math.min(sessionsInUse + desiredIdleSessions, maxSessions); + int desiredCapacity = + Math.min(Math.max(sessionsInUse + desiredIdleSessions, minSessions), maxSessions); int eventualCapacity = stats.getExpectedCapacity(); int immediateCapacity = eventualCapacity - stats.getStartingCount(); diff --git a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java index e38cd4904941..3c1255dedbf5 100644 --- a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java +++ b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java @@ -78,7 +78,7 @@ void testTestEquilibrium() { @Test void testPendingRpcs() { PoolStats stats = new PoolStats(); - Sized pendingRpcsCount = () -> 9; + Sized pendingRpcsCount = () -> 39; PoolSizer sizer = new PoolSizer(stats, pendingRpcsCount, DEFAULT_CONFIG); Preconditions.checkState( @@ -90,11 +90,11 @@ void testPendingRpcs() { stats.getTestHelper().setReadyCount(numReadySessions); stats.getTestHelper().setInUseCount(0); stats.getTestHelper().setExpectedCapacity(numReadySessions); + int effectivePending = + (int) Math.ceil((float) pendingRpcsCount.getSize() / PENDING_RPCS_PER_SESSION); + int desiredIdleSessions = (int) Math.ceil(effectivePending * IDLE_SESSION_HEADROOM_RATIO); assertThat(sizer.getScaleDelta()) - .isEqualTo( - (int) Math.ceil((float) pendingRpcsCount.getSize() / PENDING_RPCS_PER_SESSION) - - numReadySessions - + MIN_SESSIONS); + .isEqualTo(effectivePending + desiredIdleSessions - numReadySessions); assertThat(sizer.handleNewCall()).isTrue(); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isTrue(); @@ -110,10 +110,9 @@ void testInUseWithNewCall() { stats.getTestHelper().setInUseCount(1); stats.getTestHelper().setExpectedCapacity(1); - // Ensure that we always have at least MIN_SESSIONS sessions available - // Separately we want to have IDLE_SESSION_HEADROOM_RATIO available sessions, but in this case - // MIN_SESSIONS is the primary indicator - assertThat(sizer.getScaleDelta()).isEqualTo(MIN_SESSIONS); + // Ensure that we always have at least MIN_SESSIONS total sessions in the pool. + // With 1 session in use and expectedCapacity = 1, scaleDelta is MIN_SESSIONS - 1. + assertThat(sizer.getScaleDelta()).isEqualTo(MIN_SESSIONS - 1); // Remain steady in band assertThat(sizer.handleNewCall()).isTrue(); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); @@ -182,24 +181,19 @@ void testEquilibriumWhenStartingWithLoad() { pendingCount.incrementAndGet(); // ... and call handleNewCall() on the sizer. boolean newCallVerdict = sizer.handleNewCall(); - if (i % PENDING_RPCS_PER_SESSION == 0) { - // For 1st, 11th, 21st, 31st etc. pending call we should demand a new session to be started. - assertThat(newCallVerdict).isTrue(); - } else { - assertThat(newCallVerdict).isFalse(); - } int forPending = (int) Math.ceil((float) pendingCount.get() / PENDING_RPCS_PER_SESSION); int sessionsNeeded = forPending + stats.getInUseCount(); int desiredIdleSessions = (int) Math.ceil(sessionsNeeded * IDLE_SESSION_HEADROOM_RATIO); - desiredIdleSessions = Math.max(Math.min(desiredIdleSessions, MAX_SESSIONS), MIN_SESSIONS); - int desiredCapacity = Math.min(sessionsNeeded + desiredIdleSessions, MAX_SESSIONS); + int desiredCapacity = + Math.min(Math.max(sessionsNeeded + desiredIdleSessions, MIN_SESSIONS), MAX_SESSIONS); + assertThat(newCallVerdict).isEqualTo(desiredCapacity > stats.getExpectedCapacity()); assertThat(sizer.getScaleDelta()) .isEqualTo(Math.max(0, desiredCapacity - stats.getExpectedCapacity())); - if (newCallVerdict) { - // Starting new session as instructed by handleNewCall(). + while (sizer.getScaleDelta() > 0) { + // Starting new session as instructed by PoolSizer. stats.getTestHelper().setStartingCount(stats.getStartingCount() + 1); stats.getTestHelper().setExpectedCapacity(stats.getExpectedCapacity() + 1); } From 0c263615e72e8022e60463cd1bdbef2dfbba7219 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Mon, 16 Nov 2026 17:45:50 +0000 Subject: [PATCH 5/7] fix(bigtable): check isDisabled() at start of PoolSizer.getScaleDelta --- .../cloud/bigtable/data/v2/internal/session/PoolSizer.java | 6 +++--- .../bigtable/data/v2/internal/session/PoolSizerTest.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java index 9d1d017f4213..5ce11a3b641b 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java @@ -73,6 +73,9 @@ boolean isDisabled() { } public int getScaleDelta() { + if (isDisabled()) { + return 0; + } // Assume each session handles 1 RPC at a time. This should be revisited if sessions get // support for multiplexing. int effectivePending = @@ -90,9 +93,6 @@ public int getScaleDelta() { if (desiredCapacity < immediateCapacity) { return desiredCapacity - immediateCapacity; } else if (desiredCapacity > eventualCapacity) { - if (isDisabled()) { - return 0; - } return desiredCapacity - eventualCapacity; } else { return 0; diff --git a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java index 3c1255dedbf5..e084a9037c2a 100644 --- a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java +++ b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java @@ -192,8 +192,8 @@ void testEquilibriumWhenStartingWithLoad() { assertThat(sizer.getScaleDelta()) .isEqualTo(Math.max(0, desiredCapacity - stats.getExpectedCapacity())); - while (sizer.getScaleDelta() > 0) { - // Starting new session as instructed by PoolSizer. + if (newCallVerdict) { + // Starting new session as instructed by handleNewCall(). stats.getTestHelper().setStartingCount(stats.getStartingCount() + 1); stats.getTestHelper().setExpectedCapacity(stats.getExpectedCapacity() + 1); } @@ -237,7 +237,7 @@ void testEmptyConfig() { assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isFalse(); stats.getTestHelper().setInUseCount(2); - stats.getTestHelper().setExpectedCapacity(0); + stats.getTestHelper().setExpectedCapacity(2); assertThat(sizer.getScaleDelta()).isEqualTo(0); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isFalse(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isFalse(); From 81cae1c57de30a2e704e880ad08f59e6900146b6 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Mon, 16 Nov 2026 19:59:45 +0000 Subject: [PATCH 6/7] refactor(bigtable): remove unnecessary volatile modifiers in PoolSizer --- .../bigtable/data/v2/internal/session/PoolSizer.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java index 5ce11a3b641b..8dae0a9dceed 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java @@ -27,9 +27,9 @@ class PoolSizer { // Fraction of idle sessions to keep in order to manage an increase in requests-in-flight. For // example, a headroom of 50% will keep enough sessions to deal with a 50% increase in QPS or a // 50% increase in latency (since that also increases requests-in-flight). - private volatile float idlesSessionHeadRoom; - private volatile int minSessions; - private volatile int maxSessions; + private float idlesSessionHeadRoom; + private int minSessions; + private int maxSessions; private final PoolStats stats; private final Sized pendingRpcs; @@ -40,9 +40,9 @@ class PoolSizer { // point reads are expected to complete within 1-4ms. // We assume that it is okay to collect 10 pending calls per starting session so that after // the session is ready it takes 10-40ms to process all pending calls. - private volatile int pendingVRpcsPerSession; + private int pendingVRpcsPerSession; - private volatile boolean disabled; + private boolean disabled; interface Sized { int getSize(); From c27b93e3e52004de618bf85a75552d92c5448a9c Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Mon, 16 Nov 2026 20:13:23 +0000 Subject: [PATCH 7/7] fix(bigtable): throttle session creation on consecutive failures instead of capping healthy pools --- .../data/v2/internal/session/PoolSizer.java | 44 ++++-- .../v2/internal/session/SessionPoolImpl.java | 10 ++ .../v2/internal/session/PoolSizerTest.java | 125 +++++++++++------- 3 files changed, 119 insertions(+), 60 deletions(-) diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java index 8dae0a9dceed..4c339626d913 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizer.java @@ -20,6 +20,7 @@ import com.google.bigtable.v2.SessionClientConfiguration; import com.google.cloud.bigtable.data.v2.internal.session.SessionList.PoolStats; import com.google.rpc.Status; +import java.util.function.IntSupplier; import javax.annotation.CheckReturnValue; class PoolSizer { @@ -28,11 +29,12 @@ class PoolSizer { // example, a headroom of 50% will keep enough sessions to deal with a 50% increase in QPS or a // 50% increase in latency (since that also increases requests-in-flight). private float idlesSessionHeadRoom; - private int minSessions; - private int maxSessions; + private int minIdleSessions; + private int maxIdleSessions; private final PoolStats stats; private final Sized pendingRpcs; + private final IntSupplier consecutiveFailures; // This resolves cold start overprovisioning issue. // Session creation sometimes involves creating new connection and warming the backend. @@ -41,6 +43,7 @@ class PoolSizer { // We assume that it is okay to collect 10 pending calls per starting session so that after // the session is ready it takes 10-40ms to process all pending calls. private int pendingVRpcsPerSession; + private int maxConsecutiveFailures; private boolean disabled; @@ -52,20 +55,30 @@ interface Sized { PoolStats stats, Sized pendingRpcs, SessionClientConfiguration.SessionPoolConfiguration poolConfig) { + this(stats, pendingRpcs, () -> 0, poolConfig); + } + + PoolSizer( + PoolStats stats, + Sized pendingRpcs, + IntSupplier consecutiveFailures, + SessionClientConfiguration.SessionPoolConfiguration poolConfig) { this.stats = stats; this.pendingRpcs = pendingRpcs; + this.consecutiveFailures = consecutiveFailures; updateConfig(poolConfig); } void updateConfig(SessionClientConfiguration.SessionPoolConfiguration poolConfiguration) { this.idlesSessionHeadRoom = poolConfiguration.getHeadroom(); - this.minSessions = poolConfiguration.getMinSessionCount(); - this.maxSessions = poolConfiguration.getMaxSessionCount(); + this.minIdleSessions = poolConfiguration.getMinSessionCount(); + this.maxIdleSessions = poolConfiguration.getMaxSessionCount(); this.pendingVRpcsPerSession = poolConfiguration.getNewSessionQueueLength(); + this.maxConsecutiveFailures = poolConfiguration.getConsecutiveSessionFailureThreshold(); this.disabled = poolConfiguration.equals( SessionClientConfiguration.SessionPoolConfiguration.getDefaultInstance()) - || this.maxSessions <= 0; + || this.maxIdleSessions <= 0; } boolean isDisabled() { @@ -76,23 +89,32 @@ public int getScaleDelta() { if (isDisabled()) { return 0; } + int failures = consecutiveFailures.getAsInt(); // Assume each session handles 1 RPC at a time. This should be revisited if sessions get // support for multiplexing. + // Ignore queued pendingRpcs while session establishment is failing so queued requests do not + // drive unbounded session creation. int effectivePending = - pendingVRpcsPerSession > 0 - ? (int) Math.ceil((float) pendingRpcs.getSize() / pendingVRpcsPerSession) - : pendingRpcs.getSize(); + failures > 0 + ? 0 + : (pendingVRpcsPerSession > 0 + ? (int) Math.ceil((float) pendingRpcs.getSize() / pendingVRpcsPerSession) + : pendingRpcs.getSize()); int sessionsInUse = effectivePending + stats.getInUseCount(); - int desiredIdleSessions = (int) Math.ceil(sessionsInUse * idlesSessionHeadRoom); + int unboundedDesiredIdleSessions = (int) Math.ceil(sessionsInUse * idlesSessionHeadRoom); + int desiredIdleSessions = + Math.max(Math.min(unboundedDesiredIdleSessions, maxIdleSessions), minIdleSessions); - int desiredCapacity = - Math.min(Math.max(sessionsInUse + desiredIdleSessions, minSessions), maxSessions); + int desiredCapacity = sessionsInUse + desiredIdleSessions; int eventualCapacity = stats.getExpectedCapacity(); int immediateCapacity = eventualCapacity - stats.getStartingCount(); if (desiredCapacity < immediateCapacity) { return desiredCapacity - immediateCapacity; } else if (desiredCapacity > eventualCapacity) { + if (maxConsecutiveFailures > 0 && failures >= maxConsecutiveFailures) { + return stats.getStartingCount() == 0 ? 1 : 0; + } return desiredCapacity - eventualCapacity; } else { return 0; diff --git a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/SessionPoolImpl.java b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/SessionPoolImpl.java index c1abfb90232c..98e16561808c 100644 --- a/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/SessionPoolImpl.java +++ b/java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/SessionPoolImpl.java @@ -256,6 +256,7 @@ public SessionPoolImpl( new PoolSizer( sessions.getStats(), pendingRpcs::size, + () -> consecutiveFailures, configManager .getClientConfiguration() .getSessionConfiguration() @@ -597,6 +598,7 @@ private void onSessionReady(SessionHandle handle, OpenSessionResponse ignored) { "Session {0} in state {1}", new Object[] {handle.getSession().getLogName(), handle.getSession().getState()}); + boolean wasFailingToOpen = consecutiveFailures > 0; consecutiveFailures = 0; consecutiveUnimplementedFailures = 0; @@ -618,6 +620,14 @@ private void onSessionReady(SessionHandle handle, OpenSessionResponse ignored) { // handle pending rpcs tryDrainPendingRpcs(); + + if (wasFailingToOpen) { + while (poolSizer.getScaleDelta() > 0) { + if (!createSession(openParams)) { + break; + } + } + } } finally { poolLock.unlock(); } diff --git a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java index e084a9037c2a..08f1003224c7 100644 --- a/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java +++ b/java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/PoolSizerTest.java @@ -30,16 +30,18 @@ class PoolSizerTest { private static final float IDLE_SESSION_HEADROOM_RATIO = 0.5f; - private static final int MIN_SESSIONS = 5; - private static final int MAX_SESSIONS = 400; + private static final int MIN_IDLE_SESSIONS = 5; + private static final int MAX_IDLE_SESSIONS = 400; private static final int PENDING_RPCS_PER_SESSION = 10; + private static final int MAX_CONSECUTIVE_FAILURES = 10; private static final SessionClientConfiguration.SessionPoolConfiguration DEFAULT_CONFIG = SessionClientConfiguration.SessionPoolConfiguration.newBuilder() .setHeadroom(IDLE_SESSION_HEADROOM_RATIO) - .setMinSessionCount(MIN_SESSIONS) - .setMaxSessionCount(MAX_SESSIONS) + .setMinSessionCount(MIN_IDLE_SESSIONS) + .setMaxSessionCount(MAX_IDLE_SESSIONS) .setNewSessionQueueLength(PENDING_RPCS_PER_SESSION) + .setConsecutiveSessionFailureThreshold(MAX_CONSECUTIVE_FAILURES) .build(); @Test @@ -48,7 +50,7 @@ void testInitial() { Sized pendingRpcsCount = () -> 0; PoolSizer sizer = new PoolSizer(stats, pendingRpcsCount, DEFAULT_CONFIG); - assertThat(sizer.getScaleDelta()).isEqualTo(MIN_SESSIONS); + assertThat(sizer.getScaleDelta()).isEqualTo(MIN_IDLE_SESSIONS); assertThat(sizer.handleNewCall()).isTrue(); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isTrue(); @@ -60,17 +62,17 @@ void testTestEquilibrium() { Sized pendingRpcsCount = () -> 0; PoolSizer sizer = new PoolSizer(stats, pendingRpcsCount, DEFAULT_CONFIG); - stats.getTestHelper().setReadyCount(MIN_SESSIONS); - stats.getTestHelper().setExpectedCapacity(MIN_SESSIONS); + stats.getTestHelper().setReadyCount(MIN_IDLE_SESSIONS); + stats.getTestHelper().setExpectedCapacity(MIN_IDLE_SESSIONS); assertThat(sizer.getScaleDelta()).isEqualTo(0); assertThat(sizer.handleNewCall()).isFalse(); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isFalse(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isFalse(); - // After onSessionClosing / onSessionClosed decrements expectedCapacity below MIN_SESSIONS: - stats.getTestHelper().setReadyCount(MIN_SESSIONS - 1); - stats.getTestHelper().setExpectedCapacity(MIN_SESSIONS - 1); + // After onSessionClosing / onSessionClosed decrements expectedCapacity below MIN_IDLE_SESSIONS: + stats.getTestHelper().setReadyCount(MIN_IDLE_SESSIONS - 1); + stats.getTestHelper().setExpectedCapacity(MIN_IDLE_SESSIONS - 1); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isTrue(); } @@ -78,23 +80,23 @@ void testTestEquilibrium() { @Test void testPendingRpcs() { PoolStats stats = new PoolStats(); - Sized pendingRpcsCount = () -> 39; + Sized pendingRpcsCount = () -> 9; PoolSizer sizer = new PoolSizer(stats, pendingRpcsCount, DEFAULT_CONFIG); Preconditions.checkState( - pendingRpcsCount.getSize() > MIN_SESSIONS, + pendingRpcsCount.getSize() > MIN_IDLE_SESSIONS, "Test requires the pendingCount to be larger than ready count"); - int numReadySessions = MIN_SESSIONS; + int numReadySessions = MIN_IDLE_SESSIONS; stats.getTestHelper().setReadyCount(numReadySessions); stats.getTestHelper().setInUseCount(0); stats.getTestHelper().setExpectedCapacity(numReadySessions); - int effectivePending = - (int) Math.ceil((float) pendingRpcsCount.getSize() / PENDING_RPCS_PER_SESSION); - int desiredIdleSessions = (int) Math.ceil(effectivePending * IDLE_SESSION_HEADROOM_RATIO); assertThat(sizer.getScaleDelta()) - .isEqualTo(effectivePending + desiredIdleSessions - numReadySessions); + .isEqualTo( + (int) Math.ceil((float) pendingRpcsCount.getSize() / PENDING_RPCS_PER_SESSION) + - numReadySessions + + MIN_IDLE_SESSIONS); assertThat(sizer.handleNewCall()).isTrue(); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isTrue(); @@ -110,9 +112,10 @@ void testInUseWithNewCall() { stats.getTestHelper().setInUseCount(1); stats.getTestHelper().setExpectedCapacity(1); - // Ensure that we always have at least MIN_SESSIONS total sessions in the pool. - // With 1 session in use and expectedCapacity = 1, scaleDelta is MIN_SESSIONS - 1. - assertThat(sizer.getScaleDelta()).isEqualTo(MIN_SESSIONS - 1); + // Ensure that we always have at least MIN_IDLE_SESSIONS sessions available + // Separately we want to have IDLE_SESSION_HEADROOM_RATIO available sessions, but in this case + // MIN_IDLE_SESSIONS is the primary indicator + assertThat(sizer.getScaleDelta()).isEqualTo(MIN_IDLE_SESSIONS); // Remain steady in band assertThat(sizer.handleNewCall()).isTrue(); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); @@ -130,7 +133,7 @@ void testInUseWithNewCall2() { stats.getTestHelper().setExpectedCapacity(20); // Separately we want to have IDLE_SESSION_HEADROOM_RATIO available sessions - // Separately we want to ensure that we always have at least MIN_SESSIONS sessions + // Separately we want to ensure that we always have at least MIN_IDLE_SESSIONS sessions // but in this case the ratio is the primary indicator available assertThat(sizer.getScaleDelta()).isEqualTo(10); // Remain steady in band @@ -145,8 +148,8 @@ void testEquilibriumWhenStarting() { Sized pendingRpcsCount = () -> 0; PoolSizer sizer = new PoolSizer(stats, pendingRpcsCount, DEFAULT_CONFIG); - stats.getTestHelper().setStartingCount(MIN_SESSIONS); - stats.getTestHelper().setExpectedCapacity(MIN_SESSIONS); + stats.getTestHelper().setStartingCount(MIN_IDLE_SESSIONS); + stats.getTestHelper().setExpectedCapacity(MIN_IDLE_SESSIONS); assertThat(sizer.getScaleDelta()).isEqualTo(0); // Remain steady in band @@ -154,9 +157,9 @@ void testEquilibriumWhenStarting() { assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isFalse(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isFalse(); - // After onSessionClosing / onSessionClosed decrements expectedCapacity below MIN_SESSIONS: - stats.getTestHelper().setStartingCount(MIN_SESSIONS - 1); - stats.getTestHelper().setExpectedCapacity(MIN_SESSIONS - 1); + // After onSessionClosing / onSessionClosed decrements expectedCapacity below MIN_IDLE_SESSIONS: + stats.getTestHelper().setStartingCount(MIN_IDLE_SESSIONS - 1); + stats.getTestHelper().setExpectedCapacity(MIN_IDLE_SESSIONS - 1); assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isTrue(); assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isTrue(); } @@ -168,11 +171,11 @@ void testEquilibriumWhenStartingWithLoad() { Sized pendingRpcsCount = () -> pendingCount.get(); PoolSizer sizer = new PoolSizer(stats, pendingRpcsCount, DEFAULT_CONFIG); - assertThat(sizer.getScaleDelta()).isEqualTo(MIN_SESSIONS); + assertThat(sizer.getScaleDelta()).isEqualTo(MIN_IDLE_SESSIONS); - // Started 5 sessions on startup to satisfy PoolSizer.MIN_SESSIONS. - stats.getTestHelper().setStartingCount(MIN_SESSIONS); - stats.getTestHelper().setExpectedCapacity(MIN_SESSIONS); + // Started 5 sessions on startup to satisfy PoolSizer.MIN_IDLE_SESSIONS. + stats.getTestHelper().setStartingCount(MIN_IDLE_SESSIONS); + stats.getTestHelper().setExpectedCapacity(MIN_IDLE_SESSIONS); // Requests start to pile up while we still have our first 5 sessions starting. int incomingRequests = 100; @@ -181,14 +184,20 @@ void testEquilibriumWhenStartingWithLoad() { pendingCount.incrementAndGet(); // ... and call handleNewCall() on the sizer. boolean newCallVerdict = sizer.handleNewCall(); + if (i % PENDING_RPCS_PER_SESSION == 0) { + // For 1st, 11th, 21st, 31st etc. pending call we should demand a new session to be started. + assertThat(newCallVerdict).isTrue(); + } else { + assertThat(newCallVerdict).isFalse(); + } int forPending = (int) Math.ceil((float) pendingCount.get() / PENDING_RPCS_PER_SESSION); int sessionsNeeded = forPending + stats.getInUseCount(); int desiredIdleSessions = (int) Math.ceil(sessionsNeeded * IDLE_SESSION_HEADROOM_RATIO); - int desiredCapacity = - Math.min(Math.max(sessionsNeeded + desiredIdleSessions, MIN_SESSIONS), MAX_SESSIONS); + desiredIdleSessions = + Math.max(Math.min(desiredIdleSessions, MAX_IDLE_SESSIONS), MIN_IDLE_SESSIONS); + int desiredCapacity = sessionsNeeded + desiredIdleSessions; - assertThat(newCallVerdict).isEqualTo(desiredCapacity > stats.getExpectedCapacity()); assertThat(sizer.getScaleDelta()) .isEqualTo(Math.max(0, desiredCapacity - stats.getExpectedCapacity())); @@ -244,24 +253,42 @@ void testEmptyConfig() { } @Test - void testDesiredCapacityCappedAtMaxSessions() { + void testConsecutiveFailuresThrottlesSessionCreation() { PoolStats stats = new PoolStats(); - AtomicInteger pendingCount = new AtomicInteger(0); - PoolSizer sizer = new PoolSizer(stats, pendingCount::get, DEFAULT_CONFIG); - - // MAX_SESSIONS in use + 2 ready -> immediateCapacity = MAX_SESSIONS + 2. - // Total desiredCapacity is capped at MAX_SESSIONS, so scaleDelta is -2. - stats.getTestHelper().setInUseCount(MAX_SESSIONS); - stats.getTestHelper().setReadyCount(2); - stats.getTestHelper().setExpectedCapacity(MAX_SESSIONS + 2); - assertThat(sizer.getScaleDelta()).isEqualTo(-2); - - // When MAX_SESSIONS are in use and 50 RPCs are queued, scaleDelta must be 0 - // rather than scaling above MAX_SESSIONS. - stats.getTestHelper().setReadyCount(0); - stats.getTestHelper().setExpectedCapacity(MAX_SESSIONS); - pendingCount.set(50); + AtomicInteger pendingCount = new AtomicInteger(200); + AtomicInteger consecutiveFailures = new AtomicInteger(0); + PoolSizer sizer = + new PoolSizer(stats, pendingCount::get, consecutiveFailures::get, DEFAULT_CONFIG); + + // While consecutiveFailures > 0, queued pendingRpcs must not drive scale-up beyond + // MIN_IDLE_SESSIONS. + consecutiveFailures.set(1); + stats.getTestHelper().setStartingCount(MIN_IDLE_SESSIONS); + stats.getTestHelper().setExpectedCapacity(MIN_IDLE_SESSIONS); + assertThat(sizer.getScaleDelta()).isEqualTo(0); + assertThat(sizer.handleNewCall()).isFalse(); + + // Once consecutiveFailures >= MAX_CONSECUTIVE_FAILURES, allow at most 1 STARTING probe session. + consecutiveFailures.set(MAX_CONSECUTIVE_FAILURES); + stats.getTestHelper().setStartingCount(1); + stats.getTestHelper().setExpectedCapacity(1); assertThat(sizer.getScaleDelta()).isEqualTo(0); assertThat(sizer.handleNewCall()).isFalse(); + assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isFalse(); + + // When the last STARTING session closes (startingCount == 0), allow 1 probe session. + stats.getTestHelper().setStartingCount(0); + stats.getTestHelper().setExpectedCapacity(0); + assertThat(sizer.getScaleDelta()).isEqualTo(1); + assertThat(sizer.handleNewCall()).isTrue(); + assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isTrue(); + + // Once a session succeeds and resets consecutiveFailures to 0, full scaling resumes. + consecutiveFailures.set(0); + stats.getTestHelper().setReadyCount(1); + stats.getTestHelper().setExpectedCapacity(1); + int effectivePending = (int) Math.ceil((float) pendingCount.get() / PENDING_RPCS_PER_SESSION); + int desiredIdleSessions = (int) Math.ceil(effectivePending * IDLE_SESSION_HEADROOM_RATIO); + assertThat(sizer.getScaleDelta()).isEqualTo(effectivePending + desiredIdleSessions - 1); } }