-
Notifications
You must be signed in to change notification settings - Fork 617
awaitPendingWrites initial revision #689
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 all commits
74603b8
9d8841e
a683e2f
86e3abf
67fec18
ec2d7d2
a4e4355
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
import com.google.android.gms.tasks.TaskCompletionSource; | ||
import com.google.android.gms.tasks.Tasks; | ||
import com.google.common.base.Function; | ||
import com.google.common.collect.Lists; | ||
import com.google.firebase.database.collection.ImmutableSortedMap; | ||
import com.google.firebase.database.collection.ImmutableSortedSet; | ||
import com.google.firebase.firestore.FirebaseFirestoreException; | ||
|
@@ -39,6 +40,7 @@ | |
import com.google.firebase.firestore.model.NoDocument; | ||
import com.google.firebase.firestore.model.SnapshotVersion; | ||
import com.google.firebase.firestore.model.mutation.Mutation; | ||
import com.google.firebase.firestore.model.mutation.MutationBatch; | ||
import com.google.firebase.firestore.model.mutation.MutationBatchResult; | ||
import com.google.firebase.firestore.remote.Datastore; | ||
import com.google.firebase.firestore.remote.RemoteEvent; | ||
|
@@ -133,6 +135,9 @@ interface SyncEngineCallback { | |
/** Stores user completion blocks, indexed by user and batch ID. */ | ||
private final Map<User, Map<Integer, TaskCompletionSource<Void>>> mutationUserCallbacks; | ||
|
||
/** Stores user callbacks waiting for all pending writes to be acknowledged. */ | ||
private final Map<Integer, List<TaskCompletionSource<Void>>> pendingWritesCallbacks; | ||
|
||
/** Used for creating the target IDs for the listens used to resolve limbo documents. */ | ||
private final TargetIdGenerator targetIdGenerator; | ||
|
||
|
@@ -154,6 +159,8 @@ public SyncEngine(LocalStore localStore, RemoteStore remoteStore, User initialUs | |
mutationUserCallbacks = new HashMap<>(); | ||
targetIdGenerator = TargetIdGenerator.forSyncEngine(); | ||
currentUser = initialUser; | ||
|
||
pendingWritesCallbacks = new HashMap<>(); | ||
} | ||
|
||
public void setCallback(SyncEngineCallback callback) { | ||
|
@@ -407,6 +414,8 @@ public void handleSuccessfulWrite(MutationBatchResult mutationBatchResult) { | |
// they consistently happen before listen events. | ||
notifyUser(mutationBatchResult.getBatch().getBatchId(), /*status=*/ null); | ||
|
||
resolvePendingWriteTasks(mutationBatchResult.getBatch().getBatchId()); | ||
|
||
ImmutableSortedMap<DocumentKey, MaybeDocument> changes = | ||
localStore.acknowledgeBatch(mutationBatchResult); | ||
|
||
|
@@ -427,9 +436,63 @@ public void handleRejectedWrite(int batchId, Status status) { | |
// they consistently happen before listen events. | ||
notifyUser(batchId, status); | ||
|
||
resolvePendingWriteTasks(batchId); | ||
|
||
emitNewSnapsAndNotifyLocalStore(changes, /*remoteEvent=*/ null); | ||
} | ||
|
||
/** | ||
* Takes a snapshot of current mutation queue, and register a user task which will resolve when | ||
* all those mutations are either accepted or rejected by the server. | ||
*/ | ||
public void registerPendingWritesTask(TaskCompletionSource<Void> userTask) { | ||
if (!remoteStore.canUseNetwork()) { | ||
Logger.debug( | ||
TAG, | ||
"The network is disabled. The task returned by 'awaitPendingWrites()' will not " | ||
+ "complete until the network is enabled."); | ||
} | ||
|
||
int largestPendingBatchId = localStore.getHighestUnacknowledgedBatchId(); | ||
|
||
if (largestPendingBatchId == MutationBatch.UNKNOWN) { | ||
// Complete the task right away if there is no pending writes at the moment. | ||
userTask.setResult(null); | ||
return; | ||
} | ||
|
||
if (pendingWritesCallbacks.containsKey(largestPendingBatchId)) { | ||
pendingWritesCallbacks.get(largestPendingBatchId).add(userTask); | ||
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 think you don't need a multi-map for this case. You should be able to do the following:
Note that this introduces a dependency on other listeners that a user might already have added to the task, but that is likely 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. IMHO., i found this harder to understand, and i don't immediately see benefits of chaining tasks this way. I can be wrong though, but I'd like to see if more people find it better this way. 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. It's fine to stick with the multi-map. |
||
} else { | ||
pendingWritesCallbacks.put(largestPendingBatchId, Lists.newArrayList(userTask)); | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
/** Resolves tasks waiting for this batch id to get acknowledged by server, if there are any. */ | ||
private void resolvePendingWriteTasks(int batchId) { | ||
if (pendingWritesCallbacks.containsKey(batchId)) { | ||
for (TaskCompletionSource<Void> task : pendingWritesCallbacks.get(batchId)) { | ||
task.setResult(null); | ||
} | ||
|
||
pendingWritesCallbacks.remove(batchId); | ||
} | ||
} | ||
|
||
private void failOutstandingPendingWritesAwaitingTasks() { | ||
for (Map.Entry<Integer, List<TaskCompletionSource<Void>>> entry : | ||
pendingWritesCallbacks.entrySet()) { | ||
for (TaskCompletionSource<Void> task : entry.getValue()) { | ||
task.setException( | ||
new FirebaseFirestoreException( | ||
"'waitForPendingWrites' task is cancelled due to User change.", | ||
FirebaseFirestoreException.Code.CANCELLED)); | ||
} | ||
} | ||
|
||
pendingWritesCallbacks.clear(); | ||
} | ||
|
||
/** Resolves the task corresponding to this write result. */ | ||
private void notifyUser(int batchId, @Nullable Status status) { | ||
Map<Integer, TaskCompletionSource<Void>> userTasks = mutationUserCallbacks.get(currentUser); | ||
|
@@ -562,6 +625,8 @@ public void handleCredentialChange(User user) { | |
currentUser = user; | ||
|
||
if (userChanged) { | ||
// Fails tasks waiting for pending writes requested by previous user. | ||
failOutstandingPendingWritesAwaitingTasks(); | ||
// Notify local store and emit any resulting events from swapping out the mutation queue. | ||
ImmutableSortedMap<DocumentKey, MaybeDocument> changes = localStore.handleUserChange(user); | ||
emitNewSnapsAndNotifyLocalStore(changes, /*remoteEvent=*/ null); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -282,6 +282,14 @@ public ImmutableSortedMap<DocumentKey, MaybeDocument> rejectBatch(int batchId) { | |
}); | ||
} | ||
|
||
/** | ||
* Returns the largest (latest) batch id in mutation queue that is pending server response. | ||
* Returns {@link MutationBatch#UNKNOWN} if the queue is empty. | ||
*/ | ||
public int getHighestUnacknowledgedBatchId() { | ||
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 would advocate for being lazy here and name this consistently between the mutation batch API and the local store API. 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. Done. |
||
return mutationQueue.getHighestUnacknowledgedBatchId(); | ||
} | ||
|
||
/** Returns the last recorded stream token for the current user. */ | ||
public ByteString getLastStreamToken() { | ||
return mutationQueue.getLastStreamToken(); | ||
|
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.
I still think we should move this comment to a doc (it could be the API proposal doc) and do some collaborative editing:
pending writes that existed at the time of calling
could be clearer.written to the server
is an unusual way of phrasing the right thing.@return
section@mikelehen usually has a ton of good feedback on these comments, and his feedback tends to be much more valuable than what I can provide.
Thanks!
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.
The API proposal doc would be a good place to collaborate on this.
Note that the "written to the server" language is used in the iOS documentation already which is why I suggested it: https://github.com/firebase/firebase-ios-sdk/blob/master/Firestore/Source/Public/FIRDocumentReference.h#L117. I'm definitely open to finding a better way to succinctly describe this.
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.
Using Sebastian's version now, looks great.