Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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 = sessionsInUse + desiredIdleSessions;
int desiredCapacity = Math.min(sessionsInUse + desiredIdleSessions, maxSessions);
int eventualCapacity = stats.getExpectedCapacity();
int immediateCapacity = eventualCapacity - stats.getStartingCount();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -249,4 +249,26 @@ void testEmptyConfig() {
assertThat(sizer.handleGoAway(GoAwayResponse.getDefaultInstance())).isFalse();
assertThat(sizer.handleSessionClose(Status.getDefaultInstance())).isFalse();
}

@Test
void testDesiredCapacityCappedAtMaxSessions() {
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);
assertThat(sizer.getScaleDelta()).isEqualTo(0);
assertThat(sizer.handleNewCall()).isFalse();
}
}
Loading