Skip to content
Open
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
16 changes: 16 additions & 0 deletions driver-core/src/main/com/mongodb/MongoException.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ public class MongoException extends RuntimeException {
*/
public static final String UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL = "UnknownTransactionCommitResult";

/**
* An error label indicating that the server is overloaded.
*
* @see #hasErrorLabel(String)
* @since 5.7
*/
public static final String SYSTEM_OVERLOADED_ERROR_LABEL = "SystemOverloadedError";

/**
* An error label indicating that the operation is safely retryable.
*
* @see #hasErrorLabel(String)
* @since 5.7
*/
public static final String RETRYABLE_ERROR_LABEL = "RetryableError";

private static final long serialVersionUID = -4415279469780082174L;

private final int code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.mongodb.internal.connection;

import com.mongodb.MongoException;
import com.mongodb.annotations.ThreadSafe;
import com.mongodb.connection.ClusterConnectionMode;
import com.mongodb.connection.ServerDescription;
Expand Down Expand Up @@ -137,9 +138,27 @@ private void handleException(final SdamIssue sdamIssue, final boolean beforeHand
serverMonitor.connect();
} else if (sdamIssue.relatedToNetworkNotTimeout()
|| (beforeHandshake && (sdamIssue.relatedToNetworkTimeout() || sdamIssue.relatedToAuth()))) {
updateDescription(sdamIssue.serverDescription());
connectionPool.invalidate(sdamIssue.exception().orElse(null));
serverMonitor.cancelCurrentCheck();
// Backpressure spec: Don't clear pool or mark server unknown for connection establishment failures
// (network errors or timeouts during handshake). Authentication errors after handshake should still
// clear the pool as they're not related to overload.
// TLS configuration errors (certificate validation, protocol mismatches) should also clear the pool
// as they indicate configuration issues, not server overload.
if (beforeHandshake && !sdamIssue.relatedToAuth() && !sdamIssue.relatedToTlsConfigurationError()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently we attach SystemOverloadedError and RetryableError labels in the SDAM error-handling path (effectively only for DefaultServer). In load-balanced mode, SDAM isn’t involved: the LB code path invalidates the pool directly (e.g., connectionPool.invalidate(serviceId, generation)), so the labeling logic is bypassed.

This means users running the driver in LB mode (behind an NLB) can still hit network errors, TLS handshake failures, timeouts during connection establishment or hello, but won’t get the labels.

However, these labels are a CMAP requirement, not SDAM. The CMAP spec states: “The pool MUST add the error labels SystemOverloadedError and RetryableError to network errors or network timeouts it encounters during the connection establishment or the hello message.”

Since this is defined as a pool behavior (topology-agnostic), it seems we should implement the labeling in the connection pool layer so it applies consistently in both default and load-balanced modes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently we don’t distinguish DNS lookup failures (UnknownHostException) from other connection-establishment network errors. As a result, a DNS failure goes through the generic path (same as connection reset/timeout) and gets SystemOverloadedError/RetryableError labels.

The CMAP spec excludes DNS failures from backpressure labeling: `“For errors that the driver can distinguish as never occurring due to server overload, such as DNS lookup failures […] the driver MUST NOT add backpressure error labels for these error types.”.

Proposed change: detect DNS failure by walking the exception cause chain for UnknownHostException (it’s wrapped as MongoSocketException from ServerAddressHelper.getSocketAddresses()), and when present, skipping backpressure label attachment so SDAM follows the normal path (clear the pool and mark the server Unknown).

In that case, we should add coverage to assert that labeling and pool clearing behaviour. If the driver ever changes the wrapper exception type MongoSocketException (or stops wrapping UnknownHostException this way) and starts adding labels the test should fail.

// Don't update server description to Unknown
// Don't invalidate the connection pool
// Apply error labels for backpressure
sdamIssue.exception().ifPresent(exception -> {
if (exception instanceof MongoException) {
MongoException mongoException = (MongoException) exception;
mongoException.addLabel(MongoException.SYSTEM_OVERLOADED_ERROR_LABEL);
mongoException.addLabel(MongoException.RETRYABLE_ERROR_LABEL);
}
});
} else {
updateDescription(sdamIssue.serverDescription());
connectionPool.invalidate(sdamIssue.exception().orElse(null));
serverMonitor.cancelCurrentCheck();
}
} else if (sdamIssue.relatedToWriteConcern() || sdamIssue.relatedToStalePrimary()) {
updateDescription(sdamIssue.serverDescription());
serverMonitor.connect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
import com.mongodb.connection.TopologyVersion;
import com.mongodb.lang.Nullable;

import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLProtocolException;
import java.security.cert.CertPathBuilderException;
import java.security.cert.CertPathValidatorException;
import java.security.cert.CertificateException;
import java.util.Optional;

import static com.mongodb.assertions.Assertions.assertNotNull;
Expand Down Expand Up @@ -162,6 +168,49 @@ boolean relatedToWriteConcern() {
return exception instanceof MongoWriteConcernWithResponseException;
}

/**
* Checks if the exception is related to TLS configuration errors that are NOT due to server overload.
* These include certificate validation failures, protocol mismatches, etc.
*
* @return true if this is a TLS configuration error (not network-related)
*/
boolean relatedToTlsConfigurationError() {
if (!(exception instanceof MongoSocketException)) {
return false;
}
Throwable cause = exception.getCause();
while (cause != null) {
// Check for various certificate validation and TLS configuration errors
if (cause instanceof CertificateException
|| cause instanceof CertPathBuilderException
|| cause instanceof CertPathValidatorException
|| cause instanceof SSLPeerUnverifiedException
|| cause instanceof SSLProtocolException) {
return true;
}

// SSLHandshakeException can be either network or config, so we check the message
if (cause instanceof SSLHandshakeException) {
String message = cause.getMessage();
if (message != null) {
String lowerMessage = message.toLowerCase();
// These indicate configuration issues, not network issues
if (lowerMessage.contains("certificate")
|| lowerMessage.contains("verify")
|| lowerMessage.contains("trust")
|| lowerMessage.contains("hostname")
|| lowerMessage.contains("protocol")
|| lowerMessage.contains("cipher")
|| lowerMessage.contains("handshake_failure")) {
return true;
}
}
}
cause = cause.getCause();
}
return false;
}

private static boolean stale(@Nullable final Throwable t, final ServerDescription currentServerDescription) {
return TopologyVersionHelper.topologyVersion(t)
.map(candidateTopologyVersion -> TopologyVersionHelper.newerOrEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@ class DefaultServerSpecification extends Specification {
]
}

def 'failed open should invalidate the server'() {
def 'network error should not invalidate the pool'() {
given:
def connectionPool = Mock(ConnectionPool)
connectionPool.get(_) >> { throw exceptionToThrow }
connectionPool.get(_) >> {
throw exceptionToThrow
}
def serverMonitor = Mock(ServerMonitor)
def server = defaultServer(connectionPool, serverMonitor)

Expand All @@ -247,8 +249,8 @@ class DefaultServerSpecification extends Specification {
then:
def e = thrown(MongoException)
e.is(exceptionToThrow)
1 * connectionPool.invalidate(exceptionToThrow)
1 * serverMonitor.cancelCurrentCheck()
0 * connectionPool.invalidate(_)
0 * serverMonitor.cancelCurrentCheck()

where:
exceptionToThrow << [
Expand Down Expand Up @@ -281,7 +283,7 @@ class DefaultServerSpecification extends Specification {
]
}

def 'failed open should invalidate the server asynchronously'() {
def 'failed open should not invalidate the pool asynchronously'() {
given:
def connectionPool = Mock(ConnectionPool)
connectionPool.getAsync(_, _) >> { it.last().onResult(null, exceptionToThrow) }
Expand All @@ -301,8 +303,8 @@ class DefaultServerSpecification extends Specification {
then:
!receivedConnection
receivedThrowable.is(exceptionToThrow)
1 * connectionPool.invalidate(exceptionToThrow)
1 * serverMonitor.cancelCurrentCheck()
0 * connectionPool.invalidate(exceptionToThrow)
0 * serverMonitor.cancelCurrentCheck()


where:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.mongodb.ClusterFixture;
import com.mongodb.MongoClientSettings;
import com.mongodb.event.ConnectionCheckOutFailedEvent;
import com.mongodb.event.ConnectionPoolClearedEvent;
import com.mongodb.event.ConnectionPoolListener;
import com.mongodb.event.ConnectionPoolReadyEvent;
Expand Down Expand Up @@ -47,7 +48,10 @@
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

import static com.mongodb.ClusterFixture.configureFailPoint;
import static com.mongodb.ClusterFixture.disableFailPoint;
Expand Down Expand Up @@ -268,6 +272,79 @@ public void shouldEmitHeartbeatStartedBeforeSocketIsConnected() {
// As it requires mocking and package access to `com.mongodb.internal.connection`
}

/**
* See
* <a href="https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring-tests.md#connection-pool-backpressure">Connection Pool Backpressure</a>.
*/
@Test
public void testConnectionPoolBackpressure() throws InterruptedException {
assumeTrue(serverVersionAtLeast(7, 0));

AtomicInteger connectionCheckOutFailedEventCount = new AtomicInteger(0);
AtomicInteger poolClearedEventCount = new AtomicInteger(0);

ConnectionPoolListener connectionPoolListener = new ConnectionPoolListener() {
@Override
public void connectionCheckOutFailed(final ConnectionCheckOutFailedEvent event) {
connectionCheckOutFailedEventCount.incrementAndGet();
}

@Override
public void connectionPoolCleared(final ConnectionPoolClearedEvent event) {
poolClearedEventCount.incrementAndGet();
}
};
Comment on lines +283 to +296
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of introducing a new anonymous listener with counters, we can reuse the existing test listener:

TestConnectionPoolListener connectionPoolListener = new TestConnectionPoolListener();

It already provides await helpers that double as assertions and helpers to assert that zero PoolClearedEvents happened, e.g.:

connectionPoolListener.waitForEvent(ConnectionPoolClearedEvent.class, 1, 110, SECONDS);

This keeps the test more concise and reuses established utilities for clarity/consistency.


MongoClientSettings clientSettings = getMongoClientSettingsBuilder()
.applyToConnectionPoolSettings(builder -> builder
.maxConnecting(100)
.addConnectionPoolListener(connectionPoolListener))
.build();

try (MongoClient adminClient = MongoClients.create(getMongoClientSettingsBuilder().build());
MongoClient client = MongoClients.create(clientSettings)) {

MongoDatabase adminDatabase = adminClient.getDatabase("admin");
MongoDatabase database = client.getDatabase(getDefaultDatabaseName());
MongoCollection<Document> collection = database.getCollection("testCollection");

// Configure rate limiter using admin commands
adminDatabase.runCommand(new Document("setParameter", 1)
.append("ingressConnectionEstablishmentRateLimiterEnabled", true));
adminDatabase.runCommand(new Document("setParameter", 1)
.append("ingressConnectionEstablishmentRatePerSec", 20));
adminDatabase.runCommand(new Document("setParameter", 1)
.append("ingressConnectionEstablishmentBurstCapacitySecs", 1));
adminDatabase.runCommand(new Document("setParameter", 1)
.append("ingressConnectionEstablishmentMaxQueueDepth", 1));

collection.insertOne(Document.parse("{}"));

// Run 100 parallel find operations with 2-seconds sleep
ExecutorService executor = Executors.newFixedThreadPool(100);
for (int i = 0; i < 100; i++) {
executor.submit(() -> collection.find(new Document("$where", "function() { sleep(2000); return true; }")).first());
}

// Wait for all operations to complete
executor.shutdown();
boolean terminated = executor.awaitTermination(20, SECONDS);
assertTrue("Executor did not terminate within timeout", terminated);

// Assert at least 10 ConnectionCheckOutFailedEvents occurred
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this comment? The assertion message already explains the intent (e.g., “Expected at least 10 ConnectionCheckOutFailedEvents, but got …”).

assertTrue("Expected at least 10 ConnectionCheckOutFailedEvents, but got " + connectionCheckOutFailedEventCount.get(),
connectionCheckOutFailedEventCount.get() >= 10);

// Assert 0 PoolClearedEvents occurred
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertEquals("Expected 0 PoolClearedEvents", 0, poolClearedEventCount.get());

// Teardown: sleep 1 second and reset rate limiter
Thread.sleep(1000);
adminDatabase.runCommand(new Document("setParameter", 1)
.append("ingressConnectionEstablishmentRateLimiterEnabled", false));
Comment on lines +341 to +344
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cleanup is currently conditional on the code above completing successfully. If an assertion or exception happens earlier, this teardown won’t run, which can leak state into subsequent tests.

We should move it this cleanup into @AfterEach/afterEach so it runs reliably regardless of how the test exits.

}
}

private static void assertPoll(final BlockingQueue<?> queue, @Nullable final Class<?> allowed, final Set<Class<?>> required)
throws InterruptedException {
assertPoll(queue, allowed, required, Timeout.expiresIn(TEST_WAIT_TIMEOUT_MILLIS, MILLISECONDS, ZERO_DURATION_MEANS_EXPIRED));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,16 @@ private static boolean serverDescriptionChangedEventMatches(final BsonDocument e
switch (newType) {
case "Unknown":
return event.getNewDescription().getType() == ServerType.UNKNOWN;
case "LoadBalancer": {
case "LoadBalancer":
return event.getNewDescription().getType() == ServerType.LOAD_BALANCER;
}
case "Mongos":
return event.getNewDescription().getType() == ServerType.SHARD_ROUTER;
case "Standalone":
return event.getNewDescription().getType() == ServerType.STANDALONE;
case "RSPrimary":
return event.getNewDescription().getType() == ServerType.REPLICA_SET_PRIMARY;
case "RSSecondary":
return event.getNewDescription().getType() == ServerType.REPLICA_SET_SECONDARY;
default:
throw new UnsupportedOperationException();
}
Expand Down