Skip to content

Commit 485bb67

Browse files
committed
Add check for resumeTokens going back in time.
1 parent 1444a48 commit 485bb67

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

modules/module-mongodb/src/replication/ChangeStream.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,16 @@ export class ChangeStream {
947947
timestamp: changeDocument.clusterTime!,
948948
resume_token: changeDocument._id
949949
});
950+
if (batch.lastCheckpointLsn != null && lsn < batch.lastCheckpointLsn) {
951+
// Checkpoint out of order - should never happen with MongoDB.
952+
// If it does happen, we throw an error to stop the replication - restarting should recover.
953+
// Since we use batch.lastCheckpointLsn for the next resumeAfter, this should not result in an infinite loop.
954+
// This is a workaround for the issue below, but we can keep this as a safety-check even if the issue is fixed.
955+
// Driver issue report: https://jira.mongodb.org/browse/NODE-7042
956+
throw new ReplicationAssertionError(
957+
`Change resumeToken ${(changeDocument._id as any)._data} (${timestampToDate(changeDocument.clusterTime!).toISOString()}) is less than last checkpoint LSN ${batch.lastCheckpointLsn}. Restarting replication.`
958+
);
959+
}
950960

951961
if (waitForCheckpointLsn != null && lsn >= waitForCheckpointLsn) {
952962
waitForCheckpointLsn = null;

0 commit comments

Comments
 (0)