Skip to content

Commit ade9a62

Browse files
committed
refactor: default to unified topology
There is no longer any topology type other than the unified topology, so we default to it in the `connect` operation, and remove all tests that don't work with it.
1 parent 9ad0def commit ade9a62

35 files changed

+29
-2545
lines changed

lib/core/sdam/common.js

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const ServerType = {
2929
};
3030

3131
const TOPOLOGY_DEFAULTS = {
32-
useUnifiedTopology: true,
3332
localThresholdMS: 15,
3433
serverSelectionTimeoutMS: 30000,
3534
heartbeatFrequencyMS: 10000,

lib/core/utils.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,7 @@ function makeClientMetadata(options) {
224224
architecture: process.arch,
225225
version: os.release()
226226
},
227-
platform: `'Node.js ${process.version}, ${os.endianness} (${
228-
options.useUnifiedTopology ? 'unified' : 'legacy'
229-
})`
227+
platform: `'Node.js ${process.version}, ${os.endianness} (unified)`
230228
};
231229

232230
// support optionally provided wrapping driver info

lib/mongo_client.js

-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ const validOptions = require('./operations/connect').validOptions;
142142
* @param {boolean} [options.monitorCommands=false] Enable command monitoring for this client
143143
* @param {number} [options.minSize] If present, the connection pool will be initialized with minSize connections, and will never dip below minSize connections
144144
* @param {boolean} [options.useNewUrlParser=true] Determines whether or not to use the new url parser. Enables the new, spec-compliant, url parser shipped in the core driver. This url parser fixes a number of problems with the original parser, and aims to outright replace that parser in the near future. Defaults to true, and must be explicitly set to false to use the legacy url parser.
145-
* @param {boolean} [options.useUnifiedTopology] Enables the new unified topology layer
146145
* @param {AutoEncrypter~AutoEncryptionOptions} [options.autoEncryption] Optionally enable client side auto encryption
147146
* @param {DriverInfoOptions} [options.driverInfo] Allows a wrapping driver to amend the client metadata generated by the driver to include information about the wrapping driver
148147
* @param {MongoClient~connectCallback} [callback] The command result callback

lib/operations/connect.js

+1-130
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ const fs = require('fs');
1717
const BSON = require('../core/connection/utils').retrieveBSON();
1818
const CMAP_EVENT_NAMES = require('../cmap/events').CMAP_EVENT_NAMES;
1919

20-
let client;
21-
function loadClient() {
22-
if (!client) {
23-
client = require('../mongo_client');
24-
}
25-
return client;
26-
}
27-
2820
const legacyParse = deprecate(
2921
require('../url_parser'),
3022
'current URL string parser is deprecated, and will be removed in a future version. ' +
@@ -41,30 +33,6 @@ const AUTH_MECHANISM_INTERNAL_MAP = {
4133
'SCRAM-SHA-256': 'scram-sha-256'
4234
};
4335

44-
const monitoringEvents = [
45-
'timeout',
46-
'close',
47-
'serverOpening',
48-
'serverDescriptionChanged',
49-
'serverHeartbeatStarted',
50-
'serverHeartbeatSucceeded',
51-
'serverHeartbeatFailed',
52-
'serverClosed',
53-
'topologyOpening',
54-
'topologyClosed',
55-
'topologyDescriptionChanged',
56-
'commandStarted',
57-
'commandSucceeded',
58-
'commandFailed',
59-
'joined',
60-
'left',
61-
'ping',
62-
'ha',
63-
'all',
64-
'fullsetup',
65-
'open'
66-
];
67-
6836
const VALID_AUTH_MECHANISMS = new Set([
6937
'DEFAULT',
7038
'PLAIN',
@@ -137,7 +105,6 @@ const validOptionNames = [
137105
'retryWrites',
138106
'retryReads',
139107
'useNewUrlParser',
140-
'useUnifiedTopology',
141108
'serverSelectionTimeoutMS',
142109
'useRecoveryToken',
143110
'autoEncryption',
@@ -210,31 +177,6 @@ function assignTopology(client, topology) {
210177
}
211178
}
212179

213-
// Clear out all events
214-
function clearAllEvents(topology) {
215-
monitoringEvents.forEach(event => topology.removeAllListeners(event));
216-
}
217-
218-
// Collect all events in order from SDAM
219-
function collectEvents(mongoClient, topology) {
220-
let MongoClient = loadClient();
221-
const collectedEvents = [];
222-
223-
if (mongoClient instanceof MongoClient) {
224-
monitoringEvents.forEach(event => {
225-
topology.on(event, (object1, object2) => {
226-
if (event === 'open') {
227-
collectedEvents.push({ event: event, object1: mongoClient });
228-
} else {
229-
collectedEvents.push({ event: event, object1: object1, object2: object2 });
230-
}
231-
});
232-
});
233-
}
234-
235-
return collectedEvents;
236-
}
237-
238180
function resolveTLSOptions(options) {
239181
if (options.tls == null) {
240182
return;
@@ -247,9 +189,6 @@ function resolveTLSOptions(options) {
247189
});
248190
}
249191

250-
const emitDeprecationForNonUnifiedTopology = deprecate(() => {},
251-
'current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. ' + 'To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.');
252-
253192
function connect(mongoClient, url, options, callback) {
254193
options = Object.assign({}, options);
255194

@@ -317,20 +256,7 @@ function connect(mongoClient, url, options, callback) {
317256
}
318257
}
319258

320-
if (_finalOptions.useUnifiedTopology) {
321-
return createTopology(mongoClient, 'unified', _finalOptions, connectCallback);
322-
}
323-
324-
emitDeprecationForNonUnifiedTopology();
325-
326-
// Do we have a replicaset then skip discovery and go straight to connectivity
327-
if (_finalOptions.replicaSet || _finalOptions.rs_name) {
328-
return createTopology(mongoClient, 'replicaset', _finalOptions, connectCallback);
329-
} else if (object.servers.length > 1) {
330-
return createTopology(mongoClient, 'mongos', _finalOptions, connectCallback);
331-
} else {
332-
return createServer(mongoClient, _finalOptions, connectCallback);
333-
}
259+
return createTopology(mongoClient, 'unified', _finalOptions, connectCallback);
334260
});
335261

336262
function connectCallback(err, topology) {
@@ -400,52 +326,6 @@ function createListener(mongoClient, event) {
400326
};
401327
}
402328

403-
function createServer(mongoClient, options, callback) {
404-
// Pass in the promise library
405-
options.promiseLibrary = mongoClient.s.promiseLibrary;
406-
407-
// Set default options
408-
const servers = translateOptions(options);
409-
410-
const server = servers[0];
411-
412-
// Propagate the events to the client
413-
const collectedEvents = collectEvents(mongoClient, server);
414-
415-
// Connect to topology
416-
server.connect(options, (err, topology) => {
417-
if (err) {
418-
server.close(true);
419-
return callback(err);
420-
}
421-
// Clear out all the collected event listeners
422-
clearAllEvents(server);
423-
424-
// Relay all the events
425-
relayEvents(mongoClient, server);
426-
// Add listeners
427-
addListeners(mongoClient, server);
428-
// Check if we are really speaking to a mongos
429-
const ismaster = topology.lastIsMaster();
430-
431-
// Set the topology
432-
assignTopology(mongoClient, topology);
433-
434-
// Do we actually have a mongos
435-
if (ismaster && ismaster.msg === 'isdbgrid') {
436-
// Destroy the current connection
437-
topology.close();
438-
// Create mongos connection instead
439-
return createTopology(mongoClient, 'mongos', options, callback);
440-
}
441-
442-
// Fire all the events
443-
replayEvents(mongoClient, collectedEvents);
444-
// Otherwise callback
445-
callback(err, topology);
446-
});
447-
}
448-
449329
const DEPRECATED_UNIFIED_EVENTS = new Set([
450330
'reconnect',
451331
'reconnectFailed',
@@ -711,15 +591,6 @@ function relayEvents(mongoClient, topology) {
711591
});
712592
}
713593

714-
//
715-
// Replay any events due to single server connection switching to Mongos
716-
//
717-
function replayEvents(mongoClient, events) {
718-
for (let i = 0; i < events.length; i++) {
719-
mongoClient.emit(events[i].event, events[i].object1, events[i].object2);
720-
}
721-
}
722-
723594
function transformUrlOptions(_object) {
724595
let object = Object.assign({ servers: _object.hosts }, _object.options);
725596
for (let name in object) {

test/functional/client_side_encryption/corpus.test.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ describe('Client Side Encryption Corpus', function() {
128128

129129
before(function() {
130130
// 1. Create a MongoClient without encryption enabled (referred to as ``client``).
131-
client = this.configuration.newClient({
132-
useNewUrlParser: true,
133-
useUnifiedTopology: true
134-
});
131+
client = this.configuration.newClient();
135132

136133
return Promise.resolve()
137134
.then(() => client.connect())
@@ -192,14 +189,7 @@ describe('Client Side Encryption Corpus', function() {
192189
[dataNamespace]: corpusSchema
193190
};
194191
}
195-
clientEncrypted = this.configuration.newClient(
196-
{},
197-
{
198-
useNewUrlParser: true,
199-
useUnifiedTopology: true,
200-
autoEncryption
201-
}
202-
);
192+
clientEncrypted = this.configuration.newClient({}, { autoEncryption });
203193

204194
return clientEncrypted.connect().then(() => {
205195
clientEncryption = new mongodbClientEncryption.ClientEncryption(client, {

test/functional/client_side_encryption/driver.test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('Client Side Encryption Functional', function() {
2626

2727
describe('BSON Options', function() {
2828
beforeEach(function() {
29-
this.client = this.configuration.newClient({}, { useUnifiedTopology: true });
29+
this.client = this.configuration.newClient();
3030

3131
const noop = () => {};
3232
function encryptSchema(keyId, bsonType) {
@@ -77,7 +77,6 @@ describe('Client Side Encryption Functional', function() {
7777
this.encryptedClient = this.configuration.newClient(
7878
{},
7979
{
80-
useUnifiedTopology: true,
8180
autoEncryption: {
8281
keyVaultNamespace,
8382
kmsProviders

test/functional/client_side_encryption/prose.test.js

+6-29
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ describe('Client Side Encryption Prose Tests', function() {
3838
const mongodbClientEncryption = this.configuration.mongodbClientEncryption;
3939

4040
// #. Create a MongoClient without encryption enabled (referred to as ``client``). Enable command monitoring to listen for command_started events.
41-
this.client = this.configuration.newClient(
42-
{},
43-
{ useNewUrlParser: true, useUnifiedTopology: true, monitorCommands: true }
44-
);
41+
this.client = this.configuration.newClient({}, { monitorCommands: true });
4542

4643
this.commandStartedEvents = new EventCollector(this.client, 'commandStarted', {
4744
exclude: ['ismaster']
@@ -105,8 +102,6 @@ describe('Client Side Encryption Prose Tests', function() {
105102
this.clientEncrypted = this.configuration.newClient(
106103
{},
107104
{
108-
useNewUrlParser: true,
109-
useUnifiedTopology: true,
110105
autoEncryption: {
111106
keyVaultNamespace,
112107
kmsProviders: this.configuration.kmsProviders(null, localKey),
@@ -316,10 +311,7 @@ describe('Client Side Encryption Prose Tests', function() {
316311
// "aws": { <AWS credentials> }
317312
// }
318313
// Configure with ``keyVaultNamespace`` set to ``admin.datakeys``, and a default MongoClient as the ``keyVaultClient``.
319-
this.client = this.configuration.newClient(
320-
{},
321-
{ useNewUrlParser: true, useUnifiedTopology: true }
322-
);
314+
this.client = this.configuration.newClient();
323315

324316
return this.client.connect().then(() => {
325317
const mongodbClientEncryption = this.configuration.mongodbClientEncryption;
@@ -466,10 +458,7 @@ describe('Client Side Encryption Prose Tests', function() {
466458
// First, perform the setup.
467459

468460
// #. Create a MongoClient without encryption enabled (referred to as ``client``).
469-
this.client = this.configuration.newClient(
470-
{},
471-
{ useNewUrlParser: true, useUnifiedTopology: true }
472-
);
461+
this.client = this.configuration.newClient();
473462

474463
return (
475464
this.client
@@ -501,8 +490,6 @@ describe('Client Side Encryption Prose Tests', function() {
501490
this.clientEncrypted = this.configuration.newClient(
502491
{},
503492
{
504-
useNewUrlParser: true,
505-
useUnifiedTopology: true,
506493
monitorCommands: true,
507494
autoEncryption: {
508495
keyVaultNamespace,
@@ -659,10 +646,7 @@ describe('Client Side Encryption Prose Tests', function() {
659646
// First, perform the setup.
660647

661648
// #. Create a MongoClient without encryption enabled (referred to as ``client``).
662-
this.client = this.configuration.newClient(
663-
{},
664-
{ useNewUrlParser: true, useUnifiedTopology: true }
665-
);
649+
this.client = this.configuration.newClient();
666650

667651
return this.client
668652
.connect()
@@ -685,8 +669,6 @@ describe('Client Side Encryption Prose Tests', function() {
685669
this.clientEncrypted = this.configuration.newClient(
686670
{},
687671
{
688-
useNewUrlParser: true,
689-
useUnifiedTopology: true,
690672
autoEncryption: {
691673
keyVaultNamespace,
692674
kmsProviders: this.configuration.kmsProviders(null, localKey)
@@ -737,10 +719,7 @@ describe('Client Side Encryption Prose Tests', function() {
737719
const externalSchema = loadExternal('external-schema.json');
738720

739721
beforeEach(function() {
740-
this.client = this.configuration.newClient(
741-
{},
742-
{ useNewUrlParser: true, useUnifiedTopology: true }
743-
);
722+
this.client = this.configuration.newClient();
744723

745724
// #. Create a MongoClient without encryption enabled (referred to as ``client``).
746725
return (
@@ -785,7 +764,7 @@ describe('Client Side Encryption Prose Tests', function() {
785764
// this.configuration.url('fake-user', 'fake-pwd'),
786765
// TODO: Do this properly
787766
{},
788-
{ useNewUrlParser: true, useUnifiedTopology: true, monitorCommands: true }
767+
{ monitorCommands: true }
789768
);
790769

791770
this.commandStartedEvents = new EventCollector(
@@ -822,8 +801,6 @@ describe('Client Side Encryption Prose Tests', function() {
822801
this.clientEncrypted = this.configuration.newClient(
823802
{},
824803
{
825-
useNewUrlParser: true,
826-
useUnifiedTopology: true,
827804
autoEncryption: Object.assign({}, options, {
828805
schemaMap: {
829806
'db.coll': externalSchema

0 commit comments

Comments
 (0)