Skip to content

Random changes for Indexing #3169

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 4 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public void tearDown() {
}

@Test
public void testCanConfigureIndices() throws ExecutionException, InterruptedException {
public void testCanConfigureIndexes() throws ExecutionException, InterruptedException {
FirebaseFirestore db = testFirestore();
Task<Void> indexTask =
db.configureIndices(
db.setIndexConfiguration(
"{\n"
+ " \"indexes\": [\n"
+ " {\n"
Expand Down Expand Up @@ -89,7 +89,7 @@ public void testCanConfigureIndices() throws ExecutionException, InterruptedExce
public void testBadJsonDoesNotCrashClient() {
FirebaseFirestore db = testFirestore();

Assert.assertThrows(IllegalArgumentException.class, () -> db.configureIndices("{,"));
Assert.assertThrows(IllegalArgumentException.class, () -> db.setIndexConfiguration("{,"));
}

@Test
Expand All @@ -98,7 +98,7 @@ public void testBadIndexDoesNotCrashClient() {
Assert.assertThrows(
IllegalArgumentException.class,
() ->
db.configureIndices(
db.setIndexConfiguration(
"{\n"
+ " \"indexes\": [\n"
+ " {\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,14 @@ public FirebaseApp getApp() {
* automatically start using the index once the index entries have been written.
*
* <p>The method accepts the JSON format exported by the Firebase CLI (`firebase
* firestore:indexes`). If the JSON format is invalid, this method rejects the returned task.
* firestore:indexes`). If the JSON format is invalid, this method throws an exception.
*
* @param json The JSON format exported by the Firebase CLI.
* @return A task that resolves once all indices are successfully configured.
* @throws IllegalArgumentException if the JSON format is invalid
*/
@VisibleForTesting
Task<Void> configureIndices(String json) {
Task<Void> setIndexConfiguration(String json) {
ensureClientConfigured();

// Preconditions.checkState(BuildConfig.ENABLE_INDEXING, "Indexing support is not yet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.firebase.firestore.model.ResourcePath;
import com.google.firebase.firestore.model.SnapshotVersion;
import com.google.firebase.firestore.util.AsyncQueue;
import com.google.firebase.firestore.util.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -37,6 +38,8 @@

/** Implements the steps for backfilling indexes. */
public class IndexBackfiller {
private static final String LOG_TAG = "IndexBackfiller";

/** How long we wait to try running index backfill after SDK initialization. */
private static final long INITIAL_BACKFILL_DELAY_MS = TimeUnit.SECONDS.toMillis(15);
/** Minimum amount of time between backfill checks, after the first one. */
Expand Down Expand Up @@ -65,29 +68,6 @@ public void setIndexManager(IndexManager indexManager) {
this.indexManager = indexManager;
}

public static class Results {
private final boolean hasRun;

private final int documentsProcessed;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI I don't think we will ever differentiate between documentsWritten and documentsDeleted as they are handled interchangeably by IndexManager.


static IndexBackfiller.Results DidNotRun() {
return new IndexBackfiller.Results(/* hasRun= */ false, 0);
}

Results(boolean hasRun, int documentsProcessed) {
this.hasRun = hasRun;
this.documentsProcessed = documentsProcessed;
}

public boolean hasRun() {
return hasRun;
}

public int getDocumentsProcessed() {
return documentsProcessed;
}
}

public class Scheduler implements com.google.firebase.firestore.local.Scheduler {
private boolean hasRun = false;
@Nullable private AsyncQueue.DelayedTask backfillTask;
Expand Down Expand Up @@ -118,7 +98,8 @@ private void scheduleBackfill() {
AsyncQueue.TimerId.INDEX_BACKFILL,
delay,
() -> {
backfill();
int documentsProcessed = backfill();
Logger.debug(LOG_TAG, "Documents written: %s", documentsProcessed);
hasRun = true;
scheduleBackfill();
});
Expand All @@ -129,15 +110,12 @@ public Scheduler getScheduler() {
return scheduler;
}

public Results backfill() {
/** Runs a single backfill operation and returns the number of documents processed. */
public int backfill() {
hardAssert(localDocumentsView != null, "setLocalDocumentsView() not called");
hardAssert(indexManager != null, "setIndexManager() not called");
return persistence.runTransaction(
"Backfill Indexes",
() -> {
int documentsProcessed = writeIndexEntries(localDocumentsView);
return new Results(/* hasRun= */ true, documentsProcessed);
});
"Backfill Indexes", () -> writeIndexEntries(localDocumentsView));
}

/** Writes index entries until the cap is reached. Returns the number of documents processed. */
Expand All @@ -149,6 +127,7 @@ private int writeIndexEntries(LocalDocumentsView localDocumentsView) {
if (collectionGroup == null || processedCollectionGroups.contains(collectionGroup)) {
break;
}
Logger.debug(LOG_TAG, "Processing collection: %s", collectionGroup);
documentsRemaining -=
writeEntriesForCollectionGroup(localDocumentsView, collectionGroup, documentsRemaining);
processedCollectionGroups.add(collectionGroup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public final class LocalStore implements BundleCallback {
private LocalDocumentsView localDocuments;

/** Performs queries over the localDocuments (and potentially maintains indexes). */
private QueryEngine queryEngine;
private final QueryEngine queryEngine;

/** The set of document references maintained by any local views. */
private final ReferenceSet localViewReferences;
Expand Down Expand Up @@ -170,6 +170,7 @@ public LocalStore(
this.queryEngine = queryEngine;
this.indexBackfiller = indexBackfiller;
queryEngine.setLocalDocumentsView(localDocuments);
queryEngine.setIndexManager(indexManager);

localViewReferences = new ReferenceSet();
persistence.getReferenceDelegate().setInMemoryPins(localViewReferences);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,11 @@ private void createV1RemoteDocumentCache() {
* <p>The `index_configuration` table holds the configuration for all indices. Entries in this
* table apply for all users. It is not possible to only enable indices for a subset of users.
*
* <p>The `index_state` table holds backfill information on each index that stores when it was
* last updated.
*
* <p>The `index_entries` table holds the index values themselves. An index value is created for
* each field combination that matches a configured index. If there are pending mutations that
* affect an indexed field, an additional index entry is created per mutated field.
* each field combination that matches a configured index.
*/
private void createFieldIndex() {
ifTablesDontExist(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public void testBackfillWritesLatestReadTimeToFieldIndexOnCompletion() {
addDoc("coll1/docA", "foo", version(10));
addDoc("coll2/docA", "bar", version(20));

IndexBackfiller.Results results = backfiller.backfill();
assertEquals(2, results.getDocumentsProcessed());
int documentsProcessed = backfiller.backfill();
assertEquals(2, documentsProcessed);

FieldIndex fieldIndex1 = indexManager.getFieldIndexes("coll1").iterator().next();
FieldIndex fieldIndex2 = indexManager.getFieldIndexes("coll2").iterator().next();
Expand All @@ -112,8 +112,8 @@ public void testBackfillWritesLatestReadTimeToFieldIndexOnCompletion() {
addDoc("coll2/docB", "bar", version(60));
addDoc("coll2/docC", "bar", version(60, 10));

results = backfiller.backfill();
assertEquals(4, results.getDocumentsProcessed());
documentsProcessed = backfiller.backfill();
assertEquals(4, documentsProcessed);

fieldIndex1 = indexManager.getFieldIndexes("coll1").iterator().next();
fieldIndex2 = indexManager.getFieldIndexes("coll2").iterator().next();
Expand All @@ -127,17 +127,17 @@ public void testBackfillFetchesDocumentsAfterEarliestReadTime() {

// Documents before earliest read time should not be fetched.
addDoc("coll1/docA", "foo", version(9));
IndexBackfiller.Results results = backfiller.backfill();
assertEquals(0, results.getDocumentsProcessed());
int documentsProcessed = backfiller.backfill();
assertEquals(0, documentsProcessed);

// Read time of index should not change.
Iterator<FieldIndex> it = indexManager.getFieldIndexes("coll1").iterator();
assertEquals(version(10), it.next().getIndexState().getReadTime());

// Documents that are after the earliest read time but before field index read time are fetched.
addDoc("coll1/docB", "boo", version(19));
results = backfiller.backfill();
assertEquals(1, results.getDocumentsProcessed());
documentsProcessed = backfiller.backfill();
assertEquals(1, documentsProcessed);

// Field indexes should now hold the latest read time
it = indexManager.getFieldIndexes("coll1").iterator();
Expand All @@ -153,8 +153,8 @@ public void testBackfillWritesIndexEntries() {
addDoc("coll2/docA", "bar", version(10));
addDoc("coll2/docB", "car", version(10));

IndexBackfiller.Results results = backfiller.backfill();
assertEquals(4, results.getDocumentsProcessed());
int documentsProcessed = backfiller.backfill();
assertEquals(4, documentsProcessed);
}

@Test
Expand All @@ -167,13 +167,13 @@ public void testBackfillWritesOldestDocumentFirst() {
addDoc("coll1/docB", "foo", version(3));
addDoc("coll1/docC", "foo", version(10));

IndexBackfiller.Results results = backfiller.backfill();
assertEquals(2, results.getDocumentsProcessed());
int documentsProcessed = backfiller.backfill();
assertEquals(2, documentsProcessed);

verifyQueryResults("coll1", "coll1/docA", "coll1/docB");

results = backfiller.backfill();
assertEquals(1, results.getDocumentsProcessed());
documentsProcessed = backfiller.backfill();
assertEquals(1, documentsProcessed);

verifyQueryResults("coll1", "coll1/docA", "coll1/docB", "coll1/docC");
}
Expand All @@ -192,8 +192,8 @@ public void testBackfillUpdatesCollectionGroups() {
String collectionGroup = indexManager.getNextCollectionGroupToUpdate();
assertEquals("coll1", collectionGroup);

IndexBackfiller.Results results = backfiller.backfill();
assertEquals(2, results.getDocumentsProcessed());
int documentsProcessed = backfiller.backfill();
assertEquals(2, documentsProcessed);

// Check that coll1 was backfilled and that coll2 is next
collectionGroup = indexManager.getNextCollectionGroupToUpdate();
Expand All @@ -217,8 +217,8 @@ public void testBackfillPrioritizesNewCollectionGroups() {
// Check that coll3 is the next collection ID the backfiller should update
assertEquals("coll3", indexManager.getNextCollectionGroupToUpdate());

IndexBackfiller.Results results = backfiller.backfill();
assertEquals(1, results.getDocumentsProcessed());
int documentsProcessed = backfiller.backfill();
assertEquals(1, documentsProcessed);

verifyQueryResults("coll3", "coll3/doc");
}
Expand All @@ -233,8 +233,8 @@ public void testBackfillWritesUntilCap() {
addDoc("coll2/docA", "foo", version(30));
addDoc("coll2/docA", "foo", version(40));

IndexBackfiller.Results results = backfiller.backfill();
assertEquals(3, results.getDocumentsProcessed());
int documentsProcessed = backfiller.backfill();
assertEquals(3, documentsProcessed);

verifyQueryResults("coll1", "coll1/docA", "coll1/docB");
verifyQueryResults("coll2", "coll2/docA");
Expand Down