improved lastSync/modified handling

- never bump deck mod while syncing
- set lastSync to current time, not deck modified time
- don't update lastSync until the final part of the sync
- lower clock skew allowance to ~5 minutes
- bump full sync threshold to 1000 modified items
This commit is contained in:
Damien Elmes 2010-07-27 22:46:04 +09:00
parent ecb6dbe69a
commit d3fb189a72

View file

@ -120,8 +120,11 @@ class SyncTools(object):
if self.localTime == self.remoteTime: if self.localTime == self.remoteTime:
return False return False
l = self._lastSync(); r = self.server._lastSync() l = self._lastSync(); r = self.server._lastSync()
# Set lastSync to the lower of the two sides, minus some leeway for
# clock skew. Near the end of the sync we'll bump this to the new
# time.
if l != r: if l != r:
self.deck.lastSync = min(l, r) - 600 self.deck.lastSync = min(l, r) - 350
else: else:
self.deck.lastSync = l self.deck.lastSync = l
return True return True
@ -143,11 +146,11 @@ class SyncTools(object):
self.deleteObjsFromKey(diff[3], key) self.deleteObjsFromKey(diff[3], key)
# handle the remainder # handle the remainder
if self.localTime > self.remoteTime: if self.localTime > self.remoteTime:
payload['deck'] = self.bundleDeck()
payload['stats'] = self.bundleStats() payload['stats'] = self.bundleStats()
payload['history'] = self.bundleHistory() payload['history'] = self.bundleHistory()
payload['sources'] = self.bundleSources() payload['sources'] = self.bundleSources()
self.deck.lastSync = self.deck.modified # finally, set new lastSync and bundle the deck info
payload['deck'] = self.bundleDeck()
return payload return payload
def applyPayload(self, payload): def applyPayload(self, payload):
@ -164,11 +167,11 @@ class SyncTools(object):
self.deleteObjsFromKey(payload['deleted-' + key], key) self.deleteObjsFromKey(payload['deleted-' + key], key)
# send back deck-related stuff if it wasn't sent to us # send back deck-related stuff if it wasn't sent to us
if not 'deck' in payload: if not 'deck' in payload:
reply['deck'] = self.bundleDeck()
reply['stats'] = self.bundleStats() reply['stats'] = self.bundleStats()
reply['history'] = self.bundleHistory() reply['history'] = self.bundleHistory()
reply['sources'] = self.bundleSources() reply['sources'] = self.bundleSources()
self.deck.lastSync = self.deck.modified # finally, set new lastSync and bundle the deck info
reply['deck'] = self.bundleDeck()
else: else:
self.updateDeck(payload['deck']) self.updateDeck(payload['deck'])
self.updateStats(payload['stats']) self.updateStats(payload['stats'])
@ -602,6 +605,7 @@ values
########################################################################## ##########################################################################
def bundleDeck(self): def bundleDeck(self):
self.deck.lastSync = time.time()
d = self.dictFromObj(self.deck) d = self.dictFromObj(self.deck)
del d['Session'] del d['Session']
del d['engine'] del d['engine']
@ -625,7 +629,6 @@ insert or replace into deckVars
(key, value) values (:k, :v)""", k=k, v=v) (key, value) values (:k, :v)""", k=k, v=v)
del deck['meta'] del deck['meta']
self.applyDict(self.deck, deck) self.applyDict(self.deck, deck)
self.deck.lastSync = self.deck.modified
self.deck.updateDynamicIndices() self.deck.updateDynamicIndices()
def bundleStats(self): def bundleStats(self):
@ -914,11 +917,11 @@ and cards.id in %s""" % ids2str([c[0] for c in cards])))
return True return True
for sum in sums: for sum in sums:
for l in sum.values(): for l in sum.values():
if len(l) > 500: if len(l) > 1000:
return True return True
if self.deck.s.scalar( if self.deck.s.scalar(
"select count() from reviewHistory where time > :ls", "select count() from reviewHistory where time > :ls",
ls=self.deck.lastSync) > 500: ls=self.deck.lastSync) > 1000:
return True return True
lastDay = date.fromtimestamp(max(0, self.deck.lastSync - 60*60*24)) lastDay = date.fromtimestamp(max(0, self.deck.lastSync - 60*60*24))
if self.deck.s.scalar( if self.deck.s.scalar(
@ -929,8 +932,7 @@ and cards.id in %s""" % ids2str([c[0] for c in cards])))
def prepareFullSync(self): def prepareFullSync(self):
t = time.time() t = time.time()
self.deck.flushMod() self.deck.lastSync = time.time()
self.deck.lastSync = self.deck.modified
self.deck.s.commit() self.deck.s.commit()
self.deck.close() self.deck.close()
fields = { fields = {