Skip to content

Store per-user state for indexes #3153

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 7 commits into from
Nov 23, 2021

Conversation

schmidt-sebastian
Copy link
Contributor

This PR moves the "how updated is the index?" state from the index table to a per-user table and adds a sequence number. This allows the backfiller to process all index entries independently for each user.

@google-cla google-cla bot added the cla: yes Override cla label Nov 19, 2021
@google-oss-bot
Copy link
Contributor

google-oss-bot commented Nov 19, 2021

Coverage Report

Affected SDKs

  • firebase-firestore

    SDK overall coverage changed from 44.99% (a8f8dd9) to 45.09% (fef3bbbe) by +0.10%.

    Click to show coverage changes in 12 files.
    Filename Base (a8f8dd9) Head (fef3bbbe) Diff
    AutoValue_FieldIndex.java 55.56% 58.33% +2.78%
    AutoValue_FieldIndex_IndexState.java ? 45.45% ?
    DeleteMutation.java 90.00% 95.00% +5.00%
    FirebaseFirestore.java 37.71% 37.50% -0.21%
    Index.java 25.92% 26.41% +0.49%
    IndexBackfiller.java 75.31% 77.78% +2.47%
    LocalSerializer.java 90.73% 92.72% +1.99%
    MemoryIndexManager.java 66.67% 64.29% -2.38%
    PatchMutation.java 98.39% 100.00% +1.61%
    SQLiteIndexManager.java 95.71% 99.33% +3.62%
    SQLiteRemoteDocumentCache.java 95.65% 95.83% +0.18%
    SQLiteSchema.java 96.31% 96.27% -0.05%

Test Logs

Notes

HTML coverage reports can be produced locally with ./gradlew <product>:checkCoverage.
Report files are located at <product-build-dir>/reports/jacoco/.

Head commit (fef3bbbe) is created by Prow via merging commits: a8f8dd9 8152115.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Nov 19, 2021

Binary Size Report

Affected SDKs

  • firebase-firestore

    Type Base (a8f8dd9) Head (fef3bbbe) Diff
    aar 1.22 MB 1.23 MB +2.71 kB (+0.2%)
    apk (release) 3.32 MB 3.32 MB +352 B (+0.0%)

Test Logs

Notes

Head commit (fef3bbbe) is created by Prow via merging commits: a8f8dd9 8152115.

Copy link

@thebrianchen thebrianchen left a comment

Choose a reason for hiding this comment

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

First pass. Haven't looked at the tests yet, but had some initial feedback for the implementation + some questions.

@@ -340,7 +339,8 @@ public FirebaseApp getApp() {
}
}

parsedIndices.add(FieldIndex.create(-1, collectionGroup, segments, SnapshotVersion.NONE));
parsedIndices.add(
FieldIndex.create(-1, collectionGroup, segments, FieldIndex.IndexState.DEFAULT));

Choose a reason for hiding this comment

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

nit: can we use a static global var for -1?

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.

@@ -131,71 +135,87 @@ public Results backfill() {
return persistence.runTransaction(
"Backfill Indexes",
() -> {
// TODO(indexing): Handle field indexes that are removed by the user.

Choose a reason for hiding this comment

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

Did you mean to remove this TODO? It seems like we haven't handled deleting field indexes yet.

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 was handled in another PR: #3118

return oldestDocuments.size();
}

/** Returns the new read time for the index. */

Choose a reason for hiding this comment

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

nit: add comment that documents input needs to be sorted by read time.

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

() -> {
// TODO(indexing): Do we need to store a different update time per user? We need to ensure
// that we index mutations entries for all users.
// Global configuration for all existing indices

Choose a reason for hiding this comment

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

s/indices/field indexes

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

+ "FROM index_state WHERE uid = ?")
.forEach(
row -> {
int indexId = row.getInt(1);

Choose a reason for hiding this comment

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

row.getInt(0) maps to indexid

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching. Fixed and tested.

Copy link

@thebrianchen thebrianchen left a comment

Choose a reason for hiding this comment

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

lgtm, thanks for explaining some of the questions i had

db.execute(
"REPLACE INTO index_state (index_id, uid, sequence_number, "
+ "read_time_seconds, read_time_nanos) VALUES(?, ?, ?, ?, ?)",
collectionGroup,

Choose a reason for hiding this comment

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

Should we be passing in updatedIndex.getIndexId() instead of collectionGroup here? If so, please add test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Holy !$#1! So there was a test for this that should have caught this - the testChangeUser(). The only problem is that it called updateCollectionGroup with the first index. When we then call start() we call row.getInt(). The value is actually coll1, but getInt() returns 0, which happens to be the right value. I changed the test to modify the second index instead.

Thanks so much for spotting!

assertEquals("coll3", collectionGroups.get(0));
assertEquals("coll1", collectionGroups.get(1));
assertEquals("coll2", collectionGroups.get(2));
collectionGroup = indexManager.getNextCollectionGroupToUpdate();

Choose a reason for hiding this comment

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

nit: remove outdated comment

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

FieldIndex fieldIndex =
fieldIndex(
collectionGroup,
-1,

Choose a reason for hiding this comment

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

nit: use FieldIndex const values here and below

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

assertEquals(2, collectionGroups.size());
assertEquals("coll1", collectionGroups.get(0));
assertEquals("coll2", collectionGroups.get(1));
indexManager.addFieldIndex(

Choose a reason for hiding this comment

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

nit: update test name to reflect this test

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

@schmidt-sebastian
Copy link
Contributor Author

/test device-check-changed

@google-oss-bot
Copy link
Contributor

@schmidt-sebastian: The following test failed, say /retest to rerun them all:

Test name Commit Details Rerun command
device-check-changed 8152115 link /test device-check-changed

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@schmidt-sebastian schmidt-sebastian merged commit d8fedfe into master Nov 23, 2021
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/collectiongroupupdatetime branch November 23, 2021 18:03
@firebase firebase locked and limited conversation to collaborators Dec 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants