CDRIVER-5925 apply batchSize:0
to aggregate
in change stream
#1909
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Apply
batchSize:0
toaggregate
for change streams.Tested with this patch build.
Background & Motivation
WRITING-30021 reported an outage due to runaway
aggregate
operations overwhelming the server.This PR supports setting
batchSize:0
on theaggregate
command sent to create a change stream cursor. This is intended to help the driver kill the server-side cursor when destroying the client handle.Sending
batchSize:0
can help cancel runawayaggregate
commands on the server.batchSize:0
results inaggregate
immediately returning with a cursor ID and no results. Knowing the cursor ID enablesmongoc_change_stream_destroy
to send thekillCursors
command to kill the server-side cursor.Using
batchSize:0
foraggregate
is supported as a way to create a server-side cursor immediately. Quoting HELP-29972:WRITING-10467 proposes a long-term solution for all drivers: a new option
emptyInitialBatch
. The new option would also permit a non-defaultbatchSize
forgetMore
. A non-defaultbatchSize
forgetMore
is not needed to address the reported issue. This PR is intended to be a simpler short-term solution to address the urgent issue while avoiding a conflict with the spec.Existing behavior with
batchSize:0
The C driver currently ignores
batchSize:0
option when creating a change stream. This results in using the server default for both theaggregate
andgetMore
commands. Behavior forbatchSize:0
differs between drivers.In the C/C++ driver (full example):
In PyMongo (full example):
This PR changes the C driver to behave as PyMongo.
Caveats
This is a subtle behavior change. Specifying
batchSize:0
for change streams in the C driver is currently ignored. If a user were (unexpectedly?) settingbatchSize:0
, this may result in an additional needed round-trip to get initial results (the firstaggregate
would now return empty results). I expect this is low risk to cause negative impact.