Skip to content
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

Memory LRU GC #6943

Merged
merged 46 commits into from
Apr 18, 2023
Merged

Memory LRU GC #6943

merged 46 commits into from
Apr 18, 2023

Conversation

wu-hui
Copy link
Contributor

@wu-hui wu-hui commented Jan 11, 2023

Googlers see: go/firestore-memory-lru

@changeset-bot
Copy link

changeset-bot bot commented Jan 11, 2023

🦋 Changeset detected

Latest commit: 85483bf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
firebase Minor
@firebase/firestore Minor
@firebase/firestore-compat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Jan 11, 2023

Size Report 1

Affected Products

  • @firebase/firestore

    TypeBase (1d6771e)Merge (7a96fce)Diff
    browser278 kB281 kB+3.04 kB (+1.1%)
    esm5346 kB350 kB+3.83 kB (+1.1%)
    main554 kB560 kB+5.66 kB (+1.0%)
    module278 kB281 kB+3.04 kB (+1.1%)
    react-native279 kB282 kB+3.04 kB (+1.1%)
  • bundle

    TypeBase (1d6771e)Merge (7a96fce)Diff
    firestore (Persistence)284 kB284 kB+106 B (+0.0%)
    firestore (Query Cursors)222 kB222 kB+20 B (+0.0%)
    firestore (Query)220 kB220 kB+20 B (+0.0%)
    firestore (Read data once)207 kB207 kB+20 B (+0.0%)
    firestore (Realtime updates)209 kB209 kB+20 B (+0.0%)
    firestore (Transaction)190 kB190 kB+20 B (+0.0%)
    firestore (Write data)190 kB190 kB+101 B (+0.1%)
  • firebase

    TypeBase (1d6771e)Merge (7a96fce)Diff
    firebase-compat.js757 kB757 kB+24 B (+0.0%)
    firebase-firestore-compat.js324 kB324 kB+24 B (+0.0%)
    firebase-firestore.js327 kB330 kB+3.04 kB (+0.9%)

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/B53YMZxsjX.html
@google-oss-bot
Copy link
Contributor

google-oss-bot commented Jan 11, 2023

Size Analysis Report 1

This report is too large (729,651 characters) to be displayed here in a GitHub comment. Please use the below link to see the full report on Google Cloud Storage.

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/fFTP0ub9qj.html
@github-actions github-actions bot added the doc-changes PRs that affect docs label Jan 11, 2023
@wu-hui wu-hui requested a review from tom-andersen January 13, 2023 18:43
@wu-hui wu-hui assigned tom-andersen and unassigned tom-andersen Jan 13, 2023
@wu-hui wu-hui removed the request for review from tom-andersen January 16, 2023 17:47
Copy link
Contributor

@egilmorez egilmorez left a comment

Choose a reason for hiding this comment

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

A few tweaks to the public doc comments, thanks!

packages/firestore/src/api/database.ts Outdated Show resolved Hide resolved
packages/firestore/src/api/database.ts Outdated Show resolved Hide resolved
packages/firestore/src/api/database.ts Outdated Show resolved Hide resolved
@egilmorez egilmorez requested a review from markarndt February 1, 2023 16:35
@wu-hui wu-hui assigned MarkDuckworth and unassigned egilmorez Apr 13, 2023
Copy link
Contributor

@MarkDuckworth MarkDuckworth left a comment

Choose a reason for hiding this comment

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

LGTM with comments

*
* This collector tries to ensure lowest memory footprints from the SDK,
* at the risk of querying backend repeated for a document it could have
* cached locally.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this statement is misleading as it gives indication that having documents in the cache will retrieve documents stored in the cached and go to the backend when not in cache.

Consider instead "This collector tries to ensure lowest memory footprints from the SDK, at the risk of documents not being cached for offline queries or for direct queries to the cache."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


createPersistence(cfg: ComponentConfiguration): Persistence {
const lruParams =
this.cacheSizeBytes !== undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

wondering if we need a range check for this.cacheSizeBytes > 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This actually won't work because we use -1 to disable GC.

I really do not like this approach, we should do something similar to the new cache config API, using a dedicated enum branch to disable GC. Maybe something for the fixit week.

* (incoming GRPC messages), we should always schedule onto this queue. //
* This ensures all of our work is properly serialized (e.g. we don't //
* start processing a new operation while the previous one is waiting for //
* an async I/O to complete). //
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if ending // were intentional

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I hate it when my editor does this and i don't know how to stop it.

persistence: MemoryPersistence,
lruParams: LruParams | null
): MemoryLruDelegate {
return new MemoryLruDelegate(persistence, lruParams!!);
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the double exclamation at the end of lruParams!! do? I'm not familiar with the double.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It tells complier that you are sure this is a LruParams, not a null. I removed | null in this case, it was not neccessary.

withGCEnabled(gcEnabled: boolean): this {
// Ensures manual LRU GC for both memory and indexeddb persistence.
// In spec tests, GC is always manually triggered via triggerLruGC().
ensureManualLruGC(): this {
debugAssert(
!this.currentStep,
'withGCEnabled() must be called before all spec steps.'
Copy link
Contributor

Choose a reason for hiding this comment

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

Update name in error message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

.triggerLruGC(1)
.removeExpectedTargetMapping(query1)
// should get no events.
.userListens(query1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the test name and the test implementation aligned on this one? Maybe I'm not following what it's trying to assert

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, fixed.

MarkDuckworth
MarkDuckworth approved these changes Apr 14, 2023
Copy link
Contributor Author

@wu-hui wu-hui left a comment

Choose a reason for hiding this comment

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

Thank you!

*
* This collector tries to ensure lowest memory footprints from the SDK,
* at the risk of querying backend repeated for a document it could have
* cached locally.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


createPersistence(cfg: ComponentConfiguration): Persistence {
const lruParams =
this.cacheSizeBytes !== undefined
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This actually won't work because we use -1 to disable GC.

I really do not like this approach, we should do something similar to the new cache config API, using a dedicated enum branch to disable GC. Maybe something for the fixit week.

* (incoming GRPC messages), we should always schedule onto this queue. //
* This ensures all of our work is properly serialized (e.g. we don't //
* start processing a new operation while the previous one is waiting for //
* an async I/O to complete). //
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I hate it when my editor does this and i don't know how to stop it.

persistence: MemoryPersistence,
lruParams: LruParams | null
): MemoryLruDelegate {
return new MemoryLruDelegate(persistence, lruParams!!);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It tells complier that you are sure this is a LruParams, not a null. I removed | null in this case, it was not neccessary.

.triggerLruGC(1)
.removeExpectedTargetMapping(query1)
// should get no events.
.userListens(query1)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, fixed.

@wu-hui wu-hui closed this Apr 17, 2023
@wu-hui wu-hui reopened this Apr 17, 2023
@wu-hui
Copy link
Contributor Author

wu-hui commented Apr 17, 2023

@hsubox76

Please take a look, I need owner's approval for something outside of firestore. I do not understand why they show up though, they seem to be generated automatically.

Check some changes under:

common/
docs-devsite/
packages/auth/api-extractor.json

They are all auth-related.

@wu-hui wu-hui requested a review from hsubox76 April 17, 2023 15:46
Copy link
Contributor

@hsubox76 hsubox76 left a comment

Choose a reason for hiding this comment

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

  • Sorry about auth.api.md. I made a PR to ensure it's reverted after docgen is run but this branch may have been created before that: Simplify check for reference doc changes #7014 You can just revert it.
  • Can you add a changeset? It should be @firebase/firestore minor and firebase minor (the yarn changeset CLI should help you add the first automatically, the second has to be added manually)
@wu-hui
Copy link
Contributor Author

wu-hui commented Apr 17, 2023

Details

Done, thanks!

@@ -0,0 +1,6 @@
---
'firebase': patch
Copy link
Contributor

Choose a reason for hiding this comment

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

These should be minor as it's changing the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks.

Copy link
Contributor

@hsubox76 hsubox76 left a comment

Choose a reason for hiding this comment

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

LG!

@wu-hui wu-hui merged commit 253b998 into master Apr 18, 2023
@wu-hui wu-hui deleted the wuandy/LruGC branch April 18, 2023 18:32
@@ -1241,6 +1257,6 @@ export function client(
withGcEnabled?: boolean
): MultiClientSpecBuilder {
const specBuilder = new MultiClientSpecBuilder();
specBuilder.withGCEnabled(withGcEnabled === true);
specBuilder.ensureManualLruGC();
Copy link
Contributor

@dconeybe dconeybe Apr 19, 2023

Choose a reason for hiding this comment

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

Shouldn't this be:

if (withGcEnabled !== true) {
  specBuilder.ensureManualLruGC();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, thanks for checking this.

I intended to delete withGcEnabled all together, because multitab will always have lru gc. I just forgot to delete it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

see: #7234

@google-oss-bot google-oss-bot mentioned this pull request Apr 25, 2023
@firebase firebase locked and limited conversation to collaborators Jun 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
doc-changes PRs that affect docs
8 participants