| 1 |
# Low-level sync-1.5 helper component |
| 2 |
|
| 3 |
This component contains utility code to be shared between different |
| 4 |
data stores that want to sync against a Firefox Sync v1.5 sync server. |
| 5 |
It handles things like encrypting/decrypting records, obtaining and |
| 6 |
using storage node auth tokens, and so-on. |
| 7 |
|
| 8 |
There are 2 key concepts to understand here - the implementation itself, and |
| 9 |
a rust trait for a "syncable store" where component-specific logic lives - but |
| 10 |
before we dive into them, some preamble might help put things into context. |
| 11 |
|
| 12 |
## Nomenclature |
| 13 |
|
| 14 |
* The term "store" is generally used as the interface to the database - ie, the |
| 15 |
thing that gets and saves items. It can also be seen as supplying the API |
| 16 |
used by most consumers of the component. Note that the "places" component |
| 17 |
is alone in using the term "api" for this object. |
| 18 |
|
| 19 |
* The term "engine" (or ideally, "sync engine") is used for the thing that |
| 20 |
actually does the syncing for a store. Sync engines implement the SyncEngine |
| 21 |
trait - the trait is either implemented directly by a store, or a new object |
| 22 |
that has a reference to a store. |
| 23 |
|
| 24 |
## Introduction and History |
| 25 |
|
| 26 |
For many years Sync has worked exclusively against a "sync v1.5 server". This |
| 27 |
[is a REST API described here](https://mozilla-services.readthedocs.io/en/latest/storage/apis-1.5.html). |
| 28 |
The important part is that the API is conceptually quite simple - there are |
| 29 |
arbitrary "collections" containing "records" indexed by a GUID, and lacking |
| 30 |
traditonal database concepts like joins. Because the record is encrypted, |
| 31 |
there's very little scope for the server to be much smarter. Thus it's |
| 32 |
reasonably easy to create a fairly generic abstraction over the API that can be |
| 33 |
easily reused. |
| 34 |
|
| 35 |
Back in the deep past, we found ourselves with 2 different components that |
| 36 |
needed to sync against a sync v1.5 server. The apps using these components |
| 37 |
didn't have schedulers or any UI for choosing what to sync - so these |
| 38 |
components just looked at the existing state of the engines on the server and |
| 39 |
synced if they were enabled. |
| 40 |
|
| 41 |
This was also pre-megazord - the idea was that apps could choose from a "menu" |
| 42 |
of components to include - so we didn't really want to bind these components |
| 43 |
together. Therefore, there was no concept of "sync all" - instead, each of the |
| 44 |
components had to be synced individually. So this component started out as more |
| 45 |
of a "library" than a "component" which individual components could reuse - and |
| 46 |
each of these components was a "syncable store" (ie, a store which could supply |
| 47 |
a "sync engine"). |
| 48 |
|
| 49 |
Fast forward to Fenix and we needed a UI for managing all the engines supported |
| 50 |
there, and a single "sync now" experience etc - so we also have a sync_manager |
| 51 |
component - [see its README for more](../components/sync_manager/README.md). |
| 52 |
But even though it exists, there are still some parts of this component that |
| 53 |
reflect these early days - for example, it's still possible to sync just a |
| 54 |
single component using sync15 (ie, without going via the "sync manager"), |
| 55 |
although this isn't used and should be removed - the "sync manager" allows you |
| 56 |
to choose which engines to sync, so that should be used exclusively. |
| 57 |
|
| 58 |
## Metadata |
| 59 |
|
| 60 |
There's some metadata associated with a sync. Some of the metadata is "global" |
| 61 |
to the app (eg, the enabled state of engines, information about what servers to |
| 62 |
use, etc) and some is specific to an engine (eg, timestamp of the |
| 63 |
server's collection for this engine, guids for the collections, etc). |
| 64 |
|
| 65 |
We made the decision early on that no storage should be done by this |
| 66 |
component: |
| 67 |
|
| 68 |
* The "global" metadata should be stored by the application - but because it |
| 69 |
doesn't need to interpret the data, we do this with an opaque string (that |
| 70 |
is JSON, but the app should never assume or introspect that) |
| 71 |
|
| 72 |
* Each engine should store its own metadata, so we don't end up in the |
| 73 |
situation where, say, a database is moved between profiles causing the |
| 74 |
metadata to refer to a completely different data set. So each engine |
| 75 |
stores its metadata in the same database as the data itself, so if the |
| 76 |
database is moved or copied, the metadata comes with it) |
| 77 |
|
| 78 |
## Sync Implementation |
| 79 |
|
| 80 |
The core implementation does all of the interaction with things like the |
| 81 |
tokenserver, the `meta/global` and `info/collections` collections, etc. It |
| 82 |
does all network interaction (ie, individual engines don't need to interact with |
| 83 |
the network at all), tracks things like whether the server is asking us to |
| 84 |
"backoff" due to operational concerns, manages encryption keys and the |
| 85 |
encryption itself, etc. The general flow of a sync - which interacts with the |
| 86 |
`SyncEngine` trait - is: |
| 87 |
|
| 88 |
* Does all pre-sync setup, such as checking `meta/global`, and whether the |
| 89 |
sync IDs on the server match the sync IDs we last saw (ie, to check whether |
| 90 |
something drastic has happened since we last synced) |
| 91 |
* Asks the engine about how to formulate the URL query params to obtain the |
| 92 |
records the engine cares about. In most cases, this will simply be "records |
| 93 |
since the last modified timestamp of the last sync". |
| 94 |
* Downloads and decrypts these records. |
| 95 |
* Passes these records to the engine for processing, and obtains records that |
| 96 |
should be uploaded to the server. |
| 97 |
* Encrypts these outgoing records and uploads them. |
| 98 |
* Tells the engine about the result of the upload (ie, the last-modified |
| 99 |
timestamp of the POST so it can be saved as engine metadata) |
| 100 |
|
| 101 |
As above, the sync15 component really only deals with a single engine at a time. |
| 102 |
See the "sync manager" for how multiple engine are managed (but the tl;dr is |
| 103 |
that the "sync manager" leans on this very heavily, but knows about multiple |
| 104 |
engine and manages shared state) |
| 105 |
|
| 106 |
## The `SyncEngine` trait |
| 107 |
|
| 108 |
The SyncEngine trait is where all logic specific to a collection lives. A "sync |
| 109 |
engine" implements (or provides) this trait to implement actual syncing. |
| 110 |
|
| 111 |
For <handwave> reasons, it actually lives in the |
| 112 |
[sync-traits helper](https://github.com/mozilla/application-services/blob/main/components/support/sync15-traits/src/engine.rs) |
| 113 |
but for the purposes of this document, you should consider it as owned by sync15. |
| 114 |
|
| 115 |
This is actually quite a simple trait - at a high level, it's really just |
| 116 |
concerned with: |
| 117 |
|
| 118 |
* Get or set some metadata the sync15 component has decided should be saved or |
| 119 |
fetched. |
| 120 |
|
| 121 |
* In a normal sync, take some "incoming" records, process them, and return |
| 122 |
the "outgoing" records we should send to the server. |
| 123 |
|
| 124 |
* In some edge-cases, either "wipe" (ie, actually delete everything, which |
| 125 |
almost never happens) or "reset" (ie, pretend this engine has never before |
| 126 |
been synced) |
| 127 |
|
| 128 |
And that's it! |