diff --git a/src/sdam/topology.ts b/src/sdam/topology.ts index 4da824d059a..02077a4f069 100644 --- a/src/sdam/topology.ts +++ b/src/sdam/topology.ts @@ -46,7 +46,6 @@ import { makeStateMachine, noop, now, - ns, promiseWithResolvers, shuffle } from '../utils'; @@ -459,7 +458,7 @@ export class Topology extends TypedEventEmitter { waitQueueTimeoutMS: this.client.s.options.waitQueueTimeoutMS }); const selectServerOptions = { - operationName: 'ping', + operationName: 'handshake', ...options, timeoutContext }; @@ -469,9 +468,11 @@ export class Topology extends TypedEventEmitter { readPreferenceServerSelector(readPreference), selectServerOptions ); + const skipPingOnConnect = this.s.options.__skipPingOnConnect === true; if (!skipPingOnConnect && this.s.credentials) { - await server.command(ns('admin.$cmd'), { ping: 1 }, { timeoutContext }); + const connection = await server.pool.checkOut({ timeoutContext: timeoutContext }); + server.pool.checkIn(connection); stateTransition(this, STATE_CONNECTED); this.emit(Topology.OPEN, this); this.emit(Topology.CONNECT, this); diff --git a/test/integration/node-specific/abort_signal.test.ts b/test/integration/node-specific/abort_signal.test.ts index 40ad3b3414d..c128156f2e2 100644 --- a/test/integration/node-specific/abort_signal.test.ts +++ b/test/integration/node-specific/abort_signal.test.ts @@ -631,7 +631,7 @@ describe('AbortSignal support', () => { mongodbLogComponentSeverities: { serverSelection: 'debug' }, mongodbLogPath: { write: log => { - if (log.c === 'serverSelection' && log.operation === 'ping') { + if (log.c === 'serverSelection' && log.operation === 'handshake') { controller.abort(); promise.resolve(); } @@ -676,7 +676,7 @@ describe('AbortSignal support', () => { mongodbLogComponentSeverities: { serverSelection: 'debug' }, mongodbLogPath: { write: log => { - if (log.c === 'serverSelection' && log.operation === 'ping') { + if (log.c === 'serverSelection' && log.operation === 'handshake') { controller.abort(); promise.resolve(); } diff --git a/test/integration/node-specific/auto_connect.test.ts b/test/integration/node-specific/auto_connect.test.ts index 3e56b69fbef..f0850049632 100644 --- a/test/integration/node-specific/auto_connect.test.ts +++ b/test/integration/node-specific/auto_connect.test.ts @@ -13,7 +13,7 @@ import { Topology, TopologyType } from '../../mongodb'; -import { type FailPoint, sleep } from '../../tools/utils'; +import { sleep } from '../../tools/utils'; describe('When executing an operation for the first time', () => { let client: MongoClient; @@ -842,32 +842,6 @@ describe('When executing an operation for the first time', () => { }); }); - describe( - 'when the server requires auth and ping is delayed', - { requires: { auth: 'enabled', mongodb: '>=4.4' } }, - function () { - beforeEach(async function () { - // set failpoint to delay ping - // create new util client to avoid affecting the test client - const utilClient = this.configuration.newClient(); - await utilClient.db('admin').command({ - configureFailPoint: 'failCommand', - mode: { times: 1 }, - data: { failCommands: ['ping'], blockConnection: true, blockTimeMS: 1000 } - } as FailPoint); - await utilClient.close(); - }); - - it('timeoutMS from the client is not used for the internal `ping`', async function () { - const start = performance.now(); - const returnedClient = await client.connect(); - const end = performance.now(); - expect(returnedClient).to.equal(client); - expect(end - start).to.be.within(1000, 1500); // timeoutMS is 1000, did not apply. - }); - } - ); - describe( 'when server selection takes longer than the timeout', { requires: { auth: 'enabled', mongodb: '>=4.4' } }, diff --git a/test/integration/node-specific/mongo_client.test.ts b/test/integration/node-specific/mongo_client.test.ts index dd3d012fdb8..9e0394013cd 100644 --- a/test/integration/node-specific/mongo_client.test.ts +++ b/test/integration/node-specific/mongo_client.test.ts @@ -18,7 +18,7 @@ import { ServerDescription, Topology } from '../../mongodb'; -import { clearFailPoint, configureFailPoint, runLater } from '../../tools/utils'; +import { clearFailPoint, configureFailPoint } from '../../tools/utils'; import { setupDatabase } from '../shared'; describe('class MongoClient', function () { @@ -588,30 +588,27 @@ describe('class MongoClient', function () { }); it( - 'creates topology and send ping when auth is enabled', + 'creates topology and checks out connection when auth is enabled', { requires: { auth: 'enabled' } }, async function () { - const commandToBeStarted = once(client, 'commandStarted'); + const checkoutStarted = once(client, 'connectionCheckOutStarted'); await client.connect(); - const [pingOnConnect] = await commandToBeStarted; - expect(pingOnConnect).to.have.property('commandName', 'ping'); + const checkout = await checkoutStarted; + expect(checkout).to.exist; expect(client).to.have.property('topology').that.is.instanceOf(Topology); } ); it( - 'does not send ping when authentication is disabled', + 'does not checkout connection when authentication is disabled', { requires: { auth: 'disabled' } }, async function () { - const commandToBeStarted = once(client, 'commandStarted'); + const checkoutStartedEvents = []; + client.on('connectionCheckOutStarted', event => { + checkoutStartedEvents.push(event); + }); await client.connect(); - const delayedFind = runLater(async () => { - await client.db().collection('test').findOne(); - }, 300); - const [findOneOperation] = await commandToBeStarted; - // Proves that the first command started event that is emitted is a find and not a ping - expect(findOneOperation).to.have.property('commandName', 'find'); - await delayedFind; + expect(checkoutStartedEvents).to.be.empty; expect(client).to.have.property('topology').that.is.instanceOf(Topology); } ); @@ -620,15 +617,15 @@ describe('class MongoClient', function () { 'permits operations to be run after connect is called', { requires: { auth: 'enabled' } }, async function () { - const pingCommandToBeStarted = once(client, 'commandStarted'); + const checkoutStarted = once(client, 'connectionCheckOutStarted'); await client.connect(); - const [pingOnConnect] = await pingCommandToBeStarted; + const checkout = await checkoutStarted; + expect(checkout).to.exist; const findCommandToBeStarted = once(client, 'commandStarted'); await client.db('test').collection('test').findOne(); const [findCommandStarted] = await findCommandToBeStarted; - expect(pingOnConnect).to.have.property('commandName', 'ping'); expect(findCommandStarted).to.have.property('commandName', 'find'); expect(client).to.have.property('topology').that.is.instanceOf(Topology); } @@ -1186,14 +1183,18 @@ describe('class MongoClient', function () { const tests = [ // only skipInitialPing=true will have no events upon connect - { description: 'should skip ping command when set to true', value: true, expectEvents: 0 }, { - description: 'should not skip ping command when set to false', + description: 'should skip connection checkout when set to true', + value: true, + expectEvents: 0 + }, + { + description: 'should not skip connection checkout when set to false', value: false, expectEvents: 1 }, { - description: 'should not skip ping command when unset', + description: 'should not skip connection checkout command when unset', value: undefined, expectEvents: 1 } @@ -1201,9 +1202,9 @@ describe('class MongoClient', function () { for (const { description, value, expectEvents } of tests) { it(description, async function () { const options = value === undefined ? {} : { __skipPingOnConnect: value }; - const client = this.configuration.newClient({}, { ...options, monitorCommands: true }); + const client = this.configuration.newClient({}, { ...options }); const events = []; - client.on('commandStarted', event => events.push(event)); + client.on('connectionCheckOutStarted', event => events.push(event)); try { await client.connect(); @@ -1212,11 +1213,6 @@ describe('class MongoClient', function () { } expect(events).to.have.lengthOf(expectEvents); - if (expectEvents > 1) { - for (const event of events) { - expect(event).to.have.property('commandName', 'ping'); - } - } }); } }); diff --git a/test/integration/server-selection/unified-server-selection-node-specs-logging/load-balanced.json b/test/integration/server-selection/unified-server-selection-node-specs-logging/load-balanced.json index df30a43ebb5..125766ad1e4 100644 --- a/test/integration/server-selection/unified-server-selection-node-specs-logging/load-balanced.json +++ b/test/integration/server-selection/unified-server-selection-node-specs-logging/load-balanced.json @@ -79,7 +79,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true } @@ -93,7 +93,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true } diff --git a/test/integration/server-selection/unified-server-selection-node-specs-logging/replica-set.json b/test/integration/server-selection/unified-server-selection-node-specs-logging/replica-set.json index 304688bc054..b462f3b80e3 100644 --- a/test/integration/server-selection/unified-server-selection-node-specs-logging/replica-set.json +++ b/test/integration/server-selection/unified-server-selection-node-specs-logging/replica-set.json @@ -100,7 +100,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true } @@ -114,7 +114,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true }, @@ -134,7 +134,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true }, diff --git a/test/integration/server-selection/unified-server-selection-node-specs-logging/sharded.json b/test/integration/server-selection/unified-server-selection-node-specs-logging/sharded.json index ee8094be4e3..345fc84bc00 100644 --- a/test/integration/server-selection/unified-server-selection-node-specs-logging/sharded.json +++ b/test/integration/server-selection/unified-server-selection-node-specs-logging/sharded.json @@ -87,7 +87,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true } @@ -101,7 +101,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true }, @@ -121,7 +121,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true }, @@ -244,7 +244,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true } @@ -258,7 +258,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true }, @@ -278,7 +278,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true }, diff --git a/test/integration/server-selection/unified-server-selection-node-specs-logging/standalone.json b/test/integration/server-selection/unified-server-selection-node-specs-logging/standalone.json index c79a1f1f65c..2b5ee88338b 100644 --- a/test/integration/server-selection/unified-server-selection-node-specs-logging/standalone.json +++ b/test/integration/server-selection/unified-server-selection-node-specs-logging/standalone.json @@ -84,7 +84,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true } @@ -98,7 +98,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true }, @@ -118,7 +118,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true }, @@ -241,7 +241,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true } @@ -255,7 +255,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true }, @@ -275,7 +275,7 @@ "selector": { "$$exists": true }, - "operation": "ping", + "operation": "handshake", "topologyDescription": { "$$exists": true },