Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-6313): add CSOT support to sessions and transactions #4199

Merged
merged 28 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e8001b4
test: sync latest spec tests
nbbeeken Aug 5, 2024
a12f2b6
test: set defaultTimeoutMS on session options
nbbeeken Aug 5, 2024
46e6bfd
docs: fix api doc comment
nbbeeken Aug 5, 2024
1e115e9
chore: create and pass around timeout contexts
nbbeeken Aug 14, 2024
0ea664d
chore: adjust timeouts and correct promise chain
nbbeeken Aug 14, 2024
2ddf2eb
chore: fix when timeout errors throw
nbbeeken Aug 20, 2024
219b506
chore: limit server type
nbbeeken Aug 20, 2024
a082436
test: fix metadata
nbbeeken Aug 21, 2024
008de84
chore: drop collection once
nbbeeken Aug 21, 2024
c1d34e5
test: improve prose test
nbbeeken Aug 21, 2024
9cb3fe5
fix: always pass timeoutMS value
nbbeeken Aug 23, 2024
8f961b1
fix: make timeout take options object
nbbeeken Aug 23, 2024
a84a482
fix: add documentation of timeoutMS in withTransaction
nbbeeken Aug 23, 2024
eab394f
fix: add validation for timeoutMS in withTxn operations
nbbeeken Aug 23, 2024
a55fd6a
fix: only and metadata
nbbeeken Aug 23, 2024
ec174f5
chore: fix collection drop hang
nbbeeken Aug 26, 2024
174df9e
chore: remove is
nbbeeken Aug 26, 2024
7413eeb
chore: limit test to 4.4+
nbbeeken Aug 26, 2024
a3b04c9
chore: assert approximate timeout was respected
nbbeeken Aug 26, 2024
24edc0b
test: improve instanceof assertion logging
nbbeeken Aug 26, 2024
4ba842d
test: assert time first
nbbeeken Aug 27, 2024
c45b166
chore: skip test
nbbeeken Aug 29, 2024
8dc142f
chore: include more operations in validation
nbbeeken Aug 29, 2024
2e40d2b
chore: reduce condition for timeoutContext in txn
nbbeeken Aug 29, 2024
63d95e1
chore: take session into create and remove csotEnabled condition
nbbeeken Aug 29, 2024
b2096e9
chore: adjust timeouts to match spec change
nbbeeken Aug 29, 2024
8930481
fix: typescript
nbbeeken Sep 3, 2024
d6fd82e
chore: name the variable
nbbeeken Sep 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 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 @@ -99,7 +99,7 @@
"mocha": "^10.4.0",
"mocha-sinon": "^2.1.2",
"mongodb-client-encryption": "^6.1.0",
"mongodb-legacy": "^6.0.1",
"mongodb-legacy": "^6.1.1",
"nyc": "^15.1.0",
"prettier": "^2.8.8",
"semver": "^7.6.0",
Expand Down
7 changes: 7 additions & 0 deletions src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,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
13 changes: 13 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,22 @@ export class MongoUnexpectedServerResponseError extends MongoRuntimeError {
* @internal
*/
export class MongoOperationTimeoutError extends MongoRuntimeError {
get [Symbol.toStringTag]() {
return 'MongoOperationTimeoutError';
}

override get name(): string {
return 'MongoOperationTimeoutError';
}

static is(error: unknown): error is MongoOperationTimeoutError {
return (
error != null &&
typeof error === 'object' &&
Symbol.toStringTag in error &&
error[Symbol.toStringTag] === 'MongoOperationTimeoutError'
);
}
}

/**
Expand Down
17 changes: 7 additions & 10 deletions src/operations/execute_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ export async function executeOperation<
} else if (session.client !== client) {
throw new MongoInvalidArgumentError('ClientSession must be from the same MongoClient');
}
baileympearson marked this conversation as resolved.
Show resolved Hide resolved
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,11 +102,13 @@ export async function executeOperation<
session.unpin();
}

timeoutContext ??= TimeoutContext.create({
serverSelectionTimeoutMS: client.s.options.serverSelectionTimeoutMS,
waitQueueTimeoutMS: client.s.options.waitQueueTimeoutMS,
timeoutMS: operation.options.timeoutMS
});
timeoutContext ??=
session.timeoutContext ??
TimeoutContext.create({
baileympearson marked this conversation as resolved.
Show resolved Hide resolved
serverSelectionTimeoutMS: client.s.options.serverSelectionTimeoutMS,
waitQueueTimeoutMS: client.s.options.waitQueueTimeoutMS,
timeoutMS: operation.options.timeoutMS ?? session.timeoutMS
});

try {
return await tryOperation(operation, {
Expand Down
Loading