SERVER-103650: Add FTDC metrics for reconnect attempts in DBClientSession (#51054)

GitOrigin-RevId: 8fec0f1ac9d74c783c0645a0254f37607cf9ac09
This commit is contained in:
Cole Harbeck 2026-04-02 13:04:08 -04:00 committed by MongoDB Bot
parent ebc29a9614
commit bace90bea7
3 changed files with 61 additions and 8 deletions

View File

@ -0,0 +1,38 @@
/**
* Test that reconnect metrics for DBClientSession exist and are appropriately incremented.
*
* This test cannot be run on older binaries because the metrics do not exist yet.
* * @tags: [multiversion_incompatible]
*/
import {ReplSetTest} from "jstests/libs/replsettest.js";
function getMetrics(conn) {
const status = assert.commandWorked(conn.adminCommand({serverStatus: 1}));
return Object.fromEntries([
'dbClientSessionReconnectAttempts',
'dbClientSessionWithoutAutoReconnectFailures',
].map(name => [name, status.metrics.network[name]]));
}
const rst = new ReplSetTest(
{name: 'testSet', useBridge: true, nodes: 3, settings: {chainingAllowed: false}});
const nodes = rst.startSet();
rst.initiate();
const primary = rst.getPrimary();
rst.awaitSecondaryNodes();
const secondary = rst.getSecondaries()[0];
const before_disconnect = getMetrics(secondary);
// dbClientSessionWithoutAutoReconnectFailures should never be incremented because the connection
// created by the oplog fetcher is always created with autoReconnect = true.
assert.eq(before_disconnect.dbClientSessionWithoutAutoReconnectFailures, 0);
secondary.disconnect(primary);
assert.soon(() => {
const after_disconnect = getMetrics(secondary);
return after_disconnect.dbClientSessionReconnectAttempts >
before_disconnect.dbClientSessionReconnectAttempts;
});
rst.stopSet();

View File

@ -88,6 +88,16 @@
namespace mongo {
namespace {
// Tracks the number of times DbClientSession detects that the connection has been broken in
// ensureConnection() and attempts to reconnect.
auto& dbClientSessionReconnectAttempts =
*MetricBuilder<Counter64>("network.dbClientSessionReconnectAttempts");
// Tracks the number of times DbClientSession detects that the connection has been broken in
// ensureConnection() with autoReconnect = false so does not try to reconnect.
auto& dbClientSessionWithoutAutoReconnectFailures =
*MetricBuilder<Counter64>("network.dbClientSessionWithoutAutoReconnectFailures");
} // namespace
using std::string;
@ -579,4 +589,16 @@ bool DBClientSession::isTLS() {
}
#endif
void DBClientSession::ensureConnection() {
if (!_failed.load()) {
return;
}
if (!_autoReconnect) {
dbClientSessionWithoutAutoReconnectFailures.increment();
throwSocketError(SocketErrorKind::FAILED_STATE, toString());
}
dbClientSessionReconnectAttempts.increment();
_reconnectSession();
}
} // namespace mongo

View File

@ -213,14 +213,7 @@ public:
}
// Throws a NetworkException if in failed state and not reconnecting or if waiting to reconnect.
void ensureConnection() override {
if (_failed.load()) {
if (!_autoReconnect) {
throwSocketError(SocketErrorKind::FAILED_STATE, toString());
}
_reconnectSession();
}
}
void ensureConnection() override;
bool isReplicaSetMember() const override {
return _isReplicaSetMember;