From 53ebddfa9fbd031a99a269a71655acb6a96639eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 3 Mar 2015 12:00:47 +0100 Subject: [PATCH] Create a remote checkpoint during replication too Before this change, in the case of a one-way replication, the remote checkpoint was never updated, thus the "diff" step had to check all changes made through the whole history. This commit fixes the problem by creating a new remote checkpoint at the same time when a local checkpoint is created. It is important to create the new checkpoint before the replication is started to prevent a race condition where a remote change can end up being associated with an already replicated checkpoint. --- lib/persisted-model.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/persisted-model.js b/lib/persisted-model.js index 8dfe82c9..0062a64b 100644 --- a/lib/persisted-model.js +++ b/lib/persisted-model.js @@ -855,7 +855,7 @@ PersistedModel.replicate = function(since, targetModel, options, callback) { }; var tasks = [ - checkpoint, + checkpoints, getSourceChanges, getDiffFromTarget, createSourceUpdates, @@ -887,9 +887,13 @@ PersistedModel.replicate = function(since, targetModel, options, callback) { targetModel.bulkUpdate(updates, cb); } - function checkpoint() { + function checkpoints() { var cb = arguments[arguments.length - 1]; - sourceModel.checkpoint(function(err) { cb(err); }); + sourceModel.checkpoint(function(err) { + targetModel.checkpoint(function(err) { + cb(err); + }); + }); } function done(err) {