-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Problem
Batch and streaming writes become visible in the target collection before Spark commits the job. A failed job does not undo them.
What happens
- SaveMode.Overwrite drops the target collection, or deleteMany}}s it when {{truncateMode=truncate, inside MongoBatchWrite.createBatchWriterFactory. MongoStreamingWrite.createStreamingWriterFactory always drops the target when truncate is set. That runs before any task writes a row.
- MongoDataWriter.write bulk-writes as soon as maxBatchSize is reached. abort() does not remove those writes. It throws DataException and tells the user to clean up manually.
- Rows that do not contain the configured id fields are always inserted, including when operationType is replace (the default). A task retry or a speculative attempt inserts another document.
- epochId is stored on MongoWriterCommitMessage and never read, so a retried streaming micro-batch is not idempotent.
Reproduced
Connector main at 11.2.0-SNAPSHOT, Spark 4.0.1, MongoDB 9.0.4 replica set.
- Insert {{
{\{_id: 1, v: "original"\}}
}}.
- Overwrite with two rows that share _id 10, operationType=insert, maxBatchSize=1.
- The job fails with a duplicate key error.
- The collection is [\{\{_id: 10, v: "first-batch"\}}]. The original document is gone.
Appending one row with no _id in two separate save() calls correctly produces two documents, because those are two queries. Repeating a single task (same query, epoch, partition, and row ordinal, different task attempt) also produces two documents. That is the bug.
Expected
- A failed overwrite leaves the previous collection, its indexes, and its options unchanged.
- truncateMode=drop still replaces the collection only after every task succeeds.
- truncateMode=truncate still keeps indexes, collation, and sharding, and only removes existing documents after every task succeeds.
- A retried or speculative attempt of rows without _id leaves one document per input row. A later append is a new query and still inserts new documents.
Notes
Dataset filters are unrelated. This ticket is only the write path.
- pull request