[SERVER-32172] In a sharded cluster the first batch of change stream with resumeAfter is empty Created: 05/Dec/17  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Trivial - P5
Reporter: Jeffrey Yemin Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 0
Labels: change-streams-improvements, changestreams
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Assigned Teams:
Query Execution
Backwards Compatibility: Minor Change
Participants:

 Description   

The behavior of mongos when resuming a change stream cursor changed in one of the later release candidates. Previously the first batch would contain the change stream document following the resumeAfter token. Now the first batch is empty, and the change stream document doesn't appear until the first getMore.

While this doesn't violate the specification of cursors (which allow empty batches), it's not clear that the change was intentional, and it does delay the delivery of the first change stream document to the application.

Here's the commands and responses sent to the server, demonstrating the issue. Note that the firstBatch in the response to the second aggregate command is empty.

{ "create" : "com.mongodb.operation.ChangeStreamOperationSpecification", "autoIndexId" : true, "capped" : false, "$db" : "JavaDriverTest", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070041, "i" : 1 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }
{ "note" : "the autoIndexId option is deprecated and will be removed in a future release", "ok" : 1.0, "operationTime" : { "$timestamp" : { "t" : 1512070050, "i" : 1 } }, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070050, "i" : 1 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }
 
{ "aggregate" : "com.mongodb.operation.ChangeStreamOperationSpecification", "pipeline" : [{ "$changeStream" : { "fullDocument" : "default" } }, { "$match" : { "operationType" : "insert" } }], "cursor" : { }, "$db" : "JavaDriverTest", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070050, "i" : 1 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }
{ "cursor" : { "firstBatch" : [], "id" : { "$numberLong" : "7909579924173326890" }, "ns" : "JavaDriverTest.com.mongodb.operation.ChangeStreamOperationSpecification" }, "ok" : 1.0, "operationTime" : { "$timestamp" : { "t" : 1512070050, "i" : 1 } }, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070050, "i" : 1 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }
 
{ "insert" : "com.mongodb.operation.ChangeStreamOperationSpecification", "ordered" : true, "writeConcern" : { "w" : "majority" }, "$db" : "JavaDriverTest", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070050, "i" : 1 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } }, "documents" : [{ "_id" : 1, "a" : 1 }, { "_id" : 2, "a" : 2 }] }
{ "n" : 2, "opTime" : { "ts" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "t" : { "$numberLong" : "1" } }, "electionId" : { "$oid" : "7fffffff0000000000000001" }, "ok" : 1.0, "operationTime" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }
 
{ "getMore" : { "$numberLong" : "7909579924173326890" }, "collection" : "com.mongodb.operation.ChangeStreamOperationSpecification", "$db" : "JavaDriverTest", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }
{ "cursor" : { "nextBatch" : [{ "_id" : { "_data" : { "$binary" : "glogW6IAAAACRh5faWQAKwIAWhAE+t3/4MP1Sa2c7SEciCREogQ=", "$type" : "00" } }, "operationType" : "insert", "fullDocument" : { "_id" : 1, "a" : 1 }, "ns" : { "db" : "JavaDriverTest", "coll" : "com.mongodb.operation.ChangeStreamOperationSpecification" }, "documentKey" : { "_id" : 1 } }, { "_id" : { "_data" : { "$binary" : "glogW6IAAAADRh5faWQAKwQAWhAE+t3/4MP1Sa2c7SEciCREogQ=", "$type" : "00" } }, "operationType" : "insert", "fullDocument" : { "_id" : 2, "a" : 2 }, "ns" : { "db" : "JavaDriverTest", "coll" : "com.mongodb.operation.ChangeStreamOperationSpecification" }, "documentKey" : { "_id" : 2 } }], "id" : { "$numberLong" : "7909579924173326890" }, "ns" : "JavaDriverTest.com.mongodb.operation.ChangeStreamOperationSpecification" }, "ok" : 1.0, "operationTime" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }
 
{ "killCursors" : "com.mongodb.operation.ChangeStreamOperationSpecification", "cursors" : [{ "$numberLong" : "7909579924173326890" }], "$db" : "JavaDriverTest", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }
{ "cursorsKilled" : [{ "$numberLong" : "7909579924173326890" }], "cursorsNotFound" : [], "cursorsAlive" : [], "cursorsUnknown" : [], "ok" : 1.0, "operationTime" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }
 
{ "aggregate" : "com.mongodb.operation.ChangeStreamOperationSpecification", "pipeline" : [{ "$changeStream" : { "fullDocument" : "default", "resumeAfter" : { "_data" : { "$binary" : "glogW6IAAAACRh5faWQAKwIAWhAE+t3/4MP1Sa2c7SEciCREogQ=", "$type" : "00" } } } }, { "$match" : { "operationType" : "insert" } }], "cursor" : { }, "$db" : "JavaDriverTest", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }
{ "cursor" : { "firstBatch" : [], "id" : { "$numberLong" : "5745533492884672932" }, "ns" : "JavaDriverTest.com.mongodb.operation.ChangeStreamOperationSpecification" }, "ok" : 1.0, "operationTime" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1512070050, "i" : 3 } }, "signature" : { "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }


Generated at Thu Feb 08 04:29:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.