-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(NODE-5044): Write Concern 0 Must Not Affect Read Operations (#3541) #3575
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
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4b6f2ff
fix(NODE-4999): Write Concern 0 Must Not Affect Read Operations (#3541)
W-A-James 750b07c
chore: address comments
baileympearson 50e2b90
chore: remove write concer from change stream
baileympearson b036bf7
chore: address comments
baileympearson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Co-authored-by: Neal Beeken <[email protected]>
- Loading branch information
commit 4b6f2ff624d822c95d4f7b1a261d8bc9d07c4b61
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
174 changes: 174 additions & 0 deletions
174
test/integration/read-write-concern/write_concern.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import { expect } from 'chai'; | ||
import { on, once } from 'events'; | ||
|
||
import { Collection, Db, LEGACY_HELLO_COMMAND, MongoClient } from '../../mongodb'; | ||
import * as mock from '../../tools/mongodb-mock/index'; | ||
|
||
describe('Write Concern', function () { | ||
it('should respect writeConcern from uri', function (done) { | ||
const client = this.configuration.newClient( | ||
`${this.configuration.url()}&w=0&monitorCommands=true` | ||
); | ||
const events: any[] = []; | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
client.on('commandStarted', event => { | ||
if (event.commandName === 'insert') { | ||
events.push(event); | ||
} | ||
}); | ||
|
||
expect(client.writeConcern).to.eql({ w: 0 }); | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
client | ||
.db('test') | ||
.collection('test') | ||
.insertOne({ a: 1 }, (err, result) => { | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(err).to.not.exist; | ||
expect(result).to.exist; | ||
expect(events).to.be.an('array').with.lengthOf(1); | ||
expect(events[0]).to.containSubset({ | ||
commandName: 'insert', | ||
command: { | ||
writeConcern: { w: 0 } | ||
} | ||
}); | ||
client.close(done); | ||
}); | ||
}); | ||
|
||
describe('mock server write concern test', () => { | ||
let server; | ||
before(() => { | ||
return mock.createServer().then(s => { | ||
server = s; | ||
}); | ||
}); | ||
|
||
after(() => mock.cleanup()); | ||
|
||
// TODO(NODE-3816): the mock server response is looking for writeConcern on all messages, but endSessions doesn't have it | ||
it.skip('should pipe writeConcern from client down to API call', function () { | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
server.setMessageHandler(request => { | ||
if (request.document && request.document[LEGACY_HELLO_COMMAND]) { | ||
return request.reply(mock.HELLO); | ||
} | ||
expect(request.document.writeConcern).to.exist; | ||
expect(request.document.writeConcern.w).to.equal('majority'); | ||
return request.reply({ ok: 1 }); | ||
}); | ||
|
||
const uri = `mongodb://${server.uri()}`; | ||
const client = new MongoClient(uri, { writeConcern: { w: 'majority' } }); | ||
return client | ||
.connect() | ||
.then(() => { | ||
const db = client.db('wc_test'); | ||
const collection = db.collection('wc'); | ||
|
||
return collection.insertMany([{ a: 2 }]); | ||
}) | ||
.then(() => { | ||
return client.close(); | ||
}); | ||
}); | ||
}); | ||
|
||
context('when performing read operations', function () { | ||
context('when writeConcern = 0', function () { | ||
describe('cursor creating operations with a getMore', function () { | ||
let client: MongoClient; | ||
let db: Db; | ||
let col: Collection; | ||
|
||
beforeEach(async function () { | ||
client = this.configuration.newClient({ writeConcern: { w: 0 } }); | ||
await client.connect(); | ||
db = client.db('writeConcernTest'); | ||
col = db.collection('writeConcernTest'); | ||
|
||
const docs: any[] = []; | ||
for (let i = 0; i < 100; i++) { | ||
docs.push({ a: i, b: i + 1 }); | ||
} | ||
|
||
await col.insertMany(docs); | ||
}); | ||
|
||
afterEach(async function () { | ||
await db.dropDatabase(); | ||
await client.close(); | ||
}); | ||
|
||
it('succeeds on find', async function () { | ||
const findResult = col.find({}, { batchSize: 2 }); | ||
const err = await findResult.toArray().catch(e => e); | ||
|
||
expect(err).to.not.be.instanceOf(Error); | ||
}); | ||
|
||
it('succeeds on listCollections', async function () { | ||
const collections: any[] = []; | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (let i = 0; i < 10; i++) { | ||
collections.push(`writeConcernTestCol${i + 1}`); | ||
} | ||
|
||
for (const colName of collections) { | ||
await db.createCollection(colName).catch(() => null); | ||
} | ||
|
||
const cols = db.listCollections({}, { batchSize: 2 }); | ||
|
||
const err = await cols.toArray().catch(e => e); | ||
|
||
expect(err).to.not.be.instanceOf(Error); | ||
}); | ||
|
||
it('succeeds on aggregate', async function () { | ||
const aggResult = col.aggregate([{ $match: { a: { $gte: 0 } } }], { batchSize: 2 }); | ||
const err = await aggResult.toArray().catch(e => e); | ||
|
||
expect(err).to.not.be.instanceOf(Error); | ||
}); | ||
|
||
it('succeeds on listIndexes', async function () { | ||
await col.createIndex({ a: 1 }); | ||
await col.createIndex({ b: -1 }); | ||
await col.createIndex({ a: 1, b: -1 }); | ||
|
||
const listIndexesResult = col.listIndexes({ batchSize: 2 }); | ||
const err = await listIndexesResult.toArray().catch(e => e); | ||
|
||
expect(err).to.not.be.instanceOf(Error); | ||
}); | ||
|
||
it('succeeds on changeStream', { | ||
metadata: { requires: { topology: 'replicaset' } }, | ||
async test() { | ||
const changeStream = col.watch(undefined, { batchSize: 2 }); | ||
const changes = on(changeStream, 'change'); | ||
await once(changeStream.cursor, 'init'); | ||
|
||
await col.insertMany( | ||
[ | ||
{ a: 10 }, | ||
{ a: 10 }, | ||
{ a: 10 }, | ||
{ a: 10 }, | ||
{ a: 10 }, | ||
{ a: 10 }, | ||
{ a: 10 }, | ||
{ a: 10 }, | ||
{ a: 10 }, | ||
{ a: 10 }, | ||
{ a: 10 }, | ||
{ a: 10 } | ||
], | ||
{ writeConcern: { w: 'majority' } } | ||
); | ||
|
||
const err = await changes.next().catch(e => e); | ||
expect(err).to.not.be.instanceOf(Error); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.