2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-12-11 08:38:53 +03:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
2012-08-30 01:43:41 +04:00
|
|
|
"EngineManager",
|
2012-08-30 01:43:40 +04:00
|
|
|
"Engine",
|
|
|
|
"SyncEngine",
|
|
|
|
"Tracker",
|
2016-09-06 21:39:13 +03:00
|
|
|
"Store",
|
|
|
|
"Changeset"
|
2012-08-30 01:43:40 +04:00
|
|
|
];
|
2007-12-11 08:38:53 +03:00
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/JSONFile.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/Log.jsm");
|
|
|
|
ChromeUtils.import("resource://services-common/async.js");
|
|
|
|
ChromeUtils.import("resource://services-common/observers.js");
|
|
|
|
ChromeUtils.import("resource://services-common/utils.js");
|
|
|
|
ChromeUtils.import("resource://services-sync/constants.js");
|
|
|
|
ChromeUtils.import("resource://services-sync/record.js");
|
|
|
|
ChromeUtils.import("resource://services-sync/resource.js");
|
|
|
|
ChromeUtils.import("resource://services-sync/util.js");
|
2008-11-08 13:00:33 +03:00
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
|
|
|
fxAccounts: "resource://gre/modules/FxAccounts.jsm",
|
|
|
|
OS: "resource://gre/modules/osfile.jsm",
|
|
|
|
PlacesSyncUtils: "resource://gre/modules/PlacesSyncUtils.jsm",
|
|
|
|
});
|
2016-08-02 20:09:30 +03:00
|
|
|
|
2017-11-01 21:10:47 +03:00
|
|
|
function ensureDirectory(path) {
|
|
|
|
let basename = OS.Path.dirname(path);
|
|
|
|
return OS.File.makeDir(basename, { from: OS.Constants.Path.profileDir });
|
|
|
|
}
|
|
|
|
|
2011-01-19 03:23:25 +03:00
|
|
|
/*
|
|
|
|
* Trackers are associated with a single engine and deal with
|
|
|
|
* listening for changes to their particular data type.
|
|
|
|
*
|
|
|
|
* There are two things they keep track of:
|
|
|
|
* 1) A score, indicating how urgently the engine wants to sync
|
|
|
|
* 2) A list of IDs for all the changed items that need to be synced
|
|
|
|
* and updating their 'score', indicating how urgently they
|
|
|
|
* want to sync.
|
|
|
|
*
|
|
|
|
*/
|
2012-10-31 20:13:28 +04:00
|
|
|
this.Tracker = function Tracker(name, engine) {
|
2012-08-30 01:43:41 +04:00
|
|
|
if (!engine) {
|
|
|
|
throw new Error("Tracker must be associated with an Engine instance.");
|
|
|
|
}
|
|
|
|
|
2011-01-19 03:23:25 +03:00
|
|
|
name = name || "Unnamed";
|
|
|
|
this.name = this.file = name.toLowerCase();
|
2012-08-30 01:43:41 +04:00
|
|
|
this.engine = engine;
|
2011-01-19 03:23:25 +03:00
|
|
|
|
2017-12-19 01:48:31 +03:00
|
|
|
this._log = Log.repository.getLogger(`Sync.Engine.${name}.Tracker`);
|
2011-01-19 03:23:25 +03:00
|
|
|
|
|
|
|
this._score = 0;
|
|
|
|
this._ignored = [];
|
2016-12-20 22:25:27 +03:00
|
|
|
this._storage = new JSONFile({
|
|
|
|
path: Utils.jsonFilePath("changes/" + this.file),
|
|
|
|
dataPostProcessor: json => this._dataPostProcessor(json),
|
|
|
|
beforeSave: () => this._beforeSave(),
|
|
|
|
});
|
2011-01-19 03:23:25 +03:00
|
|
|
this.ignoreAll = false;
|
2013-12-19 09:42:17 +04:00
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
this.asyncObserver = Async.asyncObserver(this, this._log);
|
2013-12-19 09:42:17 +04:00
|
|
|
};
|
|
|
|
|
2011-01-19 03:23:25 +03:00
|
|
|
Tracker.prototype = {
|
|
|
|
/*
|
|
|
|
* Score can be called as often as desired to decide which engines to sync
|
|
|
|
*
|
|
|
|
* Valid values for score:
|
|
|
|
* -1: Do not sync unless the user specifically requests it (almost disabled)
|
|
|
|
* 0: Nothing has changed
|
|
|
|
* 100: Please sync me ASAP!
|
|
|
|
*
|
|
|
|
* Setting it to other values should (but doesn't currently) throw an exception
|
|
|
|
*/
|
|
|
|
get score() {
|
|
|
|
return this._score;
|
|
|
|
},
|
|
|
|
|
2016-12-20 22:25:27 +03:00
|
|
|
// Default to an empty object if the file doesn't exist.
|
|
|
|
_dataPostProcessor(json) {
|
|
|
|
return typeof json == "object" && json || {};
|
|
|
|
},
|
|
|
|
|
|
|
|
// Ensure the Weave storage directory exists before writing the file.
|
|
|
|
_beforeSave() {
|
2017-11-01 21:10:47 +03:00
|
|
|
return ensureDirectory(this._storage.path);
|
2016-12-20 22:25:27 +03:00
|
|
|
},
|
|
|
|
|
2011-01-19 03:23:25 +03:00
|
|
|
set score(value) {
|
|
|
|
this._score = value;
|
|
|
|
Observers.notify("weave:engine:score:updated", this.name);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Should be called by service everytime a sync has been done for an engine
|
2017-01-10 20:09:02 +03:00
|
|
|
resetScore() {
|
2011-01-19 03:23:25 +03:00
|
|
|
this._score = 0;
|
|
|
|
},
|
|
|
|
|
2012-12-06 04:46:25 +04:00
|
|
|
persistChangedIDs: true,
|
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
async getChangedIDs() {
|
|
|
|
await this._storage.load();
|
|
|
|
return this._storage.data;
|
|
|
|
},
|
|
|
|
|
2016-12-20 22:25:27 +03:00
|
|
|
_saveChangedIDs() {
|
2012-12-06 04:46:25 +04:00
|
|
|
if (!this.persistChangedIDs) {
|
|
|
|
this._log.debug("Not saving changedIDs.");
|
|
|
|
return;
|
|
|
|
}
|
2016-12-20 22:25:27 +03:00
|
|
|
this._storage.saveSoon();
|
2011-01-19 03:23:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// ignore/unignore specific IDs. Useful for ignoring items that are
|
|
|
|
// being processed, or that shouldn't be synced.
|
|
|
|
// But note: not persisted to disk
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
ignoreID(id) {
|
2011-01-19 03:23:25 +03:00
|
|
|
this.unignoreID(id);
|
|
|
|
this._ignored.push(id);
|
|
|
|
},
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
unignoreID(id) {
|
2011-01-19 03:23:25 +03:00
|
|
|
let index = this._ignored.indexOf(id);
|
|
|
|
if (index != -1)
|
|
|
|
this._ignored.splice(index, 1);
|
|
|
|
},
|
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
async _saveChangedID(id, when) {
|
2016-09-06 21:39:13 +03:00
|
|
|
this._log.trace(`Adding changed ID: ${id}, ${JSON.stringify(when)}`);
|
2018-01-05 02:07:10 +03:00
|
|
|
const changedIDs = await this.getChangedIDs();
|
|
|
|
changedIDs[id] = when;
|
2016-12-20 22:25:27 +03:00
|
|
|
this._saveChangedIDs();
|
2016-09-06 21:39:13 +03:00
|
|
|
},
|
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
async addChangedID(id, when) {
|
2011-01-19 03:23:25 +03:00
|
|
|
if (!id) {
|
|
|
|
this._log.warn("Attempted to add undefined ID to tracker");
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-19 09:42:17 +04:00
|
|
|
|
2016-09-06 21:39:13 +03:00
|
|
|
if (this.ignoreAll || this._ignored.includes(id)) {
|
2011-01-19 03:23:25 +03:00
|
|
|
return false;
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
2011-01-19 03:23:25 +03:00
|
|
|
|
2013-12-19 09:42:17 +04:00
|
|
|
// Default to the current time in seconds if no time is provided.
|
|
|
|
if (when == null) {
|
2016-07-05 09:02:19 +03:00
|
|
|
when = this._now();
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
2011-01-19 03:23:25 +03:00
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
const changedIDs = await this.getChangedIDs();
|
2013-12-19 09:42:17 +04:00
|
|
|
// Add/update the entry if we have a newer time.
|
2018-01-05 02:07:10 +03:00
|
|
|
if ((changedIDs[id] || -Infinity) < when) {
|
|
|
|
await this._saveChangedID(id, when);
|
2011-01-19 03:23:25 +03:00
|
|
|
}
|
2013-12-19 09:42:17 +04:00
|
|
|
|
2011-01-19 03:23:25 +03:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
async removeChangedID(...ids) {
|
2016-11-22 22:52:09 +03:00
|
|
|
if (!ids.length || this.ignoreAll) {
|
2011-01-19 03:23:25 +03:00
|
|
|
return false;
|
2016-09-06 21:39:13 +03:00
|
|
|
}
|
2016-11-22 22:52:09 +03:00
|
|
|
for (let id of ids) {
|
|
|
|
if (!id) {
|
|
|
|
this._log.warn("Attempted to remove undefined ID from tracker");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (this._ignored.includes(id)) {
|
|
|
|
this._log.debug(`Not removing ignored ID ${id} from tracker`);
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-05 02:07:10 +03:00
|
|
|
const changedIDs = await this.getChangedIDs();
|
|
|
|
if (changedIDs[id] != null) {
|
2016-11-22 22:52:09 +03:00
|
|
|
this._log.trace("Removing changed ID " + id);
|
2018-01-05 02:07:10 +03:00
|
|
|
delete changedIDs[id];
|
2016-11-22 22:52:09 +03:00
|
|
|
}
|
2011-01-19 03:23:25 +03:00
|
|
|
}
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._saveChangedIDs();
|
2011-01-19 03:23:25 +03:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
async clearChangedIDs() {
|
2011-01-19 03:23:25 +03:00
|
|
|
this._log.trace("Clearing changed ID list");
|
2016-12-20 22:25:27 +03:00
|
|
|
this._storage.data = {};
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._saveChangedIDs();
|
2013-12-19 09:42:17 +04:00
|
|
|
},
|
|
|
|
|
2016-07-05 09:02:19 +03:00
|
|
|
_now() {
|
|
|
|
return Date.now() / 1000;
|
|
|
|
},
|
|
|
|
|
2013-12-19 09:42:17 +04:00
|
|
|
_isTracking: false,
|
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
start() {
|
|
|
|
if (!this.engineIsEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._log.trace("start().");
|
|
|
|
if (!this._isTracking) {
|
|
|
|
this.onStart();
|
|
|
|
this._isTracking = true;
|
|
|
|
}
|
2013-12-19 09:42:17 +04:00
|
|
|
},
|
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
async stop() {
|
|
|
|
this._log.trace("stop().");
|
|
|
|
if (this._isTracking) {
|
|
|
|
await this.asyncObserver.promiseObserversComplete();
|
|
|
|
this.onStop();
|
|
|
|
this._isTracking = false;
|
|
|
|
}
|
2013-12-19 09:42:17 +04:00
|
|
|
},
|
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
// Override these in your subclasses.
|
|
|
|
onStart() {},
|
|
|
|
onStop() {},
|
|
|
|
async observe(subject, topic, data) {},
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
engineIsEnabled() {
|
2013-12-19 09:42:17 +04:00
|
|
|
if (!this.engine) {
|
|
|
|
// Can't tell -- we must be running in a test!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this.engine.enabled;
|
|
|
|
},
|
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
async onEngineEnabledChanged(engineEnabled) {
|
2013-12-19 09:42:17 +04:00
|
|
|
if (engineEnabled == this._isTracking) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (engineEnabled) {
|
2018-01-05 02:07:10 +03:00
|
|
|
this.start();
|
2013-12-19 09:42:17 +04:00
|
|
|
} else {
|
2018-01-05 02:07:10 +03:00
|
|
|
await this.stop();
|
|
|
|
await this.clearChangedIDs();
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
2017-02-14 00:16:30 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
async finalize() {
|
|
|
|
// Persist all pending tracked changes to disk, and wait for the final write
|
|
|
|
// to finish.
|
|
|
|
this._saveChangedIDs();
|
|
|
|
await this._storage.finalize();
|
|
|
|
},
|
2011-01-19 03:23:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-09-09 21:34:58 +04:00
|
|
|
/**
|
|
|
|
* The Store serves as the interface between Sync and stored data.
|
|
|
|
*
|
|
|
|
* The name "store" is slightly a misnomer because it doesn't actually "store"
|
|
|
|
* anything. Instead, it serves as a gateway to something that actually does
|
|
|
|
* the "storing."
|
|
|
|
*
|
|
|
|
* The store is responsible for record management inside an engine. It tells
|
|
|
|
* Sync what items are available for Sync, converts items to and from Sync's
|
|
|
|
* record format, and applies records from Sync into changes on the underlying
|
|
|
|
* store.
|
|
|
|
*
|
|
|
|
* Store implementations require a number of functions to be implemented. These
|
|
|
|
* are all documented below.
|
|
|
|
*
|
|
|
|
* For stores that deal with many records or which have expensive store access
|
|
|
|
* routines, it is highly recommended to implement a custom applyIncomingBatch
|
|
|
|
* and/or applyIncoming function on top of the basic APIs.
|
2011-01-19 03:23:25 +03:00
|
|
|
*/
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.Store = function Store(name, engine) {
|
2012-08-30 01:43:41 +04:00
|
|
|
if (!engine) {
|
|
|
|
throw new Error("Store must be associated with an Engine instance.");
|
|
|
|
}
|
|
|
|
|
2011-01-19 03:23:25 +03:00
|
|
|
name = name || "Unnamed";
|
|
|
|
this.name = name.toLowerCase();
|
2012-08-30 01:43:41 +04:00
|
|
|
this.engine = engine;
|
2011-01-19 03:23:25 +03:00
|
|
|
|
2017-12-19 01:48:31 +03:00
|
|
|
this._log = Log.repository.getLogger(`Sync.Engine.${name}.Store`);
|
2011-03-08 00:07:59 +03:00
|
|
|
|
2011-05-20 05:08:35 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "_timer", function() {
|
2011-03-08 00:07:59 +03:00
|
|
|
return Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
});
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2011-01-19 03:23:25 +03:00
|
|
|
Store.prototype = {
|
2011-01-28 20:39:12 +03:00
|
|
|
|
2011-09-09 21:34:58 +04:00
|
|
|
/**
|
|
|
|
* Apply multiple incoming records against the store.
|
|
|
|
*
|
|
|
|
* This is called with a set of incoming records to process. The function
|
|
|
|
* should look at each record, reconcile with the current local state, and
|
|
|
|
* make the local changes required to bring its state in alignment with the
|
|
|
|
* record.
|
|
|
|
*
|
|
|
|
* The default implementation simply iterates over all records and calls
|
|
|
|
* applyIncoming(). Store implementations may overwrite this function
|
|
|
|
* if desired.
|
|
|
|
*
|
|
|
|
* @param records Array of records to apply
|
|
|
|
* @return Array of record IDs which did not apply cleanly
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async applyIncomingBatch(records) {
|
2011-01-28 20:39:12 +03:00
|
|
|
let failed = [];
|
2017-06-06 01:50:07 +03:00
|
|
|
let maybeYield = Async.jankYielder();
|
2015-10-18 21:52:58 +03:00
|
|
|
for (let record of records) {
|
2017-06-06 01:50:07 +03:00
|
|
|
await maybeYield();
|
2011-01-28 20:39:12 +03:00
|
|
|
try {
|
2017-06-06 01:50:07 +03:00
|
|
|
await this.applyIncoming(record);
|
2016-01-26 18:13:31 +03:00
|
|
|
} catch (ex) {
|
|
|
|
if (ex.code == Engine.prototype.eEngineAbortApplyIncoming) {
|
|
|
|
// This kind of exception should have a 'cause' attribute, which is an
|
|
|
|
// originating exception.
|
|
|
|
// ex.cause will carry its stack with it when rethrown.
|
|
|
|
throw ex.cause;
|
|
|
|
}
|
|
|
|
if (Async.isShutdownException(ex)) {
|
|
|
|
throw ex;
|
|
|
|
}
|
2016-01-07 09:09:00 +03:00
|
|
|
this._log.warn("Failed to apply incoming record " + record.id, ex);
|
2011-01-28 20:39:12 +03:00
|
|
|
failed.push(record.id);
|
|
|
|
}
|
2017-01-10 20:09:02 +03:00
|
|
|
}
|
2011-01-28 20:39:12 +03:00
|
|
|
return failed;
|
|
|
|
},
|
|
|
|
|
2011-09-09 21:34:58 +04:00
|
|
|
/**
|
|
|
|
* Apply a single record against the store.
|
|
|
|
*
|
|
|
|
* This takes a single record and makes the local changes required so the
|
|
|
|
* local state matches what's in the record.
|
|
|
|
*
|
|
|
|
* The default implementation calls one of remove(), create(), or update()
|
|
|
|
* depending on the state obtained from the store itself. Store
|
|
|
|
* implementations may overwrite this function if desired.
|
|
|
|
*
|
|
|
|
* @param record
|
|
|
|
* Record to apply
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async applyIncoming(record) {
|
2011-01-19 03:23:25 +03:00
|
|
|
if (record.deleted)
|
2017-06-06 01:50:07 +03:00
|
|
|
await this.remove(record);
|
|
|
|
else if (!(await this.itemExists(record.id)))
|
|
|
|
await this.create(record);
|
2011-01-19 03:23:25 +03:00
|
|
|
else
|
2017-06-06 01:50:07 +03:00
|
|
|
await this.update(record);
|
2011-01-19 03:23:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// override these in derived objects
|
|
|
|
|
2011-09-09 21:34:58 +04:00
|
|
|
/**
|
|
|
|
* Create an item in the store from a record.
|
|
|
|
*
|
|
|
|
* This is called by the default implementation of applyIncoming(). If using
|
|
|
|
* applyIncomingBatch(), this won't be called unless your store calls it.
|
|
|
|
*
|
|
|
|
* @param record
|
|
|
|
* The store record to create an item from
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async create(record) {
|
2017-07-23 04:55:43 +03:00
|
|
|
throw new Error("override create in a subclass");
|
2011-01-19 03:23:25 +03:00
|
|
|
},
|
|
|
|
|
2011-09-09 21:34:58 +04:00
|
|
|
/**
|
|
|
|
* Remove an item in the store from a record.
|
|
|
|
*
|
|
|
|
* This is called by the default implementation of applyIncoming(). If using
|
|
|
|
* applyIncomingBatch(), this won't be called unless your store calls it.
|
|
|
|
*
|
|
|
|
* @param record
|
|
|
|
* The store record to delete an item from
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async remove(record) {
|
2017-07-23 04:55:43 +03:00
|
|
|
throw new Error("override remove in a subclass");
|
2011-01-19 03:23:25 +03:00
|
|
|
},
|
|
|
|
|
2011-09-09 21:34:58 +04:00
|
|
|
/**
|
|
|
|
* Update an item from a record.
|
|
|
|
*
|
|
|
|
* This is called by the default implementation of applyIncoming(). If using
|
|
|
|
* applyIncomingBatch(), this won't be called unless your store calls it.
|
|
|
|
*
|
|
|
|
* @param record
|
|
|
|
* The record to use to update an item from
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async update(record) {
|
2017-07-23 04:55:43 +03:00
|
|
|
throw new Error("override update in a subclass");
|
2011-01-19 03:23:25 +03:00
|
|
|
},
|
|
|
|
|
2011-09-09 21:34:58 +04:00
|
|
|
/**
|
|
|
|
* Determine whether a record with the specified ID exists.
|
|
|
|
*
|
|
|
|
* Takes a string record ID and returns a booleans saying whether the record
|
|
|
|
* exists.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* string record ID
|
|
|
|
* @return boolean indicating whether record exists locally
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async itemExists(id) {
|
2017-07-23 04:55:43 +03:00
|
|
|
throw new Error("override itemExists in a subclass");
|
2011-01-19 03:23:25 +03:00
|
|
|
},
|
|
|
|
|
2011-09-09 21:34:58 +04:00
|
|
|
/**
|
|
|
|
* Create a record from the specified ID.
|
|
|
|
*
|
|
|
|
* If the ID is known, the record should be populated with metadata from
|
|
|
|
* the store. If the ID is not known, the record should be created with the
|
|
|
|
* delete field set to true.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* string record ID
|
|
|
|
* @param collection
|
|
|
|
* Collection to add record to. This is typically passed into the
|
|
|
|
* constructor for the newly-created record.
|
|
|
|
* @return record type for this engine
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async createRecord(id, collection) {
|
2017-07-23 04:55:43 +03:00
|
|
|
throw new Error("override createRecord in a subclass");
|
2011-01-19 03:23:25 +03:00
|
|
|
},
|
|
|
|
|
2011-09-09 21:34:58 +04:00
|
|
|
/**
|
|
|
|
* Change the ID of a record.
|
|
|
|
*
|
|
|
|
* @param oldID
|
|
|
|
* string old/current record ID
|
|
|
|
* @param newID
|
|
|
|
* string new record ID
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async changeItemID(oldID, newID) {
|
2017-07-23 04:55:43 +03:00
|
|
|
throw new Error("override changeItemID in a subclass");
|
2011-01-19 03:23:25 +03:00
|
|
|
},
|
|
|
|
|
2011-09-09 21:34:58 +04:00
|
|
|
/**
|
|
|
|
* Obtain the set of all known record IDs.
|
|
|
|
*
|
|
|
|
* @return Object with ID strings as keys and values of true. The values
|
|
|
|
* are ignored.
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async getAllIDs() {
|
2017-07-23 04:55:43 +03:00
|
|
|
throw new Error("override getAllIDs in a subclass");
|
2011-01-19 03:23:25 +03:00
|
|
|
},
|
|
|
|
|
2011-09-09 21:34:58 +04:00
|
|
|
/**
|
|
|
|
* Wipe all data in the store.
|
|
|
|
*
|
|
|
|
* This function is called during remote wipes or when replacing local data
|
|
|
|
* with remote data.
|
|
|
|
*
|
|
|
|
* This function should delete all local data that the store is managing. It
|
|
|
|
* can be thought of as clearing out all state and restoring the "new
|
|
|
|
* browser" state.
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async wipe() {
|
2017-07-23 04:55:43 +03:00
|
|
|
throw new Error("override wipe in a subclass");
|
2011-01-19 03:23:25 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EngineManager = function EngineManager(service) {
|
2012-08-30 01:43:41 +04:00
|
|
|
this.service = service;
|
2008-04-15 05:53:35 +04:00
|
|
|
|
|
|
|
this._engines = {};
|
2014-03-14 03:37:25 +04:00
|
|
|
|
|
|
|
// This will be populated by Service on startup.
|
|
|
|
this._declined = new Set();
|
2013-08-26 22:55:58 +04:00
|
|
|
this._log = Log.repository.getLogger("Sync.EngineManager");
|
2017-12-19 01:48:31 +03:00
|
|
|
this._log.manageLevelFromPref("services.sync.log.logger.service.engines");
|
|
|
|
// define the default level for all engine logs here (although each engine
|
|
|
|
// allows its level to be controlled via a specific, non-default pref)
|
|
|
|
Log.repository.getLogger(`Sync.Engine`).manageLevelFromPref("services.sync.log.logger.engine");
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2012-08-30 01:43:40 +04:00
|
|
|
EngineManager.prototype = {
|
2017-01-10 20:09:02 +03:00
|
|
|
get(name) {
|
2009-03-20 10:13:16 +03:00
|
|
|
// Return an array of engines if we have an array of names
|
2011-08-26 21:26:11 +04:00
|
|
|
if (Array.isArray(name)) {
|
2009-03-20 10:13:16 +03:00
|
|
|
let engines = [];
|
|
|
|
name.forEach(function(name) {
|
|
|
|
let engine = this.get(name);
|
2013-12-19 09:42:17 +04:00
|
|
|
if (engine) {
|
2009-03-20 10:13:16 +03:00
|
|
|
engines.push(engine);
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
2009-03-20 10:13:16 +03:00
|
|
|
}, this);
|
|
|
|
return engines;
|
|
|
|
}
|
|
|
|
|
|
|
|
let engine = this._engines[name];
|
2010-11-30 03:41:17 +03:00
|
|
|
if (!engine) {
|
2009-03-20 10:13:16 +03:00
|
|
|
this._log.debug("Could not get engine: " + name);
|
2013-12-19 09:42:17 +04:00
|
|
|
if (Object.keys) {
|
2010-11-30 03:41:17 +03:00
|
|
|
this._log.debug("Engines are: " + JSON.stringify(Object.keys(this._engines)));
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
2010-11-30 03:41:17 +03:00
|
|
|
}
|
2009-03-20 10:13:16 +03:00
|
|
|
return engine;
|
2008-04-15 05:53:35 +04:00
|
|
|
},
|
2012-08-30 01:43:40 +04:00
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
getAll() {
|
2016-01-07 03:02:16 +03:00
|
|
|
let engines = [];
|
2016-08-09 05:40:23 +03:00
|
|
|
for (let [, engine] of Object.entries(this._engines)) {
|
2016-01-07 03:02:16 +03:00
|
|
|
engines.push(engine);
|
|
|
|
}
|
|
|
|
return engines;
|
2008-04-15 05:53:35 +04:00
|
|
|
},
|
2012-08-30 01:43:40 +04:00
|
|
|
|
2014-03-14 03:37:25 +04:00
|
|
|
/**
|
|
|
|
* N.B., does not pay attention to the declined list.
|
|
|
|
*/
|
2017-01-10 20:09:02 +03:00
|
|
|
getEnabled() {
|
2014-10-01 10:44:52 +04:00
|
|
|
return this.getAll()
|
|
|
|
.filter((engine) => engine.enabled)
|
|
|
|
.sort((a, b) => a.syncPriority - b.syncPriority);
|
2008-07-10 04:17:24 +04:00
|
|
|
},
|
2012-08-30 01:43:40 +04:00
|
|
|
|
2014-03-14 03:37:25 +04:00
|
|
|
get enabledEngineNames() {
|
2015-10-18 21:52:58 +03:00
|
|
|
return this.getEnabled().map(e => e.name);
|
2014-03-14 03:37:25 +04:00
|
|
|
},
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
persistDeclined() {
|
2014-03-14 03:37:25 +04:00
|
|
|
Svc.Prefs.set("declinedEngines", [...this._declined].join(","));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array.
|
|
|
|
*/
|
2017-01-10 20:09:02 +03:00
|
|
|
getDeclined() {
|
2014-03-14 03:37:25 +04:00
|
|
|
return [...this._declined];
|
|
|
|
},
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
setDeclined(engines) {
|
2014-03-14 03:37:25 +04:00
|
|
|
this._declined = new Set(engines);
|
|
|
|
this.persistDeclined();
|
|
|
|
},
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
isDeclined(engineName) {
|
2014-03-14 03:37:25 +04:00
|
|
|
return this._declined.has(engineName);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts a Set or an array.
|
|
|
|
*/
|
2017-01-10 20:09:02 +03:00
|
|
|
decline(engines) {
|
2014-03-14 03:37:25 +04:00
|
|
|
for (let e of engines) {
|
|
|
|
this._declined.add(e);
|
|
|
|
}
|
|
|
|
this.persistDeclined();
|
|
|
|
},
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
undecline(engines) {
|
2014-03-14 03:37:25 +04:00
|
|
|
for (let e of engines) {
|
|
|
|
this._declined.delete(e);
|
|
|
|
}
|
|
|
|
this.persistDeclined();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark any non-enabled engines as declined.
|
|
|
|
*
|
|
|
|
* This is useful after initial customization during setup.
|
|
|
|
*/
|
2017-01-10 20:09:02 +03:00
|
|
|
declineDisabled() {
|
2014-03-14 03:37:25 +04:00
|
|
|
for (let e of this.getAll()) {
|
|
|
|
if (!e.enabled) {
|
|
|
|
this._log.debug("Declining disabled engine " + e.name);
|
|
|
|
this._declined.add(e.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.persistDeclined();
|
|
|
|
},
|
|
|
|
|
2009-04-08 01:45:41 +04:00
|
|
|
/**
|
|
|
|
* Register an Engine to the service. Alternatively, give an array of engine
|
|
|
|
* objects to register.
|
|
|
|
*
|
|
|
|
* @param engineObject
|
|
|
|
* Engine object used to get an instance of the engine
|
|
|
|
* @return The engine object if anything failed
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async register(engineObject) {
|
2013-12-19 09:42:17 +04:00
|
|
|
if (Array.isArray(engineObject)) {
|
2017-06-06 01:50:07 +03:00
|
|
|
for (const e of engineObject) {
|
|
|
|
await this.register(e);
|
|
|
|
}
|
2017-02-17 04:34:45 +03:00
|
|
|
return;
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
2009-04-08 01:45:41 +04:00
|
|
|
|
|
|
|
try {
|
2012-08-30 01:43:41 +04:00
|
|
|
let engine = new engineObject(this.service);
|
2010-02-12 02:29:15 +03:00
|
|
|
let name = engine.name;
|
2013-12-19 09:42:17 +04:00
|
|
|
if (name in this._engines) {
|
2009-04-08 23:39:14 +04:00
|
|
|
this._log.error("Engine '" + name + "' is already registered!");
|
2013-12-19 09:42:17 +04:00
|
|
|
} else {
|
2017-06-06 01:50:07 +03:00
|
|
|
if (engine.initialize) {
|
|
|
|
await engine.initialize();
|
|
|
|
}
|
2010-02-12 02:29:15 +03:00
|
|
|
this._engines[name] = engine;
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
|
|
|
} catch (ex) {
|
2009-04-08 01:45:41 +04:00
|
|
|
let name = engineObject || "";
|
|
|
|
name = name.prototype || "";
|
|
|
|
name = name.name || "";
|
|
|
|
|
2016-01-07 09:09:00 +03:00
|
|
|
this._log.error(`Could not initialize engine ${name}`, ex);
|
2009-04-08 01:45:41 +04:00
|
|
|
}
|
2008-04-15 05:53:35 +04:00
|
|
|
},
|
2012-08-30 01:43:40 +04:00
|
|
|
|
2017-12-08 21:10:25 +03:00
|
|
|
async unregister(val) {
|
2008-04-15 05:53:35 +04:00
|
|
|
let name = val;
|
2013-12-19 09:42:17 +04:00
|
|
|
if (val instanceof Engine) {
|
2008-04-15 05:53:35 +04:00
|
|
|
name = val.name;
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
2017-01-24 20:59:09 +03:00
|
|
|
if (name in this._engines) {
|
|
|
|
let engine = this._engines[name];
|
|
|
|
delete this._engines[name];
|
2017-12-08 21:10:25 +03:00
|
|
|
await engine.finalize();
|
2017-01-24 20:59:09 +03:00
|
|
|
}
|
2012-08-30 01:43:40 +04:00
|
|
|
},
|
2012-08-30 01:43:41 +04:00
|
|
|
|
2017-12-08 21:10:25 +03:00
|
|
|
async clear() {
|
2012-08-30 01:43:41 +04:00
|
|
|
for (let name in this._engines) {
|
2017-02-14 00:48:08 +03:00
|
|
|
let engine = this._engines[name];
|
2012-08-30 01:43:41 +04:00
|
|
|
delete this._engines[name];
|
2017-12-08 21:10:25 +03:00
|
|
|
await engine.finalize();
|
2012-08-30 01:43:41 +04:00
|
|
|
}
|
|
|
|
},
|
2008-04-15 05:53:35 +04:00
|
|
|
};
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.Engine = function Engine(name, service) {
|
2012-08-30 01:43:41 +04:00
|
|
|
if (!service) {
|
|
|
|
throw new Error("Engine must be associated with a Service instance.");
|
|
|
|
}
|
|
|
|
|
2010-02-12 02:29:15 +03:00
|
|
|
this.Name = name || "Unnamed";
|
|
|
|
this.name = name.toLowerCase();
|
2012-08-30 01:43:41 +04:00
|
|
|
this.service = service;
|
2010-02-12 02:29:15 +03:00
|
|
|
|
2010-02-12 02:25:31 +03:00
|
|
|
this._notify = Utils.notify("weave:engine:");
|
2013-08-26 22:55:58 +04:00
|
|
|
this._log = Log.repository.getLogger("Sync.Engine." + this.Name);
|
2017-12-19 01:48:31 +03:00
|
|
|
this._log.manageLevelFromPref(`services.sync.log.logger.engine.${this.name}`);
|
2010-02-12 02:25:31 +03:00
|
|
|
|
2017-02-06 21:19:27 +03:00
|
|
|
this._modified = this.emptyChangeset();
|
2010-02-12 02:25:31 +03:00
|
|
|
this._tracker; // initialize tracker to load previously changed IDs
|
2017-06-06 01:50:07 +03:00
|
|
|
this._log.debug("Engine constructed");
|
2017-12-20 20:36:18 +03:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(this, "_enabled",
|
|
|
|
`services.sync.engine.${this.prefName}`, false,
|
|
|
|
(data, previous, latest) =>
|
2018-01-05 02:07:10 +03:00
|
|
|
// We do not await on the promise onEngineEnabledChanged returns.
|
2017-12-20 20:36:18 +03:00
|
|
|
this._tracker.onEngineEnabledChanged(latest));
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2007-12-15 05:07:25 +03:00
|
|
|
Engine.prototype = {
|
2009-01-07 00:54:18 +03:00
|
|
|
// _storeObj, and _trackerObj should to be overridden in subclasses
|
|
|
|
_storeObj: Store,
|
|
|
|
_trackerObj: Tracker,
|
2007-12-15 05:07:25 +03:00
|
|
|
|
2017-02-06 21:19:27 +03:00
|
|
|
// Override this method to return a new changeset type.
|
|
|
|
emptyChangeset() {
|
|
|
|
return new Changeset();
|
|
|
|
},
|
|
|
|
|
2011-06-15 11:03:32 +04:00
|
|
|
// Local 'constant'.
|
|
|
|
// Signal to the engine that processing further records is pointless.
|
|
|
|
eEngineAbortApplyIncoming: "error.engine.abort.applyincoming",
|
|
|
|
|
2016-08-24 23:02:14 +03:00
|
|
|
// Should we keep syncing if we find a record that cannot be uploaded (ever)?
|
|
|
|
// If this is false, we'll throw, otherwise, we'll ignore the record and
|
|
|
|
// continue. This currently can only happen due to the record being larger
|
|
|
|
// than the record upload limit.
|
2016-12-01 01:28:52 +03:00
|
|
|
allowSkippedRecord: true,
|
2016-08-24 23:02:14 +03:00
|
|
|
|
2015-01-25 10:50:01 +03:00
|
|
|
get prefName() {
|
|
|
|
return this.name;
|
|
|
|
},
|
|
|
|
|
2013-12-19 09:42:17 +04:00
|
|
|
get enabled() {
|
2017-12-20 20:36:18 +03:00
|
|
|
return this._enabled;
|
2013-12-19 09:42:17 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
set enabled(val) {
|
2018-01-31 23:09:30 +03:00
|
|
|
if (!!val != this._enabled) {
|
|
|
|
Svc.Prefs.set("engine." + this.prefName, !!val);
|
2017-12-20 20:36:18 +03:00
|
|
|
}
|
2013-12-19 09:42:17 +04:00
|
|
|
},
|
2009-05-14 20:38:15 +04:00
|
|
|
|
2015-01-25 10:50:01 +03:00
|
|
|
get score() {
|
|
|
|
return this._tracker.score;
|
|
|
|
},
|
2008-04-16 04:21:34 +04:00
|
|
|
|
2007-12-15 05:07:25 +03:00
|
|
|
get _store() {
|
2012-08-30 01:43:41 +04:00
|
|
|
let store = new this._storeObj(this.Name, this);
|
2015-01-25 10:50:01 +03:00
|
|
|
this.__defineGetter__("_store", () => store);
|
2010-02-12 02:29:15 +03:00
|
|
|
return store;
|
2007-12-15 05:07:25 +03:00
|
|
|
},
|
2008-05-23 22:05:42 +04:00
|
|
|
|
2008-05-23 02:58:29 +04:00
|
|
|
get _tracker() {
|
2012-08-30 01:43:41 +04:00
|
|
|
let tracker = new this._trackerObj(this.Name, this);
|
2015-01-25 10:50:01 +03:00
|
|
|
this.__defineGetter__("_tracker", () => tracker);
|
2010-02-12 02:29:15 +03:00
|
|
|
return tracker;
|
2007-12-11 08:38:53 +03:00
|
|
|
},
|
2009-11-21 01:34:20 +03:00
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
startTracking() {
|
|
|
|
this._tracker.start();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns a promise
|
|
|
|
stopTracking() {
|
|
|
|
return this._tracker.stop();
|
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async sync() {
|
2013-12-19 09:42:17 +04:00
|
|
|
if (!this.enabled) {
|
2017-06-06 01:50:07 +03:00
|
|
|
return false;
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
2010-04-30 03:42:21 +04:00
|
|
|
|
2013-12-19 09:42:17 +04:00
|
|
|
if (!this._sync) {
|
2017-07-23 04:55:43 +03:00
|
|
|
throw new Error("engine does not implement _sync method");
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
2009-09-04 07:11:36 +04:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
return this._notify("sync", this.name, this._sync)();
|
2008-07-25 12:06:23 +04:00
|
|
|
},
|
|
|
|
|
2009-02-27 09:36:14 +03:00
|
|
|
/**
|
2013-12-19 09:42:17 +04:00
|
|
|
* Get rid of any local meta-data.
|
2009-02-27 09:36:14 +03:00
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async resetClient() {
|
2013-12-19 09:42:17 +04:00
|
|
|
if (!this._resetClient) {
|
2017-07-23 04:55:43 +03:00
|
|
|
throw new Error("engine does not implement _resetClient method");
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
2009-02-27 09:36:14 +03:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
return this._notify("reset-client", this.name, this._resetClient)();
|
2009-02-27 09:36:14 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _wipeClient() {
|
|
|
|
await this.resetClient();
|
2009-01-07 00:54:18 +03:00
|
|
|
this._log.debug("Deleting all local data");
|
2010-04-08 04:52:22 +04:00
|
|
|
this._tracker.ignoreAll = true;
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._store.wipe();
|
2010-04-08 04:52:22 +04:00
|
|
|
this._tracker.ignoreAll = false;
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._tracker.clearChangedIDs();
|
2008-07-25 12:06:23 +04:00
|
|
|
},
|
2009-02-27 09:36:14 +03:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async wipeClient() {
|
|
|
|
return this._notify("wipe-client", this.name, this._wipeClient)();
|
2016-09-12 21:59:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If one exists, initialize and return a validator for this engine (which
|
|
|
|
* must have a `validate(engine)` method that returns a promise to an object
|
|
|
|
* with a getSummary method). Otherwise return null.
|
|
|
|
*/
|
2017-01-10 20:09:02 +03:00
|
|
|
getValidator() {
|
2016-09-12 21:59:25 +03:00
|
|
|
return null;
|
2017-01-24 20:59:09 +03:00
|
|
|
},
|
|
|
|
|
2017-02-14 00:48:08 +03:00
|
|
|
async finalize() {
|
|
|
|
await this._tracker.finalize();
|
2017-01-24 20:59:09 +03:00
|
|
|
},
|
2008-07-25 12:06:23 +04:00
|
|
|
};
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.SyncEngine = function SyncEngine(name, service) {
|
2012-08-30 01:43:41 +04:00
|
|
|
Engine.call(this, name || "SyncEngine", service);
|
|
|
|
|
2017-11-01 21:10:47 +03:00
|
|
|
this._toFetchStorage = new JSONFile({
|
|
|
|
path: Utils.jsonFilePath("toFetch/" + this.name),
|
|
|
|
dataPostProcessor: json => this._metadataPostProcessor(json),
|
|
|
|
beforeSave: () => this._beforeSaveMetadata(),
|
|
|
|
});
|
|
|
|
|
|
|
|
this._previousFailedStorage = new JSONFile({
|
|
|
|
path: Utils.jsonFilePath("failed/" + this.name),
|
|
|
|
dataPostProcessor: json => this._metadataPostProcessor(json),
|
|
|
|
beforeSave: () => this._beforeSaveMetadata(),
|
|
|
|
});
|
2017-12-20 20:36:18 +03:00
|
|
|
Utils.defineLazyIDProperty(this, "syncID", `services.sync.${this.name}.syncID`);
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(this, "_lastSync",
|
|
|
|
`services.sync.${this.name}.lastSync`,
|
|
|
|
"0", null,
|
|
|
|
v => parseFloat(v));
|
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(this, "_lastSyncLocal",
|
|
|
|
`services.sync.${this.name}.lastSyncLocal`,
|
|
|
|
"0", null,
|
|
|
|
v => parseInt(v, 10));
|
2017-06-06 01:50:07 +03:00
|
|
|
// Async initializations can be made in the initialize() method.
|
|
|
|
|
2017-07-18 21:18:04 +03:00
|
|
|
// The map of ids => metadata for records needing a weak upload.
|
|
|
|
//
|
|
|
|
// Currently the "metadata" fields are:
|
|
|
|
//
|
|
|
|
// - forceTombstone: whether or not we should ignore the local information
|
|
|
|
// about the record, and write a tombstone for it anyway -- e.g. in the case
|
|
|
|
// of records that should exist locally, but should never be uploaded to the
|
|
|
|
// server (note that not all sync engines support tombstones)
|
|
|
|
//
|
|
|
|
// The difference between this and a "normal" upload is that these records
|
|
|
|
// are only tracked in memory, and if the upload attempt fails (shutdown,
|
|
|
|
// 412, etc), we abort uploading the "weak" set (by clearing the map).
|
2017-01-17 22:45:08 +03:00
|
|
|
//
|
|
|
|
// The rationale here is for the cases where we receive a record from the
|
|
|
|
// server that we know is wrong in some (small) way. For example, the
|
|
|
|
// dateAdded field on bookmarks -- maybe we have a better date, or the server
|
|
|
|
// record is entirely missing the date, etc.
|
|
|
|
//
|
|
|
|
// In these cases, we fix our local copy of the record, and mark it for
|
2017-07-18 21:18:04 +03:00
|
|
|
// weak upload. A normal ("strong") upload is problematic here because
|
2017-01-17 22:45:08 +03:00
|
|
|
// in the case of a conflict from the server, there's a window where our
|
|
|
|
// record would be marked as modified more recently than a change that occurs
|
|
|
|
// on another device change, and we lose data from the user.
|
2017-07-18 21:18:04 +03:00
|
|
|
//
|
|
|
|
// Additionally, we use this as the set of items to upload for bookmark
|
|
|
|
// repair reponse, which has similar constraints.
|
|
|
|
this._needWeakUpload = new Map();
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2011-03-21 02:10:40 +03:00
|
|
|
|
|
|
|
// Enumeration to define approaches to handling bad records.
|
|
|
|
// Attached to the constructor to allow use as a kind of static enumeration.
|
|
|
|
SyncEngine.kRecoveryStrategy = {
|
|
|
|
ignore: "ignore",
|
|
|
|
retry: "retry",
|
|
|
|
error: "error"
|
|
|
|
};
|
|
|
|
|
2008-12-05 11:39:54 +03:00
|
|
|
SyncEngine.prototype = {
|
2008-11-07 06:18:07 +03:00
|
|
|
__proto__: Engine.prototype,
|
2009-01-07 00:54:18 +03:00
|
|
|
_recordObj: CryptoWrapper,
|
2010-03-25 20:05:21 +03:00
|
|
|
version: 1,
|
2014-03-14 03:37:25 +04:00
|
|
|
|
2014-11-13 22:05:28 +03:00
|
|
|
// Which sortindex to use when retrieving records for this engine.
|
|
|
|
_defaultSort: undefined,
|
|
|
|
|
2017-11-01 21:10:47 +03:00
|
|
|
_metadataPostProcessor(json) {
|
|
|
|
if (Array.isArray(json)) {
|
|
|
|
// Pre-`JSONFile` storage stored an array, but `JSONFile` defaults to
|
|
|
|
// an object, so we wrap the array for consistency.
|
2018-02-05 19:39:30 +03:00
|
|
|
json = { ids: json };
|
2017-11-01 21:10:47 +03:00
|
|
|
}
|
|
|
|
if (!json.ids) {
|
|
|
|
json.ids = [];
|
|
|
|
}
|
2018-01-10 03:22:33 +03:00
|
|
|
// The set serializes the same way as an array, but offers more efficient
|
|
|
|
// methods of manipulation.
|
|
|
|
json.ids = new SerializableSet(json.ids);
|
2017-11-01 21:10:47 +03:00
|
|
|
return json;
|
|
|
|
},
|
|
|
|
|
|
|
|
async _beforeSaveMetadata() {
|
|
|
|
await ensureDirectory(this._toFetchStorage.path);
|
|
|
|
await ensureDirectory(this._previousFailedStorage.path);
|
|
|
|
},
|
|
|
|
|
2014-10-01 10:44:52 +04:00
|
|
|
// A relative priority to use when computing an order
|
|
|
|
// for engines to be synced. Higher-priority engines
|
|
|
|
// (lower numbers) are synced first.
|
|
|
|
// It is recommended that a unique value be used for each engine,
|
|
|
|
// in order to guarantee a stable sequence.
|
|
|
|
syncPriority: 0,
|
|
|
|
|
2011-04-07 04:18:22 +04:00
|
|
|
// How many records to pull in a single sync. This is primarily to avoid very
|
|
|
|
// long first syncs against profiles with many history records.
|
2011-01-28 20:39:12 +03:00
|
|
|
downloadLimit: null,
|
2014-03-14 03:37:25 +04:00
|
|
|
|
2011-04-07 04:18:22 +04:00
|
|
|
// How many records to pull at one time when specifying IDs. This is to avoid
|
|
|
|
// URI length limitations.
|
|
|
|
guidFetchBatchSize: DEFAULT_GUID_FETCH_BATCH_SIZE,
|
2014-03-14 03:37:25 +04:00
|
|
|
|
Bug 1368209 - Add a test for fetching backlogged history records. r=tcsc
The test captures the existing logic in `_processIncoming`, even though
it's not quite correct:
* First, we fetch all records changed since the last sync, up to the
download limit, and without an explicit sort order. This happens to
work correctly now because the Python server uses "newest" by
default, but can change in the future.
* If we reached the download limit fetching records, we request
IDs for all records changed since the last sync, also up to the
download limit, and sorted by index. This is likely to return IDs
for records we've already seen, since the index is based on the
frecency. It's also likely to miss IDs for other changed records,
because the number of changed records might be higher than the
download limit.
* Since we then fast-forward the last sync time, we'll never download
any remaining changed records that we didn't add to our backlog.
* Finally, we backfill previously failed and backlogged records.
MozReview-Commit-ID: 7uQLXMseMIU
--HG--
extra : rebase_source : 719ee2d9e46102195251b410f093da3247095c22
2017-10-28 03:54:48 +03:00
|
|
|
downloadBatchSize: DEFAULT_DOWNLOAD_BATCH_SIZE,
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async initialize() {
|
2017-11-01 21:10:47 +03:00
|
|
|
await this._toFetchStorage.load();
|
|
|
|
await this._previousFailedStorage.load();
|
2017-06-06 01:50:07 +03:00
|
|
|
this._log.debug("SyncEngine initialized", this.name);
|
|
|
|
},
|
|
|
|
|
2015-01-25 10:50:01 +03:00
|
|
|
get storageURL() {
|
|
|
|
return this.service.storageURL;
|
|
|
|
},
|
2008-12-05 11:39:54 +03:00
|
|
|
|
2015-01-25 10:50:01 +03:00
|
|
|
get engineURL() {
|
|
|
|
return this.storageURL + this.name;
|
|
|
|
},
|
2008-12-05 11:39:54 +03:00
|
|
|
|
2015-01-25 10:50:01 +03:00
|
|
|
get cryptoKeysURL() {
|
|
|
|
return this.storageURL + "crypto/keys";
|
|
|
|
},
|
2008-11-20 03:20:25 +03:00
|
|
|
|
2015-01-25 10:50:01 +03:00
|
|
|
get metaURL() {
|
|
|
|
return this.storageURL + "meta/global";
|
|
|
|
},
|
2010-03-25 20:05:21 +03:00
|
|
|
|
2010-11-11 22:00:35 +03:00
|
|
|
/*
|
|
|
|
* lastSync is a timestamp in server time.
|
|
|
|
*/
|
2017-12-20 04:10:54 +03:00
|
|
|
async getLastSync() {
|
|
|
|
return this.lastSync;
|
|
|
|
},
|
|
|
|
async setLastSync(lastSync) {
|
|
|
|
this.lastSync = lastSync;
|
|
|
|
},
|
2008-11-20 03:20:25 +03:00
|
|
|
get lastSync() {
|
2017-12-20 20:36:18 +03:00
|
|
|
return this._lastSync;
|
2008-11-20 03:20:25 +03:00
|
|
|
},
|
2008-11-07 10:23:35 +03:00
|
|
|
set lastSync(value) {
|
2009-06-17 04:22:59 +04:00
|
|
|
// Store the value as a string to keep floating point precision
|
|
|
|
Svc.Prefs.set(this.name + ".lastSync", value.toString());
|
2009-02-18 00:20:02 +03:00
|
|
|
},
|
2017-01-10 20:09:02 +03:00
|
|
|
resetLastSync() {
|
2009-02-18 00:20:02 +03:00
|
|
|
this._log.debug("Resetting " + this.name + " last sync time");
|
2009-06-17 04:22:59 +04:00
|
|
|
Svc.Prefs.set(this.name + ".lastSync", "0");
|
2010-11-11 22:00:35 +03:00
|
|
|
this.lastSyncLocal = 0;
|
|
|
|
},
|
|
|
|
|
2015-01-25 10:50:01 +03:00
|
|
|
get toFetch() {
|
2017-11-01 21:10:47 +03:00
|
|
|
this._toFetchStorage.ensureDataReady();
|
|
|
|
return this._toFetchStorage.data.ids;
|
2011-01-27 08:34:31 +03:00
|
|
|
},
|
|
|
|
|
2017-11-01 21:10:47 +03:00
|
|
|
set toFetch(ids) {
|
2018-01-10 03:22:33 +03:00
|
|
|
if (ids.constructor.name != "SerializableSet") {
|
|
|
|
throw new Error("Bug: Attempted to set toFetch to something that isn't a SerializableSet");
|
|
|
|
}
|
2017-11-01 21:10:47 +03:00
|
|
|
this._toFetchStorage.data = { ids };
|
|
|
|
this._toFetchStorage.saveSoon();
|
2011-01-27 08:34:31 +03:00
|
|
|
},
|
|
|
|
|
2015-01-25 10:50:01 +03:00
|
|
|
get previousFailed() {
|
2017-11-01 21:10:47 +03:00
|
|
|
this._previousFailedStorage.ensureDataReady();
|
|
|
|
return this._previousFailedStorage.data.ids;
|
2015-01-25 10:50:01 +03:00
|
|
|
},
|
2018-01-10 03:22:33 +03:00
|
|
|
|
2017-11-01 21:10:47 +03:00
|
|
|
set previousFailed(ids) {
|
2018-01-10 03:22:33 +03:00
|
|
|
if (ids.constructor.name != "SerializableSet") {
|
|
|
|
throw new Error(
|
|
|
|
"Bug: Attempted to set previousFailed to something that isn't a SerializableSet");
|
|
|
|
}
|
2017-11-01 21:10:47 +03:00
|
|
|
this._previousFailedStorage.data = { ids };
|
|
|
|
this._previousFailedStorage.saveSoon();
|
2011-05-28 04:32:13 +04:00
|
|
|
},
|
|
|
|
|
2010-11-11 22:00:35 +03:00
|
|
|
/*
|
|
|
|
* lastSyncLocal is a timestamp in local time.
|
|
|
|
*/
|
|
|
|
get lastSyncLocal() {
|
2017-12-20 20:36:18 +03:00
|
|
|
return this._lastSyncLocal;
|
2010-11-11 22:00:35 +03:00
|
|
|
},
|
|
|
|
set lastSyncLocal(value) {
|
|
|
|
// Store as a string because pref can only store C longs as numbers.
|
|
|
|
Svc.Prefs.set(this.name + ".lastSyncLocal", value.toString());
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
2016-09-06 21:39:13 +03:00
|
|
|
* Returns a changeset for this sync. Engine implementations can override this
|
|
|
|
* method to bypass the tracker for certain or all changed items.
|
2010-11-11 22:00:35 +03:00
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async getChangedIDs() {
|
2018-01-05 02:07:10 +03:00
|
|
|
return this._tracker.getChangedIDs();
|
2008-11-07 10:23:35 +03:00
|
|
|
},
|
|
|
|
|
2017-02-06 21:19:27 +03:00
|
|
|
// Create a new record using the store and add in metadata.
|
2017-06-06 01:50:07 +03:00
|
|
|
async _createRecord(id) {
|
|
|
|
let record = await this._store.createRecord(id, this.name);
|
2010-03-06 01:46:48 +03:00
|
|
|
record.id = id;
|
2010-11-30 03:41:17 +03:00
|
|
|
record.collection = this.name;
|
2010-03-06 01:46:48 +03:00
|
|
|
return record;
|
2008-11-20 03:20:25 +03:00
|
|
|
},
|
|
|
|
|
2017-02-06 21:19:27 +03:00
|
|
|
// Creates a tombstone Sync record with additional metadata.
|
|
|
|
_createTombstone(id) {
|
|
|
|
let tombstone = new this._recordObj(this.name, id);
|
|
|
|
tombstone.id = id;
|
|
|
|
tombstone.collection = this.name;
|
|
|
|
tombstone.deleted = true;
|
|
|
|
return tombstone;
|
|
|
|
},
|
|
|
|
|
2017-07-18 21:18:04 +03:00
|
|
|
addForWeakUpload(id, { forceTombstone = false } = {}) {
|
|
|
|
this._needWeakUpload.set(id, { forceTombstone });
|
|
|
|
},
|
|
|
|
|
2008-12-05 11:39:54 +03:00
|
|
|
// Any setup that needs to happen at the beginning of each sync.
|
2017-06-06 01:50:07 +03:00
|
|
|
async _syncStartup() {
|
2010-03-25 20:05:21 +03:00
|
|
|
// Determine if we need to wipe on outdated versions
|
2017-06-06 01:50:07 +03:00
|
|
|
let metaGlobal = await this.service.recordManager.get(this.metaURL);
|
2010-03-25 20:05:21 +03:00
|
|
|
let engines = metaGlobal.payload.engines || {};
|
|
|
|
let engineData = engines[this.name] || {};
|
|
|
|
|
2010-11-30 03:41:17 +03:00
|
|
|
let needsWipe = false;
|
|
|
|
|
2010-03-25 20:05:21 +03:00
|
|
|
// Assume missing versions are 0 and wipe the server
|
|
|
|
if ((engineData.version || 0) < this.version) {
|
|
|
|
this._log.debug("Old engine data: " + [engineData.version, this.version]);
|
|
|
|
|
|
|
|
// Prepare to clear the server and upload everything
|
2010-11-30 03:41:17 +03:00
|
|
|
needsWipe = true;
|
2010-03-25 20:05:21 +03:00
|
|
|
this.syncID = "";
|
|
|
|
|
|
|
|
// Set the newer version and newly generated syncID
|
|
|
|
engineData.version = this.version;
|
|
|
|
engineData.syncID = this.syncID;
|
|
|
|
|
|
|
|
// Put the new data back into meta/global and mark for upload
|
|
|
|
engines[this.name] = engineData;
|
|
|
|
metaGlobal.payload.engines = engines;
|
|
|
|
metaGlobal.changed = true;
|
2017-01-13 02:24:52 +03:00
|
|
|
} else if (engineData.version > this.version) {
|
|
|
|
// Don't sync this engine if the server has newer data
|
2017-08-07 21:27:51 +03:00
|
|
|
|
|
|
|
// Changes below need to be processed in bug 1295510 that's why eslint is ignored
|
|
|
|
// eslint-disable-next-line no-new-wrappers
|
2010-03-25 20:05:21 +03:00
|
|
|
let error = new String("New data: " + [engineData.version, this.version]);
|
|
|
|
error.failureCode = VERSION_OUT_OF_DATE;
|
|
|
|
throw error;
|
2017-01-13 02:24:52 +03:00
|
|
|
} else if (engineData.syncID != this.syncID) {
|
|
|
|
// Changes to syncID mean we'll need to upload everything
|
2010-03-25 20:05:21 +03:00
|
|
|
this._log.debug("Engine syncIDs: " + [engineData.syncID, this.syncID]);
|
|
|
|
this.syncID = engineData.syncID;
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._resetClient();
|
2017-01-10 20:09:02 +03:00
|
|
|
}
|
2010-03-25 20:05:21 +03:00
|
|
|
|
2010-11-30 03:41:17 +03:00
|
|
|
// Delete any existing data and reupload on bad version or missing meta.
|
|
|
|
// No crypto component here...? We could regenerate per-collection keys...
|
|
|
|
if (needsWipe) {
|
2017-06-06 01:50:07 +03:00
|
|
|
await this.wipeServer();
|
2008-12-05 11:39:54 +03:00
|
|
|
}
|
2008-11-24 19:04:14 +03:00
|
|
|
|
2010-11-24 08:21:31 +03:00
|
|
|
// Save objects that need to be uploaded in this._modified. We also save
|
|
|
|
// the timestamp of this fetch in this.lastSyncLocal. As we successfully
|
|
|
|
// upload objects we remove them from this._modified. If an error occurs
|
|
|
|
// or any objects fail to upload, they will remain in this._modified. At
|
|
|
|
// the end of a sync, or after an error, we add all objects remaining in
|
|
|
|
// this._modified to the tracker.
|
|
|
|
this.lastSyncLocal = Date.now();
|
2017-12-20 04:10:54 +03:00
|
|
|
let initialChanges = await this.pullChanges();
|
2017-02-06 21:19:27 +03:00
|
|
|
this._modified.replace(initialChanges);
|
2010-11-24 08:21:31 +03:00
|
|
|
// Clear the tracker now. If the sync fails we'll add the ones we failed
|
|
|
|
// to upload back.
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._tracker.clearChangedIDs();
|
2017-08-28 21:24:26 +03:00
|
|
|
this._tracker.resetScore();
|
2012-01-26 01:32:05 +04:00
|
|
|
|
2016-09-06 21:39:13 +03:00
|
|
|
this._log.info(this._modified.count() +
|
2010-11-24 08:21:31 +03:00
|
|
|
" outgoing items pre-reconciliation");
|
2009-09-01 04:30:34 +04:00
|
|
|
|
|
|
|
// Keep track of what to delete at the end of sync
|
|
|
|
this._delete = {};
|
2008-12-05 11:39:54 +03:00
|
|
|
},
|
|
|
|
|
2017-12-20 04:10:54 +03:00
|
|
|
async pullChanges() {
|
|
|
|
let lastSync = await this.getLastSync();
|
|
|
|
if (lastSync) {
|
|
|
|
return this.pullNewChanges();
|
|
|
|
}
|
|
|
|
this._log.debug("First sync, uploading all items");
|
|
|
|
return this.pullAllChanges();
|
|
|
|
},
|
|
|
|
|
2012-12-15 02:00:40 +04:00
|
|
|
/**
|
|
|
|
* A tiny abstraction to make it easier to test incoming record
|
|
|
|
* application.
|
|
|
|
*/
|
2017-01-10 20:09:02 +03:00
|
|
|
itemSource() {
|
2012-12-15 02:00:40 +04:00
|
|
|
return new Collection(this.engineURL, this._recordObj, this.service);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
* Download and apply remote records changed since the last sync. This
|
|
|
|
* happens in three stages.
|
|
|
|
*
|
|
|
|
* In the first stage, we fetch full records for all changed items, newest
|
|
|
|
* first, up to the download limit. The limit lets us make progress for large
|
|
|
|
* collections, where the sync is likely to be interrupted before we
|
|
|
|
* can fetch everything.
|
|
|
|
*
|
|
|
|
* In the second stage, we fetch the IDs of any remaining records changed
|
|
|
|
* since the last sync, add them to our backlog, and fast-forward our last
|
|
|
|
* sync time.
|
|
|
|
*
|
|
|
|
* In the third stage, we fetch and apply records for all backlogged IDs,
|
|
|
|
* as well as any records that failed to apply during the last sync. We
|
|
|
|
* request records for the IDs in chunks, to avoid exceeding URL length
|
|
|
|
* limits, then remove successfully applied records from the backlog, and
|
|
|
|
* record IDs of any records that failed to apply to retry on the next sync.
|
2012-12-15 02:00:40 +04:00
|
|
|
*/
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
async _processIncoming() {
|
2009-12-18 05:51:55 +03:00
|
|
|
this._log.trace("Downloading & applying server changes");
|
2008-11-24 19:04:14 +03:00
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
let newitems = this.itemSource();
|
2017-12-20 04:10:54 +03:00
|
|
|
let lastSync = await this.getLastSync();
|
2011-04-07 04:18:22 +04:00
|
|
|
|
2017-12-20 04:10:54 +03:00
|
|
|
newitems.newer = lastSync;
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
newitems.full = true;
|
2012-12-15 02:00:40 +04:00
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
let downloadLimit = Infinity;
|
|
|
|
if (this.downloadLimit) {
|
|
|
|
// Fetch new records up to the download limit. Currently, only the history
|
|
|
|
// engine sets a limit, since the history collection has the highest volume
|
|
|
|
// of changed records between syncs. The other engines fetch all records
|
|
|
|
// changed since the last sync.
|
|
|
|
if (this._defaultSort) {
|
|
|
|
// A download limit with a sort order doesn't make sense: we won't know
|
|
|
|
// which records to backfill.
|
|
|
|
throw new Error("Can't specify download limit with default sort order");
|
|
|
|
}
|
|
|
|
newitems.sort = "newest";
|
|
|
|
downloadLimit = newitems.limit = this.downloadLimit;
|
|
|
|
} else if (this._defaultSort) {
|
|
|
|
// The bookmarks engine fetches records by sort index; other engines leave
|
|
|
|
// the order unspecified. We can remove `_defaultSort` entirely after bug
|
|
|
|
// 1305563: the sort index won't matter because we'll buffer all bookmarks
|
|
|
|
// before applying.
|
2014-11-13 22:05:28 +03:00
|
|
|
newitems.sort = this._defaultSort;
|
|
|
|
}
|
|
|
|
|
2011-05-28 04:32:13 +04:00
|
|
|
// applied => number of items that should be applied.
|
|
|
|
// failed => number of items that failed in this sync.
|
|
|
|
// newFailed => number of items that failed for the first time in this sync.
|
|
|
|
// reconciled => number of items that were reconciled.
|
|
|
|
let count = {applied: 0, failed: 0, newFailed: 0, reconciled: 0};
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
let recordsToApply = [];
|
2018-01-10 03:22:33 +03:00
|
|
|
let failedInCurrentSync = new SerializableSet();
|
2017-11-03 12:40:06 +03:00
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
let oldestModified = this.lastModified;
|
|
|
|
let downloadedIDs = new Set();
|
2017-11-03 12:40:06 +03:00
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
// Stage 1: Fetch new records from the server, up to the download limit.
|
2017-12-20 04:10:54 +03:00
|
|
|
if (this.lastModified == null || this.lastModified > lastSync) {
|
Bug 1368209 - Add a test for fetching backlogged history records. r=tcsc
The test captures the existing logic in `_processIncoming`, even though
it's not quite correct:
* First, we fetch all records changed since the last sync, up to the
download limit, and without an explicit sort order. This happens to
work correctly now because the Python server uses "newest" by
default, but can change in the future.
* If we reached the download limit fetching records, we request
IDs for all records changed since the last sync, also up to the
download limit, and sorted by index. This is likely to return IDs
for records we've already seen, since the index is based on the
frecency. It's also likely to miss IDs for other changed records,
because the number of changed records might be higher than the
download limit.
* Since we then fast-forward the last sync time, we'll never download
any remaining changed records that we didn't add to our backlog.
* Finally, we backfill previously failed and backlogged records.
MozReview-Commit-ID: 7uQLXMseMIU
--HG--
extra : rebase_source : 719ee2d9e46102195251b410f093da3247095c22
2017-10-28 03:54:48 +03:00
|
|
|
let { response, records } = await newitems.getBatched(this.downloadBatchSize);
|
2017-05-30 20:23:57 +03:00
|
|
|
if (!response.success) {
|
|
|
|
response.failureCode = ENGINE_DOWNLOAD_FAIL;
|
|
|
|
throw response;
|
2009-09-16 05:38:52 +04:00
|
|
|
}
|
2011-06-15 11:03:32 +04:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
let maybeYield = Async.jankYielder();
|
2017-05-30 20:23:57 +03:00
|
|
|
for (let record of records) {
|
2017-06-06 01:50:07 +03:00
|
|
|
await maybeYield();
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
downloadedIDs.add(record.id);
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : 13605dd3a43569a6d83dc2eb15a578a7bbd5c1ca
2017-11-01 21:09:57 +03:00
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
if (record.modified < oldestModified) {
|
|
|
|
oldestModified = record.modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
let { shouldApply, error } = await this._maybeReconcile(record);
|
|
|
|
if (error) {
|
2018-01-10 03:22:33 +03:00
|
|
|
failedInCurrentSync.add(record.id);
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
count.failed++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!shouldApply) {
|
|
|
|
count.reconciled++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
recordsToApply.push(record);
|
2017-11-03 12:40:06 +03:00
|
|
|
}
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
|
|
|
|
let failedToApply = await this._applyRecords(recordsToApply);
|
2018-01-10 03:22:33 +03:00
|
|
|
Utils.setAddAll(failedInCurrentSync, failedToApply);
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
|
|
|
|
// `applied` is a bit of a misnomer: it counts records that *should* be
|
|
|
|
// applied, so it also includes records that we tried to apply and failed.
|
|
|
|
// `recordsToApply.length - failedToApply.length` is the number of records
|
|
|
|
// that we *successfully* applied.
|
|
|
|
count.failed += failedToApply.length;
|
|
|
|
count.applied += recordsToApply.length;
|
2009-09-11 10:11:33 +04:00
|
|
|
}
|
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
// Stage 2: If we reached our download limit, we might still have records
|
|
|
|
// on the server that changed since the last sync. Fetch the IDs for the
|
|
|
|
// remaining records, and add them to the backlog. Note that this stage
|
|
|
|
// only runs for engines that set a download limit.
|
|
|
|
if (downloadedIDs.size == downloadLimit) {
|
|
|
|
let guidColl = this.itemSource();
|
2017-11-03 12:40:06 +03:00
|
|
|
|
2017-12-20 04:10:54 +03:00
|
|
|
guidColl.newer = lastSync;
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
guidColl.older = oldestModified;
|
|
|
|
guidColl.sort = "oldest";
|
2009-09-11 10:11:33 +04:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
let guids = await guidColl.get();
|
2009-09-11 10:11:33 +04:00
|
|
|
if (!guids.success)
|
|
|
|
throw guids;
|
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
// Filtering out already downloaded IDs here isn't necessary. We only do
|
|
|
|
// that in case the Sync server doesn't support `older` (bug 1316110).
|
|
|
|
let remainingIDs = guids.obj.filter(id => !downloadedIDs.has(id));
|
|
|
|
if (remainingIDs.length > 0) {
|
2018-01-10 03:22:33 +03:00
|
|
|
this.toFetch = Utils.setAddAll(this.toFetch, remainingIDs);
|
2011-01-28 20:39:12 +03:00
|
|
|
}
|
2011-01-27 08:34:31 +03:00
|
|
|
}
|
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
// Fast-foward the lastSync timestamp since we have backlogged the
|
|
|
|
// remaining items.
|
2017-12-20 04:10:54 +03:00
|
|
|
if (lastSync < this.lastModified) {
|
|
|
|
lastSync = this.lastModified;
|
|
|
|
await this.setLastSync(lastSync);
|
2009-09-11 10:11:33 +04:00
|
|
|
}
|
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
// Stage 3: Backfill records from the backlog, and those that failed to
|
|
|
|
// decrypt or apply during the last sync. We only backfill up to the
|
|
|
|
// download limit, to prevent a large backlog for one engine from blocking
|
|
|
|
// the others. We'll keep processing the backlog on subsequent engine syncs.
|
|
|
|
let failedInPreviousSync = this.previousFailed;
|
2018-01-10 03:22:33 +03:00
|
|
|
let idsToBackfill = Array.from(
|
|
|
|
Utils.setAddAll(Utils.subsetOfSize(this.toFetch, downloadLimit),
|
|
|
|
failedInPreviousSync));
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
|
|
|
|
// Note that we intentionally overwrite the previously failed list here.
|
|
|
|
// Records that fail to decrypt or apply in two consecutive syncs are likely
|
|
|
|
// corrupt; we remove them from the list because retrying and failing on
|
|
|
|
// every subsequent sync just adds noise.
|
|
|
|
this.previousFailed = failedInCurrentSync;
|
|
|
|
|
|
|
|
let backfilledItems = this.itemSource();
|
|
|
|
|
|
|
|
backfilledItems.sort = "newest";
|
|
|
|
backfilledItems.full = true;
|
2011-04-07 04:18:22 +04:00
|
|
|
|
2017-11-08 11:05:11 +03:00
|
|
|
// `getBatched` includes the list of IDs as a query parameter, so we need to fetch
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
// records in chunks to avoid exceeding URI length limits.
|
|
|
|
for (let ids of PlacesSyncUtils.chunkArray(idsToBackfill, this.guidFetchBatchSize)) {
|
|
|
|
backfilledItems.ids = ids;
|
2009-09-11 10:11:33 +04:00
|
|
|
|
2017-11-08 11:05:11 +03:00
|
|
|
let {response, records} = await backfilledItems.getBatched(this.downloadBatchSize);
|
|
|
|
if (!response.success) {
|
|
|
|
response.failureCode = ENGINE_DOWNLOAD_FAIL;
|
|
|
|
throw response;
|
2009-09-16 05:38:52 +04:00
|
|
|
}
|
2009-07-23 03:38:34 +04:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
let maybeYield = Async.jankYielder();
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
let backfilledRecordsToApply = [];
|
|
|
|
let failedInBackfill = [];
|
|
|
|
|
2017-11-08 11:05:11 +03:00
|
|
|
for (let record of records) {
|
2017-06-06 01:50:07 +03:00
|
|
|
await maybeYield();
|
2011-06-15 11:03:32 +04:00
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
let { shouldApply, error } = await this._maybeReconcile(record);
|
|
|
|
if (error) {
|
|
|
|
failedInBackfill.push(record.id);
|
|
|
|
count.failed++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!shouldApply) {
|
|
|
|
count.reconciled++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
backfilledRecordsToApply.push(record);
|
2017-11-03 12:40:06 +03:00
|
|
|
}
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
let failedToApply = await this._applyRecords(backfilledRecordsToApply);
|
|
|
|
failedInBackfill.push(...failedToApply);
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : 13605dd3a43569a6d83dc2eb15a578a7bbd5c1ca
2017-11-01 21:09:57 +03:00
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
count.failed += failedToApply.length;
|
|
|
|
count.applied += backfilledRecordsToApply.length;
|
|
|
|
|
2018-01-10 03:22:33 +03:00
|
|
|
this.toFetch = Utils.setDeleteAll(this.toFetch, ids);
|
|
|
|
this.previousFailed = Utils.setAddAll(this.previousFailed, failedInBackfill);
|
2011-06-15 11:03:32 +04:00
|
|
|
|
2017-12-20 04:10:54 +03:00
|
|
|
if (lastSync < this.lastModified) {
|
|
|
|
lastSync = this.lastModified;
|
|
|
|
await this.setLastSync(lastSync);
|
2011-01-27 08:34:31 +03:00
|
|
|
}
|
|
|
|
}
|
2008-12-31 10:52:20 +03:00
|
|
|
|
2018-01-10 03:22:33 +03:00
|
|
|
count.newFailed = 0;
|
|
|
|
for (let item of this.previousFailed) {
|
|
|
|
if (!failedInPreviousSync.has(item)) {
|
|
|
|
++count.newFailed;
|
2015-09-16 04:18:04 +03:00
|
|
|
}
|
2018-01-10 03:22:33 +03:00
|
|
|
}
|
|
|
|
|
2012-03-07 02:27:56 +04:00
|
|
|
count.succeeded = Math.max(0, count.applied - count.failed);
|
2011-01-28 20:39:12 +03:00
|
|
|
this._log.info(["Records:",
|
|
|
|
count.applied, "applied,",
|
2012-03-07 02:27:56 +04:00
|
|
|
count.succeeded, "successfully,",
|
2011-01-28 20:39:12 +03:00
|
|
|
count.failed, "failed to apply,",
|
2011-05-28 04:32:13 +04:00
|
|
|
count.newFailed, "newly failed to apply,",
|
2011-01-28 20:39:12 +03:00
|
|
|
count.reconciled, "reconciled."].join(" "));
|
2011-06-27 17:24:33 +04:00
|
|
|
Observers.notify("weave:engine:sync:applied", count, this.name);
|
2008-12-05 11:39:54 +03:00
|
|
|
},
|
|
|
|
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
async _maybeReconcile(item) {
|
|
|
|
let key = this.service.collectionKeys.keyForCollection(this.name);
|
|
|
|
|
|
|
|
// Grab a later last modified if possible
|
|
|
|
if (this.lastModified == null || item.modified > this.lastModified) {
|
|
|
|
this.lastModified = item.modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await item.decrypt(key);
|
|
|
|
} catch (ex) {
|
|
|
|
if (!Utils.isHMACMismatch(ex)) {
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
let strategy = await this.handleHMACMismatch(item, true);
|
|
|
|
if (strategy == SyncEngine.kRecoveryStrategy.retry) {
|
|
|
|
// You only get one retry.
|
|
|
|
try {
|
|
|
|
// Try decrypting again, typically because we've got new keys.
|
|
|
|
this._log.info("Trying decrypt again...");
|
|
|
|
key = this.service.collectionKeys.keyForCollection(this.name);
|
|
|
|
await item.decrypt(key);
|
|
|
|
strategy = null;
|
|
|
|
} catch (ex) {
|
|
|
|
if (!Utils.isHMACMismatch(ex)) {
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
strategy = await this.handleHMACMismatch(item, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (strategy) {
|
|
|
|
case null:
|
|
|
|
// Retry succeeded! No further handling.
|
|
|
|
break;
|
|
|
|
case SyncEngine.kRecoveryStrategy.retry:
|
|
|
|
this._log.debug("Ignoring second retry suggestion.");
|
|
|
|
// Fall through to error case.
|
|
|
|
case SyncEngine.kRecoveryStrategy.error:
|
|
|
|
this._log.warn("Error decrypting record", ex);
|
|
|
|
return { shouldApply: false, error: ex };
|
|
|
|
case SyncEngine.kRecoveryStrategy.ignore:
|
|
|
|
this._log.debug("Ignoring record " + item.id +
|
|
|
|
" with bad HMAC: already handled.");
|
|
|
|
return { shouldApply: false, error: null };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
if (Async.isShutdownException(ex)) {
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
this._log.warn("Error decrypting record", ex);
|
|
|
|
return { shouldApply: false, error: ex };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._shouldDeleteRemotely(item)) {
|
|
|
|
this._log.trace("Deleting item from server without applying", item);
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._deleteId(item.id);
|
Bug 1368209 - Refactor `Engine::_processIncoming` into three stages. r=eoger,tcsc
* In the first stage, we fetch changed records, newest first, up to the
download limit. We keep track of the oldest record modified time we
see.
* Once we've fetched all records, we reconcile, noting records that
fail to decrypt or reconcile for the next sync. We then ask the store
to apply all remaining records. Previously, `applyIncomingBatchSize`
specified how many records to apply at a time. I removed this because
it added an extra layer of indirection that's no longer necessary,
now that download batching buffers all records in memory, and all
stores are async.
* In the second stage, we fetch IDs for all remaining records changed
between the last sync and the oldest modified time we saw in the
first stage. We *don't* set the download limit here, to ensure we
add *all* changed records to our backlog, and we use the `"oldest"`
sort order instead of `"index"`.
* In the third stage, we backfill as before. We don't want large deltas
to delay other engines from syncing, so we still only take IDs up to
the download limit from the backlog, and include failed IDs from the
previous sync. On subsequent syncs, we'll keep fetching from the
backlog until it's empty.
Other changes to note in this patch:
* `Collection::_rebuildURL` now allows callers to specify both `older`
and `newer`. According to :rfkelly, this is explicitly and
intentionally supported.
* Tests that exercise `applyIncomingBatchSize` are gone, since that's
no longer a thing.
* The test server now shuffles records if the sort order is
unspecified.
MozReview-Commit-ID: 4EXvNOa8mIo
--HG--
extra : rebase_source : f382f0a883c5aa1f6a4466fefe22ad1a88ab6d20
2017-11-01 21:09:57 +03:00
|
|
|
return { shouldApply: false, error: null };
|
|
|
|
}
|
|
|
|
|
|
|
|
let shouldApply;
|
|
|
|
try {
|
|
|
|
shouldApply = await this._reconcile(item);
|
|
|
|
} catch (ex) {
|
|
|
|
if (ex.code == Engine.prototype.eEngineAbortApplyIncoming) {
|
|
|
|
this._log.warn("Reconciliation failed: aborting incoming processing.");
|
|
|
|
throw ex.cause;
|
|
|
|
} else if (!Async.isShutdownException(ex)) {
|
|
|
|
this._log.warn("Failed to reconcile incoming record " + item.id, ex);
|
|
|
|
return { shouldApply: false, error: ex };
|
|
|
|
} else {
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!shouldApply) {
|
|
|
|
this._log.trace("Skipping reconciled incoming item " + item.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { shouldApply, error: null };
|
|
|
|
},
|
|
|
|
|
|
|
|
async _applyRecords(records) {
|
|
|
|
this._tracker.ignoreAll = true;
|
|
|
|
try {
|
|
|
|
let failedIDs = await this._store.applyIncomingBatch(records);
|
|
|
|
return failedIDs;
|
|
|
|
} catch (ex) {
|
|
|
|
// Catch any error that escapes from applyIncomingBatch. At present
|
|
|
|
// those will all be abort events.
|
|
|
|
this._log.warn("Got exception, aborting processIncoming", ex);
|
|
|
|
throw ex;
|
|
|
|
} finally {
|
|
|
|
this._tracker.ignoreAll = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-11-10 01:23:54 +03:00
|
|
|
// Indicates whether an incoming item should be deleted from the server at
|
|
|
|
// the end of the sync. Engines can override this method to clean up records
|
|
|
|
// that shouldn't be on the server.
|
|
|
|
_shouldDeleteRemotely(remoteItem) {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2009-07-28 21:06:02 +04:00
|
|
|
/**
|
2009-09-01 04:27:30 +04:00
|
|
|
* Find a GUID of an item that is a duplicate of the incoming item but happens
|
|
|
|
* to have a different GUID
|
2009-07-28 21:06:02 +04:00
|
|
|
*
|
2009-09-01 04:27:30 +04:00
|
|
|
* @return GUID of the similar item; falsy otherwise
|
2009-07-28 21:06:02 +04:00
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async _findDupe(item) {
|
2009-09-01 04:27:30 +04:00
|
|
|
// By default, assume there's no dupe items for the engine
|
2009-07-28 21:06:02 +04:00
|
|
|
},
|
|
|
|
|
2017-03-23 23:32:40 +03:00
|
|
|
/**
|
|
|
|
* Called before a remote record is discarded due to failed reconciliation.
|
2017-04-29 00:08:11 +03:00
|
|
|
* Used by bookmark sync to merge folder child orders.
|
2017-03-23 23:32:40 +03:00
|
|
|
*/
|
2017-04-29 00:08:11 +03:00
|
|
|
beforeRecordDiscard(localRecord, remoteRecord, remoteIsNewer) {
|
2017-03-23 23:32:40 +03:00
|
|
|
},
|
|
|
|
|
2016-10-05 21:04:50 +03:00
|
|
|
// Called when the server has a record marked as deleted, but locally we've
|
|
|
|
// changed it more recently than the deletion. If we return false, the
|
|
|
|
// record will be deleted locally. If we return true, we'll reupload the
|
|
|
|
// record to the server -- any extra work that's needed as part of this
|
|
|
|
// process should be done at this point (such as mark the record's parent
|
|
|
|
// for reuploading in the case of bookmarks).
|
2017-06-06 01:50:07 +03:00
|
|
|
async _shouldReviveRemotelyDeletedRecord(remoteItem) {
|
2016-10-05 21:04:50 +03:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
async _deleteId(id) {
|
|
|
|
await this._tracker.removeChangedID(id);
|
2016-11-18 02:39:15 +03:00
|
|
|
this._noteDeletedId(id);
|
|
|
|
},
|
2009-09-01 04:51:26 +04:00
|
|
|
|
2016-11-18 02:39:15 +03:00
|
|
|
// Marks an ID for deletion at the end of the sync.
|
|
|
|
_noteDeletedId(id) {
|
2009-09-01 04:51:26 +04:00
|
|
|
if (this._delete.ids == null)
|
|
|
|
this._delete.ids = [id];
|
|
|
|
else
|
|
|
|
this._delete.ids.push(id);
|
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _switchItemToDupe(localDupeGUID, incomingItem) {
|
2016-07-05 09:02:19 +03:00
|
|
|
// The local, duplicate ID is always deleted on the server.
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._deleteId(localDupeGUID);
|
2016-07-05 09:02:19 +03:00
|
|
|
|
|
|
|
// We unconditionally change the item's ID in case the engine knows of
|
|
|
|
// an item but doesn't expose it through itemExists. If the API
|
|
|
|
// contract were stronger, this could be changed.
|
|
|
|
this._log.debug("Switching local ID to incoming: " + localDupeGUID + " -> " +
|
|
|
|
incomingItem.id);
|
2017-06-06 01:50:07 +03:00
|
|
|
return this._store.changeItemID(localDupeGUID, incomingItem.id);
|
2016-07-05 09:02:19 +03:00
|
|
|
},
|
|
|
|
|
2012-01-26 01:32:05 +04:00
|
|
|
/**
|
|
|
|
* Reconcile incoming record with local state.
|
|
|
|
*
|
|
|
|
* This function essentially determines whether to apply an incoming record.
|
|
|
|
*
|
|
|
|
* @param item
|
|
|
|
* Record from server to be tested for application.
|
|
|
|
* @return boolean
|
|
|
|
* Truthy if incoming record should be applied. False if not.
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async _reconcile(item) {
|
2013-08-26 22:55:58 +04:00
|
|
|
if (this._log.level <= Log.Level.Trace) {
|
2012-01-26 01:32:05 +04:00
|
|
|
this._log.trace("Incoming: " + item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We start reconciling by collecting a bunch of state. We do this here
|
|
|
|
// because some state may change during the course of this function and we
|
|
|
|
// need to operate on the original values.
|
2017-06-06 01:50:07 +03:00
|
|
|
let existsLocally = await this._store.itemExists(item.id);
|
2016-09-06 21:39:13 +03:00
|
|
|
let locallyModified = this._modified.has(item.id);
|
2012-01-26 01:32:05 +04:00
|
|
|
|
|
|
|
// TODO Handle clock drift better. Tracked in bug 721181.
|
2017-11-02 21:30:59 +03:00
|
|
|
let remoteAge = Resource.serverTime - item.modified;
|
2012-01-26 01:32:05 +04:00
|
|
|
let localAge = locallyModified ?
|
2016-09-06 21:39:13 +03:00
|
|
|
(Date.now() / 1000 - this._modified.getModifiedTimestamp(item.id)) : null;
|
2012-01-26 01:32:05 +04:00
|
|
|
let remoteIsNewer = remoteAge < localAge;
|
|
|
|
|
|
|
|
this._log.trace("Reconciling " + item.id + ". exists=" +
|
|
|
|
existsLocally + "; modified=" + locallyModified +
|
|
|
|
"; local age=" + localAge + "; incoming age=" +
|
|
|
|
remoteAge);
|
|
|
|
|
|
|
|
// We handle deletions first so subsequent logic doesn't have to check
|
|
|
|
// deleted flags.
|
|
|
|
if (item.deleted) {
|
|
|
|
// If the item doesn't exist locally, there is nothing for us to do. We
|
|
|
|
// can't check for duplicates because the incoming record has no data
|
|
|
|
// which can be used for duplicate detection.
|
|
|
|
if (!existsLocally) {
|
|
|
|
this._log.trace("Ignoring incoming item because it was deleted and " +
|
|
|
|
"the item does not exist locally.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We decide whether to process the deletion by comparing the record
|
|
|
|
// ages. If the item is not modified locally, the remote side wins and
|
|
|
|
// the deletion is processed. If it is modified locally, we take the
|
|
|
|
// newer record.
|
|
|
|
if (!locallyModified) {
|
|
|
|
this._log.trace("Applying incoming delete because the local item " +
|
|
|
|
"exists and isn't modified.");
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-05 21:04:50 +03:00
|
|
|
this._log.trace("Incoming record is deleted but we had local changes.");
|
|
|
|
|
|
|
|
if (remoteIsNewer) {
|
|
|
|
this._log.trace("Remote record is newer -- deleting local record.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// If the local record is newer, we defer to individual engines for
|
|
|
|
// how to handle this. By default, we revive the record.
|
2017-06-06 01:50:07 +03:00
|
|
|
let willRevive = await this._shouldReviveRemotelyDeletedRecord(item);
|
2016-10-05 21:04:50 +03:00
|
|
|
this._log.trace("Local record is newer -- reviving? " + willRevive);
|
2010-04-02 02:29:16 +04:00
|
|
|
|
2016-10-05 21:04:50 +03:00
|
|
|
return !willRevive;
|
2009-09-09 10:33:15 +04:00
|
|
|
}
|
2012-01-26 01:32:05 +04:00
|
|
|
|
|
|
|
// At this point the incoming record is not for a deletion and must have
|
|
|
|
// data. If the incoming record does not exist locally, we check for a local
|
|
|
|
// duplicate existing under a different ID. The default implementation of
|
|
|
|
// _findDupe() is empty, so engines have to opt in to this functionality.
|
|
|
|
//
|
|
|
|
// If we find a duplicate, we change the local ID to the incoming ID and we
|
|
|
|
// refresh the metadata collected above. See bug 710448 for the history
|
|
|
|
// of this logic.
|
|
|
|
if (!existsLocally) {
|
2017-06-06 01:50:07 +03:00
|
|
|
let localDupeGUID = await this._findDupe(item);
|
2016-07-05 09:02:19 +03:00
|
|
|
if (localDupeGUID) {
|
|
|
|
this._log.trace("Local item " + localDupeGUID + " is a duplicate for " +
|
2012-01-26 01:32:05 +04:00
|
|
|
"incoming item " + item.id);
|
|
|
|
|
|
|
|
// The current API contract does not mandate that the ID returned by
|
|
|
|
// _findDupe() actually exists. Therefore, we have to perform this
|
|
|
|
// check.
|
2017-06-06 01:50:07 +03:00
|
|
|
existsLocally = await this._store.itemExists(localDupeGUID);
|
2012-01-26 01:32:05 +04:00
|
|
|
|
|
|
|
// If the local item was modified, we carry its metadata forward so
|
|
|
|
// appropriate reconciling can be performed.
|
2016-07-05 09:02:19 +03:00
|
|
|
if (this._modified.has(localDupeGUID)) {
|
2012-01-26 01:32:05 +04:00
|
|
|
locallyModified = true;
|
2016-07-05 09:02:19 +03:00
|
|
|
localAge = this._tracker._now() - this._modified.getModifiedTimestamp(localDupeGUID);
|
2012-01-26 01:32:05 +04:00
|
|
|
remoteIsNewer = remoteAge < localAge;
|
|
|
|
|
2017-02-06 21:19:27 +03:00
|
|
|
this._modified.changeID(localDupeGUID, item.id);
|
2012-01-26 01:32:05 +04:00
|
|
|
} else {
|
|
|
|
locallyModified = false;
|
|
|
|
localAge = null;
|
|
|
|
}
|
|
|
|
|
2016-07-05 09:02:19 +03:00
|
|
|
// Tell the engine to do whatever it needs to switch the items.
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._switchItemToDupe(localDupeGUID, item);
|
2016-07-05 09:02:19 +03:00
|
|
|
|
2012-01-26 01:32:05 +04:00
|
|
|
this._log.debug("Local item after duplication: age=" + localAge +
|
|
|
|
"; modified=" + locallyModified + "; exists=" +
|
|
|
|
existsLocally);
|
|
|
|
} else {
|
|
|
|
this._log.trace("No duplicate found for incoming item: " + item.id);
|
|
|
|
}
|
2009-09-09 10:33:15 +04:00
|
|
|
}
|
|
|
|
|
2012-01-26 01:32:05 +04:00
|
|
|
// At this point we've performed duplicate detection. But, nothing here
|
|
|
|
// should depend on duplicate detection as the above should have updated
|
|
|
|
// state seamlessly.
|
|
|
|
|
|
|
|
if (!existsLocally) {
|
|
|
|
// If the item doesn't exist locally and we have no local modifications
|
|
|
|
// to the item (implying that it was not deleted), always apply the remote
|
|
|
|
// item.
|
|
|
|
if (!locallyModified) {
|
|
|
|
this._log.trace("Applying incoming because local item does not exist " +
|
|
|
|
"and was not deleted.");
|
|
|
|
return true;
|
|
|
|
}
|
2009-08-26 03:15:05 +04:00
|
|
|
|
2012-01-26 01:32:05 +04:00
|
|
|
// If the item was modified locally but isn't present, it must have
|
|
|
|
// been deleted. If the incoming record is younger, we restore from
|
|
|
|
// that record.
|
|
|
|
if (remoteIsNewer) {
|
|
|
|
this._log.trace("Applying incoming because local item was deleted " +
|
|
|
|
"before the incoming item was changed.");
|
2016-09-06 21:39:13 +03:00
|
|
|
this._modified.delete(item.id);
|
2012-01-26 01:32:05 +04:00
|
|
|
return true;
|
2010-04-02 02:54:53 +04:00
|
|
|
}
|
|
|
|
|
2012-01-26 01:32:05 +04:00
|
|
|
this._log.trace("Ignoring incoming item because the local item's " +
|
|
|
|
"deletion is newer.");
|
|
|
|
return false;
|
2008-12-30 10:28:17 +03:00
|
|
|
}
|
|
|
|
|
2012-01-26 01:32:05 +04:00
|
|
|
// If the remote and local records are the same, there is nothing to be
|
|
|
|
// done, so we don't do anything. In the ideal world, this logic wouldn't
|
|
|
|
// be here and the engine would take a record and apply it. The reason we
|
|
|
|
// want to defer this logic is because it would avoid a redundant and
|
|
|
|
// possibly expensive dip into the storage layer to query item state.
|
|
|
|
// This should get addressed in the async rewrite, so we ignore it for now.
|
2017-06-06 01:50:07 +03:00
|
|
|
let localRecord = await this._createRecord(item.id);
|
2012-01-26 01:32:05 +04:00
|
|
|
let recordsEqual = Utils.deepEquals(item.cleartext,
|
|
|
|
localRecord.cleartext);
|
|
|
|
|
|
|
|
// If the records are the same, we don't need to do anything. This does
|
|
|
|
// potentially throw away a local modification time. But, if the records
|
|
|
|
// are the same, does it matter?
|
|
|
|
if (recordsEqual) {
|
|
|
|
this._log.trace("Ignoring incoming item because the local item is " +
|
|
|
|
"identical.");
|
|
|
|
|
2016-09-06 21:39:13 +03:00
|
|
|
this._modified.delete(item.id);
|
2012-01-26 01:32:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
2008-11-20 03:20:25 +03:00
|
|
|
|
2012-01-26 01:32:05 +04:00
|
|
|
// At this point the records are different.
|
2009-01-09 08:33:37 +03:00
|
|
|
|
2012-01-26 01:32:05 +04:00
|
|
|
// If we have no local modifications, always take the server record.
|
|
|
|
if (!locallyModified) {
|
|
|
|
this._log.trace("Applying incoming record because no local conflicts.");
|
2011-12-14 03:46:54 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-26 01:32:05 +04:00
|
|
|
// At this point, records are different and the local record is modified.
|
|
|
|
// We resolve conflicts by record age, where the newest one wins. This does
|
|
|
|
// result in data loss and should be handled by giving the engine an
|
|
|
|
// opportunity to merge the records. Bug 720592 tracks this feature.
|
|
|
|
this._log.warn("DATA LOSS: Both local and remote changes to record: " +
|
|
|
|
item.id);
|
2017-03-23 23:32:40 +03:00
|
|
|
if (!remoteIsNewer) {
|
2017-04-29 00:08:11 +03:00
|
|
|
this.beforeRecordDiscard(localRecord, item, remoteIsNewer);
|
2017-03-23 23:32:40 +03:00
|
|
|
}
|
2012-01-26 01:32:05 +04:00
|
|
|
return remoteIsNewer;
|
2008-12-05 11:39:54 +03:00
|
|
|
},
|
2008-11-24 19:04:14 +03:00
|
|
|
|
2013-12-19 09:42:17 +04:00
|
|
|
// Upload outgoing records.
|
2017-06-06 01:50:07 +03:00
|
|
|
async _uploadOutgoing() {
|
2011-03-03 02:27:16 +03:00
|
|
|
this._log.trace("Uploading local changes to server.");
|
2012-01-26 01:32:05 +04:00
|
|
|
|
2017-01-17 22:45:08 +03:00
|
|
|
// collection we'll upload
|
|
|
|
let up = new Collection(this.engineURL, null, this.service);
|
2017-07-18 21:18:04 +03:00
|
|
|
let modifiedIDs = new Set(this._modified.ids());
|
|
|
|
for (let id of this._needWeakUpload.keys()) {
|
|
|
|
modifiedIDs.add(id);
|
|
|
|
}
|
2017-01-17 22:45:08 +03:00
|
|
|
let counts = { failed: 0, sent: 0 };
|
2017-07-18 21:18:04 +03:00
|
|
|
if (modifiedIDs.size) {
|
|
|
|
this._log.trace("Preparing " + modifiedIDs.size +
|
2010-11-24 08:21:31 +03:00
|
|
|
" outgoing records");
|
2009-09-11 09:57:36 +04:00
|
|
|
|
2017-07-18 21:18:04 +03:00
|
|
|
counts.sent = modifiedIDs.size;
|
2016-08-24 23:02:14 +03:00
|
|
|
|
|
|
|
let failed = [];
|
|
|
|
let successful = [];
|
2017-12-20 04:10:54 +03:00
|
|
|
let lastSync = await this.getLastSync();
|
2017-06-06 01:50:07 +03:00
|
|
|
let handleResponse = async (resp, batchOngoing = false) => {
|
2016-08-24 23:02:14 +03:00
|
|
|
// Note: We don't want to update this.lastSync, or this._modified until
|
|
|
|
// the batch is complete, however we want to remember success/failure
|
|
|
|
// indicators for when that happens.
|
2009-09-16 05:38:52 +04:00
|
|
|
if (!resp.success) {
|
|
|
|
this._log.debug("Uploading records failed: " + resp);
|
2016-08-24 23:02:14 +03:00
|
|
|
resp.failureCode = resp.status == 412 ? ENGINE_BATCH_INTERRUPTED : ENGINE_UPLOAD_FAIL;
|
2009-08-27 02:32:46 +04:00
|
|
|
throw resp;
|
2009-09-16 05:38:52 +04:00
|
|
|
}
|
2009-08-26 05:04:46 +04:00
|
|
|
|
2010-11-11 22:00:35 +03:00
|
|
|
// Update server timestamp from the upload.
|
2016-08-24 23:02:14 +03:00
|
|
|
failed = failed.concat(Object.keys(resp.obj.failed));
|
|
|
|
successful = successful.concat(resp.obj.success);
|
|
|
|
|
|
|
|
if (batchOngoing) {
|
|
|
|
// Nothing to do yet
|
|
|
|
return;
|
|
|
|
}
|
2017-12-20 04:10:54 +03:00
|
|
|
let serverModifiedTime = parseFloat(resp.headers["x-weave-timestamp"]);
|
|
|
|
|
2016-08-24 23:02:14 +03:00
|
|
|
if (failed.length && this._log.level <= Log.Level.Debug) {
|
2010-07-31 15:28:00 +04:00
|
|
|
this._log.debug("Records that will be uploaded again because "
|
|
|
|
+ "the server couldn't store them: "
|
2016-08-24 23:02:14 +03:00
|
|
|
+ failed.join(", "));
|
|
|
|
}
|
|
|
|
|
|
|
|
counts.failed += failed.length;
|
2010-07-31 15:28:00 +04:00
|
|
|
|
2016-08-24 23:02:14 +03:00
|
|
|
for (let id of successful) {
|
2016-09-06 21:39:13 +03:00
|
|
|
this._modified.delete(id);
|
2010-11-24 08:21:31 +03:00
|
|
|
}
|
2016-08-02 20:09:30 +03:00
|
|
|
|
2017-12-20 04:10:54 +03:00
|
|
|
await this._onRecordsWritten(successful, failed, serverModifiedTime);
|
|
|
|
|
|
|
|
// Advance lastSync since we've finished the batch.
|
|
|
|
if (serverModifiedTime > lastSync) {
|
|
|
|
lastSync = serverModifiedTime;
|
|
|
|
await this.setLastSync(lastSync);
|
|
|
|
}
|
2010-11-24 08:21:31 +03:00
|
|
|
|
2016-08-24 23:02:14 +03:00
|
|
|
// clear for next batch
|
|
|
|
failed.length = 0;
|
|
|
|
successful.length = 0;
|
|
|
|
};
|
|
|
|
|
2017-12-20 04:10:54 +03:00
|
|
|
let postQueue = up.newPostQueue(this._log, lastSync, handleResponse);
|
2008-12-23 22:30:31 +03:00
|
|
|
|
2015-10-18 21:52:58 +03:00
|
|
|
for (let id of modifiedIDs) {
|
2016-01-21 04:30:25 +03:00
|
|
|
let out;
|
|
|
|
let ok = false;
|
2010-01-06 20:59:05 +03:00
|
|
|
try {
|
2017-07-18 21:18:04 +03:00
|
|
|
let { forceTombstone = false } = this._needWeakUpload.get(id) || {};
|
|
|
|
if (forceTombstone) {
|
|
|
|
out = await this._createTombstone(id);
|
|
|
|
} else {
|
|
|
|
out = await this._createRecord(id);
|
|
|
|
}
|
2013-08-26 22:55:58 +04:00
|
|
|
if (this._log.level <= Log.Level.Trace)
|
2010-01-06 20:59:05 +03:00
|
|
|
this._log.trace("Outgoing: " + out);
|
2017-09-16 05:21:31 +03:00
|
|
|
await out.encrypt(this.service.collectionKeys.keyForCollection(this.name));
|
2016-01-21 04:30:25 +03:00
|
|
|
ok = true;
|
2016-01-26 18:13:31 +03:00
|
|
|
} catch (ex) {
|
2017-01-25 01:05:12 +03:00
|
|
|
this._log.warn("Error creating record", ex);
|
|
|
|
++counts.failed;
|
|
|
|
if (Async.isShutdownException(ex) || !this.allowSkippedRecord) {
|
2017-07-18 21:18:04 +03:00
|
|
|
if (!this.allowSkippedRecord) {
|
|
|
|
// Don't bother for shutdown errors
|
|
|
|
Observers.notify("weave:engine:sync:uploaded", counts, this.name);
|
|
|
|
}
|
2016-01-26 18:13:31 +03:00
|
|
|
throw ex;
|
|
|
|
}
|
2010-01-06 20:59:05 +03:00
|
|
|
}
|
2016-01-21 04:30:25 +03:00
|
|
|
if (ok) {
|
2017-06-06 01:50:07 +03:00
|
|
|
let { enqueued, error } = await postQueue.enqueue(out);
|
2016-08-24 23:02:14 +03:00
|
|
|
if (!enqueued) {
|
|
|
|
++counts.failed;
|
|
|
|
if (!this.allowSkippedRecord) {
|
2017-07-18 21:18:04 +03:00
|
|
|
Observers.notify("weave:engine:sync:uploaded", counts, this.name);
|
2017-09-14 04:02:41 +03:00
|
|
|
this._log.warn(`Failed to enqueue record "${id}" (aborting)`, error);
|
2016-08-24 23:02:14 +03:00
|
|
|
throw error;
|
|
|
|
}
|
2016-11-21 23:37:35 +03:00
|
|
|
this._modified.delete(id);
|
|
|
|
this._log.warn(`Failed to enqueue record "${id}" (skipping)`, error);
|
2016-08-24 23:02:14 +03:00
|
|
|
}
|
2016-01-21 04:30:25 +03:00
|
|
|
}
|
2017-06-06 01:50:07 +03:00
|
|
|
await Async.promiseYield();
|
2008-12-23 22:30:31 +03:00
|
|
|
}
|
2017-06-06 01:50:07 +03:00
|
|
|
await postQueue.flush(true);
|
2017-01-17 22:45:08 +03:00
|
|
|
}
|
2017-07-18 21:18:04 +03:00
|
|
|
this._needWeakUpload.clear();
|
2017-01-17 22:45:08 +03:00
|
|
|
|
|
|
|
if (counts.sent || counts.failed) {
|
2016-07-11 20:37:23 +03:00
|
|
|
Observers.notify("weave:engine:sync:uploaded", counts, this.name);
|
2008-11-08 13:00:33 +03:00
|
|
|
}
|
2008-07-26 04:02:43 +04:00
|
|
|
},
|
|
|
|
|
2017-12-20 04:10:54 +03:00
|
|
|
async _onRecordsWritten(succeeded, failed, serverModifiedTime) {
|
2016-08-02 20:09:30 +03:00
|
|
|
// Implement this method to take specific actions against successfully
|
|
|
|
// uploaded records and failed records.
|
|
|
|
},
|
|
|
|
|
2008-12-05 11:39:54 +03:00
|
|
|
// Any cleanup necessary.
|
|
|
|
// Save the current snapshot so as to calculate changes at next sync
|
2017-06-06 01:50:07 +03:00
|
|
|
async _syncFinish() {
|
2009-07-23 08:40:18 +04:00
|
|
|
this._log.trace("Finishing up sync");
|
2009-09-01 04:30:34 +04:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
let doDelete = async (key, val) => {
|
2012-09-15 03:02:32 +04:00
|
|
|
let coll = new Collection(this.engineURL, this._recordObj, this.service);
|
2009-10-13 05:11:31 +04:00
|
|
|
coll[key] = val;
|
2017-06-06 01:50:07 +03:00
|
|
|
await coll.delete();
|
|
|
|
};
|
2009-10-13 05:11:31 +04:00
|
|
|
|
2016-08-09 05:40:23 +03:00
|
|
|
for (let [key, val] of Object.entries(this._delete)) {
|
2009-09-01 04:30:34 +04:00
|
|
|
// Remove the key for future uses
|
|
|
|
delete this._delete[key];
|
|
|
|
|
2017-05-31 11:42:45 +03:00
|
|
|
this._log.trace("doing post-sync deletions", {key, val});
|
2009-10-13 05:11:31 +04:00
|
|
|
// Send a simple delete for the property
|
|
|
|
if (key != "ids" || val.length <= 100)
|
2017-06-06 01:50:07 +03:00
|
|
|
await doDelete(key, val);
|
2009-10-13 05:11:31 +04:00
|
|
|
else {
|
|
|
|
// For many ids, split into chunks of at most 100
|
|
|
|
while (val.length > 0) {
|
2017-06-06 01:50:07 +03:00
|
|
|
await doDelete(key, val.slice(0, 100));
|
2009-10-13 05:11:31 +04:00
|
|
|
val = val.slice(100);
|
|
|
|
}
|
|
|
|
}
|
2009-09-01 04:30:34 +04:00
|
|
|
}
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._tracker.asyncObserver.promiseObserversComplete();
|
2008-11-07 06:18:07 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _syncCleanup() {
|
2017-07-18 21:18:04 +03:00
|
|
|
this._needWeakUpload.clear();
|
2013-12-19 09:42:17 +04:00
|
|
|
if (!this._modified) {
|
2010-11-11 22:00:35 +03:00
|
|
|
return;
|
2013-12-19 09:42:17 +04:00
|
|
|
}
|
2010-11-11 22:00:35 +03:00
|
|
|
|
2016-11-18 02:39:15 +03:00
|
|
|
try {
|
|
|
|
// Mark failed WBOs as changed again so they are reuploaded next time.
|
2017-06-06 01:50:07 +03:00
|
|
|
await this.trackRemainingChanges();
|
2016-11-18 02:39:15 +03:00
|
|
|
} finally {
|
|
|
|
this._modified.clear();
|
|
|
|
}
|
2010-11-11 22:00:35 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _sync() {
|
2008-12-05 11:55:19 +03:00
|
|
|
try {
|
2017-10-27 20:37:49 +03:00
|
|
|
Async.checkAppReady();
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._syncStartup();
|
2017-10-27 20:37:49 +03:00
|
|
|
Async.checkAppReady();
|
2009-02-22 11:04:58 +03:00
|
|
|
Observers.notify("weave:engine:sync:status", "process-incoming");
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._processIncoming();
|
2017-10-27 20:37:49 +03:00
|
|
|
Async.checkAppReady();
|
2009-02-22 11:04:58 +03:00
|
|
|
Observers.notify("weave:engine:sync:status", "upload-outgoing");
|
2017-08-08 05:02:28 +03:00
|
|
|
try {
|
|
|
|
await this._uploadOutgoing();
|
2017-10-27 20:37:49 +03:00
|
|
|
Async.checkAppReady();
|
2017-08-08 05:02:28 +03:00
|
|
|
await this._syncFinish();
|
|
|
|
} catch (ex) {
|
|
|
|
if (!ex.status || ex.status != 412) {
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
// a 412 posting just means another client raced - but we don't want
|
|
|
|
// to treat that as a sync error - the next sync is almost certain
|
|
|
|
// to work.
|
2017-10-15 21:50:30 +03:00
|
|
|
this._log.warn("412 error during sync - will retry.");
|
2017-08-08 05:02:28 +03:00
|
|
|
}
|
2010-11-24 08:21:31 +03:00
|
|
|
} finally {
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._syncCleanup();
|
2008-12-05 11:55:19 +03:00
|
|
|
}
|
2008-12-05 11:39:54 +03:00
|
|
|
},
|
2008-08-09 01:42:57 +04:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async canDecrypt() {
|
2009-10-13 03:22:54 +04:00
|
|
|
// Report failure even if there's nothing to decrypt
|
|
|
|
let canDecrypt = false;
|
|
|
|
|
|
|
|
// Fetch the most recently uploaded record and try to decrypt it
|
2012-09-15 03:02:32 +04:00
|
|
|
let test = new Collection(this.engineURL, this._recordObj, this.service);
|
2009-10-13 03:22:54 +04:00
|
|
|
test.limit = 1;
|
|
|
|
test.sort = "newest";
|
|
|
|
test.full = true;
|
2012-09-15 03:02:33 +04:00
|
|
|
|
|
|
|
let key = this.service.collectionKeys.keyForCollection(this.name);
|
2009-10-13 03:22:54 +04:00
|
|
|
|
|
|
|
// Any failure fetching/decrypting will just result in false
|
|
|
|
try {
|
|
|
|
this._log.trace("Trying to decrypt a record from the server..");
|
2017-06-06 01:50:07 +03:00
|
|
|
let json = (await test.get()).obj[0];
|
2017-05-30 20:23:57 +03:00
|
|
|
let record = new this._recordObj();
|
|
|
|
record.deserialize(json);
|
2017-09-16 05:21:31 +03:00
|
|
|
await record.decrypt(key);
|
2017-05-30 20:23:57 +03:00
|
|
|
canDecrypt = true;
|
2016-01-26 18:13:31 +03:00
|
|
|
} catch (ex) {
|
|
|
|
if (Async.isShutdownException(ex)) {
|
|
|
|
throw ex;
|
|
|
|
}
|
2016-01-07 09:09:00 +03:00
|
|
|
this._log.debug("Failed test decrypt", ex);
|
2009-10-13 03:22:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return canDecrypt;
|
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _resetClient() {
|
2009-02-27 09:36:14 +03:00
|
|
|
this.resetLastSync();
|
2018-01-10 03:22:33 +03:00
|
|
|
this.previousFailed = new SerializableSet();
|
|
|
|
this.toFetch = new SerializableSet();
|
2017-07-18 21:18:04 +03:00
|
|
|
this._needWeakUpload.clear();
|
2010-09-07 20:44:01 +04:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async wipeServer() {
|
|
|
|
let response = await this.service.resource(this.engineURL).delete();
|
2011-10-28 09:25:00 +04:00
|
|
|
if (response.status != 200 && response.status != 404) {
|
|
|
|
throw response;
|
|
|
|
}
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._resetClient();
|
2010-12-10 10:06:44 +03:00
|
|
|
},
|
2011-03-21 02:10:40 +03:00
|
|
|
|
2017-04-11 16:40:53 +03:00
|
|
|
async removeClientData() {
|
2011-04-09 01:51:55 +04:00
|
|
|
// Implement this method in engines that store client specific data
|
|
|
|
// on the server.
|
|
|
|
},
|
|
|
|
|
2011-03-21 02:10:40 +03:00
|
|
|
/*
|
|
|
|
* Decide on (and partially effect) an error-handling strategy.
|
|
|
|
*
|
|
|
|
* Asks the Service to respond to an HMAC error, which might result in keys
|
|
|
|
* being downloaded. That call returns true if an action which might allow a
|
|
|
|
* retry to occur.
|
|
|
|
*
|
|
|
|
* If `mayRetry` is truthy, and the Service suggests a retry,
|
|
|
|
* handleHMACMismatch returns kRecoveryStrategy.retry. Otherwise, it returns
|
|
|
|
* kRecoveryStrategy.error.
|
|
|
|
*
|
|
|
|
* Subclasses of SyncEngine can override this method to allow for different
|
|
|
|
* behavior -- e.g., to delete and ignore erroneous entries.
|
|
|
|
*
|
|
|
|
* All return values will be part of the kRecoveryStrategy enumeration.
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async handleHMACMismatch(item, mayRetry) {
|
2011-03-21 02:10:40 +03:00
|
|
|
// By default we either try again, or bail out noisily.
|
2017-06-06 01:50:07 +03:00
|
|
|
return ((await this.service.handleHMACEvent()) && mayRetry) ?
|
2011-03-21 02:10:40 +03:00
|
|
|
SyncEngine.kRecoveryStrategy.retry :
|
|
|
|
SyncEngine.kRecoveryStrategy.error;
|
2016-09-06 21:39:13 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a changeset containing all items in the store. The default
|
|
|
|
* implementation returns a changeset with timestamps from long ago, to
|
|
|
|
* ensure we always use the remote version if one exists.
|
|
|
|
*
|
|
|
|
* This function is only called for the first sync. Subsequent syncs call
|
|
|
|
* `pullNewChanges`.
|
|
|
|
*
|
|
|
|
* @return A `Changeset` object.
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async pullAllChanges() {
|
2017-02-06 21:19:27 +03:00
|
|
|
let changes = {};
|
2017-06-06 01:50:07 +03:00
|
|
|
let ids = await this._store.getAllIDs();
|
|
|
|
for (let id in ids) {
|
2017-02-06 21:19:27 +03:00
|
|
|
changes[id] = 0;
|
2016-09-06 21:39:13 +03:00
|
|
|
}
|
2017-02-06 21:19:27 +03:00
|
|
|
return changes;
|
2016-09-06 21:39:13 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a changeset containing entries for all currently tracked items.
|
|
|
|
* The default implementation returns a changeset with timestamps indicating
|
|
|
|
* when the item was added to the tracker.
|
|
|
|
*
|
|
|
|
* @return A `Changeset` object.
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async pullNewChanges() {
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._tracker.asyncObserver.promiseObserversComplete();
|
2017-02-06 21:19:27 +03:00
|
|
|
return this.getChangedIDs();
|
2016-09-06 21:39:13 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds all remaining changeset entries back to the tracker, typically for
|
|
|
|
* items that failed to upload. This method is called at the end of each sync.
|
|
|
|
*
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async trackRemainingChanges() {
|
2016-09-06 21:39:13 +03:00
|
|
|
for (let [id, change] of this._modified.entries()) {
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._tracker.addChangedID(id, change);
|
2016-09-06 21:39:13 +03:00
|
|
|
}
|
|
|
|
},
|
2017-11-01 21:10:47 +03:00
|
|
|
|
|
|
|
async finalize() {
|
|
|
|
await super.finalize();
|
|
|
|
await this._toFetchStorage.finalize();
|
|
|
|
await this._previousFailedStorage.finalize();
|
|
|
|
},
|
2008-07-25 12:06:23 +04:00
|
|
|
};
|
2016-09-06 21:39:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A changeset is created for each sync in `Engine::get{Changed, All}IDs`,
|
|
|
|
* and stores opaque change data for tracked IDs. The default implementation
|
|
|
|
* only records timestamps, though engines can extend this to store additional
|
|
|
|
* data for each entry.
|
|
|
|
*/
|
|
|
|
class Changeset {
|
2017-02-06 21:19:27 +03:00
|
|
|
// Creates an empty changeset.
|
|
|
|
constructor() {
|
|
|
|
this.changes = {};
|
2016-09-06 21:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the last modified time, in seconds, for an entry in the changeset.
|
|
|
|
// `id` is guaranteed to be in the set.
|
|
|
|
getModifiedTimestamp(id) {
|
|
|
|
return this.changes[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a change for a tracked ID to the changeset.
|
|
|
|
set(id, change) {
|
|
|
|
this.changes[id] = change;
|
|
|
|
}
|
|
|
|
|
2017-02-06 21:19:27 +03:00
|
|
|
// Adds multiple entries to the changeset, preserving existing entries.
|
2016-11-18 02:04:19 +03:00
|
|
|
insert(changes) {
|
|
|
|
Object.assign(this.changes, changes);
|
|
|
|
}
|
|
|
|
|
2017-02-06 21:19:27 +03:00
|
|
|
// Overwrites the existing set of tracked changes with new entries.
|
|
|
|
replace(changes) {
|
|
|
|
this.changes = changes;
|
|
|
|
}
|
|
|
|
|
2016-09-06 21:39:13 +03:00
|
|
|
// Indicates whether an entry is in the changeset.
|
|
|
|
has(id) {
|
|
|
|
return id in this.changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes an entry from the changeset. Used to clean up entries for
|
|
|
|
// reconciled and successfully uploaded records.
|
|
|
|
delete(id) {
|
|
|
|
delete this.changes[id];
|
|
|
|
}
|
|
|
|
|
2017-02-06 21:19:27 +03:00
|
|
|
// Changes the ID of an entry in the changeset. Used when reconciling
|
|
|
|
// duplicates that have local changes.
|
|
|
|
changeID(oldID, newID) {
|
2016-09-06 21:39:13 +03:00
|
|
|
this.changes[newID] = this.changes[oldID];
|
|
|
|
delete this.changes[oldID];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an array of all tracked IDs in this changeset.
|
|
|
|
ids() {
|
|
|
|
return Object.keys(this.changes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an array of `[id, change]` tuples. Used to repopulate the tracker
|
|
|
|
// with entries for failed uploads at the end of a sync.
|
|
|
|
entries() {
|
|
|
|
return Object.entries(this.changes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the number of entries in this changeset.
|
|
|
|
count() {
|
|
|
|
return this.ids().length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clears the changeset.
|
|
|
|
clear() {
|
|
|
|
this.changes = {};
|
|
|
|
}
|
|
|
|
}
|