Skip to content

Conversation

papafe
Copy link
Contributor

@papafe papafe commented Sep 18, 2025

At the moment drivers handle internally snapshot reads inside a single session when requested. This is done when a session is opened with snapshot = true, by saving the atClusterTime sent by the server in a private snapshotTime property of the ClientSession. snapshotTime is then passed in all subsequent read operations to the sever. At the moment, following our spec, this is supported internally and only in the context of a single session.
Some users have been requesting us to expose snapshotTime or at least to allow snapshot reads across different sessions.

This PR is a POC for exposing atClusterTime to developers. In the context of this PR atClusterTime has been renamed to SnapshotTime in our public API, to be consistent with our current public terminology.

The changes to the public API are:

  • Added SnapshotTime to ClientSessionOptions.
  • Added SnapshotTime to CoreSessionOptions and added relative constructor.
  • Added extension method GetSnapshotTime on IClientSessionHandle. This has been added as an extension method to avoid having breaking changes.

Example use:

collection.InsertOne(new TestObject { Name = "obj1" });

BsonTimestamp clusterTime1;

var sessionOptions1 = new ClientSessionOptions
{
    Snapshot = true
};

using (var session1 = client.StartSession(sessionOptions1))
{
    var results = GetTestObjects(collection, session1);
    Assert.Equal(1, results.Count);

    clusterTime1 = session1.GetSnapshotTime();
}

//Inserting new object
collection.InsertOne(new TestObject { Name = "obj2" });

var sessionOptions2 = new ClientSessionOptions
{
    Snapshot = true,
    SnapshotTime = clusterTime1
};

//Snapshot read session at clusterTime1 should not see obj2
using (var session2 = client.StartSession(sessionOptions2))
{
    var results = GetTestObjects(collection, session2);
    Assert.Equal(1, results.Count);

    var clusterTime2 = session2.GetSnapshotTime();
    Assert.Equal(clusterTime2, clusterTime1);
}

@papafe papafe added the feature label Sep 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant