From 1d8aa168d4ce54e3c7e4dfe595a3361bdb088d4f Mon Sep 17 00:00:00 2001 From: Ankita Jhawar Date: Mon, 18 May 2020 16:32:41 -0700 Subject: [PATCH 1/2] Cache FID to avoid multiple disk reads. --- .../installations/FirebaseInstallations.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java index 81546875fcb..4f5f69e167b 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java @@ -68,6 +68,8 @@ public class FirebaseInstallations implements FirebaseInstallationsApi { private final ExecutorService backgroundExecutor; private final ExecutorService networkExecutor; + private String cachedFid = null; + @GuardedBy("lock") private final List listeners = new ArrayList<>(); @@ -215,7 +217,11 @@ String getName() { public Task getId() { preConditionChecks(); TaskCompletionSource taskCompletionSource = new TaskCompletionSource<>(); - taskCompletionSource.trySetResult(doGetId()); + if (cachedFid != null) { + taskCompletionSource.trySetResult(cachedFid); + } else { + taskCompletionSource.trySetResult(doGetId()); + } return taskCompletionSource.getTask(); } @@ -454,6 +460,7 @@ private PersistedInstallationEntry registerFidWithServer(PersistedInstallationEn switch (response.getResponseCode()) { case OK: + cachedFid = response.getFid(); return prefs.withRegisteredFid( response.getFid(), response.getRefreshToken(), @@ -493,6 +500,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer( case AUTH_ERROR: // The the server refused to generate a new auth token due to bad credentials, clear the // FID to force the generation of a new one. + cachedFid = null; return prefs.withNoGeneratedFid(); default: throw new IOException(); @@ -519,7 +527,7 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio "Failed to delete a Firebase Installation.", Status.BAD_CONFIG); } } - + cachedFid = null; insertOrUpdatePrefs(entry.withNoGeneratedFid()); return null; } From 3c974c49849006bf0500283b7bb8bf77ef7f3c5a Mon Sep 17 00:00:00 2001 From: Ankita Jhawar Date: Tue, 19 May 2020 10:39:11 -0700 Subject: [PATCH 2/2] Addressing Rayo's comments. --- .../installations/FirebaseInstallations.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java index 4f5f69e167b..c9c3676f414 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java @@ -67,7 +67,8 @@ public class FirebaseInstallations implements FirebaseInstallationsApi { private final Object lock = new Object(); private final ExecutorService backgroundExecutor; private final ExecutorService networkExecutor; - + /* FID of this Firebase Installations instance. Cached after successfully registering and + persisting the FID locally. NOTE: cachedFid resets if FID is deleted.*/ private String cachedFid = null; @GuardedBy("lock") @@ -217,11 +218,7 @@ String getName() { public Task getId() { preConditionChecks(); TaskCompletionSource taskCompletionSource = new TaskCompletionSource<>(); - if (cachedFid != null) { - taskCompletionSource.trySetResult(cachedFid); - } else { - taskCompletionSource.trySetResult(doGetId()); - } + taskCompletionSource.trySetResult(doGetId()); return taskCompletionSource.getTask(); } @@ -290,6 +287,9 @@ private void triggerOnException(PersistedInstallationEntry prefs, Exception exce } private String doGetId() { + if (cachedFid != null) { + return cachedFid; + } PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe(); // Execute network calls (CreateInstallations) to the FIS Servers on a separate executor // i.e networkExecutor @@ -343,6 +343,11 @@ private void doNetworkCall(boolean forceRefresh) { // Store the prefs to persist the result of the previous step. insertOrUpdatePrefs(prefs); + // Update cachedFID, if FID is successfully REGISTERED and persisted. + if (prefs.isRegistered()) { + cachedFid = prefs.getFirebaseInstallationId(); + } + // Let the caller know about the result. if (prefs.isErrored()) { triggerOnException(prefs, new FirebaseInstallationsException(Status.BAD_CONFIG)); @@ -460,7 +465,6 @@ private PersistedInstallationEntry registerFidWithServer(PersistedInstallationEn switch (response.getResponseCode()) { case OK: - cachedFid = response.getFid(); return prefs.withRegisteredFid( response.getFid(), response.getRefreshToken(), @@ -512,6 +516,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer( * storage. */ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsException, IOException { + cachedFid = null; PersistedInstallationEntry entry = getMultiProcessSafePrefs(); if (entry.isRegistered()) { // Call the FIS servers to delete this Firebase Installation Id. @@ -527,7 +532,6 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio "Failed to delete a Firebase Installation.", Status.BAD_CONFIG); } } - cachedFid = null; insertOrUpdatePrefs(entry.withNoGeneratedFid()); return null; }