Skip to content

Commit 3fd9813

Browse files
author
Divjot Arora
authored
GODRIVER-1509 Whitelist errors for change streams (#323)
1 parent 4f5fc19 commit 3fd9813

12 files changed

+6137
-156
lines changed

data/change-streams/README.rst

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ Spec Test Runner
9393

9494
Before running the tests
9595

96-
- Create a MongoClient ``globalClient``, and connect to the server
96+
- Create a MongoClient ``globalClient``, and connect to the server.
97+
When executing tests against a sharded cluster, ``globalClient`` must only connect to one mongos. This is because tests
98+
that set failpoints will only work consistently if both the ``configureFailPoint`` and failing commands are sent to the
99+
same mongos.
97100

98101
For each YAML file, for each element in ``tests``:
99102

@@ -110,13 +113,10 @@ For each YAML file, for each element in ``tests``:
110113

111114
- Create a new MongoClient ``client``
112115
- Begin monitoring all APM events for ``client``. (If the driver uses global listeners, filter out all events that do not originate with ``client``). Filter out any "internal" commands (e.g. ``isMaster``)
113-
- Using ``client``, create a changeStream ``changeStream`` against the specified ``target``. Use ``changeStreamPipeline`` and ``changeStreamOptions`` if they are non-empty
114-
- Using ``globalClient``, run every operation in ``operations`` in serial against the server
115-
- Wait until either:
116-
117-
- An error occurs
118-
- All operations have been successful AND the changeStream has received as many changes as there are in ``result.success``
119-
116+
- Using ``client``, create a changeStream ``changeStream`` against the specified ``target``. Use ``changeStreamPipeline`` and ``changeStreamOptions`` if they are non-empty. Capture any error.
117+
- If there was no error, use ``globalClient`` and run every operation in ``operations`` in serial against the server until all operations have been executed or an error is thrown. Capture any error.
118+
- If there was no error and ``result.error`` is set, iterate ``changeStream`` once and capture any error.
119+
- If there was no error and ``result.success`` is non-empty, iterate ``changeStream`` until it returns as many changes as there are elements in the ``result.success`` array or an error is thrown. Capture any error.
120120
- Close ``changeStream``
121121
- If there was an error:
122122

@@ -131,8 +131,8 @@ For each YAML file, for each element in ``tests``:
131131
- If there are any ``expectations``
132132

133133
- For each (``expected``, ``idx``) in ``expectations``
134-
135-
- Assert that ``actual[idx]`` MATCHES ``expected``
134+
- If ``actual[idx]`` is a ``killCursors`` event, skip it and move to ``actual[idx+1]``.
135+
- Else assert that ``actual[idx]`` MATCHES ``expected``
136136

137137
- Close the MongoClient ``client``
138138

@@ -142,62 +142,72 @@ After running all tests
142142
- Drop database ``database_name``
143143
- Drop database ``database2_name``
144144

145+
Iterating the Change Stream
146+
---------------------------
147+
148+
Although synchronous drivers must provide a `non-blocking mode of iteration <../change-streams.rst#not-blocking-on-iteration>`_, asynchronous drivers may not have such a mechanism. Those drivers with only a blocking mode of iteration should be careful not to iterate the change stream unnecessarily, as doing so could cause the test runner to block indefinitely. For this reason, the test runner procedure above advises drivers to take a conservative approach to iteration.
149+
150+
If the test expects an error and one was not thrown by either creating the change stream or executing the test's operations, iterating the change stream once allows for an error to be thrown by a ``getMore`` command. If the test does not expect any error, the change stream should be iterated only until it returns as many result documents as are expected by the test.
145151

146152
Prose Tests
147153
===========
148154

149-
The following tests have not yet been automated, but MUST still be tested
155+
The following tests have not yet been automated, but MUST still be tested. All tests SHOULD be run on both replica sets and sharded clusters unless otherwise specified:
150156

151157
#. ``ChangeStream`` must continuously track the last seen ``resumeToken``
152158
#. ``ChangeStream`` will throw an exception if the server response is missing the resume token (if wire version is < 8, this is a driver-side error; for 8+, this is a server-side error)
153-
#. ``ChangeStream`` will automatically resume one time on a resumable error (including `not master`) with the initial pipeline and options, except for the addition/update of a ``resumeToken``.
154-
#. ``ChangeStream`` will not attempt to resume on any error encountered while executing an ``aggregate`` command.
155-
#. ``ChangeStream`` will not attempt to resume after encountering error code 11601 (Interrupted), 136 (CappedPositionLost), or 237 (CursorKilled) while executing a ``getMore`` command.
159+
#. After receiving a ``resumeToken``, ``ChangeStream`` will automatically resume one time on a resumable error with the initial pipeline and options, except for the addition/update of a ``resumeToken``.
160+
#. ``ChangeStream`` will not attempt to resume on any error encountered while executing an ``aggregate`` command. Note that retryable reads may retry ``aggregate`` commands. Drivers should be careful to distinguish retries from resume attempts. Alternatively, drivers may specify `retryReads=false` or avoid using a [retryable error](../../retryable-reads/retryable-reads.rst#retryable-error) for this test.
161+
#. **Removed**
156162
#. ``ChangeStream`` will perform server selection before attempting to resume, using initial ``readPreference``
157163
#. Ensure that a cursor returned from an aggregate command with a cursor id and an initial empty batch is not closed on the driver side.
158164
#. The ``killCursors`` command sent during the "Resume Process" must not be allowed to throw an exception.
159-
#. ``$changeStream`` stage for ``ChangeStream`` against a server ``>=4.0`` and ``<4.0.7`` that has not received any results yet MUST include a ``startAtOperationTime`` option when resuming a changestream.
160-
#. ``ChangeStream`` will resume after a ``killCursors`` command is issued for its child cursor.
161-
#. - For a ``ChangeStream`` under these conditions:
162-
- Running against a server ``>=4.0.7``.
163-
- The batch is empty or has been iterated to the last document.
164-
- Expected result:
165-
- ``getResumeToken`` must return the ``postBatchResumeToken`` from the current command response.
166-
#. - For a ``ChangeStream`` under these conditions:
167-
- Running against a server ``<4.0.7``.
168-
- The batch is empty or has been iterated to the last document.
169-
- Expected result:
170-
- ``getResumeToken`` must return the ``_id`` of the last document returned if one exists.
171-
- ``getResumeToken`` must return ``startAfter`` from the initial aggregate if the option was specified.
172-
- ``getResumeToken`` must return ``resumeAfter`` from the initial aggregate if the option was specified.
173-
- If neither the ``startAfter`` nor ``resumeAfter`` options were specified, the ``getResumeToken`` result must be empty.
174-
#. - For a ``ChangeStream`` under these conditions:
175-
- The batch is not empty.
176-
- The batch has been iterated up to but not including the last element.
177-
- Expected result:
178-
- ``getResumeToken`` must return the ``_id`` of the previous document returned.
179-
#. - For a ``ChangeStream`` under these conditions:
180-
- The batch is not empty.
181-
- The batch hasn’t been iterated at all.
182-
- Only the initial ``aggregate`` command has been executed.
183-
- Expected result:
184-
- ``getResumeToken`` must return ``startAfter`` from the initial aggregate if the option was specified.
185-
- ``getResumeToken`` must return ``resumeAfter`` from the initial aggregate if the option was specified.
186-
- If neither the ``startAfter`` nor ``resumeAfter`` options were specified, the ``getResumeToken`` result must be empty.
187-
#. - For a ``ChangeStream`` under these conditions:
188-
- Running against a server ``>=4.0.7``.
189-
- The batch is not empty.
190-
- The batch hasn’t been iterated at all.
191-
- The stream has iterated beyond a previous batch and a ``getMore`` command has just been executed.
192-
- Expected result:
193-
- ``getResumeToken`` must return the ``postBatchResumeToken`` from the previous command response.
194-
#. - For a ``ChangeStream`` under these conditions:
195-
- Running against a server ``<4.0.7``.
196-
- The batch is not empty.
197-
- The batch hasn’t been iterated at all.
198-
- The stream has iterated beyond a previous batch and a ``getMore`` command has just been executed.
199-
- Expected result:
200-
- ``getResumeToken`` must return the ``_id`` of the previous document returned if one exists.
201-
- ``getResumeToken`` must return ``startAfter`` from the initial aggregate if the option was specified.
202-
- ``getResumeToken`` must return ``resumeAfter`` from the initial aggregate if the option was specified.
203-
- If neither the ``startAfter`` nor ``resumeAfter`` options were specified, the ``getResumeToken`` result must be empty.
165+
#. ``$changeStream`` stage for ``ChangeStream`` against a server ``>=4.0`` and ``<4.0.7`` that has not received any results yet MUST include a ``startAtOperationTime`` option when resuming a change stream.
166+
#. **Removed**
167+
#. For a ``ChangeStream`` under these conditions:
168+
169+
- Running against a server ``>=4.0.7``.
170+
- The batch is empty or has been iterated to the last document.
171+
172+
Expected result:
173+
174+
- ``getResumeToken`` must return the ``postBatchResumeToken`` from the current command response.
175+
176+
#. For a ``ChangeStream`` under these conditions:
177+
178+
- Running against a server ``<4.0.7``.
179+
- The batch is empty or has been iterated to the last document.
180+
181+
Expected result:
182+
183+
- ``getResumeToken`` must return the ``_id`` of the last document returned if one exists.
184+
- ``getResumeToken`` must return ``resumeAfter`` from the initial aggregate if the option was specified.
185+
- If ``resumeAfter`` was not specified, the ``getResumeToken`` result must be empty.
186+
187+
#. For a ``ChangeStream`` under these conditions:
188+
189+
- The batch is not empty.
190+
- The batch has been iterated up to but not including the last element.
191+
192+
Expected result:
193+
194+
- ``getResumeToken`` must return the ``_id`` of the previous document returned.
195+
196+
#. For a ``ChangeStream`` under these conditions:
197+
198+
- The batch is not empty.
199+
- The batch hasn’t been iterated at all.
200+
- Only the initial ``aggregate`` command has been executed.
201+
202+
Expected result:
203+
204+
- ``getResumeToken`` must return ``startAfter`` from the initial aggregate if the option was specified.
205+
- ``getResumeToken`` must return ``resumeAfter`` from the initial aggregate if the option was specified.
206+
- If neither the ``startAfter`` nor ``resumeAfter`` options were specified, the ``getResumeToken`` result must be empty.
207+
208+
Note that this test cannot be run against sharded topologies because in that case the initial ``aggregate`` command only establishes cursors on the shards and always returns an empty ``firstBatch``.
209+
210+
#. **Removed**
211+
#. **Removed**
212+
#. ``$changeStream`` stage for ``ChangeStream`` started with ``startAfter`` against a server ``>=4.1.1`` that has not received any results yet MUST include a ``startAfter`` option and MUST NOT include a ``resumeAfter`` option when resuming a change stream.
213+
#. ``$changeStream`` stage for ``ChangeStream`` started with ``startAfter`` against a server ``>=4.1.1`` that has received at least one result MUST include a ``resumeAfter`` option and MUST NOT include a ``startAfter`` option when resuming a change stream.

data/change-streams/change-streams-errors.json

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@
5454
"cursor": {},
5555
"pipeline": [
5656
{
57-
"$changeStream": {
58-
"fullDocument": "default"
59-
}
57+
"$changeStream": {}
6058
},
6159
{
6260
"$unsupported": "foo"
@@ -110,6 +108,53 @@
110108
]
111109
}
112110
}
111+
},
112+
{
113+
"description": "change stream errors on MaxTimeMSExpired",
114+
"minServerVersion": "4.2",
115+
"failPoint": {
116+
"configureFailPoint": "failCommand",
117+
"mode": {
118+
"times": 1
119+
},
120+
"data": {
121+
"failCommands": [
122+
"getMore"
123+
],
124+
"errorCode": 50,
125+
"closeConnection": false
126+
}
127+
},
128+
"target": "collection",
129+
"topology": [
130+
"replicaset",
131+
"sharded"
132+
],
133+
"changeStreamPipeline": [
134+
{
135+
"$project": {
136+
"_id": 0
137+
}
138+
}
139+
],
140+
"changeStreamOptions": {},
141+
"operations": [
142+
{
143+
"database": "change-stream-tests",
144+
"collection": "test",
145+
"name": "insertOne",
146+
"arguments": {
147+
"document": {
148+
"z": 3
149+
}
150+
}
151+
}
152+
],
153+
"result": {
154+
"error": {
155+
"code": 50
156+
}
157+
}
113158
}
114159
]
115160
}

data/change-streams/change-streams-errors.yml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ tests:
4242
cursor: {}
4343
pipeline:
4444
-
45-
$changeStream:
46-
fullDocument: default
45+
$changeStream: {}
4746
-
4847
$unsupported: foo
4948
command_name: aggregate
@@ -74,3 +73,32 @@ tests:
7473
error:
7574
code: 280
7675
errorLabels: [ "NonResumableChangeStreamError" ]
76+
-
77+
description: change stream errors on MaxTimeMSExpired
78+
minServerVersion: "4.2"
79+
failPoint:
80+
configureFailPoint: failCommand
81+
mode: { times: 1 }
82+
data:
83+
failCommands: ["getMore"]
84+
errorCode: 50 # An error code that's not on the old blacklist or whitelist
85+
closeConnection: false
86+
target: collection
87+
topology:
88+
- replicaset
89+
- sharded
90+
changeStreamPipeline:
91+
-
92+
$project: { _id: 0 }
93+
changeStreamOptions: {}
94+
operations:
95+
-
96+
database: *database_name
97+
collection: *collection_name
98+
name: insertOne
99+
arguments:
100+
document:
101+
z: 3
102+
result:
103+
error:
104+
code: 50

0 commit comments

Comments
 (0)