Skip to content

Commit 4193f7d

Browse files
committed
refactor: try to revert query
1 parent 2c20053 commit 4193f7d

File tree

2 files changed

+98
-83
lines changed

2 files changed

+98
-83
lines changed

Diff for: src/cmap/connection.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
366366
}
367367

368368
private prepareCommand(db: string, command: Document, options: CommandOptions) {
369-
const cmd = { ...command };
369+
let cmd = { ...command };
370370
const readPreference = getReadPreference(options);
371371
const session = options?.session;
372372

@@ -399,24 +399,28 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
399399
cmd.$clusterTime = clusterTime;
400400
}
401401

402-
// Driver does not use legacy hello for anything other than the first handshake
403-
// (we do not support servers that only speak OP_QUERY),
404-
// therefore $readPreference is not relevant for servers that do not support OP_MSG.
405-
406402
// For standalone, drivers MUST NOT set $readPreference.
407-
if (this.supportsOpMsg && this.description.type !== ServerType.Standalone) {
408-
// For mongos and load balancers with 'primary' mode, drivers MUST NOT set $readPreference.
409-
// For all other types with a direct connection, if the read preference is 'primary'
410-
// (driver sets 'primary' as default if no read preference is configured),
411-
// the $readPreference MUST be set to 'primaryPreferred'
412-
// to ensure that any server type can handle the request.
403+
if (this.description.type !== ServerType.Standalone) {
413404
if (
414405
!isSharded(this) &&
415406
!this.description.loadBalanced &&
407+
this.supportsOpMsg &&
416408
this.description.directConnection === true &&
417409
readPreference?.mode === 'primary'
418410
) {
411+
// For mongos and load balancers with 'primary' mode, drivers MUST NOT set $readPreference.
412+
// For all other types with a direct connection, if the read preference is 'primary'
413+
// (driver sets 'primary' as default if no read preference is configured),
414+
// the $readPreference MUST be set to 'primaryPreferred'
415+
// to ensure that any server type can handle the request.
419416
cmd.$readPreference = ReadPreference.primaryPreferred.toJSON();
417+
} else if (isSharded(this) && !this.supportsOpMsg && readPreference?.mode !== 'primary') {
418+
// When sending a read operation via OP_QUERY and the $readPreference modifier,
419+
// the query MUST be provided using the $query modifier.
420+
cmd = {
421+
$query: cmd,
422+
$readPreference: readPreference.toJSON()
423+
};
420424
} else if (readPreference?.mode !== 'primary') {
421425
// For mode 'primary', drivers MUST NOT set $readPreference.
422426
// For all other read preference modes (i.e. 'secondary', 'primaryPreferred', ...),

Diff for: test/integration/max-staleness/max_staleness.test.js

+83-72
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Max Staleness', function () {
1818
// Primary server states
1919
const serverIsPrimary = [Object.assign({}, defaultFields)];
2020
server.setMessageHandler(request => {
21-
var doc = request.document;
21+
const doc = request.document;
2222
if (isHello(doc)) {
2323
request.reply(serverIsPrimary[0]);
2424
return;
@@ -46,33 +46,31 @@ describe('Max Staleness', function () {
4646
metadata: {
4747
requires: {
4848
generators: true,
49-
topology: 'single'
49+
topology: 'replicaset'
5050
}
5151
},
5252

53-
test: function (done) {
54-
var self = this;
53+
test: function () {
54+
const self = this;
5555
const configuration = this.configuration;
5656
const client = configuration.newClient(
5757
`mongodb://${test.server.uri()}/test?readPreference=secondary&maxStalenessSeconds=250`,
5858
{ serverApi: null } // TODO(NODE-3807): remove resetting serverApi when the usage of mongodb mock server is removed
5959
);
6060

61-
client.connect(function (err, client) {
61+
client.connect(async function (err, client) {
6262
expect(err).to.not.exist;
63-
var db = client.db(self.configuration.db);
64-
65-
db.collection('test')
66-
.find({})
67-
.toArray(function (err) {
68-
expect(err).to.not.exist;
69-
expect(test.checkCommand).to.containSubset({
70-
$query: { find: 'test', filter: {} },
71-
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
72-
});
73-
74-
client.close(done);
75-
});
63+
const db = client.db(self.configuration.db);
64+
65+
try {
66+
await db.collection('test').find({}).toArray();
67+
} catch (err) {
68+
expect(err).to.not.exist;
69+
}
70+
71+
expect(test.checkCommand).to.containSubset({
72+
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
73+
});
7674
});
7775
}
7876
});
@@ -81,36 +79,38 @@ describe('Max Staleness', function () {
8179
metadata: {
8280
requires: {
8381
generators: true,
84-
topology: 'single'
82+
topology: 'replicaset'
8583
}
8684
},
8785

88-
test: function (done) {
86+
test: async function () {
8987
const configuration = this.configuration;
9088
const client = configuration.newClient(`mongodb://${test.server.uri()}/test`, {
9189
serverApi: null // TODO(NODE-3807): remove resetting serverApi when the usage of mongodb mock server is removed
9290
});
93-
client.connect(function (err, client) {
91+
92+
try {
93+
await client.connect();
94+
} catch (err) {
9495
expect(err).to.not.exist;
96+
}
9597

96-
// Get a db with a new readPreference
97-
var db1 = client.db('test', {
98-
readPreference: new ReadPreference('secondary', null, { maxStalenessSeconds: 250 })
99-
});
98+
// Get a db with a new readPreference
99+
const db1 = client.db('test', {
100+
readPreference: new ReadPreference('secondary', null, { maxStalenessSeconds: 250 })
101+
});
100102

101-
db1
102-
.collection('test')
103-
.find({})
104-
.toArray(function (err) {
105-
expect(err).to.not.exist;
106-
expect(test.checkCommand).to.containSubset({
107-
$query: { find: 'test', filter: {} },
108-
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
109-
});
110-
111-
client.close(done);
112-
});
103+
try {
104+
await db1.collection('test').find({}).toArray();
105+
} catch (err) {
106+
expect(err).to.not.exist;
107+
}
108+
109+
expect(test.checkCommand).to.containSubset({
110+
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
113111
});
112+
113+
client.close();
114114
}
115115
});
116116

@@ -120,35 +120,42 @@ describe('Max Staleness', function () {
120120
metadata: {
121121
requires: {
122122
generators: true,
123-
topology: 'single'
123+
topology: 'replicaset'
124124
}
125125
},
126126

127-
test: function (done) {
128-
var self = this;
127+
test: async function () {
128+
const self = this;
129129
const configuration = this.configuration;
130130
const client = configuration.newClient(`mongodb://${test.server.uri()}/test`, {
131131
serverApi: null // TODO(NODE-3807): remove resetting serverApi when the usage of mongodb mock server is removed
132132
});
133-
client.connect(function (err, client) {
133+
134+
try {
135+
await client.connect();
136+
} catch (err) {
134137
expect(err).to.not.exist;
135-
var db = client.db(self.configuration.db);
138+
}
136139

140+
const db = client.db(self.configuration.db);
141+
142+
try {
137143
// Get a db with a new readPreference
138-
db.collection('test', {
139-
readPreference: new ReadPreference('secondary', null, { maxStalenessSeconds: 250 })
140-
})
144+
await db
145+
.collection('test', {
146+
readPreference: new ReadPreference('secondary', null, { maxStalenessSeconds: 250 })
147+
})
141148
.find({})
142-
.toArray(function (err) {
143-
expect(err).to.not.exist;
144-
expect(test.checkCommand).to.containSubset({
145-
$query: { find: 'test', filter: {} },
146-
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
147-
});
148-
149-
client.close(done);
150-
});
149+
.toArray();
150+
} catch (err) {
151+
expect(err).to.not.exist;
152+
}
153+
154+
expect(test.checkCommand).to.containSubset({
155+
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
151156
});
157+
158+
client.close();
152159
}
153160
}
154161
);
@@ -157,35 +164,39 @@ describe('Max Staleness', function () {
157164
metadata: {
158165
requires: {
159166
generators: true,
160-
topology: 'single'
167+
topology: 'replicaset'
161168
}
162169
},
163170

164-
test: function (done) {
165-
var self = this;
171+
test: async function () {
172+
const self = this;
166173
const configuration = this.configuration;
167174
const client = configuration.newClient(`mongodb://${test.server.uri()}/test`, {
168175
serverApi: null // TODO(NODE-3807): remove resetting serverApi when the usage of mongodb mock server is removed
169176
});
170-
client.connect(function (err, client) {
177+
178+
try {
179+
await client.connect();
180+
} catch (err) {
171181
expect(err).to.not.exist;
172-
var db = client.db(self.configuration.db);
173-
var readPreference = new ReadPreference('secondary', null, { maxStalenessSeconds: 250 });
182+
}
183+
184+
const db = client.db(self.configuration.db);
185+
const readPreference = new ReadPreference('secondary', null, { maxStalenessSeconds: 250 });
174186

187+
try {
175188
// Get a db with a new readPreference
176-
db.collection('test')
177-
.find({})
178-
.withReadPreference(readPreference)
179-
.toArray(function (err) {
180-
expect(err).to.not.exist;
181-
expect(test.checkCommand).to.containSubset({
182-
$query: { find: 'test', filter: {} },
183-
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
184-
});
185-
186-
client.close(done);
187-
});
189+
await db.collection('test').find({}).withReadPreference(readPreference).toArray();
190+
} catch (err) {
191+
expect(err).to.not.exist;
192+
}
193+
194+
expect(test.checkCommand).to.containSubset({
195+
$query: { find: 'test', filter: {} },
196+
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
188197
});
198+
199+
client.close();
189200
}
190201
});
191202
});

0 commit comments

Comments
 (0)