-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserversync.js
544 lines (465 loc) · 17.8 KB
/
serversync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import { Meteor } from "meteor/meteor";
import { DDP } from "meteor/ddp-client";
const logger = console.log;
// const logger = function(){};
/** the local change set, i.e., things that have changed locally but
have not yet been synced up, e.g., because we were/are offline */
let ChangeSet = new Mongo.Collection('_serversync_change-set');
/** set of remote changes, stored here for batch application to local
collection once sync is complete */
let remoteChanges = [];
/** Server code */
export default class ServerSyncClient {
/** establish the connection and setup onReconnect handler
@param options:
- onConnect: a function to be called upon initial connection
- onReconnect: a function to be called upon reconnect
- beforeSyncDirty: a function to be called before syncing
offline changes
- afterSyncDirty: a function to be called after syncing
offline changes
*/
constructor(URL, options) {
const self = this;
this._initialized = false;
this._connection = DDP.connect(URL);
// this._connection.call("", function(e, r) {
// console.log("fake method call", e, r);
// });
this._options = options;
this._connection.onReconnect = function() {
logger("reconnected");
if (!self._initialized) {
options.onConnect && options.onConnect();
// always clean the local collection before joining the sync,
// otherwise we won't get destructive changes the master made
// while we weren't running
_.each(self._collections, function(collectionSet, name) {
logger("clearing collection", name);
collectionSet.local.direct.remove({});
});
self._initialized = true;
} else {
options.onReconnect && options.onReconnect();
}
// self._connection.call("", function(e, r) {
// logger("sync complete");
// // apply remote changes to local before applying local changes
// // to remote (they may overwrite local changes)
// self._applyChanges();
// // apply local change (to remote)
// self._syncDirty();
// });
self._rescheduleSyncDirty();
}
// We are impatient: forcing a higher rate of reconnection
// attempts when unable to reach master
Meteor.setInterval(function() {
if (self._connection.status().status == "waiting") {
self._connection.reconnect();
}
}, 10000);
/**
for each sync we have one object of the form
{
remote: .. ,
local: ..,
subscription: ..,
options: ..
}
*/
this._collections = {};
// whether or not the initial sync is completed
this._ready = false;
}
/** schedule a syncDirty for some time from now, if it is already
scheduled, reschedule. Each remote reschedules this. This way we
won't syncDirty while changes are still coming in. TODO: This is
a bit of a #hack. It would be better to have a signal when the
sync from server is all caught up.
*/
_rescheduleSyncDirty() {
const self = this;
if (this._syncDirtyTimeout) {
Meteor.clearTimeout(this._syncDirtyTimeout);
}
this._syncDirtyTimeout = Meteor.setTimeout(function() {
self._syncDirty();
}, 2000);
}
/** Subscribe to the given remote collection, creating a synced
local copy of the result set for the given query.
@param collectionName: name of publication to sync with
@param options: an object containing:
- mode: "write" (default), or "read"
- args: subscription arguments (as given to Meteor.subscribe(.., args))
- onReady: a callback function for when the subscription becomes
ready (initial sync is complete)
- beforeSyncUp: a function to be called before syncing up
- beforeSyncDown: a function to be called before syncing down
- afterSyncUp: a function to be called after syncing up
- afterSyncDown: a function to be called after syncing down
*/
sync(collectionName, options = {mode: "write", args: []}) {
const self = this;
const query = options.query || {};
this._connection.reconnect();
// check(collectionName, String);
if (_.has(this._collections, collectionName)) {
logger("already subscribed to", collectionName);
return;
}
this._collections[collectionName] = {
options: options
};
// remote collection
const remoteCollection =
new Mongo.Collection(collectionName, this._connection);
this._collections[collectionName].remote = remoteCollection;
// local collection
let localCollection = null;
if (options.collection) {
localCollection = options.collection;
} else {
localCollection = new Mongo.Collection(collectionName);
}
this._collections[collectionName].local = localCollection;
// add subscription arguments
let args = options.args || [];
args.unshift(collectionName);
args.push({
onReady: function() {
logger("onReady", collectionName);
self._ready = true;
options.onReady && options.onReady();
collectionName && self._syncDirty(collectionName);
},
onError: function(e) {
logger("onError", e);
},
onStop: function(e) {
logger("onStop", e);
}
});
const subscription =
this._connection.subscribe.apply(this._connection, args);
this._collections[collectionName].subscription = subscription;
this._collections[collectionName].options = options;
const collectionSet = this._collections[collectionName];
// ---------------------------------------------------------
let syncInProgress = false;
/** put a marker in the DDP pipe by calling a non-existant method.
This will be put in the queue on the server and tell us when
the current batch of messages is done (which will be once the
callback comes back).
*/
function placeMarker() {
if (!syncInProgress) {
self._connection.call("", function(e, r) {
logger("sync complete");
// apply remote changes to local before applying local changes
// to remote (they may overwrite local changes)
self._applyChanges();
syncInProgress = false;
// we can now also apply local changes to remote, since we
// know that sync down is done; no need to wait for timeout
self._syncDirty();
});
syncInProgress = true;
// now that sync down has started we can use the DPP pipe
// marker to indicate when it is safe to sync up, remove timer
if (self._syncDirtyTimeout) {
Meteor.clearTimeout(self._syncDirtyTimeout);
this._syncDirtyTimeout = null;
}
}
}
// sync down (from remote to local)
// each remote change resets the timer for syncDirty
remoteCollection.find(query).observeChanges({
added(id, fields) {
if (ChangeSet.findOne(id)) {
// this remote addition just confirms the local addition
ChangeSet.remove(id);
} else {
if (self._syncDirtyTimeout) {
self._rescheduleSyncDirty();
// TODO: replace this by sync-done marker
}
var obj = fields;
obj._id = id;
placeMarker();
remoteChanges.push({
collectionName: collectionName,
action: "insert",
_id: id,
obj: obj,
options: options
});
}
},
changed(id, fields) {
const change = ChangeSet.findOne(id);
// if (fields._updated) {
// // our marker has come back, this batch of DDP messages is done
// logger("sync complete");
// syncInProgress = false;
// self._applyChanges();
// } else
if (change && change._synced) {
// remote confirmed the update
logger("ignoring remote update");
ChangeSet.remove(id);
} else {
if (self._syncDirtyTimeout) {
self._rescheduleSyncDirty();
}
var obj = fields;
obj._id = id;
placeMarker();
remoteChanges.push({
collectionName: collectionName,
action: "update",
_id: id,
obj: obj,
options: options
});
}
},
removed(id) {
const change = ChangeSet.findOne(id);
if (change && change._synced) {
// remote confirmed removal
logger("ignoring remote removal");
ChangeSet.remove(id);
} else {
if (self._syncDirtyTimeout) {
self._rescheduleSyncDirty();
}
placeMarker();
remoteChanges.push({
collectionName: collectionName,
action: "remove",
_id: id,
options: options
});
}
}
});
// ---------------------------------------------------------
// sync up (from local to remote)
if (options.mode != "read") {
// Local changes are noted using hooks. Note that this means
// that direct changes to the DB, e.g., via mongorestore, will
// not be synced.
localCollection.after.insert(
function(userId, obj) {
const id = obj._id;
logger("local insertion of ", id);
let change = {
collectionName: collectionName
};
// only sync back up after initial sync down
if (self._ready
&& self._connection.status().connected) {
change._synced = true;
ChangeSet.upsert(id, change);
options.beforeSyncUp && options.beforeSyncUp("insert", id, obj);
remoteCollection.upsert(id, obj);
options.afterSyncUp && options.afterSyncUp("insert", id, obj);
logger("added to remote");
} else {
// can't sync this right now, add to change set
logger("insert queued until reconnect", id);
change.obj = obj;
change.action = "insert";
ChangeSet.upsert(id, change);
}
});
localCollection.after.update(
function(userId, doc, fieldNames, modifier, update_options) {
const id = doc._id;
logger("local update to ", id, doc, fieldNames,
modifier, update_options);
// there may already be a prior change in this id
let change = ChangeSet.findOne(id);
if (!change) {
change = {
collectionName: collectionName
};
}
var obj = _.pick(doc, fieldNames);
if (self._ready) {
if (self._connection.status().connected) {
change._synced = true;
ChangeSet.upsert(id, change);
options.beforeSyncUp && options.beforeSyncUp("update", id, obj);
remoteCollection.upsert(id, {$set: obj});
options.afterSyncUp && options.afterSyncUp("update", id, obj);
logger("changed in remote");
} else {
logger("update queued until reconnect", id);
if (!change.obj) {
change.obj = {};
}
_.extend(change.obj, obj);
change.action = "update"; // may overwrite "insert"
ChangeSet.upsert(id, change);
}
} else {
// we have not yet connected to master, but we have
// inserted this document earlier (and potentially
// already updated it, too), so it's OK to edit
if (change && change.action ) {
logger("updated newly inserted item", id);
_.extend(change.obj, obj);
change.action = "update"; // may overwrite "insert"
ChangeSet.upsert(id, change);
}
}
});
localCollection.after.remove(
function(userId, doc) {
const id = doc._id;
logger("local removal of ", id);
let change = {
collectionName: collectionName
}; // this will overwrite any updates or inserts, but that's OK
if (self._ready) {
if (self._connection.status().connected) {
change._synced = true;
ChangeSet.upsert(id, change);
options.beforeSyncUp && options.beforeSyncUp("remove", id, doc);
remoteCollection.remove(id);
options.afterSyncUp && options.afterSyncUp("remove", id, doc);
logger("removed in remote");
} else {
logger("removal queued until reconnect", id);
change.action = "remove";
ChangeSet.upsert(id, change);
}
}
});
}
}
/** Batch apply all remote changes. This is called when sync down is
complete. Batching is necessary to ensure sync atomicity, i.e.,
avoid partial syncs. This can be important when changes in two
documents or collections need to happen simultaneous, e.g., to
ensure data consistency */
_applyChanges() {
logger("starting applyChanges");
const self = this;
_.each(remoteChanges, function(change) {
const localCollection = self._collections[change.collectionName].local;
const options = change.options;
options.beforeSyncDown
&& options.beforeSyncDown(change.action, change._id, change.obj);
if (change.action == "insert") {
localCollection.direct.upsert(change._id, change.obj);
} else if (change.action == "update") {
// remote changes invalidates local changes:
const localChange = ChangeSet.findOne(change._id);
if (localCollection.findOne(change._id)) {
if (localChange) {
// we made local changes as well; overwrite the object
// completely (don't just patch it)
let obj = remoteCollection.findOne(change._id);
localCollection.direct.upsert(change._id, obj);
} else {
delete change.obj._id;
localCollection.direct.upsert(change._id, {$set: change.obj});
}
} else {
// document was removed locally (presumably while
// offline), recreate it completely (not just the change)
localCollection.direct.insert(remoteCollection.findOne(change._id));
}
} else if (change.action == "remove") {
// remote changes invalidates local changes:
localCollection.direct.remove(change._id);
}
options.afterSyncDown
&& options.afterSyncDown(change.action, change._id, change.obj);
logger(change.action, ": remote -> local");
ChangeSet.remove(change._id);
});
remoteChanges = [];
logger("applyChanges done");
}
/** sync any offline changes that were not overwritten by remote up
to master */
_syncDirty(collectionName) {
if (this._syncDirtyTimeout) {
Meteor.clearTimeout(this._syncDirtyTimeout);
this._syncDirtyTimeout = null;
}
if (!this._connection.status().connected) {
console.warn("(serversync) _syncDirty failed;",
"lost connection again before we could sync",
this._connection.status());
return;
}
const self = this;
logger("_syncDirty", ChangeSet.find().fetch());
const numberOfChanges = ChangeSet.find().count();
this._options.beforeSyncDirty
&& this._options.beforeSyncDirty(numberOfChanges);
_.each(ChangeSet.find().fetch(), function(changeInfo) {
const id = changeInfo._id;
if (changeInfo._synced) {
// why does this happen?
ChangeSet.remove(id);
return;
}
ChangeSet.update(id, {$set: { _synced: true }});
// ^ we need to mark this changeInfo as sync, so we can
// recognize it when remote confirms it
if (changeInfo.collectionName == collectionName
|| collectionName == undefined) {
const remoteCollection =
self._collections[changeInfo.collectionName].remote;
const options =
self._collections[changeInfo.collectionName].options;
options.beforeSyncUp
&& options.beforeSyncUp(changeInfo.action, id, changeInfo.obj);
if (changeInfo.action == "insert") {
remoteCollection.upsert(id, changeInfo.obj, function(err, res) {
logger("insert local -> remote:", id, err, res);
const localCollection =
self._collections[changeInfo.collectionName].local;
if (!localCollection.findOne(id)) {
// this item was removed locally, due to initial clean
// up; re-add it
localCollection.upsert(id, changeInfo.obj);
}
});
} else if (changeInfo.action == "update") {
remoteCollection.upsert(
id, {$set: changeInfo.obj}, function(err, res) {
logger("update local -> remote:", id, err, res);
const localCollection =
self._collections[changeInfo.collectionName].local;
if (!localCollection.findOne(id)) {
// this item was removed locally, due to initial clean
// up; re-add it
localCollection.upsert(id, remoteCollection.findOne(id));
}
});
} else if (changeInfo.action == "remove") {
remoteCollection.remove(id, function(err, res) {
logger("removed local -> remote:", id, err, res);
});
}
options.afterSyncUp
&& options.afterSyncUp(changeInfo.action, id, changeInfo.obj);
}
});
this._options.afterSyncDirty
&& this._options.afterSyncDirty(numberOfChanges);
}
/** get (local) collection by name */
getCollection(name) {
return this._collections[name].local;
}
};