Skip to content

Encapsulate scheduling in an inner class #111

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 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -54,7 +54,6 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
Expand All @@ -65,11 +64,6 @@ public final class FirestoreClient implements RemoteStore.RemoteStoreCallback {

private static final String LOG_TAG = "FirestoreClient";

/** How long we wait to try running LRU GC after SDK initialization. */
private static final long INITIAL_GC_DELAY_MS = TimeUnit.MINUTES.toMillis(1);
/** Minimum amount of time between GC checks, after the first one. */
private static final long REGULAR_GC_DELAY_MS = TimeUnit.MINUTES.toMillis(5);

private final DatabaseInfo databaseInfo;
private final CredentialsProvider credentialsProvider;
private final AsyncQueue asyncQueue;
Expand All @@ -81,9 +75,7 @@ public final class FirestoreClient implements RemoteStore.RemoteStoreCallback {
private EventManager eventManager;

// LRU-related
private boolean gcHasRun = false;
@Nullable private LruDelegate lruDelegate;
@Nullable private AsyncQueue.DelayedTask gcTask;
@Nullable private LruGarbageCollector.Scheduler lruScheduler;

public FirestoreClient(
final Context context,
Expand Down Expand Up @@ -145,8 +137,8 @@ public Task<Void> shutdown() {
() -> {
remoteStore.shutdown();
persistence.shutdown();
if (gcTask != null) {
gcTask.cancel();
if (lruScheduler != null) {
lruScheduler.stop();
}
});
}
Expand Down Expand Up @@ -221,6 +213,7 @@ private void initialize(Context context, User user, boolean usePersistence, long
// completes.
Logger.debug(LOG_TAG, "Initializing. user=%s", user.getUid());

LruGarbageCollector gc = null;
if (usePersistence) {
LocalSerializer serializer =
new LocalSerializer(new RemoteSerializer(databaseInfo.getDatabaseId()));
Expand All @@ -233,15 +226,19 @@ private void initialize(Context context, User user, boolean usePersistence, long
databaseInfo.getDatabaseId(),
serializer,
params);
lruDelegate = sqlitePersistence.getReferenceDelegate();
LruDelegate lruDelegate = sqlitePersistence.getReferenceDelegate();
gc = lruDelegate.getGarbageCollector();
persistence = sqlitePersistence;
scheduleLruGarbageCollection();
} else {
persistence = MemoryPersistence.createEagerGcMemoryPersistence();
}

persistence.start();
localStore = new LocalStore(persistence, user);
if (gc != null) {
lruScheduler = gc.new Scheduler(asyncQueue, localStore);
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks really strange to me. Can this be new gc.Scheduler()? I am not sure if that would work.

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 can't, but I wrapped it in a static method so I guess it's a little less weird?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Er, not static. Because it's not a static class. But a helper method.

lruScheduler.start();
}

Datastore datastore = new Datastore(databaseInfo, asyncQueue, credentialsProvider);
remoteStore = new RemoteStore(this, localStore, datastore, asyncQueue);
Expand All @@ -255,19 +252,6 @@ private void initialize(Context context, User user, boolean usePersistence, long
remoteStore.start();
}

private void scheduleLruGarbageCollection() {
long delay = gcHasRun ? REGULAR_GC_DELAY_MS : INITIAL_GC_DELAY_MS;
gcTask =
asyncQueue.enqueueAfterDelay(
AsyncQueue.TimerId.GARBAGE_COLLECTION,
delay,
() -> {
localStore.collectGarbage(lruDelegate.getGarbageCollector());
gcHasRun = true;
scheduleLruGarbageCollection();
});
}

@Override
public void handleRemoteEvent(RemoteEvent remoteEvent) {
syncEngine.handleRemoteEvent(remoteEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@

package com.google.firebase.firestore.local;

import android.support.annotation.Nullable;
import android.util.SparseArray;
import com.google.firebase.firestore.FirebaseFirestoreSettings;
import com.google.firebase.firestore.core.ListenSequence;
import com.google.firebase.firestore.util.AsyncQueue;
import com.google.firebase.firestore.util.Logger;
import java.util.Comparator;
import java.util.Locale;
import java.util.PriorityQueue;
import java.util.concurrent.TimeUnit;

/** Implements the steps for LRU garbage collection. */
public class LruGarbageCollector {
/** How long we wait to try running LRU GC after SDK initialization. */
private static final long INITIAL_GC_DELAY_MS = TimeUnit.MINUTES.toMillis(1);
/** Minimum amount of time between GC checks, after the first one. */
private static final long REGULAR_GC_DELAY_MS = TimeUnit.MINUTES.toMillis(5);

public static class Params {
private static final long COLLECTION_DISABLED = FirebaseFirestoreSettings.CACHE_SIZE_UNLIMITED;
private static final long DEFAULT_CACHE_SIZE_BYTES = 100 * 1024 * 1024; // 100mb
Expand Down Expand Up @@ -99,6 +107,43 @@ public int getDocumentsRemoved() {
}
}

public class Scheduler {
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this PR! While there is some special-casing in FirestoreClient with this PR, I don't think it's necessarily more than before, and some of it might even go away if we always run LRU (for Memory persistence for example). The encapsulation in Scheduler is really nice and hides all implementation details. Thanks.

private final AsyncQueue asyncQueue;
private final LocalStore localStore;
private boolean hasRun = false;
@Nullable private AsyncQueue.DelayedTask gcTask;

public Scheduler(AsyncQueue asyncQueue, LocalStore localStore) {
this.asyncQueue = asyncQueue;
this.localStore = localStore;
}

public void start() {
if (params.minBytesThreshold != Params.COLLECTION_DISABLED) {
scheduleGC();
}
}

public void stop() {
if (gcTask != null) {
gcTask.cancel();
}
}

private void scheduleGC() {
long delay = hasRun ? REGULAR_GC_DELAY_MS : INITIAL_GC_DELAY_MS;
gcTask =
asyncQueue.enqueueAfterDelay(
AsyncQueue.TimerId.GARBAGE_COLLECTION,
delay,
() -> {
localStore.collectGarbage(LruGarbageCollector.this);
hasRun = true;
scheduleGC();
});
}
}

private final LruDelegate delegate;
private final Params params;

Expand Down