-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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-3392): enable snapshot reads on secondaries #2897
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7acdf80
chore: move non-unified sessions tests to legacy folder; fix readme
dariakp 6e8a655
test: Add the base snapshot reads tests
dariakp cd05607
test: Bring in snapshot spec changes from DRIVERS-1820for server erro…
dariakp 2f8c7fc
first pass at snapshot sessions implementation with up to date spec t…
dariakp ebb9a92
clean up snapshot session implementation
dariakp 3df029e
Pull in latest snapshot-sessions spec tests
dariakp 7c43565
Temporarily skip test runner related test failures and pull in client…
dariakp 53938aa
Pull in unsupported ops snapshot spec tests
dariakp 63062ca
make typescript happy
dariakp c531099
Move server version check for snapshots to operation level
dariakp 9b1d320
Skip distinct client error sessions test
dariakp 73378fa
Clean up
dariakp e077392
Update legacy exports
dariakp 87e981b
Add unit test for causalConsistency and snapshot error
dariakp 52d538d
add unit tests for new errors
dariakp 92737a7
Move getter for consistency
dariakp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ import type { AbstractCursor } from './cursor/abstract_cursor'; | |
import type { CommandOptions } from './cmap/connection'; | ||
import type { WriteConcern } from './write_concern'; | ||
import { TypedEventEmitter } from './mongo_types'; | ||
import { ReadConcernLevel } from './read_concern'; | ||
|
||
const minWireVersionForShardedTransactions = 8; | ||
|
||
|
@@ -51,6 +52,8 @@ function assertAlive(session: ClientSession, callback?: Callback): boolean { | |
export interface ClientSessionOptions { | ||
/** Whether causal consistency should be enabled on this session */ | ||
causalConsistency?: boolean; | ||
/** Whether all read operations should be read from the same snapshot for this session (NOTE: not compatible with `causalConsistency=true`) */ | ||
snapshot?: boolean; | ||
/** The default TransactionOptions to use for transactions started on this session. */ | ||
defaultTransactionOptions?: TransactionOptions; | ||
|
||
|
@@ -72,14 +75,18 @@ export type ClientSessionEvents = { | |
|
||
/** @internal */ | ||
const kServerSession = Symbol('serverSession'); | ||
/** @internal */ | ||
const kSnapshotTime = Symbol('snapshotTime'); | ||
/** @internal */ | ||
const kSnapshotEnabled = Symbol('snapshotEnabled'); | ||
|
||
/** | ||
* A class representing a client session on the server | ||
* | ||
* NOTE: not meant to be instantiated directly. | ||
* @public | ||
*/ | ||
class ClientSession extends TypedEventEmitter<ClientSessionEvents> { | ||
export class ClientSession extends TypedEventEmitter<ClientSessionEvents> { | ||
/** @internal */ | ||
topology: Topology; | ||
/** @internal */ | ||
|
@@ -96,6 +103,14 @@ class ClientSession extends TypedEventEmitter<ClientSessionEvents> { | |
transaction: Transaction; | ||
/** @internal */ | ||
[kServerSession]?: ServerSession; | ||
/** @internal */ | ||
[kSnapshotTime]?: Timestamp; | ||
/** @internal */ | ||
[kSnapshotEnabled] = false; | ||
|
||
get snapshotEnabled(): boolean { | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return this[kSnapshotEnabled]; | ||
} | ||
|
||
/** | ||
* Create a client session. | ||
|
@@ -123,15 +138,23 @@ class ClientSession extends TypedEventEmitter<ClientSessionEvents> { | |
|
||
options = options ?? {}; | ||
|
||
if (options.snapshot === true) { | ||
this[kSnapshotEnabled] = true; | ||
if (options.causalConsistency === true) { | ||
throw new MongoDriverError( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to use a more specific error here now that they are available? |
||
'Properties "causalConsistency" and "snapshot" are mutually exclusive' | ||
); | ||
} | ||
} | ||
|
||
this.topology = topology; | ||
this.sessionPool = sessionPool; | ||
this.hasEnded = false; | ||
this.clientOptions = clientOptions; | ||
this[kServerSession] = undefined; | ||
|
||
this.supports = { | ||
causalConsistency: | ||
typeof options.causalConsistency === 'boolean' ? options.causalConsistency : true | ||
causalConsistency: options.snapshot !== true && options.causalConsistency !== false | ||
}; | ||
|
||
this.clusterTime = options.initialClusterTime; | ||
|
@@ -257,6 +280,10 @@ class ClientSession extends TypedEventEmitter<ClientSessionEvents> { | |
* @param options - Options for the transaction | ||
*/ | ||
startTransaction(options?: TransactionOptions): void { | ||
if (this[kSnapshotEnabled]) { | ||
throw new MongoDriverError('Transactions are not allowed with snapshot sessions'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to use a more specific error here now that they are available? |
||
} | ||
|
||
assertAlive(this); | ||
if (this.inTransaction()) { | ||
throw new MongoDriverError('Transaction already in progress'); | ||
|
@@ -623,7 +650,7 @@ export type ServerSessionId = { id: Binary }; | |
* WARNING: not meant to be instantiated directly. For internal use only. | ||
* @public | ||
*/ | ||
class ServerSession { | ||
export class ServerSession { | ||
id: ServerSessionId; | ||
lastUse: number; | ||
txnNumber: number; | ||
|
@@ -658,7 +685,7 @@ class ServerSession { | |
* For internal use only | ||
* @internal | ||
*/ | ||
class ServerSessionPool { | ||
export class ServerSessionPool { | ||
topology: Topology; | ||
sessions: ServerSession[]; | ||
|
||
|
@@ -746,7 +773,7 @@ class ServerSessionPool { | |
|
||
// TODO: this should be codified in command construction | ||
// @see https://github.com/mongodb/specifications/blob/master/source/read-write-concern/read-write-concern.rst#read-concern | ||
function commandSupportsReadConcern(command: Document, options?: Document): boolean { | ||
export function commandSupportsReadConcern(command: Document, options?: Document): boolean { | ||
if (command.aggregate || command.count || command.distinct || command.find || command.geoNear) { | ||
return true; | ||
} | ||
|
@@ -770,7 +797,7 @@ function commandSupportsReadConcern(command: Document, options?: Document): bool | |
* @param command - the command to decorate | ||
* @param options - Optional settings passed to calling operation | ||
*/ | ||
function applySession( | ||
export function applySession( | ||
session: ClientSession, | ||
command: Document, | ||
options?: CommandOptions | ||
|
@@ -801,28 +828,35 @@ function applySession( | |
// first apply non-transaction-specific sessions data | ||
const inTransaction = session.inTransaction() || isTransactionCommand(command); | ||
const isRetryableWrite = options?.willRetryWrite || false; | ||
const shouldApplyReadConcern = commandSupportsReadConcern(command, options); | ||
|
||
if (serverSession.txnNumber && (isRetryableWrite || inTransaction)) { | ||
command.txnNumber = Long.fromNumber(serverSession.txnNumber); | ||
} | ||
|
||
// now attempt to apply transaction-specific sessions data | ||
if (!inTransaction) { | ||
if (session.transaction.state !== TxnState.NO_TRANSACTION) { | ||
session.transaction.transition(TxnState.NO_TRANSACTION); | ||
} | ||
|
||
// TODO: the following should only be applied to read operation per spec. | ||
// for causal consistency | ||
if (session.supports.causalConsistency && session.operationTime && shouldApplyReadConcern) { | ||
if ( | ||
session.supports.causalConsistency && | ||
session.operationTime && | ||
commandSupportsReadConcern(command, options) | ||
) { | ||
command.readConcern = command.readConcern || {}; | ||
Object.assign(command.readConcern, { afterClusterTime: session.operationTime }); | ||
} else if (session[kSnapshotEnabled]) { | ||
command.readConcern = command.readConcern || { level: ReadConcernLevel.snapshot }; | ||
if (session[kSnapshotTime] !== undefined) { | ||
Object.assign(command.readConcern, { atClusterTime: session[kSnapshotTime] }); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
// now attempt to apply transaction-specific sessions data | ||
|
||
// `autocommit` must always be false to differentiate from retryable writes | ||
command.autocommit = false; | ||
|
||
|
@@ -843,7 +877,7 @@ function applySession( | |
} | ||
} | ||
|
||
function updateSessionFromResponse(session: ClientSession, document: Document): void { | ||
export function updateSessionFromResponse(session: ClientSession, document: Document): void { | ||
if (document.$clusterTime) { | ||
resolveClusterTime(session, document.$clusterTime); | ||
} | ||
|
@@ -855,14 +889,12 @@ function updateSessionFromResponse(session: ClientSession, document: Document): | |
if (document.recoveryToken && session && session.inTransaction()) { | ||
session.transaction._recoveryToken = document.recoveryToken; | ||
} | ||
} | ||
|
||
export { | ||
ClientSession, | ||
ServerSession, | ||
ServerSessionPool, | ||
TxnState, | ||
applySession, | ||
updateSessionFromResponse, | ||
commandSupportsReadConcern | ||
}; | ||
if ( | ||
document.cursor?.atClusterTime && | ||
session?.[kSnapshotEnabled] && | ||
session[kSnapshotTime] === undefined | ||
) { | ||
session[kSnapshotTime] = document.cursor.atClusterTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to use a more specific error here now that they are available?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compat and InvalidArgument are still not available so I think leaving the DriverError in there would make sense for now.