-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Stop persisting last acknowledged batch ID #2243
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
Changes from 10 commits
7dc6324
1db0d68
92bc0ab
7f28b14
ff09356
28cd407
f559b0e
aebc1a6
e35b9ab
58e4eef
5719e77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,12 +31,14 @@ | |
#include "Firestore/core/src/firebase/firestore/auth/user.h" | ||
#include "Firestore/core/src/firebase/firestore/model/document_key.h" | ||
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h" | ||
#include "Firestore/core/src/firebase/firestore/model/mutation_batch.h" | ||
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h" | ||
|
||
namespace testutil = firebase::firestore::testutil; | ||
using firebase::firestore::auth::User; | ||
using firebase::firestore::model::DocumentKey; | ||
using firebase::firestore::model::DocumentKeySet; | ||
using firebase::firestore::model::kBatchIdUnknown; | ||
using firebase::firestore::testutil::Key; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
@@ -83,32 +85,30 @@ - (void)testCountBatches { | |
- (void)testAcknowledgeBatchID { | ||
if ([self isTestBaseClass]) return; | ||
|
||
// Initial state of an empty queue | ||
XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], kFSTBatchIDUnknown); | ||
|
||
// Adding mutation batches should not change the highest acked batchID. | ||
self.persistence.run("testAcknowledgeBatchID", [&]() { | ||
XCTAssertEqual([self batchCount], 0); | ||
|
||
FSTMutationBatch *batch1 = [self addMutationBatch]; | ||
FSTMutationBatch *batch2 = [self addMutationBatch]; | ||
FSTMutationBatch *batch3 = [self addMutationBatch]; | ||
XCTAssertGreaterThan(batch1.batchID, kFSTBatchIDUnknown); | ||
XCTAssertGreaterThan(batch1.batchID, kBatchIdUnknown); | ||
XCTAssertGreaterThan(batch2.batchID, batch1.batchID); | ||
XCTAssertGreaterThan(batch3.batchID, batch2.batchID); | ||
|
||
XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], kFSTBatchIDUnknown); | ||
XCTAssertEqual([self batchCount], 3); | ||
|
||
[self.mutationQueue acknowledgeBatch:batch1 streamToken:nil]; | ||
[self.mutationQueue removeMutationBatch:batch1]; | ||
XCTAssertEqual([self batchCount], 2); | ||
|
||
[self.mutationQueue acknowledgeBatch:batch2 streamToken:nil]; | ||
XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID); | ||
XCTAssertEqual([self batchCount], 2); | ||
|
||
[self.mutationQueue removeMutationBatch:batch2]; | ||
XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID); | ||
XCTAssertEqual([self batchCount], 1); | ||
|
||
// Batch 3 never acknowledged. | ||
[self.mutationQueue removeMutationBatch:batch3]; | ||
XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID); | ||
XCTAssertEqual([self batchCount], 0); | ||
}); | ||
} | ||
|
||
|
@@ -122,7 +122,6 @@ - (void)testAcknowledgeThenRemove { | |
[self.mutationQueue removeMutationBatch:batch1]; | ||
|
||
XCTAssertEqual([self batchCount], 0); | ||
XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch1.batchID); | ||
}); | ||
} | ||
|
||
|
@@ -186,28 +185,6 @@ - (void)testNextMutationBatchAfterBatchID { | |
}); | ||
} | ||
|
||
- (void)testNextMutationBatchAfterBatchIDSkipsAcknowledgedBatches { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test appears to be no longer relevant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. The mutation queue now contains only unacknowledged writes now. |
||
if ([self isTestBaseClass]) return; | ||
|
||
NSMutableArray<FSTMutationBatch *> *batches = self.persistence.run( | ||
"testNextMutationBatchAfterBatchIDSkipsAcknowledgedBatches newBatches", | ||
[&]() -> NSMutableArray<FSTMutationBatch *> * { | ||
NSMutableArray<FSTMutationBatch *> *newBatches = [self createBatches:3]; | ||
XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:kFSTBatchIDUnknown], | ||
newBatches[0]); | ||
return newBatches; | ||
}); | ||
self.persistence.run("testNextMutationBatchAfterBatchIDSkipsAcknowledgedBatches", [&]() { | ||
[self.mutationQueue acknowledgeBatch:batches[0] streamToken:nil]; | ||
XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:kFSTBatchIDUnknown], | ||
batches[1]); | ||
XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:batches[0].batchID], | ||
batches[1]); | ||
XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:batches[1].batchID], | ||
batches[2]); | ||
}); | ||
} | ||
|
||
- (void)testAllMutationBatchesAffectingDocumentKey { | ||
if ([self isTestBaseClass]) return; | ||
|
||
|
@@ -406,7 +383,6 @@ - (void)testStreamToken { | |
XCTAssertEqualObjects([self.mutationQueue lastStreamToken], streamToken1); | ||
|
||
[self.mutationQueue acknowledgeBatch:batch1 streamToken:streamToken2]; | ||
XCTAssertEqual(self.mutationQueue.highestAcknowledgedBatchID, batch1.batchID); | ||
XCTAssertEqualObjects([self.mutationQueue lastStreamToken], streamToken2); | ||
}); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
#include "Firestore/core/src/firebase/firestore/local/leveldb_key.h" | ||
#include "Firestore/core/src/firebase/firestore/local/leveldb_transaction.h" | ||
#include "Firestore/core/src/firebase/firestore/local/leveldb_util.h" | ||
#include "Firestore/core/src/firebase/firestore/model/mutation_batch.h" | ||
#include "Firestore/core/src/firebase/firestore/model/resource_path.h" | ||
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h" | ||
#include "Firestore/core/src/firebase/firestore/util/string_apple.h" | ||
|
@@ -52,6 +53,7 @@ | |
using firebase::firestore::local::LevelDbTransaction; | ||
using firebase::firestore::local::MakeStringView; | ||
using firebase::firestore::model::BatchId; | ||
using firebase::firestore::model::kBatchIdUnknown; | ||
using firebase::firestore::model::DocumentKey; | ||
using firebase::firestore::model::DocumentKeySet; | ||
using firebase::firestore::model::ResourcePath; | ||
|
@@ -115,31 +117,13 @@ - (instancetype)initWithUserID:(std::string)userID | |
} | ||
|
||
- (void)start { | ||
BatchId nextBatchID = [FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr]; | ||
self.nextBatchID = [FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr]; | ||
|
||
// On restart, nextBatchId may end up lower than lastAcknowledgedBatchId since it's computed from | ||
// the queue contents, and there may be no mutations in the queue. In this case, we need to reset | ||
// lastAcknowledgedBatchId (which is safe since the queue must be empty). | ||
std::string key = [self keyForCurrentMutationQueue]; | ||
FSTPBMutationQueue *metadata = [self metadataForKey:key]; | ||
if (!metadata) { | ||
metadata = [FSTPBMutationQueue message]; | ||
|
||
// proto3's default value for lastAcknowledgedBatchId is zero, but that would consider the first | ||
// entry in the queue to be acknowledged without that acknowledgement actually happening. | ||
metadata.lastAcknowledgedBatchId = kFSTBatchIDUnknown; | ||
} else { | ||
BatchId lastAcked = metadata.lastAcknowledgedBatchId; | ||
if (lastAcked >= nextBatchID) { | ||
HARD_ASSERT([self isEmpty], "Reset nextBatchID is only possible when the queue is empty"); | ||
lastAcked = kFSTBatchIDUnknown; | ||
|
||
metadata.lastAcknowledgedBatchId = lastAcked; | ||
_db.currentTransaction->Put([self keyForCurrentMutationQueue], metadata); | ||
} | ||
} | ||
|
||
self.nextBatchID = nextBatchID; | ||
self.metadata = metadata; | ||
} | ||
|
||
|
@@ -151,7 +135,7 @@ + (BatchId)loadNextBatchIDFromDB:(DB *)db { | |
auto tableKey = LevelDbMutationKey::KeyPrefix(); | ||
|
||
LevelDbMutationKey rowKey; | ||
BatchId maxBatchID = kFSTBatchIDUnknown; | ||
BatchId maxBatchID = kBatchIdUnknown; | ||
|
||
BOOL moreUserIDs = NO; | ||
std::string nextUserID; | ||
|
@@ -220,17 +204,8 @@ - (BOOL)isEmpty { | |
return empty; | ||
} | ||
|
||
- (BatchId)highestAcknowledgedBatchID { | ||
return self.metadata.lastAcknowledgedBatchId; | ||
} | ||
|
||
- (void)acknowledgeBatch:(FSTMutationBatch *)batch streamToken:(nullable NSData *)streamToken { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this function is now equivalent to |
||
BatchId batchID = batch.batchID; | ||
HARD_ASSERT(batchID > self.highestAcknowledgedBatchID, | ||
"Mutation batchIDs must be acknowledged in order"); | ||
|
||
FSTPBMutationQueue *metadata = self.metadata; | ||
metadata.lastAcknowledgedBatchId = batchID; | ||
metadata.lastStreamToken = streamToken; | ||
|
||
_db.currentTransaction->Put([self keyForCurrentMutationQueue], metadata); | ||
|
@@ -304,10 +279,7 @@ - (nullable FSTMutationBatch *)lookupMutationBatch:(BatchId)batchID { | |
} | ||
|
||
- (nullable FSTMutationBatch *)nextMutationBatchAfterBatchID:(BatchId)batchID { | ||
// All batches with batchID <= self.metadata.lastAcknowledgedBatchId have been acknowledged so | ||
// the first unacknowledged batch after batchID will have a batchID larger than both of these | ||
// values. | ||
BatchId nextBatchID = MAX(batchID, self.metadata.lastAcknowledgedBatchId) + 1; | ||
BatchId nextBatchID = batchID + 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I presume this is fine? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
|
||
std::string key = [self mutationKeyForBatchID:nextBatchID]; | ||
auto it = _db.currentTransaction->NewIterator(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2018 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "Firestore/core/src/firebase/firestore/model/mutation_batch.h" | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace model { | ||
|
||
const BatchId kBatchIdUnknown = -1; | ||
|
||
} // namespace model | ||
} // namespace firestore | ||
} // namespace firebase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking
batchCount
is the only replacement for the previous check that I could think of.