Skip to content

Commit

Permalink
feat(NODE-6313): add CSOT support to sessions and transactions (#4199)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Sep 12, 2024
1 parent c63d102 commit 1eab23d
Show file tree
Hide file tree
Showing 24 changed files with 776 additions and 233 deletions.
82 changes: 41 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"mocha": "^10.4.0",
"mocha-sinon": "^2.1.2",
"mongodb-client-encryption": "^6.1.0",
"mongodb-legacy": "^6.1.0",
"mongodb-legacy": "^6.1.1",
"nyc": "^15.1.0",
"prettier": "^3.3.3",
"semver": "^7.6.3",
Expand Down
7 changes: 7 additions & 0 deletions src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,13 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
return;
}
}
} catch (readError) {
if (TimeoutError.is(readError)) {
throw new MongoOperationTimeoutError(
`Timed out during socket read (${readError.duration}ms)`
);
}
throw readError;
} finally {
this.dataEvents = null;
this.throwIfAborted();
Expand Down
15 changes: 7 additions & 8 deletions src/cmap/wire_protocol/on_data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type EventEmitter } from 'events';

import { MongoOperationTimeoutError } from '../../error';
import { type TimeoutContext, TimeoutError } from '../../timeout';
import { type TimeoutContext } from '../../timeout';
import { List, promiseWithResolvers } from '../../utils';

/**
Expand Down Expand Up @@ -91,8 +90,11 @@ export function onData(
// Adding event handlers
emitter.on('data', eventHandler);
emitter.on('error', errorHandler);

const timeoutForSocketRead = timeoutContext?.timeoutForSocketRead;
timeoutForSocketRead?.throwIfExpired();
// eslint-disable-next-line github/no-then
timeoutContext?.timeoutForSocketRead?.then(undefined, errorHandler);
timeoutForSocketRead?.then(undefined, errorHandler);

return iterator;

Expand All @@ -104,12 +106,9 @@ export function onData(

function errorHandler(err: Error) {
const promise = unconsumedPromises.shift();
const timeoutError = TimeoutError.is(err)
? new MongoOperationTimeoutError('Timed out during socket read')
: undefined;

if (promise != null) promise.reject(timeoutError ?? err);
else error = timeoutError ?? err;
if (promise != null) promise.reject(err);
else error = err;
void closeHandler();
}

Expand Down
12 changes: 8 additions & 4 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,14 @@ export class Collection<TSchema extends Document = Document> {
// Intentionally, we do not inherit options from parent for this operation.
return await executeOperation(
this.client,
new RenameOperation(this as TODO_NODE_3286, newName, {
...options,
readPreference: ReadPreference.PRIMARY
}) as TODO_NODE_3286
new RenameOperation(
this as TODO_NODE_3286,
newName,
resolveOptions(undefined, {
...options,
readPreference: ReadPreference.PRIMARY
})
) as TODO_NODE_3286
);
}

Expand Down
22 changes: 15 additions & 7 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,16 @@ export class Db {
// Intentionally, we do not inherit options from parent for this operation.
return await executeOperation(
this.client,
new RunCommandOperation(this, command, {
...resolveBSONOptions(options),
timeoutMS: options?.timeoutMS ?? this.timeoutMS,
session: options?.session,
readPreference: options?.readPreference
})
new RunCommandOperation(
this,
command,
resolveOptions(undefined, {
...resolveBSONOptions(options),
timeoutMS: options?.timeoutMS ?? this.timeoutMS,
session: options?.session,
readPreference: options?.readPreference
})
)
);
}

Expand Down Expand Up @@ -385,7 +389,11 @@ export class Db {
new RenameOperation(
this.collection<TSchema>(fromCollection) as TODO_NODE_3286,
toCollection,
{ ...options, new_collection: true, readPreference: ReadPreference.primary }
resolveOptions(undefined, {
...options,
new_collection: true,
readPreference: ReadPreference.primary
})
) as TODO_NODE_3286
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ function isAggregateError(e: unknown): e is Error & { errors: Error[] } {
* mongodb-client-encryption has a dependency on this error, it uses the constructor with a string argument
*/
export class MongoError extends Error {
get [Symbol.toStringTag]() {
return this.name;
}
/** @internal */
[kErrorLabels]: Set<string>;
/**
Expand Down
8 changes: 2 additions & 6 deletions src/operations/execute_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type ResultTypeFromOperation<TOperation> =
export async function executeOperation<
T extends AbstractOperation<TResult>,
TResult = ResultTypeFromOperation<T>
>(client: MongoClient, operation: T, timeoutContext?: TimeoutContext): Promise<TResult> {
>(client: MongoClient, operation: T, timeoutContext?: TimeoutContext | null): Promise<TResult> {
if (!(operation instanceof AbstractOperation)) {
// TODO(NODE-3483): Extend MongoRuntimeError
throw new MongoRuntimeError('This method requires a valid operation instance');
Expand All @@ -81,11 +81,6 @@ export async function executeOperation<
} else if (session.client !== client) {
throw new MongoInvalidArgumentError('ClientSession must be from the same MongoClient');
}
if (session.explicit && session?.timeoutMS != null && operation.options.timeoutMS != null) {
throw new MongoInvalidArgumentError(
'Do not specify timeoutMS on operation if already specified on an explicit session'
);
}

const readPreference = operation.readPreference ?? ReadPreference.primary;
const inTransaction = !!session?.inTransaction();
Expand All @@ -107,6 +102,7 @@ export async function executeOperation<
}

timeoutContext ??= TimeoutContext.create({
session,
serverSelectionTimeoutMS: client.s.options.serverSelectionTimeoutMS,
waitQueueTimeoutMS: client.s.options.waitQueueTimeoutMS,
timeoutMS: operation.options.timeoutMS
Expand Down
Loading

0 comments on commit 1eab23d

Please sign in to comment.