Skip to content

Commit

Permalink
test: remove read preference pre 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Nov 3, 2021
1 parent 4e2d168 commit 900cc19
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 68 deletions.
1 change: 1 addition & 0 deletions src/cmap/connection.ts
Expand Up @@ -97,6 +97,7 @@ export interface CommandOptions extends BSONSerializeOptions {
session?: ClientSession;
documentsReturnedIn?: string;
noResponse?: boolean;
omitReadPreference?: boolean;

// FIXME: NODE-2802
willRetryWrite?: boolean;
Expand Down
7 changes: 7 additions & 0 deletions src/operations/command.ts
Expand Up @@ -10,6 +10,7 @@ import type { Server } from '../sdam/server';
import type { BSONSerializeOptions, Document } from '../bson';
import type { ReadConcernLike } from './../read_concern';
import { Explain, ExplainOptions } from '../explain';
import { MIN_SECONDARY_WRITE_WIRE_VERSION } from '../sdam/server_selection';

const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5;

Expand Down Expand Up @@ -126,6 +127,12 @@ export abstract class CommandOperation<T> extends AbstractOperation<T> {
Object.assign(cmd, { readConcern: this.readConcern });
}

if (this.trySecondaryWrite && serverWireVersion < MIN_SECONDARY_WRITE_WIRE_VERSION) {
/* eslint no-console: 0 */
console.log('need to remove read preference from options');
options.omitReadPreference = true;
}

if (options.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
callback(
new MongoCompatibilityError(
Expand Down
4 changes: 3 additions & 1 deletion src/operations/operation.ts
Expand Up @@ -31,6 +31,7 @@ export interface OperationOptions extends BSONSerializeOptions {

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

/** @internal */
Expand All @@ -49,7 +50,7 @@ export abstract class AbstractOperation<TResult = any> {
readPreference: ReadPreference;
server!: Server;
bypassPinningCheck: boolean;
trySecondaryWrite = false;
trySecondaryWrite: boolean;

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

this.options = options;
this.bypassPinningCheck = !!options.bypassPinningCheck;
this.trySecondaryWrite = false;
}

abstract execute(server: Server, session: ClientSession, callback: Callback<TResult>): void;
Expand Down
8 changes: 8 additions & 0 deletions src/sdam/server.ts
Expand Up @@ -299,6 +299,14 @@ export class Server extends TypedEventEmitter<ServerEvents> {
// Clone the options
const finalOptions = Object.assign({}, options, { wireProtocolCommand: false });

// There are cases where we need to flag the read preference not to get sent in
// the command, such as pre-5.0 servers attempting to perform an aggregate write
// with a non-primary read preference. In this case the effective read preference
// (primary) is not the same as the provided and must be removed completely.
if (finalOptions.omitReadPreference) {
delete finalOptions.readPreference;
}

// error if collation not supported
if (collationNotSupported(this, cmd)) {
callback(new MongoCompatibilityError(`Server ${this.name} does not support collation`));
Expand Down
2 changes: 2 additions & 0 deletions src/sdam/server_selection.ts
Expand Up @@ -46,6 +46,8 @@ export function secondaryWritableServerSelector(
/* eslint no-console: 0 */
console.log('select', readPreference, wireVersion);
if (!readPreference || (wireVersion && wireVersion < MIN_SECONDARY_WRITE_WIRE_VERSION)) {
/* eslint no-console: 0 */
console.log('force select primary');
return readPreferenceServerSelector(ReadPreference.primary);
}
return readPreferenceServerSelector(readPreference);
Expand Down
3 changes: 2 additions & 1 deletion test/functional/crud_spec.test.js
Expand Up @@ -424,7 +424,8 @@ describe('CRUD spec v1', function () {
}
});

describe('CRUD unified', function () {
// https://jira.mongodb.org/browse/NODE-3655
describe.only('CRUD unified', function () {
for (const crudSpecTest of loadSpecTests('crud/unified')) {
expect(crudSpecTest).to.exist;
const testDescription = String(crudSpecTest.description);
Expand Down

0 comments on commit 900cc19

Please sign in to comment.