-
Notifications
You must be signed in to change notification settings - Fork 619
Dequeue limbo resolutions when their respective queries are stopped #2404
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
20da793
Remove enqueued limbo resolutions when a query stops and avoid starti…
dconeybe 4e1f815
Add change log entry
dconeybe 9a438c5
Add a link to the PR in the change log entry
dconeybe 2fc9656
Code format via ./gradlew :firebase-firestore:googleJavaFormat
dconeybe 414b86a
SyncEngine.java: Clean up iterator usage in `pumpEnqueuedLimboResolut…
dconeybe e32d407
Apply code review feedback.
dconeybe b428adc
SyncEngine.java: Add missing import of java.util.Iterator and change …
dconeybe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
import androidx.annotation.VisibleForTesting; | ||
import com.google.android.gms.tasks.Task; | ||
import com.google.android.gms.tasks.TaskCompletionSource; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
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. One of our goals is to eventually kick out Guava from our dependencies. Can you find another solution? 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. |
||
import com.google.firebase.database.collection.ImmutableSortedMap; | ||
import com.google.firebase.database.collection.ImmutableSortedSet; | ||
import com.google.firebase.firestore.FirebaseFirestoreException; | ||
|
@@ -55,13 +57,12 @@ | |
import com.google.firebase.firestore.util.Util; | ||
import io.grpc.Status; | ||
import java.io.IOException; | ||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Queue; | ||
import java.util.Set; | ||
|
||
/** | ||
|
@@ -130,7 +131,7 @@ interface SyncEngineCallback { | |
* The keys of documents that are in limbo for which we haven't yet started a limbo resolution | ||
* query. | ||
*/ | ||
private final Queue<DocumentKey> enqueuedLimboResolutions; | ||
private final LinkedHashSet<DocumentKey> enqueuedLimboResolutions; | ||
|
||
/** Keeps track of the target ID for each document that is in limbo with an active target. */ | ||
private final Map<DocumentKey, Integer> activeLimboTargetsByKey; | ||
|
@@ -169,7 +170,7 @@ public SyncEngine( | |
queryViewsByQuery = new HashMap<>(); | ||
queriesByTarget = new HashMap<>(); | ||
|
||
enqueuedLimboResolutions = new ArrayDeque<>(); | ||
enqueuedLimboResolutions = new LinkedHashSet<>(); | ||
activeLimboTargetsByKey = new HashMap<>(); | ||
activeLimboResolutionsByTarget = new HashMap<>(); | ||
limboDocumentRefs = new ReferenceSet(); | ||
|
@@ -603,6 +604,7 @@ private void removeAndCleanupTarget(int targetId, Status status) { | |
} | ||
|
||
private void removeLimboTarget(DocumentKey key) { | ||
enqueuedLimboResolutions.remove(key); | ||
// It's possible that the target already got removed because the query failed. In that case, | ||
// the key won't exist in `limboTargetsByKey`. Only do the cleanup if we still have the target. | ||
Integer targetId = activeLimboTargetsByKey.get(key); | ||
|
@@ -676,7 +678,7 @@ private void updateTrackedLimboDocuments(List<LimboDocumentChange> limboChanges, | |
|
||
private void trackLimboChange(LimboDocumentChange change) { | ||
DocumentKey key = change.getKey(); | ||
if (!activeLimboTargetsByKey.containsKey(key)) { | ||
if (!activeLimboTargetsByKey.containsKey(key) && !enqueuedLimboResolutions.contains(key)) { | ||
Logger.debug(TAG, "New document in limbo: %s", key); | ||
enqueuedLimboResolutions.add(key); | ||
pumpEnqueuedLimboResolutions(); | ||
|
@@ -694,7 +696,8 @@ private void trackLimboChange(LimboDocumentChange change) { | |
private void pumpEnqueuedLimboResolutions() { | ||
while (!enqueuedLimboResolutions.isEmpty() | ||
&& activeLimboTargetsByKey.size() < maxConcurrentLimboResolutions) { | ||
DocumentKey key = enqueuedLimboResolutions.remove(); | ||
DocumentKey key = enqueuedLimboResolutions.iterator().next(); | ||
enqueuedLimboResolutions.remove(key); | ||
dconeybe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int limboTargetId = targetIdGenerator.nextId(); | ||
activeLimboResolutionsByTarget.put(limboTargetId, new LimboResolution(key)); | ||
activeLimboTargetsByKey.put(key, limboTargetId); | ||
|
@@ -708,15 +711,15 @@ private void pumpEnqueuedLimboResolutions() { | |
} | ||
|
||
@VisibleForTesting | ||
public Map<DocumentKey, Integer> getActiveLimboDocumentResolutions() { | ||
public ImmutableMap<DocumentKey, Integer> getActiveLimboDocumentResolutions() { | ||
// Make a defensive copy as the Map continues to be modified. | ||
return new HashMap<>(activeLimboTargetsByKey); | ||
return ImmutableMap.copyOf(activeLimboTargetsByKey); | ||
} | ||
|
||
@VisibleForTesting | ||
public Queue<DocumentKey> getEnqueuedLimboDocumentResolutions() { | ||
// Make a defensive copy as the Queue continues to be modified. | ||
return new ArrayDeque<>(enqueuedLimboResolutions); | ||
public ImmutableSet<DocumentKey> getEnqueuedLimboDocumentResolutions() { | ||
// Make a defensive copy as the LinkedHashMap continues to be modified. | ||
return ImmutableSet.copyOf(enqueuedLimboResolutions); | ||
} | ||
|
||
public void handleCredentialChange(User user) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
s/incompletion/something that is easier to parse
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.
Done.