Skip to content

Commit 6529f20

Browse files
committed
test: remove read preference pre 5.0
1 parent 4e2d168 commit 6529f20

File tree

6 files changed

+353
-69
lines changed

6 files changed

+353
-69
lines changed

src/cmap/connection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export interface CommandOptions extends BSONSerializeOptions {
9797
session?: ClientSession;
9898
documentsReturnedIn?: string;
9999
noResponse?: boolean;
100+
omitReadPreference?: boolean;
100101

101102
// FIXME: NODE-2802
102103
willRetryWrite?: boolean;

src/operations/command.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { Server } from '../sdam/server';
1010
import type { BSONSerializeOptions, Document } from '../bson';
1111
import type { ReadConcernLike } from './../read_concern';
1212
import { Explain, ExplainOptions } from '../explain';
13+
import { MIN_SECONDARY_WRITE_WIRE_VERSION } from '../sdam/server_selection';
1314

1415
const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5;
1516

@@ -126,6 +127,10 @@ export abstract class CommandOperation<T> extends AbstractOperation<T> {
126127
Object.assign(cmd, { readConcern: this.readConcern });
127128
}
128129

130+
if (this.trySecondaryWrite && serverWireVersion < MIN_SECONDARY_WRITE_WIRE_VERSION) {
131+
options.omitReadPreference = true;
132+
}
133+
129134
if (options.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
130135
callback(
131136
new MongoCompatibilityError(

src/operations/operation.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface OperationOptions extends BSONSerializeOptions {
3131

3232
/** @internal Hints to `executeOperation` that this operation should not unpin on an ended transaction */
3333
bypassPinningCheck?: boolean;
34+
omitReadPreference?: boolean;
3435
}
3536

3637
/** @internal */
@@ -49,7 +50,7 @@ export abstract class AbstractOperation<TResult = any> {
4950
readPreference: ReadPreference;
5051
server!: Server;
5152
bypassPinningCheck: boolean;
52-
trySecondaryWrite = false;
53+
trySecondaryWrite: boolean;
5354

5455
// BSON serialization options
5556
bsonOptions?: BSONSerializeOptions;
@@ -73,6 +74,7 @@ export abstract class AbstractOperation<TResult = any> {
7374

7475
this.options = options;
7576
this.bypassPinningCheck = !!options.bypassPinningCheck;
77+
this.trySecondaryWrite = false;
7678
}
7779

7880
abstract execute(server: Server, session: ClientSession, callback: Callback<TResult>): void;

src/sdam/server.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,14 @@ export class Server extends TypedEventEmitter<ServerEvents> {
299299
// Clone the options
300300
const finalOptions = Object.assign({}, options, { wireProtocolCommand: false });
301301

302+
// There are cases where we need to flag the read preference not to get sent in
303+
// the command, such as pre-5.0 servers attempting to perform an aggregate write
304+
// with a non-primary read preference. In this case the effective read preference
305+
// (primary) is not the same as the provided and must be removed completely.
306+
if (finalOptions.omitReadPreference) {
307+
delete finalOptions.readPreference;
308+
}
309+
302310
// error if collation not supported
303311
if (collationNotSupported(this, cmd)) {
304312
callback(new MongoCompatibilityError(`Server ${this.name} does not support collation`));

src/sdam/server_selection.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ export function secondaryWritableServerSelector(
4343
// If server version >= 5.0...
4444
// - If read preference is supplied, use that.
4545
// - If no read preference is supplied, use primary.
46-
/* eslint no-console: 0 */
47-
console.log('select', readPreference, wireVersion);
4846
if (!readPreference || (wireVersion && wireVersion < MIN_SECONDARY_WRITE_WIRE_VERSION)) {
4947
return readPreferenceServerSelector(ReadPreference.primary);
5048
}

0 commit comments

Comments
 (0)