Skip to content

RUST-2030 Add more event fields: lsid, txnNumber and disambiguatedPaths #1197

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

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions src/change_stream/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ pub struct ChangeStreamEvent<T> {
/// The new name for the `ns` collection. Only included for `OperationType::Rename`.
pub to: Option<ChangeNamespace>,

/// The identifier for the session associated with the transaction.
/// Only present if the operation is part of a multi-document transaction.
pub lsid: Option<Document>,

/// Together with the lsid, a number that helps uniquely identify a transaction.
/// Only present if the operation is part of a multi-document transaction.
pub txn_number: Option<i64>,

/// A `Document` that contains the `_id` of the document created or modified by the `insert`,
/// `replace`, `delete`, `update` operations (i.e. CRUD operations). For sharded collections,
/// also displays the full shard key for the document. The `_id` field is not repeated if it is
Expand Down Expand Up @@ -126,6 +134,12 @@ pub struct UpdateDescription {

/// Arrays that were truncated in the `Document`.
pub truncated_arrays: Option<Vec<TruncatedArray>>,

/// When an update event reports changes involving ambiguous fields, the disambiguatedPaths
/// document provides the path key with an array listing each path component.
/// Note: The disambiguatedPaths field is only available on change streams started with the
/// showExpandedEvents option
pub disambiguated_paths: Option<Document>,
}

/// Describes an array that has been truncated.
Expand Down
52 changes: 52 additions & 0 deletions src/test/change_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,58 @@ async fn split_large_event() -> Result<()> {
Ok(())
}

/// Test that transaction fields are parsed correctly
#[tokio::test]
async fn transaction_fields() -> Result<()> {
let (client, coll, mut stream) =
match init_stream("chang_stream_transaction_fields", true).await? {
Some(t) => t,
None => return Ok(()),
};
if client.is_sharded() {
log_uncaptured("skipping change stream test transaction_fields on unsupported topology");
return Ok(());
}
if !VersionReq::parse(">=5.0")
.unwrap()
.matches(&client.server_version)
{
log_uncaptured(format!(
"skipping change stream test transaction_fields on unsupported version {:?}",
client.server_version
));
return Ok(());
}
if !client.supports_transactions() {
log_uncaptured(
"skipping change stream transaction_fields test due to lack of transaction support",
);
return Ok(());
}

let mut session = client.start_session().await.unwrap();
let session_id = session.id().get("id").cloned();
assert!(session_id.is_some());
session.start_transaction().await.unwrap();
coll.insert_one(doc! {"_id": 1})
.session(&mut session)
.await?;
session.commit_transaction().await.unwrap();

let next_event = stream.next().await.transpose()?;
assert!(matches!(next_event,
Some(ChangeStreamEvent {
operation_type: OperationType::Insert,
document_key: Some(key),
lsid: Some(lsid),
txn_number: Some(1),
..
}) if key == doc! { "_id": 1 } && lsid.get("id") == session_id.as_ref()
Copy link
Contributor Author

@arthurprs arthurprs Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lsid contains a second field "uid", which I'm not familiar with. But the test should still be valid.

));

Ok(())
}

// Regression test: `Collection::watch` uses the type parameter. This is not flagged as a test to
// run because it's just asserting that this compiles.
#[allow(unreachable_code, unused_variables, clippy::diverging_sub_expression)]
Expand Down