-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
Catalog and Routing
-
ALL
-
🟦 Shard Catalog
-
None
-
None
-
None
-
None
-
None
-
None
Problem
On a replica set, cloneCollectionAsCapped and convertToCapped copy documents via insertDocument(..., fromMigrate=true). Change streams filter out fromMigrate entries by default, so consumers see the DDL events but not the documents:
| Command | Visible events | Hidden events |
|---|---|---|
| cloneCollectionAsCapped src → dst | create dst (capped) | an insert into dst for each document |
| convertToCapped coll | create tmpXXXX.convertToCapped.coll (capped, temp), rename tmp… → coll (dropTarget) | an insert into the temp collection for each document |
A consumer that replays the stream ends up with the following, and gets no error:
- After cloneCollectionAsCapped: an empty capped dst.
- After convertToCapped: an empty capped coll that replaces the original.
Compare $out, which uses the same temp-collection-plus-rename pattern but logs ordinary inserts, so consumers can replay it.
Reproduction (8.0.35 replica set; the r9.0.8 code paths are the same)
const d = db.getSiblingDB("cappedprobe"); d.src.insertMany([{_id: 1}, {_id: 2}]); const start = d.runCommand({ping: 1}).operationTime; d.runCommand({cloneCollectionAsCapped: "src", toCollection: "clone", size: 4096}); d.marker.insertOne({_id: 1}); d.aggregate([{$changeStream: {startAtOperationTime: start, showExpandedEvents: true}}]); // → create clone, create marker, insert marker. There are no inserts into clone.
Oplog:
{op: "c", o: {create: "clone", capped: true, size: 4096}}
{op: "i", ns: "cappedprobe.clone", o: {_id: 1}, fromMigrate: true}
{op: "i", ns: "cappedprobe.clone", o: {_id: 2}, fromMigrate: true}
With showMigrationEvents: true (passed in a raw $changeStream stage), the inserts appear and the sequence replays correctly. That option is undocumented, though, and mongos rejects it (31123).
History
The flag on these inserts looks accidental:
- Before
SERVER-17573(2015): the clone loop inserted each document and called OpObserver::onInsert directly when logForReplication was set. The inserts were ordinary. SERVER-17573(1725d76f448): added insertDocument(..., bool fromMigrate = false) and rewrote the call as insertDocument(txn, obj, true, txn->writesAreReplicated()). That passes the "should replicate" boolean into the new fromMigrate parameter, so on a primary every cloned document became fromMigrate: true. Change streams didn't exist yet, so nothing exposed the change.- Later refactors: reduced the value to a literal true. It's now at src/mongo/db/collection_crud/capped_utils.cpp:223.
SERVER-106916:- It made fromMigrate a deliberate parameter of convertToCapped, used to hide the leftover drop and the final rename when the sharded coordinator runs on a primary that isn't a data-bearing shard.
- The plain command passes false, so its create and rename are visible.
- The inserts stay hard-coded to true on every path.
So on a replica set the stream shows the DDL events but hides the documents, which is the case no consumer can handle.
Proposal
- Inserts: log the inserts from cloneCollectionAsCapped with the same fromMigrate value as the surrounding DDL:
- false for the cloneCollectionAsCapped command and the plain convertToCapped command.
- The coordinator-supplied value for the sharded ConvertToCappedCoordinator path.
- Alternative: if hiding the inserts is intentional, mark the create and rename as fromMigrate too, or emit a dedicated change event. Then consumers can at least detect the command instead of silently losing data.
Impact
Change-stream-based replication can't support either command.
- convertToCapped: mongosync fails on it, detected by the temp collection's name.
- cloneCollectionAsCapped: mongosync has no defense. In a mongosync e2e test (8.0 replica set → 8.0 replica set), running it on the source during change event application gave this result:
- The migration committed without an error.
- The destination got an empty capped collection.
- mongosync's embedded verifier didn't report a mismatch.
- is related to
-
SERVER-106916 Add support to change stream readers V2 into ConvertToCappedCoordinator
-
- Closed
-
-
SERVER-17573 move collection creation OpObserver::onCreateCollection down into createCollection, et cetera
-
- Closed
-