Skip to content

Commit 2b2e79c

Browse files
committed
SyncEngine.java: Replace contents with the fix in #2404
1 parent 6e05ab1 commit 2b2e79c

File tree

1 file changed

+14
-36
lines changed
  • firebase-firestore/src/main/java/com/google/firebase/firestore/core

1 file changed

+14
-36
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/core/SyncEngine.java

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import androidx.annotation.VisibleForTesting;
2222
import com.google.android.gms.tasks.Task;
2323
import com.google.android.gms.tasks.TaskCompletionSource;
24+
import com.google.common.collect.ImmutableMap;
25+
import com.google.common.collect.ImmutableSet;
2426
import com.google.firebase.database.collection.ImmutableSortedMap;
2527
import com.google.firebase.database.collection.ImmutableSortedSet;
2628
import com.google.firebase.firestore.FirebaseFirestoreException;
@@ -55,13 +57,12 @@
5557
import com.google.firebase.firestore.util.Util;
5658
import io.grpc.Status;
5759
import java.io.IOException;
58-
import java.util.ArrayDeque;
5960
import java.util.ArrayList;
6061
import java.util.Collections;
6162
import java.util.HashMap;
63+
import java.util.LinkedHashSet;
6264
import java.util.List;
6365
import java.util.Map;
64-
import java.util.Queue;
6566
import java.util.Set;
6667

6768
/**
@@ -130,7 +131,7 @@ interface SyncEngineCallback {
130131
* The keys of documents that are in limbo for which we haven't yet started a limbo resolution
131132
* query.
132133
*/
133-
private final Queue<DocumentKey> enqueuedLimboResolutions;
134+
private final LinkedHashSet<DocumentKey> enqueuedLimboResolutions;
134135

135136
/** Keeps track of the target ID for each document that is in limbo with an active target. */
136137
private final Map<DocumentKey, Integer> activeLimboTargetsByKey;
@@ -169,7 +170,7 @@ public SyncEngine(
169170
queryViewsByQuery = new HashMap<>();
170171
queriesByTarget = new HashMap<>();
171172

172-
enqueuedLimboResolutions = new ArrayDeque<>();
173+
enqueuedLimboResolutions = new LinkedHashSet<>();
173174
activeLimboTargetsByKey = new HashMap<>();
174175
activeLimboResolutionsByTarget = new HashMap<>();
175176
limboDocumentRefs = new ReferenceSet();
@@ -321,8 +322,6 @@ public void handleRemoteEvent(RemoteEvent event) {
321322
TargetChange targetChange = entry.getValue();
322323
LimboResolution limboResolution = activeLimboResolutionsByTarget.get(targetId);
323324
if (limboResolution != null) {
324-
Logger.warn("zzyzx", "handleRemoteEvent() limboResolution.key=" + limboResolution.key + " targetChange.getAddedDocuments().size()=" + targetChange.getAddedDocuments().size() + " targetChange.getModifiedDocuments().size()=" + targetChange.getModifiedDocuments().size() + " targetChange.getRemovedDocuments().size()=" + targetChange.getRemovedDocuments().size());
325-
326325
// Since this is a limbo resolution lookup, it's for a single document and it could be
327326
// added, modified, or removed, but not a combination.
328327
hardAssert(
@@ -398,7 +397,6 @@ public void handleRejectedListen(int targetId, Status error) {
398397
LimboResolution limboResolution = activeLimboResolutionsByTarget.get(targetId);
399398
DocumentKey limboKey = limboResolution != null ? limboResolution.key : null;
400399
if (limboKey != null) {
401-
Logger.warn("zzyzx", "handleRejectedListen() limboKey=" + limboKey + " targetId=" + targetId);
402400
// Since this query failed, we won't want to manually unlisten to it.
403401
// So go ahead and remove it from bookkeeping.
404402
activeLimboTargetsByKey.remove(limboKey);
@@ -605,26 +603,11 @@ private void removeAndCleanupTarget(int targetId, Status status) {
605603
}
606604
}
607605

608-
private String describeKeyInActiveLimboTargetsByKey(DocumentKey key) {
609-
ArrayList<String> list = new ArrayList<>();
610-
for (DocumentKey currentKey : activeLimboTargetsByKey.keySet()) {
611-
list.add(currentKey.toString());
612-
}
613-
Collections.sort(list);
614-
return "activeLimboTargetsByKey.containsKey(key)="
615-
+ activeLimboTargetsByKey.containsKey(key)
616-
+ " (by string: "
617-
+ list.contains(key.toString())
618-
+ ") activeLimboTargetsByKey="
619-
+ list;
620-
}
621-
622606
private void removeLimboTarget(DocumentKey key) {
607+
enqueuedLimboResolutions.remove(key);
623608
// It's possible that the target already got removed because the query failed. In that case,
624609
// the key won't exist in `limboTargetsByKey`. Only do the cleanup if we still have the target.
625610
Integer targetId = activeLimboTargetsByKey.get(key);
626-
Logger.warn("zzyzx", "removeLimboTarget() start; key=" + key + " targetId=" + targetId + " " + describeKeyInActiveLimboTargetsByKey(key));
627-
enqueuedLimboResolutions.remove(key);
628611
if (targetId != null) {
629612
remoteStore.stopListening(targetId);
630613
activeLimboTargetsByKey.remove(key);
@@ -675,12 +658,10 @@ private void updateTrackedLimboDocuments(List<LimboDocumentChange> limboChanges,
675658
for (LimboDocumentChange limboChange : limboChanges) {
676659
switch (limboChange.getType()) {
677660
case ADDED:
678-
Logger.warn("zzyzx", "updateTrackedLimboDocuments() ADDED " + limboChange.getKey());
679661
limboDocumentRefs.addReference(limboChange.getKey(), targetId);
680662
trackLimboChange(limboChange);
681663
break;
682664
case REMOVED:
683-
Logger.warn("zzyzx", "updateTrackedLimboDocuments() REMOVED " + limboChange.getKey());
684665
Logger.debug(TAG, "Document no longer in limbo: %s", limboChange.getKey());
685666
DocumentKey limboDocKey = limboChange.getKey();
686667
limboDocumentRefs.removeReference(limboDocKey, targetId);
@@ -697,8 +678,7 @@ private void updateTrackedLimboDocuments(List<LimboDocumentChange> limboChanges,
697678

698679
private void trackLimboChange(LimboDocumentChange change) {
699680
DocumentKey key = change.getKey();
700-
Logger.warn("zzyzx", "trackLimboChange() for " + key + ": activeLimboTargetsByKey.get(key)==" + activeLimboTargetsByKey.get(key));
701-
if (!activeLimboTargetsByKey.containsKey(key)) {
681+
if (!activeLimboTargetsByKey.containsKey(key) && !enqueuedLimboResolutions.contains(key)) {
702682
Logger.debug(TAG, "New document in limbo: %s", key);
703683
enqueuedLimboResolutions.add(key);
704684
pumpEnqueuedLimboResolutions();
@@ -714,12 +694,11 @@ private void trackLimboChange(LimboDocumentChange change) {
714694
* https://github.com/firebase/firebase-js-sdk/issues/2683.
715695
*/
716696
private void pumpEnqueuedLimboResolutions() {
717-
Logger.warn("zzyzx", "pumpEnqueuedLimboResolutions() start; enqueuedLimboResolutions.size()=" + enqueuedLimboResolutions.size() + " activeLimboTargetsByKey.size()=" + activeLimboTargetsByKey.size());
718697
while (!enqueuedLimboResolutions.isEmpty()
719698
&& activeLimboTargetsByKey.size() < maxConcurrentLimboResolutions) {
720-
DocumentKey key = enqueuedLimboResolutions.remove();
699+
DocumentKey key = enqueuedLimboResolutions.iterator().next();
700+
enqueuedLimboResolutions.remove(key);
721701
int limboTargetId = targetIdGenerator.nextId();
722-
Logger.warn("zzyzx", "pumpEnqueuedLimboResolutions() starting listen for " + key + " (limboTargetId=" + limboTargetId + ")");
723702
activeLimboResolutionsByTarget.put(limboTargetId, new LimboResolution(key));
724703
activeLimboTargetsByKey.put(key, limboTargetId);
725704
remoteStore.listen(
@@ -729,19 +708,18 @@ private void pumpEnqueuedLimboResolutions() {
729708
ListenSequence.INVALID,
730709
QueryPurpose.LIMBO_RESOLUTION));
731710
}
732-
Logger.warn("zzyzx", "pumpEnqueuedLimboResolutions() done; enqueuedLimboResolutions.size()=" + enqueuedLimboResolutions.size() + " activeLimboTargetsByKey.size()=" + activeLimboTargetsByKey.size());
733711
}
734712

735713
@VisibleForTesting
736-
public Map<DocumentKey, Integer> getActiveLimboDocumentResolutions() {
714+
public ImmutableMap<DocumentKey, Integer> getActiveLimboDocumentResolutions() {
737715
// Make a defensive copy as the Map continues to be modified.
738-
return new HashMap<>(activeLimboTargetsByKey);
716+
return ImmutableMap.copyOf(activeLimboTargetsByKey);
739717
}
740718

741719
@VisibleForTesting
742-
public Queue<DocumentKey> getEnqueuedLimboDocumentResolutions() {
743-
// Make a defensive copy as the Queue continues to be modified.
744-
return new ArrayDeque<>(enqueuedLimboResolutions);
720+
public ImmutableSet<DocumentKey> getEnqueuedLimboDocumentResolutions() {
721+
// Make a defensive copy as the LinkedHashMap continues to be modified.
722+
return ImmutableSet.copyOf(enqueuedLimboResolutions);
745723
}
746724

747725
public void handleCredentialChange(User user) {

0 commit comments

Comments
 (0)