~kevang/mnemosyne-proj/grade-shortcuts-improvements

« back to all changes in this revision

Viewing changes to mnemosyne/openSM2sync/README

  • Committer: pbienst
  • Date: 2006-02-09 16:13:13 UTC
  • Revision ID: svn-v3-trunk0:e5e6b78b-db40-0410-9517-b98c64f8d2c1:trunk:2
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Introduction:
2
 
 
3
 
The aim of this document is to provide a high level overview of the syncing protocol which is used to synchronise different instances of Mnemosyne (e.g. the desktop version, versions on mobile phones, webserver). However, the protocol is sufficiently general so that other SRS programs based on the SM2 algorithm can implement it, leading to increased interoperabibilty. 
4
 
 
5
 
Also, some of the design choices are explained here. For the nitty-gritty and the details needed to actually implement the protocol, see the documentation in the reference implementation of the client and the server.
6
 
 
7
 
 
8
 
Features:
9
 
 
10
 
-full bidirectional sync, i.e. after your last sync you can add new cards on your desktop and review existing cards on your phone. During the next sync, these changes will be merged seamlessly.
11
 
-sufficiently general, so that many different SM2-based programs can interoperate.
12
 
-support for multiple partners, i.e. you can sync your phone both with your desktop and with a webserver. (The only limitation is that there can be no cycles in the sync graph -see later).
13
 
-can be implemented efficiently and without using much memory on a mobile device.
14
 
 
15
 
 
16
 
Some use cases and definitions:
17
 
 
18
 
The client is the one that initiates the sync, and the server is the other party. E.g. we could have a Windows Mobile client syncing to an application running on the desktop, a laptop client syncing to that same desktop application. At the same time, that desktop application could also act as client when it syncs to a publically accesible website. In order to make all of these scenarios possible, we need to have the concept of a partnership: a link between two different instances (where instance is defined as being either client or server). Each instance can have multiple partnerships, each with a different other instance. 
19
 
 
20
 
A server can be either single user or multiuser. Applications running on desktops or mobile devices are typically single user, whereas a public webserver is multiuser.
21
 
 
22
 
 
23
 
 
24
 
The history log as main driver for the sync process:
25
 
 
26
 
In order to avoid the calculation of many checksums at sync time, each instance should keep track of a history of all the events that happened since the last sync. This seems like a heavy burden, but most SRS systems already store the repetition history for statistical reasons. The only extra information to be stored is when e.g. cards were added, edited, deleted, ...
27
 
 
28
 
As already mentioned, the history log is the main driver of the syncing process, i.e. we send across each log event successively, if necessary supplemented by extra information. E.g., the 'new card' event would need to be supplemented by sending the actual card data across. The entire process can also easily be implemented in a streaming manner both on the client and the server side, such that it does not require a lot of memory.
29
 
 
30
 
Storing all of this in the history also makes it very efficient to handle multiple partnerships: for each partnership, we only need to store the index in the history corresponding to the last sync.  This can be used to easily create the changeset for that particular partnership. (Other options, like e.g. setting a 'needs_sync' flag on datastructures would be less efficient as that would need to be done once per partnership, leading to much redundancy in storage.)
31
 
 
32
 
 
33
 
 
34
 
Interoperability with other SRS systems:
35
 
 
36
 
Some SRS systems are based on two sided cards only, others have a fact/card model for N-sided cards, yet others support card types which can incorporate different behaviour still. Also, the set of attributes describing a card can vary between different SRS systems. It would be possible to design a protocal in such a way that taking any path between any number of SRS systems would result in no information loss, but this would place a heavy burden on each SRS system, as it would require each SRS system to hold on to the information from the other SRS systems. Therefore, we went instead with a protocol with the following properties:
37
 
 
38
 
-moving data from SRS system A to SRS system B and back will never result in information loss.
39
 
-Say you have three systems. A and C support a feature that B does not. If you move data from A to B and then onto C, you will have information loss (e.g. an N sided card being flatted out to N 2-sided cards, even though C has support for N-sided cards). However, this is not a big limitation from a practical point of view: the user just has to move its data from B back to A before syncing with C, as opposed to directly syncing B with C.
40
 
 
41
 
If the destination does not know about a card type that the source does know about, then the source sends along the question and the answer explicitly. The destination is then required to treat this card as review-only, i.e. no editing is allowed. When the data flows back to the original source, no information is lost in this way.
42
 
 
43
 
If both source and destination know about the card type, there is no need to send across question and answer, but both instances need to know the card's fact id and factview id Together with the fact data and their joint knowledge of the card type, this will allow both parties to generate the question and answer from the fact data. (Which capabilities both SRS systems have is determined during the login phase of the protocol.)
44
 
 
45
 
If one SRS system requires extra variables for its functioning that the other one does not know about, there is no need to send those across during sync or for these extra variables to be stored by the other party. The other party will not modify those anyway, and the sender still has access to these variables. If the sync happens between two instances of the same SRS system, and the SRS system requires extra variables, then of course that extra data needs to be sent across.
46
 
 
47
 
 
48
 
 
49
 
Identifying people and machines:
50
 
 
51
 
Be sure to distinguish the following concepts:
52
 
 
53
 
-the user chosen id and password
54
 
-an anonymous but unique machine generated id identifying the user, used to upload revision history anonymously to the science servers
55
 
-an anonymous but unique machine generated id identifying each machine
56
 
 
57
 
 
58
 
 
59
 
Media files:
60
 
 
61
 
Stuff like pictures, sound files, etc... is stored in a special media directory, which obviously can be in an entirely different location in both the client and the server. During the sync process, only pathnames relative to that mediadir are sent across. Subdirs in the mediadir are designated by the unix path convention, i.e. / instead of \.
62
 
 
63
 
Since media can be modified outside of the program, it makes no sense to use the history to track media. Rather, each instance should keep track of which media files are associated with a deck, and what the last know modification date or checksum is was in order to determine which media files need syncing. This timestamp or checksum is considered internal to the program, and not sent across during sync, to allow for the fact that different programs can use different methods to track file updates. E.g., checksums could be more expensive that timestapms on a mobile device.
64
 
 
65
 
If the client identifies itself as a card-based client, then the server will automatically convert latex tags to images, without the client having to code anything.
66
 
 
67
 
 
68
 
 
69
 
Cycles in the syncing graph:
70
 
 
71
 
Cycles in the syncing graph are not allowed. E.g., A can sync with B, and B can sync with C, but then A and C are no longer allowed to sync directly, without going through C.
72
 
 
73
 
To detect this, each sync partner not only keeps a list a partnerships resulting from a direct sync, but also from an indirect sync. I.e., if A sync with B and then B with C, afterwards A should figure in the partnerships of C (but with e.g. a dummy index). 
74
 
 
75
 
Therefore, upon login, the list of all the partnerships is sent to the sync partner. It is then the server that will check for the existence of cycles.
76
 
 
77
 
Getting rid of this limitation would be possible, but costly in terms of extra storage and processing power. Consider e.g. the following scenario:
78
 
 
79
 
-A and B sync, and afterwards both make separate local changes
80
 
-B and C sync, and afterwards C makes local changes
81
 
-A and B sync, and afterwards A makes local changes
82
 
 
83
 
Upon the following sync between A and C, the changes of A to be sent across to C are no longer continuous in A's log. To be able to correctly determine with log entries should be synced, each log entry should have a UUID as opposed to local id, but this will cost much more storage and processing time. Another option might be to introduce sync events in the log, recording which partner synced to which partner and at what point, but this would be rather tricky to implement.
84
 
 
85
 
 
86
 
Conflicts:
87
 
 
88
 
Upon detecting conflicts, the full database from either the client or the server is copied to the other side, the direction depending on the user's indication. After doing so, the partnership table of the database that was copied over is adapted such that client and server id's are reversed. There are routines to either send across the database as xml (for non-Mnemosyne clients which have not implemented a binary extension to the server) or as binary. 
89
 
 
90
 
Note however that there is no way to undo logs that have been prepared to send on the science server. This is somewhat unfortunate, but the sheer size of science database should mitigate this effect.
91
 
 
92
 
Another side effect is this approach is that it is only possible to keep the local version after conflict, if the client keeps all the same information as the server. So e.g. card-only based clients will only be able to keep the remote server version after conflict. The same is true for libmnemosyne based clients which have chosen not to keep the entire revision history in order to save resources.
93
 
 
94
 
The initial plan was to do something faster by doing incremental changes to the database: erasing the parts of the logs that need to be discarded, and constructing a stream of log entries to undo the changes based on information from the other side (e.g. a DELETED_TAG entry would be converted to an ADDED_TAG entry). However, this is not full proof in the case of multiple partners (for similar reasons as the cycle problem). Consider e.g. the following scenario:
95
 
 
96
 
-A, B, C are in sync
97
 
-A and B make conflicting changes
98
 
-B and C sync
99
 
-C makes non conflicting changes
100
 
-A and B sync, conficts are detected and A wins
101
 
 
102
 
When now B and C sync, the part of C's log that needs to be undone is no longer last in its log, so there is no easy way for B to tell C which parts need to be discarded. Also here, a way around would be UUIDs for each log entry or logging sync events, but that would either be very costly or complicated.
103
 
 
104
 
 
105
 
 
106
 
Reference implementation:
107
 
 
108
 
For the actual details of the syncing process itself, the code of the reference implementation and the comments in that code are the places to look:
109
 
 
110
 
-ui.py: interface class abstracting away how the client and the server interact with the user
111
 
-database.py: the interface of the database class. Also contains an example SQL schema for a minimal client which only supports cards, not facts.
112
 
-log_entry.py: describes all the types of events stored in the history log, together with the information that needs to be associated with it
113
 
-partner.py: code shared between client and server, mainly dealing with how to bundle log entries and media files
114
 
-text_format.py: interface for a class describing how the info should be sent across in a textual way. Only implemented so for is an xml protocal, described in text_formats/xml_format.py
115
 
-binary_format.py: in some cases, it's much faster to be able to send across a binary copy of the full database, e.g. on the initial sync. This of courses requires that the server is able to generate a binary database in the format that the client can read. Implement so far is binary_formats/mnemosyne_format.py, for clients based on libmnemosyne.
116
 
-client.py: the syncing process as seen from the client side
117
 
-server.py: the sycing process as seen from the server side
118
 
 
119
 
It's also interesting to have a look at the testsuite in tests/test_sync.py, for inspriration on what corner cases to be prepared for :-)
120
 
 
121
 
 
122
 
 
123
 
Syncing speed:
124
 
 
125
 
After robustness, speed is the most important property of any implementation of the sync protocol. An efficient sync will contribute greatly to the comfort of the user and the percieved quality of the client.
126
 
 
127
 
The Python reference client and server implementation have already been optimised to this effect. For a typical daily scenario of doing 200 repetitions of your phone and adding 20 cards on your desktop, a sync takes about 4 seconds on a home WLAN with a rather old (2007) Windows Mobile phone. Memory requirement is about 4 Mb. This is without any GUI running, and does not take the time into account to backup the database before sync (which in this case turns out to be the dominant factor).
128
 
 
129
 
When implementing the client in a different language, here are some tips to help improve performance:
130
 
 
131
 
-profile your client to detect where the bottlenecks are
132
 
-look for the most optimal XML parsing library, preferably a compiled one as opposed to an interpreted one
133
 
-buffer *all* the http connections, i.e. read them block by block (e.g. 8kB), as opposed to e.g. log entry by log entry
134
 
-to save memory, do all processing in a streaming manner, i.e. don't read in everything at once, but process block by block by block and throw away the block as soon as it's processed
135
 
-make sure to support all the time saving feature the server supports, like binary download of the very first sync
136
 
-at the beginning of 'client.py', there are a few network tweaks which are useful in the context of the Python network stack, but which could also be worth investigating
137
 
-reuse the same http connection for the different stages of the sync
138
 
-if you don't care about historical statistics, don't download the entire history on the first sync ('interested_in_old_reps' setting)
139
 
-run the sync operation in a separate thread to keep the UI responsive
140
 
-progress bars are nice, but don't update them after every log entry, as then you would spend more time updating the GUI than doing the actual processing