Skip to content

Commit 03795d2

Browse files
authored
DRIVERS-2782: Expose snapshotTime (#1843)
1 parent d41d48b commit 03795d2

File tree

5 files changed

+1286
-14
lines changed

5 files changed

+1286
-14
lines changed

source/sessions/snapshot-sessions.md

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,28 @@ response. The `atClusterTime` field represents the timestamp of the read and is
6363
## Specification
6464

6565
An application requests snapshot reads by creating a `ClientSession` with options that specify that snapshot reads are
66-
desired. An application then passes the session as an argument to methods in the `MongoDatabase` and `MongoCollection`
67-
classes. Read operations (find/aggregate/distinct) performed against that session will be read from the same snapshot.
66+
desired and optionally specifying a `snapshotTime`. An application then passes the session as an argument to methods in
67+
the `MongoDatabase` and `MongoCollection` classes. Read operations (find/aggregate/distinct) performed against that
68+
session will be read from the same snapshot.
6869

6970
## High level summary of the API changes for snapshot reads
7071

7172
Snapshot reads are built on top of client sessions.
7273

73-
Applications will start a new client session for snapshot reads like this:
74+
Applications will start a new client session for snapshot reads and possibly retrieve the snapshot time like this:
7475

7576
```typescript
76-
options = new SessionOptions(snapshot = true);
77+
options = new SessionOptions(snapshot = true, snapshotTime = timestampValue);
7778
session = client.startSession(options);
79+
snapshotTime = session.snapshotTime;
7880
```
7981

8082
All read operations performed using this session will be read from the same snapshot.
8183

82-
If no value is provided for `snapshot` a value of false is implied. There are no MongoDatabase, MongoClient, or
83-
MongoCollection API changes.
84+
If no value is provided for `snapshot` a value of false is implied. `snapshotTime` is an optional parameter and if not
85+
passed the snapshot time will be set internally after the first find/aggregate/distinct operation inside the session.
86+
87+
There are no MongoDatabase, MongoClient, or MongoCollection API changes.
8488

8589
## SessionOptions changes
8690

@@ -89,14 +93,16 @@ MongoCollection API changes.
8993
```typescript
9094
class SessionOptions {
9195
Optional<bool> snapshot;
96+
Optional<BsonTimestamp> snapshotTime;
9297

9398
// other options defined by other specs
9499
}
95100
```
96101

97-
In order to support snapshot reads a new property named `snapshot` is added to `SessionOptions`. Applications set
98-
`snapshot` when starting a client session to indicate whether they want snapshot reads. All read operations performed
99-
using that client session will share the same snapshot.
102+
In order to support snapshot reads two properties called `snapshot` and `snapshotTime` are added to `SessionOptions`.
103+
Applications set `snapshot` when starting a client session to indicate whether they want snapshot reads and optionally
104+
set `snapshotTime` to specify the desired snapshot time beforehand. All read operations performed using that client
105+
session will share the same snapshot.
100106

101107
Each new member is documented below.
102108

@@ -110,8 +116,29 @@ Snapshot reads and causal consistency are mutually exclusive. Therefore if `snap
110116
`causalConsistency` must be false. Client MUST throw an error if both `snapshot` and `causalConsistency` are set to
111117
true. Snapshot reads are supported on both primaries and secondaries.
112118

119+
### snapshotTime
120+
121+
Applications set `snapshotTime` when starting a snapshot session to specify the desired snapshot time.
122+
123+
Note that the `snapshotTime` property is optional. The default value of this property is null.
124+
125+
Client MUST throw an error if `snapshotTime` is set and `snapshot` is not set to true.
126+
113127
## ClientSession changes
114128

129+
A readonly property called `snapshotTime` will be added to `ClientSession` that allows applications to retrieve the
130+
snapshot time of the session:
131+
132+
```typescript
133+
class ClientSession {
134+
readonly Optional<BsonTimestamp> snapshotTime;
135+
136+
// other options defined by other specs
137+
}
138+
```
139+
140+
Getting the value of `snapshotTime` on a non-snapshot session MUST raise an error.
141+
115142
Transactions are not allowed with snapshot sessions. Calling `session.startTransaction(options)` on a snapshot session
116143
MUST raise an error.
117144

@@ -123,10 +150,12 @@ MUST raise an error.
123150

124151
There are no new server commands related to snapshot reads. Instead, snapshot reads are implemented by:
125152

126-
1. Saving the `atClusterTime` returned by 5.0+ servers for the first find/aggregate/distinct operation in a private
127-
`snapshotTime` property of the `ClientSession` object. Drivers MUST save `atClusterTime` in the `ClientSession`
128-
object.
129-
2. Passing that `snapshotTime` in the `atClusterTime` field of the `readConcern` field for subsequent snapshot read
153+
1. If `snapshotTime` is specified in `SessionOptions`, saving the value in a `snapshotTime` property of the
154+
`ClientSession`.
155+
2. If `snapshotTime` is not specified in `SessionOptions`, saving the `atClusterTime` returned by 5.0+ servers for the
156+
first find/aggregate/distinct operation in a `snapshotTime` property of the `ClientSession` object. Drivers MUST
157+
save `atClusterTime` in the `ClientSession` object.
158+
3. Passing that `snapshotTime` in the `atClusterTime` field of the `readConcern` field for subsequent snapshot read
130159
operations (i.e. find/aggregate/distinct commands).
131160

132161
## Server Command Responses
@@ -241,6 +270,7 @@ C# driver will provide the reference implementation. The corresponding ticket is
241270

242271
## Changelog
243272

273+
- 2025-09-23: Exposed snapshotTime to applications.
244274
- 2024-05-08: Migrated from reStructuredText to Markdown.
245275
- 2021-06-15: Initial version.
246276
- 2021-06-28: Raise client side error on < 5.0.

source/sessions/tests/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ As part of the test setup for these cases, create a `MongoClient` pointed at the
2929
in the test case and verify that the test server does NOT define a value for `logicalSessionTimeoutMinutes` by sending a
3030
hello command and checking the response.
3131

32+
## Specific operations and behaviour for unified tests
33+
34+
An operation on sessions called `getSnapshotTime` must be supported for unified tests. This operation returns the value
35+
of `snapshotTime` on the session, so that it can be used in subsequent operations.
36+
37+
When parsing `snapshotTime` from `sessionOptions` for unified tests, the parsed string is the name of the key for the
38+
actual value of `snapshotTime` to be found in the
39+
[entity map](https://github.com/mongodb/specifications/blob/master/source/unified-test-format/unified-test-format.md#entity-map).
40+
3241
## Prose tests
3342

3443
### 1. Setting both `snapshot` and `causalConsistency` to true is not allowed
@@ -250,8 +259,24 @@ and configure a `MongoClient` with default options.
250259
- Run a ping command using C1 and assert that `$clusterTime` sent is the same as the `clusterTime` recorded earlier.
251260
This assertion proves that C1's `$clusterTime` was not advanced by gossiping through SDAM.
252261

262+
### 21. Having `snapshotTime` set and `snapshot` set to false is not allowed
263+
264+
Snapshot sessions tests require server of version 5.0 or higher and replica set or a sharded cluster deployment.
265+
266+
- Start a session by calling `startSession` with `snapshot = false` and `snapshotTime = new Timestamp(1)`.
267+
- Assert that a client side error was raised.
268+
269+
### 22. Retrieving `snapshotTime` on a non-snapshot session raises an error
270+
271+
Snapshot sessions tests require server of version 5.0 or higher and replica set or a sharded cluster deployment.
272+
273+
- Start a session by calling `startSession` on with `snapshot = false`.
274+
- Try to access the session's snapshot time.
275+
- Assert that a client side error was raised.
276+
253277
## Changelog
254278

279+
- 2025-09-25: Added tests for snapshotTime.
255280
- 2025-02-24: Test drivers do not gossip $clusterTime on SDAM.
256281
- 2024-05-08: Migrated from reStructuredText to Markdown.
257282
- 2019-05-15: Initial version.

0 commit comments

Comments
 (0)