-
Notifications
You must be signed in to change notification settings - Fork 617
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
Changes from 6 commits
20da793
4e1f815
9a438c5
2fc9656
414b86a
e32d407
b428adc
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 |
---|---|---|
|
@@ -55,13 +55,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 +129,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 +168,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 +602,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 +676,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 +694,9 @@ private void trackLimboChange(LimboDocumentChange change) { | |
private void pumpEnqueuedLimboResolutions() { | ||
while (!enqueuedLimboResolutions.isEmpty() | ||
&& activeLimboTargetsByKey.size() < maxConcurrentLimboResolutions) { | ||
DocumentKey key = enqueuedLimboResolutions.remove(); | ||
Iterator<DocumentKey> it = enqueuedLimboResolutions.iterator(); | ||
DocumentKey key = it.next(); | ||
it.remove(); | ||
int limboTargetId = targetIdGenerator.nextId(); | ||
activeLimboResolutionsByTarget.put(limboTargetId, new LimboResolution(key)); | ||
activeLimboTargetsByKey.put(key, limboTargetId); | ||
|
@@ -708,15 +710,15 @@ private void pumpEnqueuedLimboResolutions() { | |
} | ||
|
||
@VisibleForTesting | ||
public Map<DocumentKey, Integer> getActiveLimboDocumentResolutions() { | ||
public HashMap<DocumentKey, Integer> getActiveLimboDocumentResolutions() { | ||
// Make a defensive copy as the Map continues to be modified. | ||
return new HashMap<>(activeLimboTargetsByKey); | ||
return new HashMap(activeLimboTargetsByKey); | ||
} | ||
|
||
@VisibleForTesting | ||
public Queue<DocumentKey> getEnqueuedLimboDocumentResolutions() { | ||
// Make a defensive copy as the Queue continues to be modified. | ||
return new ArrayDeque<>(enqueuedLimboResolutions); | ||
public LinkedHashSet<DocumentKey> getEnqueuedLimboDocumentResolutions() { | ||
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. Super nit (here and above): We usually try to use more generic types in our return types, as this allows us to change the implementation without changing the callsites. If the callsites don't require the Please note that this might not apply if 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. |
||
// Make a defensive copy as the LinkedHashSet continues to be modified. | ||
return new LinkedHashSet(enqueuedLimboResolutions); | ||
} | ||
|
||
public void handleCredentialChange(User user) { | ||
|
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.
Looks like you need to import
Iterator
.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.