-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(NODE-3358): Command monitoring objects hold internal state references #2858
Changes from 8 commits
d9b9cde
d66f84c
b5fe9ee
79b35ae
53c25d8
83970e7
c4b4160
404e89d
08c5b83
7c6fbce
026b509
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -871,6 +871,85 @@ function emitWarningOnce(message) { | |
} | ||
} | ||
|
||
// Copied from https://github.com/mongodb/node-mongodb-native/blob/70cace8343bead2f3c1a6d0cf07e20a8c4a9f45f/src/utils.ts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think these "copied from" comments are necessary. |
||
function isSuperset(set, subset) { | ||
set = Array.isArray(set) ? new Set(set) : set; | ||
subset = Array.isArray(subset) ? new Set(subset) : subset; | ||
for (const elem of subset) { | ||
if (!set.has(elem)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
// Copied from https://github.com/mongodb/node-mongodb-native/blob/70cace8343bead2f3c1a6d0cf07e20a8c4a9f45f/src/utils.ts | ||
function isRecord(value, requiredKeys) { | ||
const toString = Object.prototype.toString; | ||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
const isObject = v => toString.call(v) === '[object Object]'; | ||
if (!isObject(value)) { | ||
return false; | ||
} | ||
|
||
const ctor = value.constructor; | ||
if (ctor && ctor.prototype) { | ||
if (!isObject(ctor.prototype)) { | ||
return false; | ||
} | ||
|
||
// Check to see if some method exists from the Object exists | ||
if (!hasOwnProperty.call(ctor.prototype, 'isPrototypeOf')) { | ||
return false; | ||
} | ||
} | ||
|
||
if (requiredKeys) { | ||
const keys = Object.keys(value); | ||
return isSuperset(keys, requiredKeys); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Make a deep copy of an object | ||
* | ||
* NOTE: This is not meant to be the perfect implementation of a deep copy, | ||
* but instead something that is good enough for the purposes of | ||
* command monitoring. | ||
* Copied from https://github.com/mongodb/node-mongodb-native/blob/70cace8343bead2f3c1a6d0cf07e20a8c4a9f45f/src/utils.ts | ||
*/ | ||
function deepCopy(value) { | ||
if (value == null) { | ||
return value; | ||
} else if (Array.isArray(value)) { | ||
return value.map(item => deepCopy(item)); | ||
} else if (isRecord(value)) { | ||
const res = {}; | ||
for (const key in value) { | ||
res[key] = deepCopy(value[key]); | ||
} | ||
return res; | ||
} | ||
|
||
const ctor = value.constructor; | ||
if (ctor) { | ||
switch (ctor.name.toLowerCase()) { | ||
case 'date': | ||
return new ctor(Number(value)); | ||
case 'map': | ||
return new Map(value); | ||
case 'set': | ||
return new Set(value); | ||
case 'buffer': | ||
return Buffer.from(value); | ||
} | ||
} | ||
|
||
return value; | ||
} | ||
|
||
module.exports = { | ||
filterOptions, | ||
mergeOptions, | ||
|
@@ -907,5 +986,6 @@ module.exports = { | |
hasAtomicOperators, | ||
MONGODB_WARNING_CODE, | ||
emitWarning, | ||
emitWarningOnce | ||
emitWarningOnce, | ||
deepCopy | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -701,6 +701,56 @@ describe('APM', function() { | |||||
} | ||||||
}); | ||||||
|
||||||
// NODE-3358 | ||||||
describe('Internal state references', function() { | ||||||
let client; | ||||||
|
||||||
beforeEach(function(done) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||||||
client = this.configuration.newClient( | ||||||
{ writeConcern: { w: 1 } }, | ||||||
{ maxPoolSize: 1, monitorCommands: true } | ||||||
); | ||||||
done(); | ||||||
}); | ||||||
|
||||||
afterEach(function(done) { | ||||||
client.close(() => done()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}); | ||||||
|
||||||
it('should not allow mutation of internal state from commands returned by event monitoring', function() { | ||||||
const started = []; | ||||||
const succeeded = []; | ||||||
const documentToInsert = { a: { b: 1 } }; | ||||||
client.on('commandStarted', filterForCommands('insert', started)); | ||||||
client.on('commandSucceeded', filterForCommands('insert', succeeded)); | ||||||
return client | ||||||
.connect() | ||||||
.then(client => { | ||||||
const db = client.db(this.configuration.db); | ||||||
return db.collection('apm_test').insertOne(documentToInsert); | ||||||
}) | ||||||
.then(r => { | ||||||
expect(r) | ||||||
.to.have.property('insertedId') | ||||||
.that.is.an('object'); | ||||||
expect(started).to.have.lengthOf(1); | ||||||
// Check if contents of returned document are equal to document inserted (by value) | ||||||
expect(documentToInsert).to.deep.equal(started[0].command.documents[0]); | ||||||
// Check if the returned document is a clone of the original. This confirms that the | ||||||
// reference is not the same. | ||||||
expect(documentToInsert !== started[0].command.documents[0]).to.equal(true); | ||||||
expect(documentToInsert.a !== started[0].command.documents[0].a).to.equal(true); | ||||||
|
||||||
started[0].command.documents[0].a.b = 2; | ||||||
expect(documentToInsert.a.b).to.equal(1); | ||||||
|
||||||
expect(started[0].commandName).to.equal('insert'); | ||||||
expect(started[0].command.insert).to.equal('apm_test'); | ||||||
expect(succeeded).to.have.lengthOf(1); | ||||||
}); | ||||||
}); | ||||||
}); | ||||||
|
||||||
it('should correctly receive the APM events for deleteOne', { | ||||||
metadata: { requires: { topology: ['single', 'replicaset'] } }, | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
const Msg = require('../../../lib/core/connection/msg').Msg; | ||
const GetMore = require('../../../lib/core/connection/commands').GetMore; | ||
const Query = require('../../../lib/core/connection/commands').Query; | ||
const KillCursor = require('../../../lib/core/connection/commands').KillCursor; | ||
const CommandStartedEvent = require('../../../lib/core/connection/apm').CommandStartedEvent; | ||
const expect = require('chai').expect; | ||
const Long = require('bson').Long; | ||
|
||
describe('Command Monitoring Events - unit/cmap', function() { | ||
const commands = [ | ||
new Query({}, 'admin.$cmd', { a: { b: 10 }, $query: { b: 10 } }, {}), | ||
new Query({}, 'hello', { a: { b: 10 }, $query: { b: 10 } }, {}), | ||
new Msg({}, 'admin.$cmd', { b: { c: 20 } }, {}), | ||
new Msg({}, 'hello', { b: { c: 20 } }, {}), | ||
new GetMore({}, 'admin.$cmd', Long.fromNumber(10)), | ||
new GetMore({}, 'hello', Long.fromNumber(10)), | ||
new KillCursor({}, 'admin.$cmd', [Long.fromNumber(100), Long.fromNumber(200)]), | ||
new KillCursor({}, 'hello', [Long.fromNumber(100), Long.fromNumber(200)]), | ||
{ ns: 'admin.$cmd', query: { $query: { a: 16 } } }, | ||
{ ns: 'hello there', f1: { h: { a: 52, b: { c: 10, d: [1, 2, 3, 5] } } } } | ||
]; | ||
|
||
for (const command of commands) { | ||
it(`CommandStartedEvent should make a deep copy of object of type: ${command.constructor.name}`, () => { | ||
const ev = new CommandStartedEvent({ id: 'someId', address: 'someHost' }, command); | ||
if (command instanceof Query) { | ||
if (command.ns === 'admin.$cmd') { | ||
expect(ev.command !== command.query.$query).to.equal(true); | ||
for (const k in command.query.$query) { | ||
expect(ev.command[k]).to.deep.equal(command.query.$query[k]); | ||
} | ||
} else { | ||
expect(ev.command.filter !== command.query.$query).to.equal(true); | ||
for (const k in command.query.$query) { | ||
expect(ev.command.filter[k]).to.deep.equal(command.query.$query[k]); | ||
} | ||
} | ||
} else if (command instanceof Msg) { | ||
expect(ev.command !== command.command).to.equal(true); | ||
expect(ev.command).to.deep.equal(command.command); | ||
} else if (command instanceof GetMore) { | ||
// NOTE: BSON Longs pass strict equality when their internal values are equal | ||
// i.e. | ||
// let l1 = Long(10); | ||
// let l2 = Long(10); | ||
// l1 === l2 // returns true | ||
// expect(ev.command.getMore !== command.cursorId).to.equal(true); | ||
expect(ev.command.getMore).to.deep.equal(command.cursorId); | ||
|
||
ev.command.getMore = Long.fromNumber(50128); | ||
expect(command.cursorId).to.not.deep.equal(ev.command.getMore); | ||
} else if (command instanceof KillCursor) { | ||
expect(ev.command.cursors !== command.cursorIds).to.equal(true); | ||
expect(ev.command.cursors).to.deep.equal(command.cursorIds); | ||
} else if (typeof command === 'object') { | ||
if (command.ns === 'admin.$cmd') { | ||
expect(ev.command !== command.query.$query).to.equal(true); | ||
for (const k in command.query.$query) { | ||
expect(ev.command[k]).to.deep.equal(command.query.$query[k]); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep the more concise || syntax from the original code.