Skip to content

Commit 420da30

Browse files
committed
JAVA-2859: Use current cluster description for server session pruning
By replacing a call to Cluster.getDescription to Cluster.getDescription when evaluating whether to prune a server session, the driver avoids potentially blocking and timing out if the cluster description is unavailable (as when it loses connections to all servers in the cluster)
1 parent 84b5a34 commit 420da30

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

driver-core/src/main/com/mongodb/internal/session/ServerSessionPool.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ private void endClosedSessions() {
132132
}
133133

134134
private boolean shouldPrune(final ServerSessionImpl serverSession) {
135-
Integer logicalSessionTimeoutMinutes = cluster.getDescription().getLogicalSessionTimeoutMinutes();
135+
Integer logicalSessionTimeoutMinutes = cluster.getCurrentDescription().getLogicalSessionTimeoutMinutes();
136+
// if the server no longer supports sessions, prune the session
136137
if (logicalSessionTimeoutMinutes == null) {
137138
return false;
138139
}

driver-core/src/test/unit/com/mongodb/internal/session/ServerSessionPoolSpecification.groovy

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class ServerSessionPoolSpecification extends Specification {
6767
def 'should get session'() {
6868
given:
6969
def cluster = Stub(Cluster) {
70-
getDescription() >> connectedDescription
70+
getCurrentDescription() >> connectedDescription
7171
}
7272
def pool = new ServerSessionPool(cluster)
7373

@@ -81,7 +81,7 @@ class ServerSessionPoolSpecification extends Specification {
8181
def 'should throw IllegalStateException if pool is closed'() {
8282
given:
8383
def cluster = Stub(Cluster) {
84-
getDescription() >> connectedDescription
84+
getCurrentDescription() >> connectedDescription
8585
}
8686
def pool = new ServerSessionPool(cluster)
8787
pool.close()
@@ -96,7 +96,7 @@ class ServerSessionPoolSpecification extends Specification {
9696
def 'should pool session'() {
9797
given:
9898
def cluster = Stub(Cluster) {
99-
getDescription() >> connectedDescription
99+
getCurrentDescription() >> connectedDescription
100100
}
101101
def pool = new ServerSessionPool(cluster)
102102
def session = pool.get()
@@ -112,7 +112,7 @@ class ServerSessionPoolSpecification extends Specification {
112112
def 'should prune sessions on release'() {
113113
given:
114114
def cluster = Mock(Cluster) {
115-
getDescription() >> connectedDescription
115+
getCurrentDescription() >> connectedDescription
116116
}
117117
def clock = Stub(ServerSessionPool.Clock) {
118118
millis() >>> [0, 0, // first get
@@ -156,7 +156,7 @@ class ServerSessionPoolSpecification extends Specification {
156156
def 'should prune sessions when getting'() {
157157
given:
158158
def cluster = Mock(Cluster) {
159-
getDescription() >> connectedDescription
159+
getCurrentDescription() >> connectedDescription
160160
}
161161
def clock = Stub(ServerSessionPool.Clock) {
162162
millis() >>> [0, 0, // first get
@@ -185,11 +185,10 @@ class ServerSessionPoolSpecification extends Specification {
185185
def 'should not prune session when timeout is null'() {
186186
given:
187187
def cluster = Stub(Cluster) {
188-
getDescription() >> unconnectedDescription
188+
getCurrentDescription() >> unconnectedDescription
189189
}
190190
def clock = Stub(ServerSessionPool.Clock) {
191-
millis() >>> [0, 0,
192-
MINUTES.toMillis(29) + 1]
191+
millis() >>> [0, 0, 0]
193192
}
194193
def pool = new ServerSessionPool(cluster, clock)
195194
def session = pool.get()
@@ -205,7 +204,7 @@ class ServerSessionPoolSpecification extends Specification {
205204
def 'should initialize session'() {
206205
given:
207206
def cluster = Stub(Cluster) {
208-
getDescription() >> connectedDescription
207+
getCurrentDescription() >> connectedDescription
209208
}
210209
def clock = Stub(ServerSessionPool.Clock) {
211210
millis() >> 42
@@ -233,7 +232,7 @@ class ServerSessionPoolSpecification extends Specification {
233232
getConnection() >> connection
234233
}
235234
def cluster = Mock(Cluster) {
236-
getDescription() >> connectedDescription
235+
getCurrentDescription() >> connectedDescription
237236
}
238237
def pool = new ServerSessionPool(cluster)
239238
// check out sessions up the the endSessions batch size

0 commit comments

Comments
 (0)