Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newly-pulled docs shouldn't show up in pusher's pendingDocuments #1332

Merged
merged 1 commit into from
Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Source/API/CouchbaseLitePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@

@interface CBLReplication ()
@property (nonatomic, readonly) NSDictionary* properties;
@property (nonatomic, readonly) SInt64 lastSequencePushed;
@end


Expand Down
31 changes: 21 additions & 10 deletions Source/CBLRestPusher.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ - (void) beginReplicating {
[self performSelector: @selector(stopped) withObject: nil afterDelay: 0.0];
return;
}
for (CBL_Revision* rev in unpushedRevisions)
[self addPending: rev];
[self addRevsToInbox: unpushedRevisions];
[_batcher flush]; // process up to the first 100 revs

Expand Down Expand Up @@ -196,24 +198,36 @@ - (void) removePending: (CBL_Revision*)rev {
[_purgeQueue queueObject: rev];
}

// I'm not going to do anything with this sequence, so increase the lastSequence up to it
- (void) skipSequence: (SequenceNumber)seq {
if (seq < (SequenceNumber)_pendingSequences.firstIndex)
if (seq > [self.lastSequence longLongValue])
self.lastSequence = $sprintf(@"%lld", seq);
}


- (void) dbChanged: (NSNotification*)n {
CBLDatabase* db = _db;
CBLFilterBlock filter = _settings.filterBlock;
NSArray* changes = (n.userInfo)[@"changes"];
for (CBLDatabaseChange* change in changes) {
// Skip revisions that originally came from the database I'm syncing to:
if (![change.source isEqual: _settings.remote]) {
CBL_Revision* rev = change.addedRevision;
if (!rev)
continue; // ignore purges
if (filter && ![db runFilter: filter params: _settings.filterParameters onRevision: rev])
continue;
CBL_Revision* rev = change.addedRevision;
if (!rev)
continue; // ignore purges
// Skip revisions that originally came from the database I'm syncing to,
// or which don't match the filter:
if (![change.source isEqual: _settings.remote] &&
(!filter || [db runFilter: filter params: _settings.filterParameters
onRevision: rev]))
{
CBL_MutableRevision* nuRev = [rev mutableCopy];
nuRev.body = nil; // save memory
LogVerbose(Sync, @"%@: Queuing #%lld %@",
self, [db getRevisionSequence: nuRev], nuRev);
[self addPending: nuRev];
[self addToInbox: nuRev];
} else {
[self skipSequence: rev.sequence];
}
}
}
Expand All @@ -222,8 +236,6 @@ - (void) dbChanged: (NSNotification*)n {
- (void) processInbox: (CBL_RevisionList*)changes {
if ([_settings.options[kCBLReplicatorOption_AllNew] isEqual: @YES]) {
// If 'allNew' option is set, upload new revs without checking first:
for (CBL_Revision* rev in changes)
[self addPending: rev];
[self uploadChanges: changes fromDiffs: nil];
return;
}
Expand All @@ -239,7 +251,6 @@ - (void) processInbox: (CBL_RevisionList*)changes {
diffs[docID] = revs;
}
[revs addObject: rev.revIDString];
[self addPending: rev];
}

// Call _revs_diff on the target db:
Expand Down
5 changes: 3 additions & 2 deletions Source/CBLRestReplicator.m
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,15 @@ - (void) setLastSequence:(id)lastSequence {
_lastSequenceChanged = YES;
[self performSelector: @selector(saveLastSequence) withObject: nil afterDelay: 5.0];
}
[self postProgressChanged];
}
}


- (void) postProgressChanged {
LogVerbose(Sync, @"%@: postProgressChanged (%u/%u, active=%d (batch=%u, net=%u), online=%d)",
LogVerbose(Sync, @"%@: postProgressChanged (%u/%u, active=%d (batch=%u, net=%u), lastSeq=%@, online=%d, error=%@)",
self, (unsigned)_changesProcessed, (unsigned)_changesTotal,
_active, (unsigned)_batcher.count, _asyncTaskCount, _online);
_active, (unsigned)_batcher.count, _asyncTaskCount, _lastSequence, _online, _error.my_compactDescription);
NSNotification* n = [NSNotification notificationWithName: CBL_ReplicatorProgressChangedNotification
object: self];
[[NSNotificationQueue defaultQueue] enqueueNotification: n
Expand Down
31 changes: 28 additions & 3 deletions Unit-Tests/Replication_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -606,23 +606,23 @@ - (void) test10_ReplicationCookie {
NSHTTPCookieOriginURL: remoteDbURL,
NSHTTPCookiePath: remoteDbURL.path,
NSHTTPCookieValue: @"logmein",
NSHTTPCookieExpires: [NSDate dateWithTimeIntervalSinceNow: 10]
NSHTTPCookieExpires: [NSDate distantFuture]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Not related to this fix, but I think this will prevent intermittent failures in this unit test. (I got one of those failures just now.)

}];

NSHTTPCookie* cookie2 = [NSHTTPCookie cookieWithProperties:
@{ NSHTTPCookieName: @"UnitTestCookie2",
NSHTTPCookieOriginURL: remoteDbURL,
NSHTTPCookiePath: remoteDbURL.path,
NSHTTPCookieValue: @"logmein",
NSHTTPCookieExpires: [NSDate dateWithTimeIntervalSinceNow: 10]
NSHTTPCookieExpires: [NSDate distantFuture]
}];

NSHTTPCookie* cookie3 = [NSHTTPCookie cookieWithProperties:
@{ NSHTTPCookieName: @"UnitTestCookie3",
NSHTTPCookieOriginURL: remoteDbURL,
NSHTTPCookiePath: remoteDbURL.path,
NSHTTPCookieValue: @"logmein",
NSHTTPCookieExpires: [NSDate dateWithTimeIntervalSinceNow: 10]
NSHTTPCookieExpires: [NSDate distantFuture]
}];

CBLReplication* repl = [db createPullReplication: remoteDbURL];
Expand Down Expand Up @@ -651,6 +651,7 @@ - (void) test10_ReplicationCookie {
AssertNil(repl.lastError);

// Recreate the replicator and delete a cookie:
Log(@"***** Testing cookie deletion *****");
repl = [db createPullReplication: remoteDbURL];
[repl deleteCookieNamed: cookie3.name];
[repl start];
Expand Down Expand Up @@ -1162,6 +1163,30 @@ - (void)test18_PendingDocumentIDs {
}


// Issue #1274: Just-pulled docs shouldn't be treated as pending by the pusher
- (void)test18_PendingDocumentIDs_OnFirstPull {
NSURL* remoteDbURL = [self remoteTestDBURL: @"public"];
if (!remoteDbURL)
return;

// Push replication:
CBLReplication* push = [db createPushReplication: remoteDbURL];
push.continuous = YES;
[push start];

// Run a one-shot pull:
CBLReplication* pull = [db createPullReplication: remoteDbURL];
[self runReplication: pull expectedChangesCount: 2];

// Give the push CBLReplication a chance to receive the progress notification,
// so it learns the current checkpoint
[NSRunLoop.currentRunLoop runMode: NSDefaultRunLoopMode
beforeDate: [NSDate dateWithTimeIntervalSinceNow: 1.0]];
AssertEq(push.lastSequencePushed, 2);
AssertEqual(push.pendingDocumentIDs, [NSSet new]);
}


- (void) test_19_Auth_Failure {
_timeout = 2.0; // Failure should be immediate, with no retries
NSURL* remoteDbURL = [self remoteTestDBURL: @"cbl_auth_test"];
Expand Down