Permalink
Browse files

Fix: Pull checkpoints weren't getting restored correctly

TDPuller was saving its checkpoints to the remote JSON doc as numbers but
they were then read back and cast to string, causing the comparison to
fail. This meant the puller would start over from the beginning every time.

I also added some more logging.
  • Loading branch information...
snej committed Mar 26, 2013
1 parent 6728a6c commit ba085225dcd3645dc2f1dfa8af4c639b015e0573
Showing with 9 additions and 7 deletions.
  1. +1 −0 Source/ChangeTracker/TDSocketChangeTracker.m
  2. +1 −0 Source/TDPuller.m
  3. +1 −0 Source/TDPusher.m
  4. +6 −7 Source/TDReplicator.m
@@ -92,6 +92,7 @@ - (BOOL) start {
} }
// Now open the connection: // Now open the connection:
LogTo(SyncVerbose, @"%@: GET %@", self, url.resourceSpecifier);
CFReadStreamRef cfInputStream = CFReadStreamCreateForHTTPRequest(NULL, request); CFReadStreamRef cfInputStream = CFReadStreamCreateForHTTPRequest(NULL, request);
CFRelease(request); CFRelease(request);
if (!cfInputStream) if (!cfInputStream)
View
@@ -192,6 +192,7 @@ - (void) changeTrackerReceivedChanges: (NSArray*)changes {
rev.remoteSequenceID = remoteSequenceID; rev.remoteSequenceID = remoteSequenceID;
if (changes.count > 1) if (changes.count > 1)
rev.conflicted = true; rev.conflicted = true;
LogTo(SyncVerbose, @"%@: Received #%@ %@", self, remoteSequenceID, rev);
[self addToInbox: rev]; [self addToInbox: rev];
changeCount++; changeCount++;
View
@@ -168,6 +168,7 @@ - (void) dbChanged: (NSNotification*)n {
return; return;
} }
LogTo(SyncVerbose, @"%@: Queuing #%lld %@", self, rev.sequence, rev);
[self addToInbox: rev]; [self addToInbox: rev];
} }
View
@@ -164,7 +164,7 @@ - (void) setLastSequence:(NSString*)lastSequence {
- (void) postProgressChanged { - (void) postProgressChanged {
LogTo(SyncVerbose, @"%@: postProgressChanged (%u/%u, active=%d (batch=%u, net=%u), online=%d)", LogTo(Sync, @"%@: postProgressChanged (%u/%u, active=%d (batch=%u, net=%u), online=%d)",
self, (unsigned)_changesProcessed, (unsigned)_changesTotal, self, (unsigned)_changesProcessed, (unsigned)_changesTotal,
_active, (unsigned)_batcher.count, _asyncTaskCount, _online); _active, (unsigned)_batcher.count, _asyncTaskCount, _online);
NSNotification* n = [NSNotification notificationWithName: TDReplicatorProgressChangedNotification NSNotification* n = [NSNotification notificationWithName: TDReplicatorProgressChangedNotification
@@ -411,7 +411,6 @@ - (void) asyncTasksFinished: (NSUInteger)numTasks {
- (void) addToInbox: (TD_Revision*)rev { - (void) addToInbox: (TD_Revision*)rev {
Assert(_running); Assert(_running);
LogTo(SyncVerbose, @"%@: Received #%lld %@", self, rev.sequence, rev);
[_batcher queueObject: rev]; [_batcher queueObject: rev];
[self updateActive]; [self updateActive];
} }
@@ -596,21 +595,21 @@ - (void) fetchRemoteCheckpointDoc {
onCompletion: ^(id response, NSError* error) { onCompletion: ^(id response, NSError* error) {
// Got the response: // Got the response:
if (error && error.code != kTDStatusNotFound) { if (error && error.code != kTDStatusNotFound) {
LogTo(Sync, @"%@: Error fetching last sequence: %@", self, error.localizedDescription);
self.error = error; self.error = error;
} else { } else {
if (error.code == kTDStatusNotFound) if (error.code == kTDStatusNotFound)
[self maybeCreateRemoteDB]; [self maybeCreateRemoteDB];
response = $castIf(NSDictionary, response); response = $castIf(NSDictionary, response);
self.remoteCheckpoint = response; self.remoteCheckpoint = response;
NSString* remoteLastSequence = $castIf(NSString, NSString* remoteLastSequence = response[@"lastSequence"];
response[@"lastSequence"]);
if ($equal(remoteLastSequence, localLastSequence)) { if ($equal(remoteLastSequence, localLastSequence)) {
_lastSequence = localLastSequence; _lastSequence = localLastSequence;
LogTo(Sync, @"%@: Replicating from lastSequence=%@", self, _lastSequence); LogTo(Sync, @"%@: Replicating from lastSequence=%@", self, _lastSequence);
} else { } else {
LogTo(Sync, @"%@: lastSequence mismatch: I had %@, remote had %@", LogTo(Sync, @"%@: lastSequence mismatch: I had %@, remote had %@ (response = %@)",
self, localLastSequence, remoteLastSequence); self, localLastSequence, remoteLastSequence, response);
} }
[self beginReplicating]; [self beginReplicating];
} }
@@ -641,7 +640,7 @@ - (void) saveLastSequence {
NSMutableDictionary* body = [_remoteCheckpoint mutableCopy]; NSMutableDictionary* body = [_remoteCheckpoint mutableCopy];
if (!body) if (!body)
body = $mdict(); body = $mdict();
[body setValue: _lastSequence forKey: @"lastSequence"]; [body setValue: _lastSequence.description forKey: @"lastSequence"]; // always save as a string
_savingCheckpoint = YES; _savingCheckpoint = YES;
NSString* checkpointID = self.remoteCheckpointDocID; NSString* checkpointID = self.remoteCheckpointDocID;

0 comments on commit ba08522

Please sign in to comment.