Skip to content

Commit 741914b

Browse files
authored
Always specify the known cause when calling ConnectionPool.invalidate (#836)
1 parent feda7e5 commit 741914b

File tree

8 files changed

+16
-37
lines changed

8 files changed

+16
-37
lines changed

driver-core/src/main/com/mongodb/internal/connection/ConnectionPool.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.util.concurrent.TimeUnit;
2727

2828
/**
29-
* An instance of an implementation must be created in the {@linkplain #invalidate() paused} state.
29+
* An instance of an implementation must be created in the {@linkplain #invalidate(Throwable) paused} state.
3030
*/
3131
interface ConnectionPool extends Closeable {
3232
/**
@@ -36,37 +36,30 @@ interface ConnectionPool extends Closeable {
3636

3737
/**
3838
* @param timeout See {@link com.mongodb.internal.Timeout#startNow(long, TimeUnit)}.
39-
* @throws MongoConnectionPoolClearedException If detects that the pool is {@linkplain #invalidate() paused}.
39+
* @throws MongoConnectionPoolClearedException If detects that the pool is {@linkplain #invalidate(Throwable) paused}.
4040
*/
4141
InternalConnection get(long timeout, TimeUnit timeUnit) throws MongoConnectionPoolClearedException;
4242

4343
/**
4444
* Completes the {@code callback} with a {@link MongoConnectionPoolClearedException}
45-
* if detects that the pool is {@linkplain #invalidate() paused}.
45+
* if detects that the pool is {@linkplain #invalidate(Throwable) paused}.
4646
*/
4747
void getAsync(SingleResultCallback<InternalConnection> callback);
4848

4949
/**
5050
* Mark the pool as paused, unblock all threads waiting in {@link #get() get…} methods, unless they are blocked
5151
* doing an IO operation, increment {@linkplain #getGeneration() generation} to lazily clear all connections managed by the pool
5252
* (this is done via {@link #get() get…} and {@linkplain InternalConnection#close() check in} methods, and may also be done
53-
* by a background task). In the {@linkplain #invalidate() paused} state, connections can be created neither in the background
53+
* by a background task). In the paused state, connections can be created neither in the background
5454
* nor via {@link #get() get…} methods.
55-
* If the pool is {@linkplain #invalidate() paused}, the method does nothing except for recording the specified {@code cause}.
55+
* If the pool is paused, the method does nothing except for recording the specified {@code cause}.
5656
*
5757
* @see #ready()
5858
*/
5959
void invalidate(@Nullable Throwable cause);
6060

6161
/**
62-
* Is equivalent to {@link #invalidate(Throwable)} called with {@code null}.
63-
*
64-
* @see #invalidate(Throwable)
65-
*/
66-
void invalidate();
67-
68-
/**
69-
* Unlike {@link #invalidate()}, this method neither marks the pool as paused,
62+
* Unlike {@link #invalidate(Throwable)}, this method neither marks the pool as paused,
7063
* nor affects the {@linkplain #getGeneration() generation}.
7164
*
7265
* @param generation The expected service-specific generation.

driver-core/src/main/com/mongodb/internal/connection/DefaultConnectionPool.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,6 @@ public void invalidate(@Nullable final Throwable cause) {
263263
}
264264
}
265265

266-
@Override
267-
public void invalidate() {
268-
invalidate(null);
269-
}
270-
271266
@Override
272267
public void ready() {
273268
if (stateAndGeneration.ready()) {

driver-core/src/main/com/mongodb/internal/connection/DefaultSdamServerDescriptionManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ public void update(final ServerDescription candidateDescription) {
7979
markedPoolReady = true;
8080
}
8181
updateDescription(candidateDescription);
82-
if (candidateDescription.getException() != null) {
82+
Throwable candidateDescriptionException = candidateDescription.getException();
83+
if (candidateDescriptionException != null) {
8384
assertTrue(newServerType == UNKNOWN);
8485
assertFalse(markedPoolReady);
85-
connectionPool.invalidate();
86+
connectionPool.invalidate(candidateDescriptionException);
8687
}
8788
} finally {
8889
lock.unlock();

driver-core/src/test/functional/com/mongodb/internal/connection/DefaultConnectionPoolTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,11 @@ private static void useConcurrently(final DefaultConnectionPool pool, final int
465465
assertTrue(invalidateAndReadyProb >= 0 && invalidateAndReadyProb <= 1);
466466
Runnable spontaneouslyInvalidateReady = () -> {
467467
if (ThreadLocalRandom.current().nextFloat() < invalidateAndReadyProb) {
468-
pool.invalidate();
468+
pool.invalidate(null);
469469
pool.ready();
470470
}
471471
if (ThreadLocalRandom.current().nextFloat() < invalidateProb) {
472-
pool.invalidate();
472+
pool.invalidate(null);
473473
}
474474
if (ThreadLocalRandom.current().nextFloat() < readyProb) {
475475
pool.ready();

driver-core/src/test/unit/com/mongodb/internal/connection/AbstractConnectionPoolTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void shouldPassAllOutcomes() throws Exception {
239239
.longValue();
240240
listener.waitForEvent(eventClass, operation.getNumber("count").intValue(), timeoutMillis, TimeUnit.MILLISECONDS);
241241
} else if (name.equals("clear")) {
242-
pool.invalidate();
242+
pool.invalidate(null);
243243
} else if (name.equals("ready")) {
244244
pool.ready();
245245
} else if (name.equals("close")) {
@@ -582,11 +582,6 @@ public void invalidate(@Nullable final Throwable cause) {
582582
pool.invalidate(cause);
583583
}
584584

585-
@Override
586-
public void invalidate() {
587-
pool.invalidate();
588-
}
589-
590585
@Override
591586
public void invalidate(final ObjectId serviceId, final int generation) {
592587
pool.invalidate(serviceId, generation);

driver-core/src/test/unit/com/mongodb/internal/connection/DefaultConnectionPoolSpecification.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class DefaultConnectionPoolSpecification extends Specification {
152152
connectionFactory.createdConnections.get(0).opened() // if the first one is opened, they all should be
153153

154154
when: 'the pool is invalidated and the maintenance tasks runs'
155-
pool.invalidate()
155+
pool.invalidate(null)
156156
pool.ready()
157157
pool.doMaintenance()
158158
//not cool - but we have no way of being notified that the maintenance task has finished
@@ -410,7 +410,7 @@ class DefaultConnectionPoolSpecification extends Specification {
410410
when:
411411
pool.ready()
412412
pool.ready()
413-
pool.invalidate()
413+
pool.invalidate(null)
414414
pool.invalidate(new RuntimeException())
415415

416416
then:
@@ -504,7 +504,7 @@ class DefaultConnectionPoolSpecification extends Specification {
504504
pool.close()
505505
506506
when:
507-
pool.invalidate()
507+
pool.invalidate(null)
508508
509509
then:
510510
noExceptionThrown()

driver-core/src/test/unit/com/mongodb/internal/connection/DefaultServerSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class DefaultServerSpecification extends Specification {
179179
server.invalidate()
180180

181181
then:
182-
0 * connectionPool.invalidate()
182+
0 * connectionPool.invalidate(null)
183183
0 * serverMonitor.connect()
184184
}
185185

driver-core/src/test/unit/com/mongodb/internal/connection/TestConnectionPool.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,6 @@ public void invalidate(@Nullable final Throwable cause) {
159159
generation++;
160160
}
161161

162-
@Override
163-
public void invalidate() {
164-
invalidate(null);
165-
}
166-
167162
@Override
168163
public void invalidate(final ObjectId serviceId, final int generation) {
169164
throw new UnsupportedOperationException();

0 commit comments

Comments
 (0)