Skip to content

fix(firestore): remote target ID mapping to avoid possible race condition on rapid subscribe and unsubscribe#8108

Open
MarkDuckworth wants to merge 2 commits intomainfrom
markduckworth/remote-store-remote-id
Open

fix(firestore): remote target ID mapping to avoid possible race condition on rapid subscribe and unsubscribe#8108
MarkDuckworth wants to merge 2 commits intomainfrom
markduckworth/remote-store-remote-id

Conversation

@MarkDuckworth
Copy link
Copy Markdown
Contributor

No description provided.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

📝 PRs merging into main branch

Our main branch should always be in a releasable state. If you are working on a larger change, or if you don't want this change to see the light of the day just yet, consider using a feature branch first, and only merge into the main branch when the code complete and ready to be released.

@google-oss-bot
Copy link
Copy Markdown
Collaborator

1 Warning
⚠️ Did you forget to add a changelog entry? (Add the 'no-changelog' label to the PR to silence this warning.)

Generated by 🚫 Danger

@MarkDuckworth
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mapping layer between SDK target IDs and session-specific remote target IDs to facilitate target reuse and improve watch stream management. Key changes include the addition of the RemoteTargetId class, specialized ID generators, and translation logic in RemoteStore and WatchChangeAggregator. Feedback highlights performance concerns in RemoteStore, specifically identifying O(N^2) complexity in raiseWatchSnapshot when processing target changes and mismatches, as well as inefficient repeated canonical ID calculations for pipelines within the listen method's loop.

Comment on lines +389 to +400
private String getPipelineCanonicalIdExceptSort(
com.google.firebase.firestore.RealtimePipeline p) {
String canonicalId = p.toString();
String[] stages = canonicalId.split("\\|");
List<String> stripped = new ArrayList<>();
for (String stage : stages) {
if (!stage.startsWith("sort")) {
stripped.add(stage);
}
}
return String.join("|", stripped);
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did this come from?

Comment on lines +374 to +381
private int generateRemoteTargetId(int sdkTargetId) {
if (sdkTargetId % 2 != 0) {
return syncEngineTargetIdGenerator.nextId();
} else {
return targetCacheTargetIdGenerator.nextId();
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return the type RemoteTargetId

Comment on lines +438 to +443
if (remoteTargetId != null) {
targetIdMapRemoteToSdk.remove(remoteTargetId);
}
remoteTargetId = RemoteTargetId.from(generateRemoteTargetId(sdkTargetId));
targetIdMapSdkToRemote.put(sdkTargetId, remoteTargetId);
targetIdMapRemoteToSdk.put(remoteTargetId, sdkTargetId);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THis appears to be ported from allocateRemoteTargetId. Keep that helper method


if (remoteTargetId != null && listenTargets.containsKey(remoteTargetId)) {
return;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this short circuit execution to the top

return String.join("|", stripped);
}

public void listen(TargetData targetData) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

port the debug logging in this method from remoteStoreListen

Comment on lines +406 to +431
if (remoteTargetId == null) {
TargetOrPipeline top = targetData.getTarget();
String topCanonicalId =
top.isPipeline()
? getPipelineCanonicalIdExceptSort(
top.pipeline$com_google_firebase_firebase_firestore())
: null;

for (Map.Entry<RemoteTargetId, TargetData> entry : listenTargets.entrySet()) {
TargetOrPipeline activeTop = entry.getValue().getTarget();
if (top.isTarget() && activeTop.isTarget()) {
if (top.target().equals(activeTop.target())) {
remoteTargetId = entry.getKey();
targetIdMapSdkToRemote.put(sdkTargetId, remoteTargetId);
break;
}
} else if (top.isPipeline() && activeTop.isPipeline()) {
if (topCanonicalId.equals(
getPipelineCanonicalIdExceptSort(
activeTop.pipeline$com_google_firebase_firebase_firestore()))) {
remoteTargetId = entry.getKey();
targetIdMapSdkToRemote.put(sdkTargetId, remoteTargetId);
break;
}
}
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document this code. What does it do, why do we need it?

TargetData remoteTargetData =
new TargetData(
targetData.getTarget(),
remoteTargetId.value(),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should TargetData be generic, like in the web SDK, so we don't have to unbox the remoteTargetId and lose our type safety

Comment on lines 487 to +490
hardAssert(
targetData != null, "stopListening called on target no currently watched: %d", targetId);
remoteTargetId != null,
"stopListening called on target not currently watched: %d",
sdkTargetId);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is duplicated. Add the debug logging from the web SDK

}
}

private void sendWatchRequest(TargetData targetData) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First param should be type TargetData<RemoteTargetId>. Then update the implementation

if (!targetData.getResumeToken().isEmpty()
|| targetData.getSnapshotVersion().compareTo(SnapshotVersion.NONE) > 0) {
int expectedCount = this.getRemoteKeysForTarget(targetData.getTargetId()).size();
int expectedCount = this.getRemoteKeysForTarget(remoteTargetId).size();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRemoteKeysForTarget takes an sdkTargetId, so you'll have to look that up first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants