Skip to content

Commit

Permalink
chore(NODE-4641): remove wire version checks for unsupported servers (m…
Browse files Browse the repository at this point in the history
…ongodb#3416)

Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
  • Loading branch information
2 people authored and ZLY201 committed Nov 5, 2022
1 parent 2b7d844 commit 69b0dd8
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 132 deletions.
6 changes: 2 additions & 4 deletions src/operations/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ export class AggregateOperation<T = Document> extends CommandOperation<T> {
this.readConcern = undefined;
}

if (serverWireVersion >= 5) {
if (this.hasWriteStage && this.writeConcern) {
Object.assign(command, { writeConcern: this.writeConcern });
}
if (this.hasWriteStage && this.writeConcern) {
Object.assign(command, { writeConcern: this.writeConcern });
}

if (options.bypassDocumentValidation === true) {
Expand Down
34 changes: 8 additions & 26 deletions src/operations/command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BSONSerializeOptions, Document } from '../bson';
import { MongoCompatibilityError, MongoInvalidArgumentError } from '../error';
import { MongoInvalidArgumentError } from '../error';
import { Explain, ExplainOptions } from '../explain';
import type { Logger } from '../logger';
import { ReadConcern } from '../read_concern';
Expand All @@ -18,8 +18,6 @@ import { WriteConcern, WriteConcernOptions } from '../write_concern';
import type { ReadConcernLike } from './../read_concern';
import { AbstractOperation, Aspect, OperationOptions } from './operation';

const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5;

/** @public */
export interface CollationOptions {
locale: string;
Expand Down Expand Up @@ -152,40 +150,24 @@ export abstract class CommandOperation<T> extends AbstractOperation<T> {
options.omitReadPreference = true;
}

if (options.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
callback(
new MongoCompatibilityError(
`Server ${server.name}, which reports wire version ${serverWireVersion}, does not support collation`
)
);
return;
}

if (this.writeConcern && this.hasAspect(Aspect.WRITE_OPERATION) && !inTransaction) {
Object.assign(cmd, { writeConcern: this.writeConcern });
}

if (serverWireVersion >= SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
if (
options.collation &&
typeof options.collation === 'object' &&
!this.hasAspect(Aspect.SKIP_COLLATION)
) {
Object.assign(cmd, { collation: options.collation });
}
if (
options.collation &&
typeof options.collation === 'object' &&
!this.hasAspect(Aspect.SKIP_COLLATION)
) {
Object.assign(cmd, { collation: options.collation });
}

if (typeof options.maxTimeMS === 'number') {
cmd.maxTimeMS = options.maxTimeMS;
}

if (this.hasAspect(Aspect.EXPLAINABLE) && this.explain) {
if (serverWireVersion < 6 && cmd.aggregate) {
// Prior to 3.6, with aggregate, verbosity is ignored, and we must pass in "explain: true"
cmd.explain = true;
} else {
cmd = decorateWithExplain(cmd, this.explain);
}
cmd = decorateWithExplain(cmd, this.explain);
}

server.command(this.ns, cmd, options, callback);
Expand Down
18 changes: 2 additions & 16 deletions src/operations/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Collection } from '../collection';
import { MongoCompatibilityError, MongoServerError } from '../error';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { Callback, collationNotSupported, maxWireVersion, MongoDBNamespace } from '../utils';
import type { Callback, MongoDBNamespace } from '../utils';
import type { WriteConcernOptions } from '../write_concern';
import { CollationOptions, CommandOperation, CommandOperationOptions } from './command';
import { Aspect, defineAspects, Hint } from './operation';
Expand Down Expand Up @@ -82,28 +82,14 @@ export class DeleteOperation extends CommandOperation<Document> {
command.comment = options.comment;
}

if (options.explain != null && maxWireVersion(server) < 3) {
return callback
? callback(
new MongoCompatibilityError(`Server ${server.name} does not support explain on delete`)
)
: undefined;
}

const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0;
if (unacknowledgedWrite || maxWireVersion(server) < 5) {
if (unacknowledgedWrite) {
if (this.statements.find((o: Document) => o.hint)) {
callback(new MongoCompatibilityError(`Servers < 3.4 do not support hint on delete`));
return;
}
}

const statementWithCollation = this.statements.find(statement => !!statement.collation);
if (statementWithCollation && collationNotSupported(server, statementWithCollation)) {
callback(new MongoCompatibilityError(`Server ${server.name} does not support collation`));
return;
}

super.executeCommand(server, session, command, callback);
}
}
Expand Down
29 changes: 2 additions & 27 deletions src/operations/find.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import type { Document } from '../bson';
import type { Collection } from '../collection';
import { MongoCompatibilityError, MongoInvalidArgumentError } from '../error';
import { MongoInvalidArgumentError } from '../error';
import { ReadConcern } from '../read_concern';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { formatSort, Sort } from '../sort';
import {
Callback,
decorateWithExplain,
maxWireVersion,
MongoDBNamespace,
normalizeHintField
} from '../utils';
import { Callback, decorateWithExplain, MongoDBNamespace, normalizeHintField } from '../utils';
import { CollationOptions, CommandOperation, CommandOperationOptions } from './command';
import { Aspect, defineAspects, Hint } from './operation';

Expand Down Expand Up @@ -70,8 +64,6 @@ export interface FindOptions<TSchema extends Document = Document> extends Comman
oplogReplay?: boolean;
}

const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5;

/** @internal */
export class FindOperation extends CommandOperation<Document> {
override options: FindOptions;
Expand Down Expand Up @@ -113,24 +105,7 @@ export class FindOperation extends CommandOperation<Document> {
): void {
this.server = server;

const serverWireVersion = maxWireVersion(server);
const options = this.options;
if (options.allowDiskUse != null && serverWireVersion < 4) {
callback(
new MongoCompatibilityError('Option "allowDiskUse" is not supported on MongoDB < 3.2')
);
return;
}

if (options.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
callback(
new MongoCompatibilityError(
`Server ${server.name}, which reports wire version ${serverWireVersion}, does not support collation`
)
);

return;
}

let findCommand = makeFindCommand(this.ns, this.filter, options);
if (this.explain) {
Expand Down
9 changes: 0 additions & 9 deletions src/operations/find_and_modify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,6 @@ class FindAndModifyOperation extends CommandOperation<Document> {
cmd.hint = options.hint;
}

if (this.explain && maxWireVersion(server) < 4) {
callback(
new MongoCompatibilityError(
`Server ${server.name} does not support explain on findAndModify`
)
);
return;
}

// Execute the command
super.executeCommand(server, session, cmd, (err, result) => {
if (err) return callback(err);
Expand Down
33 changes: 2 additions & 31 deletions src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ import type { Collection } from '../collection';
import { MongoCompatibilityError, MongoInvalidArgumentError, MongoServerError } from '../error';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import {
Callback,
collationNotSupported,
hasAtomicOperators,
maxWireVersion,
MongoDBNamespace
} from '../utils';
import { Callback, hasAtomicOperators, MongoDBNamespace } from '../utils';
import { CollationOptions, CommandOperation, CommandOperationOptions } from './command';
import { Aspect, defineAspects, Hint } from './operation';

Expand Down Expand Up @@ -113,37 +107,14 @@ export class UpdateOperation extends CommandOperation<Document> {
command.comment = options.comment;
}

const statementWithCollation = this.statements.find(statement => !!statement.collation);
if (
collationNotSupported(server, options) ||
(statementWithCollation && collationNotSupported(server, statementWithCollation))
) {
callback(new MongoCompatibilityError(`Server ${server.name} does not support collation`));
return;
}

const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0;
if (unacknowledgedWrite || maxWireVersion(server) < 5) {
if (unacknowledgedWrite) {
if (this.statements.find((o: Document) => o.hint)) {
callback(new MongoCompatibilityError(`Servers < 3.4 do not support hint on update`));
return;
}
}

if (this.explain && maxWireVersion(server) < 3) {
callback(
new MongoCompatibilityError(`Server ${server.name} does not support explain on update`)
);
return;
}

if (this.statements.some(statement => !!statement.arrayFilters) && maxWireVersion(server) < 6) {
callback(
new MongoCompatibilityError('Option "arrayFilters" is only supported on MongoDB 3.6+')
);
return;
}

super.executeCommand(server, session, command, callback);
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/sdam/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
isNetworkErrorBeforeHandshake,
isNodeShuttingDownError,
isSDAMUnrecoverableError,
MongoCompatibilityError,
MongoError,
MongoErrorLabel,
MongoInvalidArgumentError,
Expand All @@ -41,7 +40,6 @@ import type { ClientSession } from '../sessions';
import { isTransactionCommand } from '../transactions';
import {
Callback,
collationNotSupported,
EventEmitterWithState,
makeStateMachine,
maxWireVersion,
Expand Down Expand Up @@ -314,12 +312,6 @@ export class Server extends TypedEventEmitter<ServerEvents> {
delete finalOptions.readPreference;
}

// error if collation not supported
if (collationNotSupported(this, cmd)) {
callback(new MongoCompatibilityError(`Server ${this.name} does not support collation`));
return;
}

const session = finalOptions.session;
const conn = session?.pinnedConnection;

Expand Down
11 changes: 0 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,17 +563,6 @@ export function maxWireVersion(topologyOrServer?: Connection | Topology | Server
return 0;
}

/**
* Checks that collation is supported by server.
* @internal
*
* @param server - to check against
* @param cmd - object where collation may be specified
*/
export function collationNotSupported(server: Server, cmd: Document): boolean {
return cmd && cmd.collation && maxWireVersion(server) < 5;
}

/**
* Applies the function `eachFn` to each item in `arr`, in parallel.
* @internal
Expand Down

0 comments on commit 69b0dd8

Please sign in to comment.