Skip to content

Commit cf2892b

Browse files
authored
CSHARP-5472: Investigate changes in SERVER-97071: Ensure stability of resumeTokens across versions (#1634)
1 parent 1d5b551 commit cf2892b

File tree

8 files changed

+316
-3
lines changed

8 files changed

+316
-3
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"description": "change-streams-nsType",
3+
"schemaVersion": "1.7",
4+
"runOnRequirements": [
5+
{
6+
"minServerVersion": "8.1.0",
7+
"topologies": [
8+
"replicaset",
9+
"sharded"
10+
],
11+
"serverless": "forbid"
12+
}
13+
],
14+
"createEntities": [
15+
{
16+
"client": {
17+
"id": "client0",
18+
"useMultipleMongoses": false
19+
}
20+
},
21+
{
22+
"database": {
23+
"id": "database0",
24+
"client": "client0",
25+
"databaseName": "database0"
26+
}
27+
}
28+
],
29+
"tests": [
30+
{
31+
"description": "nsType is present when creating collections",
32+
"operations": [
33+
{
34+
"name": "dropCollection",
35+
"object": "database0",
36+
"arguments": {
37+
"collection": "foo"
38+
}
39+
},
40+
{
41+
"name": "createChangeStream",
42+
"object": "database0",
43+
"arguments": {
44+
"pipeline": [],
45+
"showExpandedEvents": true
46+
},
47+
"saveResultAsEntity": "changeStream0"
48+
},
49+
{
50+
"name": "createCollection",
51+
"object": "database0",
52+
"arguments": {
53+
"collection": "foo"
54+
}
55+
},
56+
{
57+
"name": "iterateUntilDocumentOrError",
58+
"object": "changeStream0",
59+
"expectResult": {
60+
"operationType": "create",
61+
"nsType": "collection"
62+
}
63+
}
64+
]
65+
},
66+
{
67+
"description": "nsType is present when creating timeseries",
68+
"operations": [
69+
{
70+
"name": "dropCollection",
71+
"object": "database0",
72+
"arguments": {
73+
"collection": "foo"
74+
}
75+
},
76+
{
77+
"name": "createChangeStream",
78+
"object": "database0",
79+
"arguments": {
80+
"pipeline": [],
81+
"showExpandedEvents": true
82+
},
83+
"saveResultAsEntity": "changeStream0"
84+
},
85+
{
86+
"name": "createCollection",
87+
"object": "database0",
88+
"arguments": {
89+
"collection": "foo",
90+
"timeseries": {
91+
"timeField": "time",
92+
"metaField": "meta",
93+
"granularity": "minutes"
94+
}
95+
}
96+
},
97+
{
98+
"name": "iterateUntilDocumentOrError",
99+
"object": "changeStream0",
100+
"expectResult": {
101+
"operationType": "create",
102+
"nsType": "timeseries"
103+
}
104+
}
105+
]
106+
},
107+
{
108+
"description": "nsType is present when creating views",
109+
"operations": [
110+
{
111+
"name": "dropCollection",
112+
"object": "database0",
113+
"arguments": {
114+
"collection": "foo"
115+
}
116+
},
117+
{
118+
"name": "createChangeStream",
119+
"object": "database0",
120+
"arguments": {
121+
"pipeline": [],
122+
"showExpandedEvents": true
123+
},
124+
"saveResultAsEntity": "changeStream0"
125+
},
126+
{
127+
"name": "createCollection",
128+
"object": "database0",
129+
"arguments": {
130+
"collection": "foo",
131+
"viewOn": "testName"
132+
}
133+
},
134+
{
135+
"name": "iterateUntilDocumentOrError",
136+
"object": "changeStream0",
137+
"expectResult": {
138+
"operationType": "create",
139+
"nsType": "view"
140+
}
141+
}
142+
]
143+
}
144+
]
145+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
description: "change-streams-nsType"
2+
schemaVersion: "1.7"
3+
runOnRequirements:
4+
- minServerVersion: "8.1.0"
5+
topologies: [ replicaset, sharded ]
6+
serverless: forbid
7+
createEntities:
8+
- client:
9+
id: &client0 client0
10+
useMultipleMongoses: false
11+
- database:
12+
id: &database0 database0
13+
client: *client0
14+
databaseName: *database0
15+
16+
tests:
17+
- description: "nsType is present when creating collections"
18+
operations:
19+
- name: dropCollection
20+
object: *database0
21+
arguments:
22+
collection: &collection0 foo
23+
- name: createChangeStream
24+
object: *database0
25+
arguments:
26+
pipeline: []
27+
showExpandedEvents: true
28+
saveResultAsEntity: &changeStream0 changeStream0
29+
- name: createCollection
30+
object: *database0
31+
arguments:
32+
collection: *collection0
33+
- name: iterateUntilDocumentOrError
34+
object: *changeStream0
35+
expectResult:
36+
operationType: create
37+
nsType: collection
38+
39+
- description: "nsType is present when creating timeseries"
40+
operations:
41+
- name: dropCollection
42+
object: *database0
43+
arguments:
44+
collection: &collection0 foo
45+
- name: createChangeStream
46+
object: *database0
47+
arguments:
48+
pipeline: []
49+
showExpandedEvents: true
50+
saveResultAsEntity: &changeStream0 changeStream0
51+
- name: createCollection
52+
object: *database0
53+
arguments:
54+
collection: *collection0
55+
timeseries:
56+
timeField: "time"
57+
metaField: "meta"
58+
granularity: "minutes"
59+
- name: iterateUntilDocumentOrError
60+
object: *changeStream0
61+
expectResult:
62+
operationType: create
63+
nsType: timeseries
64+
65+
- description: "nsType is present when creating views"
66+
operations:
67+
- name: dropCollection
68+
object: *database0
69+
arguments:
70+
collection: &collection0 foo
71+
- name: createChangeStream
72+
object: *database0
73+
arguments:
74+
pipeline: []
75+
showExpandedEvents: true
76+
saveResultAsEntity: &changeStream0 changeStream0
77+
- name: createCollection
78+
object: *database0
79+
arguments:
80+
collection: *collection0
81+
viewOn: testName
82+
- name: iterateUntilDocumentOrError
83+
object: *changeStream0
84+
expectResult:
85+
operationType: create
86+
nsType: view

src/MongoDB.Driver/Core/ChangeStreamDocument.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using MongoDB.Bson;
1818
using MongoDB.Bson.Serialization;
1919
using MongoDB.Bson.Serialization.Attributes;
20+
using MongoDB.Driver.Core;
2021

2122
namespace MongoDB.Driver
2223
{
@@ -186,6 +187,15 @@ public TDocument FullDocumentBeforeChange
186187
}
187188
}
188189

190+
/// <summary>
191+
/// The type of the newly created object.
192+
/// Only present when the showExpandedEvents change stream option is enabled and for the following event types (MongoDB 8.1 and later):
193+
/// <list type="bullet">
194+
/// <item><description><see cref="ChangeStreamOperationType.Create"/></description></item>
195+
/// </list>
196+
/// </summary>
197+
public ChangeStreamNamespaceType NamespaceType => GetValue(nameof(NamespaceType), ChangeStreamNamespaceType.Unknown);
198+
189199
/// <summary>
190200
/// Gets the description for the operation.
191201
/// Only present when the showExpandedEvents change stream option is enabled and for the following event types (MongoDB 6.0 and later):

src/MongoDB.Driver/Core/ChangeStreamDocumentSerializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
using MongoDB.Bson;
1717
using MongoDB.Bson.Serialization;
1818
using MongoDB.Bson.Serialization.Serializers;
19+
using MongoDB.Driver.Core;
1920
using MongoDB.Driver.Core.Misc;
2021

2122
namespace MongoDB.Driver
2223
{
23-
2424
/// <summary>
2525
/// A serializer for ChangeStreamDocument instances.
2626
/// </summary>
@@ -47,6 +47,7 @@ public ChangeStreamDocumentSerializer(
4747
RegisterMember("DocumentKey", "documentKey", BsonDocumentSerializer.Instance);
4848
RegisterMember("FullDocument", "fullDocument", _documentSerializer);
4949
RegisterMember("FullDocumentBeforeChange", "fullDocumentBeforeChange", _documentSerializer);
50+
RegisterMember("NamespaceType", "nsType", ChangeStreamNamespaceTypeSerializer.Instance);
5051
RegisterMember("OperationDescription", "operationDescription", BsonDocumentSerializer.Instance);
5152
RegisterMember("OperationType", "operationType", ChangeStreamOperationTypeSerializer.Instance);
5253
RegisterMember("RenameTo", "to", ChangeStreamDocumentCollectionNamespaceSerializer.Instance);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
namespace MongoDB.Driver.Core
17+
{
18+
/// <summary>
19+
/// Describing the type of the changed object.
20+
/// </summary>
21+
public enum ChangeStreamNamespaceType
22+
{
23+
/// <summary>
24+
/// Unknown namespace type.
25+
/// </summary>
26+
Unknown = 0,
27+
/// <summary>
28+
/// Collection.
29+
/// </summary>
30+
Collection,
31+
/// <summary>
32+
/// Timeseries.
33+
/// </summary>
34+
Timeseries,
35+
/// <summary>
36+
/// View.
37+
/// </summary>
38+
View
39+
}
40+
}
41+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson.Serialization;
17+
using MongoDB.Bson.Serialization.Serializers;
18+
19+
namespace MongoDB.Driver.Core
20+
{
21+
internal static class ChangeStreamNamespaceTypeSerializer
22+
{
23+
public static IBsonSerializer Instance = new EnumSerializer<ChangeStreamNamespaceType>();
24+
}
25+
}
26+

tests/MongoDB.Driver.Tests/Core/ChangeStreamDocumentSerializerTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using MongoDB.Bson.Serialization.Serializers;
2323
using MongoDB.Bson.TestHelpers;
2424
using MongoDB.Bson.TestHelpers.Reflectors;
25+
using MongoDB.Driver.Core;
2526
using Xunit;
2627

2728
namespace MongoDB.Driver
@@ -36,14 +37,15 @@ public void constructor_should_initialize_instance()
3637
var result = new ChangeStreamDocumentSerializer<BsonDocument>(documentSerializer);
3738

3839
result._documentSerializer().Should().BeSameAs(documentSerializer);
39-
result._memberSerializationInfo().Count.Should().Be(14);
40+
result._memberSerializationInfo().Count.Should().Be(15);
4041
AssertRegisteredMember(result, "ClusterTime", "clusterTime", BsonTimestampSerializer.Instance);
4142
AssertRegisteredMember(result, "CollectionNamespace", "ns", ChangeStreamDocumentCollectionNamespaceSerializer.Instance);
4243
AssertRegisteredMember(result, "CollectionUuid", "collectionUUID", GuidSerializer.StandardInstance);
4344
AssertRegisteredMember(result, "DatabaseNamespace", "ns", ChangeStreamDocumentDatabaseNamespaceSerializer.Instance);
4445
AssertRegisteredMember(result, "DocumentKey", "documentKey", BsonDocumentSerializer.Instance);
4546
AssertRegisteredMember(result, "FullDocument", "fullDocument", documentSerializer);
4647
AssertRegisteredMember(result, "FullDocumentBeforeChange", "fullDocumentBeforeChange", documentSerializer);
48+
AssertRegisteredMember(result, "NamespaceType", "nsType", ChangeStreamNamespaceTypeSerializer.Instance);
4749
AssertRegisteredMember(result, "OperationDescription", "operationDescription", BsonDocumentSerializer.Instance);
4850
AssertRegisteredMember(result, "OperationType", "operationType", ChangeStreamOperationTypeSerializer.Instance);
4951
AssertRegisteredMember(result, "RenameTo", "to", ChangeStreamDocumentCollectionNamespaceSerializer.Instance);

tests/MongoDB.Driver.Tests/UnifiedTestOperations/UnifiedChangeStreamDocumentConverter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
using System.Linq;
1818
using MongoDB.Bson;
1919
using MongoDB.Bson.Serialization.Serializers;
20+
using MongoDB.Driver.Core;
2021
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
2122

2223
namespace MongoDB.Driver.Tests.UnifiedTestOperations
2324
{
2425
public class UnifiedChangeStreamDocumentConverter
2526
{
26-
private static readonly string[] __changeStreamFields = ["_id", "clusterTime", "documentKey", "fullDocument", "fullDocumentBeforeChange", "ns", "operationDescription", "operationType", "to", "splitEvent", "collectionUUID", "updateDescription", "wallTime"];
27+
private static readonly string[] __changeStreamFields = ["_id", "clusterTime", "documentKey", "fullDocument", "fullDocumentBeforeChange", "ns", "operationDescription", "operationType", "to", "splitEvent", "collectionUUID", "updateDescription", "wallTime", "nsType"];
2728
private static readonly string[] __nsFields = ["db", "coll"];
2829

2930
public static BsonDocument Convert(ChangeStreamDocument<BsonDocument> changeStreamDocument)
@@ -33,6 +34,7 @@ public static BsonDocument Convert(ChangeStreamDocument<BsonDocument> changeStre
3334
{ "_id", changeStreamDocument.ResumeToken },
3435
{ "clusterTime", changeStreamDocument.ClusterTime, changeStreamDocument.ClusterTime != null },
3536
{ "documentKey", changeStreamDocument.DocumentKey, changeStreamDocument.DocumentKey != null },
37+
{ "nsType", () => changeStreamDocument.NamespaceType.ToString().ToLower(), changeStreamDocument.NamespaceType != ChangeStreamNamespaceType.Unknown },
3638
{ "operationDescription", changeStreamDocument.OperationDescription, changeStreamDocument.OperationDescription != null },
3739
{ "to", () => SerializationHelper.SerializeValue(ChangeStreamDocumentCollectionNamespaceSerializer.Instance, changeStreamDocument.RenameTo), changeStreamDocument.RenameTo != null },
3840
{ "splitEvent", () => SerializationHelper.SerializeValue(ChangeStreamSplitEventSerializer.Instance, changeStreamDocument.SplitEvent), changeStreamDocument.SplitEvent != null },

0 commit comments

Comments
 (0)