Skip to content

Commit

Permalink
refactor: default to unified topology
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbroadst committed Mar 9, 2020
1 parent 9ad0def commit ade9a62
Show file tree
Hide file tree
Showing 35 changed files with 29 additions and 2,545 deletions.
1 change: 0 additions & 1 deletion lib/core/sdam/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const ServerType = {
};

const TOPOLOGY_DEFAULTS = {
useUnifiedTopology: true,
localThresholdMS: 15,
serverSelectionTimeoutMS: 30000,
heartbeatFrequencyMS: 10000,
Expand Down
4 changes: 1 addition & 3 deletions lib/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,7 @@ function makeClientMetadata(options) {
architecture: process.arch,
version: os.release()
},
platform: `'Node.js ${process.version}, ${os.endianness} (${
options.useUnifiedTopology ? 'unified' : 'legacy'
})`
platform: `'Node.js ${process.version}, ${os.endianness} (unified)`
};

// support optionally provided wrapping driver info
Expand Down
1 change: 0 additions & 1 deletion lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ const validOptions = require('./operations/connect').validOptions;
* @param {boolean} [options.monitorCommands=false] Enable command monitoring for this client
* @param {number} [options.minSize] If present, the connection pool will be initialized with minSize connections, and will never dip below minSize connections
* @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.
* @param {boolean} [options.useUnifiedTopology] Enables the new unified topology layer
* @param {AutoEncrypter~AutoEncryptionOptions} [options.autoEncryption] Optionally enable client side auto encryption
* @param {DriverInfoOptions} [options.driverInfo] Allows a wrapping driver to amend the client metadata generated by the driver to include information about the wrapping driver
* @param {MongoClient~connectCallback} [callback] The command result callback
Expand Down
131 changes: 1 addition & 130 deletions lib/operations/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ const fs = require('fs');
const BSON = require('../core/connection/utils').retrieveBSON();
const CMAP_EVENT_NAMES = require('../cmap/events').CMAP_EVENT_NAMES;

let client;
function loadClient() {
if (!client) {
client = require('../mongo_client');
}
return client;
}

const legacyParse = deprecate(
require('../url_parser'),
'current URL string parser is deprecated, and will be removed in a future version. ' +
Expand All @@ -41,30 +33,6 @@ const AUTH_MECHANISM_INTERNAL_MAP = {
'SCRAM-SHA-256': 'scram-sha-256'
};

const monitoringEvents = [
'timeout',
'close',
'serverOpening',
'serverDescriptionChanged',
'serverHeartbeatStarted',
'serverHeartbeatSucceeded',
'serverHeartbeatFailed',
'serverClosed',
'topologyOpening',
'topologyClosed',
'topologyDescriptionChanged',
'commandStarted',
'commandSucceeded',
'commandFailed',
'joined',
'left',
'ping',
'ha',
'all',
'fullsetup',
'open'
];

const VALID_AUTH_MECHANISMS = new Set([
'DEFAULT',
'PLAIN',
Expand Down Expand Up @@ -137,7 +105,6 @@ const validOptionNames = [
'retryWrites',
'retryReads',
'useNewUrlParser',
'useUnifiedTopology',
'serverSelectionTimeoutMS',
'useRecoveryToken',
'autoEncryption',
Expand Down Expand Up @@ -210,31 +177,6 @@ function assignTopology(client, topology) {
}
}

// Clear out all events
function clearAllEvents(topology) {
monitoringEvents.forEach(event => topology.removeAllListeners(event));
}

// Collect all events in order from SDAM
function collectEvents(mongoClient, topology) {
let MongoClient = loadClient();
const collectedEvents = [];

if (mongoClient instanceof MongoClient) {
monitoringEvents.forEach(event => {
topology.on(event, (object1, object2) => {
if (event === 'open') {
collectedEvents.push({ event: event, object1: mongoClient });
} else {
collectedEvents.push({ event: event, object1: object1, object2: object2 });
}
});
});
}

return collectedEvents;
}

function resolveTLSOptions(options) {
if (options.tls == null) {
return;
Expand All @@ -247,9 +189,6 @@ function resolveTLSOptions(options) {
});
}

const emitDeprecationForNonUnifiedTopology = deprecate(() => {},
'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.');

function connect(mongoClient, url, options, callback) {
options = Object.assign({}, options);

Expand Down Expand Up @@ -317,20 +256,7 @@ function connect(mongoClient, url, options, callback) {
}
}

if (_finalOptions.useUnifiedTopology) {
return createTopology(mongoClient, 'unified', _finalOptions, connectCallback);
}

emitDeprecationForNonUnifiedTopology();

// Do we have a replicaset then skip discovery and go straight to connectivity
if (_finalOptions.replicaSet || _finalOptions.rs_name) {
return createTopology(mongoClient, 'replicaset', _finalOptions, connectCallback);
} else if (object.servers.length > 1) {
return createTopology(mongoClient, 'mongos', _finalOptions, connectCallback);
} else {
return createServer(mongoClient, _finalOptions, connectCallback);
}
return createTopology(mongoClient, 'unified', _finalOptions, connectCallback);
});

function connectCallback(err, topology) {
Expand Down Expand Up @@ -400,52 +326,6 @@ function createListener(mongoClient, event) {
};
}

function createServer(mongoClient, options, callback) {
// Pass in the promise library
options.promiseLibrary = mongoClient.s.promiseLibrary;

// Set default options
const servers = translateOptions(options);

const server = servers[0];

// Propagate the events to the client
const collectedEvents = collectEvents(mongoClient, server);

// Connect to topology
server.connect(options, (err, topology) => {
if (err) {
server.close(true);
return callback(err);
}
// Clear out all the collected event listeners
clearAllEvents(server);

// Relay all the events
relayEvents(mongoClient, server);
// Add listeners
addListeners(mongoClient, server);
// Check if we are really speaking to a mongos
const ismaster = topology.lastIsMaster();

// Set the topology
assignTopology(mongoClient, topology);

// Do we actually have a mongos
if (ismaster && ismaster.msg === 'isdbgrid') {
// Destroy the current connection
topology.close();
// Create mongos connection instead
return createTopology(mongoClient, 'mongos', options, callback);
}

// Fire all the events
replayEvents(mongoClient, collectedEvents);
// Otherwise callback
callback(err, topology);
});
}

const DEPRECATED_UNIFIED_EVENTS = new Set([
'reconnect',
'reconnectFailed',
Expand Down Expand Up @@ -711,15 +591,6 @@ function relayEvents(mongoClient, topology) {
});
}

//
// Replay any events due to single server connection switching to Mongos
//
function replayEvents(mongoClient, events) {
for (let i = 0; i < events.length; i++) {
mongoClient.emit(events[i].event, events[i].object1, events[i].object2);
}
}

function transformUrlOptions(_object) {
let object = Object.assign({ servers: _object.hosts }, _object.options);
for (let name in object) {
Expand Down
14 changes: 2 additions & 12 deletions test/functional/client_side_encryption/corpus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ describe('Client Side Encryption Corpus', function() {

before(function() {
// 1. Create a MongoClient without encryption enabled (referred to as ``client``).
client = this.configuration.newClient({
useNewUrlParser: true,
useUnifiedTopology: true
});
client = this.configuration.newClient();

return Promise.resolve()
.then(() => client.connect())
Expand Down Expand Up @@ -192,14 +189,7 @@ describe('Client Side Encryption Corpus', function() {
[dataNamespace]: corpusSchema
};
}
clientEncrypted = this.configuration.newClient(
{},
{
useNewUrlParser: true,
useUnifiedTopology: true,
autoEncryption
}
);
clientEncrypted = this.configuration.newClient({}, { autoEncryption });

return clientEncrypted.connect().then(() => {
clientEncryption = new mongodbClientEncryption.ClientEncryption(client, {
Expand Down
3 changes: 1 addition & 2 deletions test/functional/client_side_encryption/driver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Client Side Encryption Functional', function() {

describe('BSON Options', function() {
beforeEach(function() {
this.client = this.configuration.newClient({}, { useUnifiedTopology: true });
this.client = this.configuration.newClient();

const noop = () => {};
function encryptSchema(keyId, bsonType) {
Expand Down Expand Up @@ -77,7 +77,6 @@ describe('Client Side Encryption Functional', function() {
this.encryptedClient = this.configuration.newClient(
{},
{
useUnifiedTopology: true,
autoEncryption: {
keyVaultNamespace,
kmsProviders
Expand Down
35 changes: 6 additions & 29 deletions test/functional/client_side_encryption/prose.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ describe('Client Side Encryption Prose Tests', function() {
const mongodbClientEncryption = this.configuration.mongodbClientEncryption;

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

this.commandStartedEvents = new EventCollector(this.client, 'commandStarted', {
exclude: ['ismaster']
Expand Down Expand Up @@ -105,8 +102,6 @@ describe('Client Side Encryption Prose Tests', function() {
this.clientEncrypted = this.configuration.newClient(
{},
{
useNewUrlParser: true,
useUnifiedTopology: true,
autoEncryption: {
keyVaultNamespace,
kmsProviders: this.configuration.kmsProviders(null, localKey),
Expand Down Expand Up @@ -316,10 +311,7 @@ describe('Client Side Encryption Prose Tests', function() {
// "aws": { <AWS credentials> }
// }
// Configure with ``keyVaultNamespace`` set to ``admin.datakeys``, and a default MongoClient as the ``keyVaultClient``.
this.client = this.configuration.newClient(
{},
{ useNewUrlParser: true, useUnifiedTopology: true }
);
this.client = this.configuration.newClient();

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

// #. Create a MongoClient without encryption enabled (referred to as ``client``).
this.client = this.configuration.newClient(
{},
{ useNewUrlParser: true, useUnifiedTopology: true }
);
this.client = this.configuration.newClient();

return (
this.client
Expand Down Expand Up @@ -501,8 +490,6 @@ describe('Client Side Encryption Prose Tests', function() {
this.clientEncrypted = this.configuration.newClient(
{},
{
useNewUrlParser: true,
useUnifiedTopology: true,
monitorCommands: true,
autoEncryption: {
keyVaultNamespace,
Expand Down Expand Up @@ -659,10 +646,7 @@ describe('Client Side Encryption Prose Tests', function() {
// First, perform the setup.

// #. Create a MongoClient without encryption enabled (referred to as ``client``).
this.client = this.configuration.newClient(
{},
{ useNewUrlParser: true, useUnifiedTopology: true }
);
this.client = this.configuration.newClient();

return this.client
.connect()
Expand All @@ -685,8 +669,6 @@ describe('Client Side Encryption Prose Tests', function() {
this.clientEncrypted = this.configuration.newClient(
{},
{
useNewUrlParser: true,
useUnifiedTopology: true,
autoEncryption: {
keyVaultNamespace,
kmsProviders: this.configuration.kmsProviders(null, localKey)
Expand Down Expand Up @@ -737,10 +719,7 @@ describe('Client Side Encryption Prose Tests', function() {
const externalSchema = loadExternal('external-schema.json');

beforeEach(function() {
this.client = this.configuration.newClient(
{},
{ useNewUrlParser: true, useUnifiedTopology: true }
);
this.client = this.configuration.newClient();

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

this.commandStartedEvents = new EventCollector(
Expand Down Expand Up @@ -822,8 +801,6 @@ describe('Client Side Encryption Prose Tests', function() {
this.clientEncrypted = this.configuration.newClient(
{},
{
useNewUrlParser: true,
useUnifiedTopology: true,
autoEncryption: Object.assign({}, options, {
schemaMap: {
'db.coll': externalSchema
Expand Down
Loading

0 comments on commit ade9a62

Please sign in to comment.