From 7686d1554abcc2032aa0a0805b59325b242935b8 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Mon, 3 Jul 2023 21:22:22 +0200 Subject: [PATCH 01/17] feat(NODE-5274): deprecate write concern options --- src/operations/command.ts | 2 +- src/write_concern.ts | 79 +++++++++++++++++++----- test/unit/write_concern.test.ts | 106 ++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 17 deletions(-) create mode 100644 test/unit/write_concern.test.ts diff --git a/src/operations/command.ts b/src/operations/command.ts index 0fb804aeb40..7880ad95a2d 100644 --- a/src/operations/command.ts +++ b/src/operations/command.ts @@ -135,7 +135,7 @@ export abstract class CommandOperation extends AbstractCallbackOperation { } if (this.writeConcern && this.hasAspect(Aspect.WRITE_OPERATION) && !inTransaction) { - Object.assign(cmd, { writeConcern: this.writeConcern }); + WriteConcern.apply(cmd, this.writeConcern); } if ( diff --git a/src/write_concern.ts b/src/write_concern.ts index 0d57cacd8d9..f2d87c2117f 100644 --- a/src/write_concern.ts +++ b/src/write_concern.ts @@ -1,3 +1,5 @@ +import { Document } from "bson"; + /** @public */ export type W = number | 'majority'; @@ -17,16 +19,36 @@ export interface WriteConcernSettings { journal?: boolean; // legacy options - /** The journal write concern */ + /** + * The journal write concern. + * @deprecated Will be removed in the next major version. Please use the journal option. + */ j?: boolean; - /** The write concern timeout */ + /** + * The write concern timeout. + * @deprecated Will be removed in the next major version. Please use the wtimeoutMS option. + */ wtimeout?: number; - /** The file sync write concern */ + /** + * The file sync write concern. + * @deprecated Will be removed in the next major version. Please use the journal option. + */ fsync?: boolean | 1; } export const WRITE_CONCERN_KEYS = ['w', 'wtimeout', 'j', 'journal', 'fsync']; +interface CommandWriteConcernOptions { + /** The write concern */ + w?: W; + /** The journal write concern. */ + j?: boolean; + /** The write concern timeout. */ + wtimeout?: number; + /** The file sync write concern. */ + fsync?: boolean | 1; +} + /** * A MongoDB WriteConcern, which describes the level of acknowledgement * requested from MongoDB for write operations. @@ -35,23 +57,36 @@ export const WRITE_CONCERN_KEYS = ['w', 'wtimeout', 'j', 'journal', 'fsync']; * @see https://www.mongodb.com/docs/manual/reference/write-concern/ */ export class WriteConcern { - /** request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags. */ - w?: W; - /** specify a time limit to prevent write operations from blocking indefinitely */ + /** Request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags. */ + readonly w?: W; + /** Request acknowledgment that the write operation has been written to the on-disk journal */ + readonly journal?: boolean; + /** Specify a time limit to prevent write operations from blocking indefinitely */ + readonly wtimeoutMS?: number; + /** + * Specify a time limit to prevent write operations from blocking indefinitely. + * @deprecated Will be removed in the next major version. Please use wtimeoutMS. + */ wtimeout?: number; - /** request acknowledgment that the write operation has been written to the on-disk journal */ + /** + * Request acknowledgment that the write operation has been written to the on-disk journal. + * @deprecated Will be removed in the next major version. Please use journal. + */ j?: boolean; - /** equivalent to the j option */ + /** + * Equivalent to the j option. + * @deprecated Will be removed in the next major version. Please use journal. + */ fsync?: boolean | 1; /** * Constructs a WriteConcern from the write concern properties. * @param w - request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags. - * @param wtimeout - specify a time limit to prevent write operations from blocking indefinitely - * @param j - request acknowledgment that the write operation has been written to the on-disk journal + * @param wtimeoutMS - specify a time limit to prevent write operations from blocking indefinitely + * @param journal - request acknowledgment that the write operation has been written to the on-disk journal * @param fsync - equivalent to the j option */ - constructor(w?: W, wtimeout?: number, j?: boolean, fsync?: boolean | 1) { + constructor(w?: W, wtimeoutMS?: number, journal?: boolean, fsync?: boolean | 1) { if (w != null) { if (!Number.isNaN(Number(w))) { this.w = Number(w); @@ -59,17 +94,29 @@ export class WriteConcern { this.w = w; } } - if (wtimeout != null) { - this.wtimeout = wtimeout; + if (wtimeoutMS != null) { + this.wtimeoutMS = this.wtimeout = wtimeoutMS; } - if (j != null) { - this.j = j; + if (journal != null) { + this.journal = this.j = journal; } if (fsync != null) { - this.fsync = fsync; + this.journal = this.j = fsync ? true : false; } } + /** + * Apply a write concern to a command document. + */ + static apply(command: Document, writeConcern: WriteConcern): Document { + const wc: CommandWriteConcernOptions = {}; + // The write concern document sent to the server has w/wtimeout/j fields. + if (writeConcern.w != null) wc.w = writeConcern.w; + if (writeConcern.wtimeoutMS != null) wc.wtimeout = writeConcern.wtimeoutMS; + if (writeConcern.journal != null) wc.j = writeConcern.j; + return Object.assign(command, { writeConcern: wc }); + } + /** Construct a WriteConcern given an options object. */ static fromOptions( options?: WriteConcernOptions | WriteConcern | W, diff --git a/test/unit/write_concern.test.ts b/test/unit/write_concern.test.ts new file mode 100644 index 00000000000..bb74cffde2c --- /dev/null +++ b/test/unit/write_concern.test.ts @@ -0,0 +1,106 @@ +import { expect } from 'chai'; + +import { WriteConcern } from '../mongodb'; + +describe('WriteConcern', function () { + describe('#constructor', function () { + context('when w is provided', function () { + const writeConcern = new WriteConcern(1); + + it('sets the w property', function () { + expect(writeConcern.w).to.equal(1); + }); + }); + + context('when wtimeoutMS is provided', function () { + const writeConcern = new WriteConcern(1, 50); + + it('sets the wtimeoutMS property', function () { + expect(writeConcern.wtimeoutMS).to.equal(50); + }); + + it('sets the wtimeout property', function () { + expect(writeConcern.wtimeout).to.equal(50); + }); + }); + + context('when journal is provided', function () { + const writeConcern = new WriteConcern(1, 50, true); + + it('sets the journal property', function () { + expect(writeConcern.journal).to.be.true; + }); + + it('sets the j property', function () { + expect(writeConcern.j).to.be.true; + }); + }); + + context('when fsync is provided', function () { + const writeConcern = new WriteConcern(1, 50, false, true); + + it('sets the journal property', function () { + expect(writeConcern.journal).to.be.true; + }); + + it('sets the j property', function () { + expect(writeConcern.j).to.be.true; + }); + }); + }); + + describe('.apply', function () { + context('when no options are set', function () { + const document = {}; + const writeConcern = new WriteConcern(); + + it('returns an empty write concern', function () { + expect(WriteConcern.apply(document, writeConcern)).to.deep.equal({ writeConcern: {} }); + }); + }); + + context('when w is in the write concern', function () { + const document = {}; + const writeConcern = new WriteConcern(2); + + it('adds w to the write concern document', function () { + expect(WriteConcern.apply(document, writeConcern)).to.deep.equal({ + writeConcern: { w: 2 } + }); + }); + }); + + context('when wtimeoutMS is in the write concern', function () { + const document = {}; + const writeConcern = new WriteConcern(2, 30); + + it('adds wtimeout to the write concern document', function () { + expect(WriteConcern.apply(document, writeConcern)).to.deep.equal({ + writeConcern: { w: 2, wtimeout: 30 } + }); + }); + }); + + context('when journal is in the write concern', function () { + const document = {}; + const writeConcern = new WriteConcern(2, 30, true); + + it('adds j to the write concern document', function () { + expect(WriteConcern.apply(document, writeConcern)).to.deep.equal({ + writeConcern: { w: 2, wtimeout: 30, j: true } + }); + }); + }); + + context('when fsync is in the write concern', function () { + const document = {}; + const writeConcern = new WriteConcern(2, 30, true, false); + + it('overrites j to the write concern document', function () { + expect(WriteConcern.apply(document, writeConcern)).to.deep.equal({ + writeConcern: { w: 2, wtimeout: 30, j: false } + }); + }); + }); + }); +}); From 6fb01f38deda1517dcfe761df92377e16d87f539 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Mon, 3 Jul 2023 21:44:06 +0200 Subject: [PATCH 02/17] fix: lint --- src/write_concern.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/write_concern.ts b/src/write_concern.ts index f2d87c2117f..3a02d4bc015 100644 --- a/src/write_concern.ts +++ b/src/write_concern.ts @@ -1,4 +1,4 @@ -import { Document } from "bson"; +import { type Document } from 'bson'; /** @public */ export type W = number | 'majority'; From acca5348ae9948ab962e015fee7384fd25edd01d Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Mon, 3 Jul 2023 22:11:50 +0200 Subject: [PATCH 03/17] fix: command write concern options --- src/write_concern.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/write_concern.ts b/src/write_concern.ts index 3a02d4bc015..df839353ae1 100644 --- a/src/write_concern.ts +++ b/src/write_concern.ts @@ -38,6 +38,7 @@ export interface WriteConcernSettings { export const WRITE_CONCERN_KEYS = ['w', 'wtimeout', 'j', 'journal', 'fsync']; +/** The write concern options that decorate the server command. */ interface CommandWriteConcernOptions { /** The write concern */ w?: W; @@ -45,8 +46,6 @@ interface CommandWriteConcernOptions { j?: boolean; /** The write concern timeout. */ wtimeout?: number; - /** The file sync write concern. */ - fsync?: boolean | 1; } /** From 5e5c38b8af72ba165f0334046bca8c7071cc8cb4 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 15:17:58 +0200 Subject: [PATCH 04/17] test: update tests --- src/write_concern.ts | 7 +++-- test/integration/crud/find_and_modify.test.ts | 28 +++++++++---------- test/unit/write_concern.test.ts | 24 ++++++++++++++-- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/write_concern.ts b/src/write_concern.ts index df839353ae1..e50b9cc8469 100644 --- a/src/write_concern.ts +++ b/src/write_concern.ts @@ -1,4 +1,4 @@ -import { type Document } from 'bson'; +import { type Document } from './bson'; /** @public */ export type W = number | 'majority'; @@ -105,7 +105,7 @@ export class WriteConcern { } /** - * Apply a write concern to a command document. + * Apply a write concern to a command document. Will modify and return the command. */ static apply(command: Document, writeConcern: WriteConcern): Document { const wc: CommandWriteConcernOptions = {}; @@ -113,7 +113,8 @@ export class WriteConcern { if (writeConcern.w != null) wc.w = writeConcern.w; if (writeConcern.wtimeoutMS != null) wc.wtimeout = writeConcern.wtimeoutMS; if (writeConcern.journal != null) wc.j = writeConcern.j; - return Object.assign(command, { writeConcern: wc }); + command.writeConcern = wc; + return command; } /** Construct a WriteConcern given an options object. */ diff --git a/test/integration/crud/find_and_modify.test.ts b/test/integration/crud/find_and_modify.test.ts index 2ecb02c178b..81312425034 100644 --- a/test/integration/crud/find_and_modify.test.ts +++ b/test/integration/crud/find_and_modify.test.ts @@ -119,8 +119,8 @@ describe('Collection (#findOneAnd...)', function () { }); it('passes through the writeConcern', async function () { - await collection.findOneAndDelete({}, { writeConcern: { fsync: 1 } }); - expect(started[0].command.writeConcern).to.deep.equal({ fsync: 1 }); + await collection.findOneAndDelete({}, { writeConcern: { j: 1 } }); + expect(started[0].command.writeConcern).to.deep.equal({ j: 1 }); }); }); @@ -128,27 +128,27 @@ describe('Collection (#findOneAnd...)', function () { beforeEach(async function () { collection = client .db('test') - .collection('findAndModifyTest', { writeConcern: { fsync: 1 } }); + .collection('findAndModifyTest', { writeConcern: { j: 1 } }); await collection.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); }); it('passes through the writeConcern', async function () { await collection.findOneAndDelete({}); - expect(started[0].command.writeConcern).to.deep.equal({ fsync: 1 }); + expect(started[0].command.writeConcern).to.deep.equal({ j: 1 }); }); }); context('when provided at the db level', function () { beforeEach(async function () { collection = client - .db('test', { writeConcern: { fsync: 1 } }) + .db('test', { writeConcern: { j: 1 } }) .collection('findAndModifyTest'); await collection.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); }); it('passes through the writeConcern', async function () { await collection.findOneAndDelete({}); - expect(started[0].command.writeConcern).to.deep.equal({ fsync: 1 }); + expect(started[0].command.writeConcern).to.deep.equal({ j: 1 }); }); }); }); @@ -297,8 +297,8 @@ describe('Collection (#findOneAnd...)', function () { }); it('passes through the writeConcern', async function () { - await collection.findOneAndUpdate({}, { $set: { a: 1 } }, { writeConcern: { fsync: 1 } }); - expect(started[0].command.writeConcern).to.deep.equal({ fsync: 1 }); + await collection.findOneAndUpdate({}, { $set: { a: 1 } }, { writeConcern: { j: 1 } }); + expect(started[0].command.writeConcern).to.deep.equal({ j: 1 }); }); }); @@ -306,27 +306,27 @@ describe('Collection (#findOneAnd...)', function () { beforeEach(async function () { collection = client .db('test') - .collection('findAndModifyTest', { writeConcern: { fsync: 1 } }); + .collection('findAndModifyTest', { writeConcern: { j: 1 } }); await collection.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); }); it('passes through the writeConcern', async function () { await collection.findOneAndUpdate({}, { $set: { a: 1 } }); - expect(started[0].command.writeConcern).to.deep.equal({ fsync: 1 }); + expect(started[0].command.writeConcern).to.deep.equal({ j: 1 }); }); }); context('when provided at the db level', function () { beforeEach(async function () { collection = client - .db('test', { writeConcern: { fsync: 1 } }) + .db('test', { writeConcern: { j: 1 } }) .collection('findAndModifyTest'); await collection.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); }); it('passes through the writeConcern', async function () { await collection.findOneAndUpdate({}, { $set: { a: 1 } }); - expect(started[0].command.writeConcern).to.deep.equal({ fsync: 1 }); + expect(started[0].command.writeConcern).to.deep.equal({ j: 1 }); }); }); }); @@ -468,8 +468,8 @@ describe('Collection (#findOneAnd...)', function () { }); it('passes through the writeConcern', async function () { - await collection.findOneAndReplace({}, { b: 1 }, { writeConcern: { fsync: 1 } }); - expect(started[0].command.writeConcern).to.deep.equal({ fsync: 1 }); + await collection.findOneAndReplace({}, { b: 1 }, { writeConcern: { j: 1 } }); + expect(started[0].command.writeConcern).to.deep.equal({ j: 1 }); }); }); diff --git a/test/unit/write_concern.test.ts b/test/unit/write_concern.test.ts index bb74cffde2c..547ce86417a 100644 --- a/test/unit/write_concern.test.ts +++ b/test/unit/write_concern.test.ts @@ -5,11 +5,29 @@ import { WriteConcern } from '../mongodb'; describe('WriteConcern', function () { describe('#constructor', function () { context('when w is provided', function () { - const writeConcern = new WriteConcern(1); + context('when w is a number', function () { + const writeConcern = new WriteConcern(1); - it('sets the w property', function () { - expect(writeConcern.w).to.equal(1); + it('sets the w property', function () { + expect(writeConcern.w).to.equal(1); + }); + }); + + context('when w is a string number', function () { + const writeConcern = new WriteConcern('10'); + + it('sets the w property to a number', function () { + expect(writeConcern.w).to.equal(10); + }); }); + + context('when w is a string', function () { + const writeConcern = new WriteConcern('majority'); + + it('sets the w property to the string', function () { + expect(writeConcern.w).to.equal('majority'); + }); + }) }); context('when wtimeoutMS is provided', function () { From 903dbedd40b5d78bb9025d79958558fdd8aba50b Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 15:21:13 +0200 Subject: [PATCH 05/17] test: fix other tests --- test/integration/node-specific/mongo_client.test.ts | 5 ++--- test/integration/uri-options/uri.test.js | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/test/integration/node-specific/mongo_client.test.ts b/test/integration/node-specific/mongo_client.test.ts index 03278dc3ce8..b0bdfd71618 100644 --- a/test/integration/node-specific/mongo_client.test.ts +++ b/test/integration/node-specific/mongo_client.test.ts @@ -52,9 +52,8 @@ describe('class MongoClient', function () { expect(db).to.have.property('writeConcern'); expect(db.writeConcern).to.have.property('w', 1); - expect(db.writeConcern).to.have.property('wtimeout', 1000); - expect(db.writeConcern).to.have.property('fsync', true); - expect(db.writeConcern).to.have.property('j', true); + expect(db.writeConcern).to.have.property('wtimeoutMS', 1000); + expect(db.writeConcern).to.have.property('journal', true); expect(db).to.have.property('s'); expect(db.s).to.have.property('readPreference'); diff --git a/test/integration/uri-options/uri.test.js b/test/integration/uri-options/uri.test.js index 293c65ee1fa..ef62c3408bd 100644 --- a/test/integration/uri-options/uri.test.js +++ b/test/integration/uri-options/uri.test.js @@ -66,7 +66,7 @@ describe('URI', function () { const client = this.configuration.newClient('mongodb://127.0.0.1:27017/?fsync=true'); client.connect((err, client) => { var db = client.db(this.configuration.db); - expect(db.writeConcern.fsync).to.be.true; + expect(db.writeConcern.journal).to.be.true; client.close(done); }); } From ec0588503bb52e0a1e520135324c9e9a6b977212 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 16:07:15 +0200 Subject: [PATCH 06/17] fix: session write concern --- src/sessions.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sessions.ts b/src/sessions.ts index 0f24cea71ce..32ce96ca3e8 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -45,6 +45,7 @@ import { now, uuidV4 } from './utils'; +import { WriteConcern } from './write_concern'; const minWireVersionForShardedTransactions = 8; @@ -697,7 +698,7 @@ function endTransaction( // apply a writeConcern if specified let writeConcern; if (session.transaction.options.writeConcern) { - writeConcern = Object.assign({}, session.transaction.options.writeConcern); + writeConcern = WriteConcern.apply({}, session.transaction.options.writeConcern); } else if (session.clientOptions && session.clientOptions.writeConcern) { writeConcern = { w: session.clientOptions.writeConcern.w }; } From 10eccbfb502dbe5d53795625c3a26437fe58102b Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 16:35:23 +0200 Subject: [PATCH 07/17] fix: lint --- test/unit/write_concern.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/write_concern.test.ts b/test/unit/write_concern.test.ts index 547ce86417a..f367bf83ac4 100644 --- a/test/unit/write_concern.test.ts +++ b/test/unit/write_concern.test.ts @@ -27,7 +27,7 @@ describe('WriteConcern', function () { it('sets the w property to the string', function () { expect(writeConcern.w).to.equal('majority'); }); - }) + }); }); context('when wtimeoutMS is provided', function () { From 28e5d3098bdde39526ec49ab56029861c33fab94 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 18:22:05 +0200 Subject: [PATCH 08/17] fix: aggregate --- src/operations/aggregate.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/operations/aggregate.ts b/src/operations/aggregate.ts index 301f39cfd3a..a742b6ce9fd 100644 --- a/src/operations/aggregate.ts +++ b/src/operations/aggregate.ts @@ -3,6 +3,7 @@ import { MongoInvalidArgumentError } from '../error'; import type { Server } from '../sdam/server'; import type { ClientSession } from '../sessions'; import { type Callback, maxWireVersion, type MongoDBNamespace } from '../utils'; +import { WriteConcern } from '../write_concern'; import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command'; import { Aspect, defineAspects, type Hint } from './operation'; @@ -102,7 +103,7 @@ export class AggregateOperation extends CommandOperation { } if (this.hasWriteStage && this.writeConcern) { - Object.assign(command, { writeConcern: this.writeConcern }); + WriteConcern.apply(command, this.writeConcern); } if (options.bypassDocumentValidation === true) { From dd7fa37e755bc632ca410989ac2e2d8b55e4c0fd Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 19:33:22 +0200 Subject: [PATCH 09/17] fix: sessions --- src/sessions.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/sessions.ts b/src/sessions.ts index 32ce96ca3e8..bc15b792687 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -698,7 +698,7 @@ function endTransaction( // apply a writeConcern if specified let writeConcern; if (session.transaction.options.writeConcern) { - writeConcern = WriteConcern.apply({}, session.transaction.options.writeConcern); + writeConcern = Object.assign({}, session.transaction.options.writeConcern); } else if (session.clientOptions && session.clientOptions.writeConcern) { writeConcern = { w: session.clientOptions.writeConcern.w }; } @@ -708,9 +708,10 @@ function endTransaction( } if (writeConcern) { - Object.assign(command, { writeConcern }); + WriteConcern.apply(command, writeConcern); } + console.log(command); if (commandName === 'commitTransaction' && session.transaction.options.maxTimeMS) { Object.assign(command, { maxTimeMS: session.transaction.options.maxTimeMS }); } From 4da9fc5a8b9e54f02efa30402d3a4594b41f6267 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 19:56:50 +0200 Subject: [PATCH 10/17] fix: txns --- src/sessions.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/sessions.ts b/src/sessions.ts index bc15b792687..544ab539368 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -704,14 +704,13 @@ function endTransaction( } if (txnState === TxnState.TRANSACTION_COMMITTED) { - writeConcern = Object.assign({ wtimeout: 10000 }, writeConcern, { w: 'majority' }); + writeConcern = Object.assign({ wtimeoutMS: 10000 }, writeConcern, { w: 'majority' }); } if (writeConcern) { WriteConcern.apply(command, writeConcern); } - console.log(command); if (commandName === 'commitTransaction' && session.transaction.options.maxTimeMS) { Object.assign(command, { maxTimeMS: session.transaction.options.maxTimeMS }); } From 1b9ec0113d65af5898c2ebadea06fe3f92becaa8 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 20:01:06 +0200 Subject: [PATCH 11/17] fix: grifs --- src/gridfs/upload.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gridfs/upload.ts b/src/gridfs/upload.ts index a2109af4ed4..a0afba13902 100644 --- a/src/gridfs/upload.ts +++ b/src/gridfs/upload.ts @@ -458,8 +458,8 @@ function getWriteOptions(stream: GridFSBucketWriteStream): WriteConcernOptions { if (stream.writeConcern) { obj.writeConcern = { w: stream.writeConcern.w, - wtimeout: stream.writeConcern.wtimeout, - j: stream.writeConcern.j + wtimeout: stream.writeConcern.wtimeoutMS, + j: stream.writeConcern.journal }; } return obj; From bf721ac5bf1be0768bc7cf9c2ed1cd67c141d21c Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 20:29:38 +0200 Subject: [PATCH 12/17] docs: update docs --- src/write_concern.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/write_concern.ts b/src/write_concern.ts index e50b9cc8469..e3a3f5510e3 100644 --- a/src/write_concern.ts +++ b/src/write_concern.ts @@ -83,7 +83,7 @@ export class WriteConcern { * @param w - request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags. * @param wtimeoutMS - specify a time limit to prevent write operations from blocking indefinitely * @param journal - request acknowledgment that the write operation has been written to the on-disk journal - * @param fsync - equivalent to the j option + * @param fsync - equivalent to the j option. Is deprecated and will be removed in the next major version. */ constructor(w?: W, wtimeoutMS?: number, journal?: boolean, fsync?: boolean | 1) { if (w != null) { From 7518c6509b092f35aa342041e3e0f03bbca891a4 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 22:40:18 +0200 Subject: [PATCH 13/17] fix: comments --- src/gridfs/upload.ts | 21 ++++----------------- src/sessions.ts | 9 ++++++--- test/unit/write_concern.test.ts | 2 +- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/gridfs/upload.ts b/src/gridfs/upload.ts index a0afba13902..8ef76e1a572 100644 --- a/src/gridfs/upload.ts +++ b/src/gridfs/upload.ts @@ -263,9 +263,8 @@ async function checkChunksIndex(stream: GridFSBucketWriteStream): Promise }); if (!hasChunksIndex) { - const writeConcernOptions = getWriteOptions(stream); await stream.chunks.createIndex(index, { - ...writeConcernOptions, + ...stream.writeConcern, background: true, unique: true }); @@ -292,7 +291,7 @@ function checkDone(stream: GridFSBucketWriteStream, callback?: Callback): boolea return false; } - stream.files.insertOne(filesDoc, getWriteOptions(stream)).then( + stream.files.insertOne(filesDoc, { writeConcern: stream.writeConcern }).then( () => { stream.emit(GridFSBucketWriteStream.FINISH, filesDoc); stream.emit(GridFSBucketWriteStream.CLOSE); @@ -423,7 +422,7 @@ function doWrite( return false; } - stream.chunks.insertOne(doc, getWriteOptions(stream)).then( + stream.chunks.insertOne(doc, { writeConcern: stream.writeConcern }).then( () => { --stream.state.outstandingRequests; --outstandingRequests; @@ -453,18 +452,6 @@ function doWrite( return false; } -function getWriteOptions(stream: GridFSBucketWriteStream): WriteConcernOptions { - const obj: WriteConcernOptions = {}; - if (stream.writeConcern) { - obj.writeConcern = { - w: stream.writeConcern.w, - wtimeout: stream.writeConcern.wtimeoutMS, - j: stream.writeConcern.journal - }; - } - return obj; -} - function waitForIndexes( stream: GridFSBucketWriteStream, callback: (res: boolean) => boolean @@ -499,7 +486,7 @@ function writeRemnant(stream: GridFSBucketWriteStream, callback?: Callback): boo return false; } - stream.chunks.insertOne(doc, getWriteOptions(stream)).then( + stream.chunks.insertOne(doc, { writeConcern: stream.writeConcern }).then( () => { --stream.state.outstandingRequests; checkDone(stream); diff --git a/src/sessions.ts b/src/sessions.ts index 544ab539368..dc130adf8e9 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -771,9 +771,12 @@ function endTransaction( // per txns spec, must unpin session in this case session.unpin({ force: true }); - command.writeConcern = Object.assign({ wtimeout: 10000 }, command.writeConcern, { - w: 'majority' - }); + WriteConcern.apply( + command, + Object.assign({ wtimeout: 10000 }, command.writeConcern, { + w: 'majority' + }) + ); } return executeOperation( diff --git a/test/unit/write_concern.test.ts b/test/unit/write_concern.test.ts index f367bf83ac4..0f59a2c3b04 100644 --- a/test/unit/write_concern.test.ts +++ b/test/unit/write_concern.test.ts @@ -114,7 +114,7 @@ describe('WriteConcern', function () { const document = {}; const writeConcern = new WriteConcern(2, 30, true, false); - it('overrites j to the write concern document', function () { + it('overwrites j to the write concern document', function () { expect(WriteConcern.apply(document, writeConcern)).to.deep.equal({ writeConcern: { w: 2, wtimeout: 30, j: false } }); From 558b16d736e165950752bd26d22742223d444b84 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 22:57:18 +0200 Subject: [PATCH 14/17] fix: sessions --- src/sessions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sessions.ts b/src/sessions.ts index dc130adf8e9..668c05ad9ff 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -773,7 +773,7 @@ function endTransaction( WriteConcern.apply( command, - Object.assign({ wtimeout: 10000 }, command.writeConcern, { + Object.assign({ wtimeoutMS: 10000 }, command.writeConcern, { w: 'majority' }) ); From 673fa1617acd9e3aef4547f7ca0da66207af44ce Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 23:25:07 +0200 Subject: [PATCH 15/17] fix: sessions apply --- src/sessions.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sessions.ts b/src/sessions.ts index 668c05ad9ff..9cf438847a6 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -773,9 +773,9 @@ function endTransaction( WriteConcern.apply( command, - Object.assign({ wtimeoutMS: 10000 }, command.writeConcern, { - w: 'majority' - }) + WriteConcern.fromOptions( + Object.assign({ wtimeout: 10000 }, command.writeConcern, { w: 'majority' }) + ) || {} ); } From e9c5651a0821d2cf2c3c814781438b6631618ce9 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 23:40:45 +0200 Subject: [PATCH 16/17] fix: sessions raw retry --- src/sessions.ts | 9 +++------ test/integration/transactions/transactions.spec.test.js | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/sessions.ts b/src/sessions.ts index 9cf438847a6..544ab539368 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -771,12 +771,9 @@ function endTransaction( // per txns spec, must unpin session in this case session.unpin({ force: true }); - WriteConcern.apply( - command, - WriteConcern.fromOptions( - Object.assign({ wtimeout: 10000 }, command.writeConcern, { w: 'majority' }) - ) || {} - ); + command.writeConcern = Object.assign({ wtimeout: 10000 }, command.writeConcern, { + w: 'majority' + }); } return executeOperation( diff --git a/test/integration/transactions/transactions.spec.test.js b/test/integration/transactions/transactions.spec.test.js index 05b829324a4..6ffe41fb436 100644 --- a/test/integration/transactions/transactions.spec.test.js +++ b/test/integration/transactions/transactions.spec.test.js @@ -93,7 +93,7 @@ const SKIP_TESTS = [ 'Client side error when transaction is in progress' ]; -describe('Transactions Spec Legacy Tests', function () { +describe.only('Transactions Spec Legacy Tests', function () { const testContext = new TransactionsRunnerContext(); if (process.env.SERVERLESS) { // TODO(NODE-3550): these tests should pass on serverless but currently fail From c92c0a7822ab2ddb6d79d1eca1fc9a138b1a6d27 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 5 Jul 2023 23:50:35 +0200 Subject: [PATCH 17/17] fix: lint --- test/integration/transactions/transactions.spec.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/transactions/transactions.spec.test.js b/test/integration/transactions/transactions.spec.test.js index 6ffe41fb436..05b829324a4 100644 --- a/test/integration/transactions/transactions.spec.test.js +++ b/test/integration/transactions/transactions.spec.test.js @@ -93,7 +93,7 @@ const SKIP_TESTS = [ 'Client side error when transaction is in progress' ]; -describe.only('Transactions Spec Legacy Tests', function () { +describe('Transactions Spec Legacy Tests', function () { const testContext = new TransactionsRunnerContext(); if (process.env.SERVERLESS) { // TODO(NODE-3550): these tests should pass on serverless but currently fail