Skip to content

Commit ecfb4ca

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 83ecdc2 commit ecfb4ca

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()
@@ -97,7 +97,7 @@ class ServerSessionPoolSpecification extends Specification {
9797
def 'should pool session'() {
9898
given:
9999
def cluster = Stub(Cluster) {
100-
getDescription() >> connectedDescription
100+
getCurrentDescription() >> connectedDescription
101101
}
102102
def pool = new ServerSessionPool(cluster)
103103
def session = pool.get()
@@ -113,7 +113,7 @@ class ServerSessionPoolSpecification extends Specification {
113113
def 'should prune sessions on release'() {
114114
given:
115115
def cluster = Mock(Cluster) {
116-
getDescription() >> connectedDescription
116+
getCurrentDescription() >> connectedDescription
117117
}
118118
def clock = Stub(ServerSessionPool.Clock) {
119119
millis() >>> [0, 0, // first get
@@ -157,7 +157,7 @@ class ServerSessionPoolSpecification extends Specification {
157157
def 'should prune sessions when getting'() {
158158
given:
159159
def cluster = Mock(Cluster) {
160-
getDescription() >> connectedDescription
160+
getCurrentDescription() >> connectedDescription
161161
}
162162
def clock = Stub(ServerSessionPool.Clock) {
163163
millis() >>> [0, 0, // first get
@@ -186,11 +186,10 @@ class ServerSessionPoolSpecification extends Specification {
186186
def 'should not prune session when timeout is null'() {
187187
given:
188188
def cluster = Stub(Cluster) {
189-
getDescription() >> unconnectedDescription
189+
getCurrentDescription() >> unconnectedDescription
190190
}
191191
def clock = Stub(ServerSessionPool.Clock) {
192-
millis() >>> [0, 0,
193-
MINUTES.toMillis(29) + 1]
192+
millis() >>> [0, 0, 0]
194193
}
195194
def pool = new ServerSessionPool(cluster, clock)
196195
def session = pool.get()
@@ -207,7 +206,7 @@ class ServerSessionPoolSpecification extends Specification {
207206
def 'should initialize session'() {
208207
given:
209208
def cluster = Stub(Cluster) {
210-
getDescription() >> connectedDescription
209+
getCurrentDescription() >> connectedDescription
211210
}
212211
def clock = Stub(ServerSessionPool.Clock) {
213212
millis() >> 42
@@ -235,7 +234,7 @@ class ServerSessionPoolSpecification extends Specification {
235234
getConnection() >> connection
236235
}
237236
def cluster = Mock(Cluster) {
238-
getDescription() >> connectedDescription
237+
getCurrentDescription() >> connectedDescription
239238
}
240239
def pool = new ServerSessionPool(cluster)
241240
// check out sessions up the the endSessions batch size

0 commit comments

Comments
 (0)