Skip to content

Commit 5187d09

Browse files
var-constwilhuff
authored andcommitted
Port "Stop persisting last acknowledged batch ID" from iOS (#188)
1 parent a15a89b commit 5187d09

File tree

5 files changed

+7
-68
lines changed

5 files changed

+7
-68
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryMutationQueue.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ final class MemoryMutationQueue implements MutationQueue {
6161
/** The next value to use when assigning sequential IDs to each mutation batch. */
6262
private int nextBatchId;
6363

64-
/** The highest acknowledged mutation in the queue. */
65-
private int highestAcknowledgedBatchId;
66-
6764
/**
6865
* The last received stream token from the server, used to acknowledge which responses the client
6966
* has processed. Stream tokens are opaque checkpoint markers whose only real value is their
@@ -79,7 +76,6 @@ final class MemoryMutationQueue implements MutationQueue {
7976

8077
batchesByDocumentKey = new ImmutableSortedSet<>(emptyList(), DocumentReference.BY_KEY);
8178
nextBatchId = 1;
82-
highestAcknowledgedBatchId = MutationBatch.UNKNOWN;
8379
lastStreamToken = WriteStream.EMPTY_STREAM_TOKEN;
8480
}
8581

@@ -90,14 +86,10 @@ public void start() {
9086
// Note: The queue may be shutdown / started multiple times, since we maintain the queue for the
9187
// duration of the app session in case a user logs out / back in. To behave like the
9288
// SQLite-backed MutationQueue (and accommodate tests that expect as much), we reset nextBatchId
93-
// and highestAcknowledgedBatchId if the queue is empty.
89+
// if the queue is empty.
9490
if (isEmpty()) {
9591
nextBatchId = 1;
96-
highestAcknowledgedBatchId = MutationBatch.UNKNOWN;
9792
}
98-
hardAssert(
99-
highestAcknowledgedBatchId < nextBatchId,
100-
"highestAcknowledgedBatchId must be less than the nextBatchId");
10193
}
10294

10395
@Override
@@ -110,9 +102,6 @@ public boolean isEmpty() {
110102
@Override
111103
public void acknowledgeBatch(MutationBatch batch, ByteString streamToken) {
112104
int batchId = batch.getBatchId();
113-
hardAssert(
114-
batchId > highestAcknowledgedBatchId, "Mutation batchIds must be acknowledged in order");
115-
116105
int batchIndex = indexOfExistingBatchId(batchId, "acknowledged");
117106
hardAssert(batchIndex == 0, "Can only acknowledge the first batch in the mutation queue");
118107

@@ -124,7 +113,6 @@ public void acknowledgeBatch(MutationBatch batch, ByteString streamToken) {
124113
batchId,
125114
check.getBatchId());
126115

127-
highestAcknowledgedBatchId = batchId;
128116
lastStreamToken = checkNotNull(streamToken);
129117
}
130118

@@ -180,10 +168,7 @@ public MutationBatch lookupMutationBatch(int batchId) {
180168
@Nullable
181169
@Override
182170
public MutationBatch getNextMutationBatchAfterBatchId(int batchId) {
183-
// All batches with batchId <= highestAcknowledgedBatchId have been acknowledged so the
184-
// first unacknowledged batch after batchId will have a batchId larger than both of these
185-
// values.
186-
int nextBatchId = Math.max(batchId, highestAcknowledgedBatchId) + 1;
171+
int nextBatchId = batchId + 1;
187172

188173
// The requested batchId may still be out of range so normalize it to the start of the queue.
189174
int rawIndex = indexOfBatchId(nextBatchId);

firebase-firestore/src/main/java/com/google/firebase/firestore/local/MutationQueue.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ interface MutationQueue {
2828
/**
2929
* Starts the mutation queue, performing any initial reads that might be required to establish
3030
* invariants, etc.
31-
*
32-
* <p>After starting, the mutation queue must guarantee that the highestAcknowledgedBatchID is
33-
* less than nextBatchID. This prevents the local store from creating new batches that the
34-
* mutation queue would consider erroneously acknowledged.
3531
*/
3632
void start();
3733

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteMutationQueue.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ final class SQLiteMutationQueue implements MutationQueue {
5757
*/
5858
private int nextBatchId;
5959

60-
/**
61-
* An identifier for the highest numbered batch that has been acknowledged by the server. All
62-
* MutationBatches in this queue with batch_ids less than or equal to this value are considered to
63-
* have been acknowledged by the server.
64-
*/
65-
private int lastAcknowledgedBatchId;
66-
6760
/**
6861
* A stream token that was previously sent by the server.
6962
*
@@ -94,30 +87,18 @@ final class SQLiteMutationQueue implements MutationQueue {
9487
public void start() {
9588
loadNextBatchIdAcrossAllUsers();
9689

97-
// On restart, nextBatchId may end up lower than lastAcknowledgedBatchId since it's computed
98-
// from the queue contents, and there may be no mutations in the queue. In this case, we need
99-
// to reset lastAcknowledgedBatchId (which is safe since the queue must be empty).
100-
lastAcknowledgedBatchId = MutationBatch.UNKNOWN;
10190
int rows =
102-
db.query(
103-
"SELECT last_acknowledged_batch_id, last_stream_token "
104-
+ "FROM mutation_queues WHERE uid = ?")
91+
db.query("SELECT last_stream_token FROM mutation_queues WHERE uid = ?")
10592
.binding(uid)
10693
.first(
10794
row -> {
108-
lastAcknowledgedBatchId = row.getInt(0);
109-
lastStreamToken = ByteString.copyFrom(row.getBlob(1));
95+
lastStreamToken = ByteString.copyFrom(row.getBlob(0));
11096
});
11197

11298
if (rows == 0) {
11399
// Ensure we write a default entry in mutation_queues since loadNextBatchIdAcrossAllUsers()
114100
// depends upon every queue having an entry.
115101
writeMutationQueueMetadata();
116-
117-
} else if (lastAcknowledgedBatchId >= nextBatchId) {
118-
hardAssert(isEmpty(), "Reset nextBatchId is only possible when the queue is empty");
119-
lastAcknowledgedBatchId = MutationBatch.UNKNOWN;
120-
writeMutationQueueMetadata();
121102
}
122103
}
123104

@@ -161,11 +142,6 @@ public boolean isEmpty() {
161142

162143
@Override
163144
public void acknowledgeBatch(MutationBatch batch, ByteString streamToken) {
164-
int batchId = batch.getBatchId();
165-
hardAssert(
166-
batchId > lastAcknowledgedBatchId, "Mutation batchIds must be acknowledged in order");
167-
168-
lastAcknowledgedBatchId = batchId;
169145
lastStreamToken = checkNotNull(streamToken);
170146
writeMutationQueueMetadata();
171147
}
@@ -187,7 +163,7 @@ private void writeMutationQueueMetadata() {
187163
+ "(uid, last_acknowledged_batch_id, last_stream_token) "
188164
+ "VALUES (?, ?, ?)",
189165
uid,
190-
lastAcknowledgedBatchId,
166+
-1,
191167
lastStreamToken.toByteArray());
192168
}
193169

@@ -236,9 +212,7 @@ public MutationBatch lookupMutationBatch(int batchId) {
236212
@Nullable
237213
@Override
238214
public MutationBatch getNextMutationBatchAfterBatchId(int batchId) {
239-
// All batches with batchId <= lastAcknowledgedBatchId have been acknowledged so the first
240-
// unacknowledged batch after batchID will have a batchID larger than both of these values.
241-
int nextBatchId = Math.max(batchId, lastAcknowledgedBatchId) + 1;
215+
int nextBatchId = batchId + 1;
242216

243217
return db.query(
244218
"SELECT mutations FROM mutations "

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteSchema.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ private void createV1MutationQueue() {
193193
});
194194
}
195195

196+
/** Note: as of this migration, `last_acknowledged_batch_id` is no longer used by the code. */
196197
private void removeAcknowledgedMutations() {
197198
SQLitePersistence.Query mutationQueuesQuery =
198199
new SQLitePersistence.Query(

firebase-firestore/src/test/java/com/google/firebase/firestore/local/MutationQueueTestCase.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,6 @@ public void testNextMutationBatchAfterBatchId() {
174174
assertNull(notFound);
175175
}
176176

177-
@Test
178-
public void testNextMutationBatchAfterBatchIdSkipsAcknowledgedBatches() {
179-
List<MutationBatch> batches = createBatches(3);
180-
assertEquals(
181-
batches.get(0), mutationQueue.getNextMutationBatchAfterBatchId(MutationBatch.UNKNOWN));
182-
183-
acknowledgeBatch(batches.get(0));
184-
assertEquals(
185-
batches.get(1), mutationQueue.getNextMutationBatchAfterBatchId(MutationBatch.UNKNOWN));
186-
assertEquals(
187-
batches.get(1),
188-
mutationQueue.getNextMutationBatchAfterBatchId(batches.get(0).getBatchId()));
189-
assertEquals(
190-
batches.get(2),
191-
mutationQueue.getNextMutationBatchAfterBatchId(batches.get(1).getBatchId()));
192-
}
193-
194177
@Test
195178
public void testAllMutationBatchesAffectingDocumentKey() {
196179
List<Mutation> mutations =

0 commit comments

Comments
 (0)