Skip to content

Commit d0cd9e3

Browse files
committed
Refactor RCM fetch random delay to not depend on FirebaseApp initiialization.
1 parent a4897eb commit d0cd9e3

File tree

2 files changed

+14
-42
lines changed

2 files changed

+14
-42
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/config/RemoteConfigManager.java

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import androidx.annotation.Keep;
2424
import androidx.annotation.Nullable;
2525
import androidx.annotation.VisibleForTesting;
26-
import com.google.firebase.FirebaseApp;
27-
import com.google.firebase.StartupTime;
2826
import com.google.firebase.inject.Provider;
2927
import com.google.firebase.perf.logging.AndroidLogger;
3028
import com.google.firebase.perf.util.Optional;
@@ -54,15 +52,14 @@ public class RemoteConfigManager {
5452
private static final long TIME_AFTER_WHICH_A_FETCH_IS_CONSIDERED_STALE_MS =
5553
TimeUnit.HOURS.toMillis(12);
5654
private static final long FETCH_NEVER_HAPPENED_TIMESTAMP_MS = 0;
57-
private static final long MIN_APP_START_CONFIG_FETCH_DELAY_MS = 5000;
58-
private static final int RANDOM_APP_START_CONFIG_FETCH_DELAY_MS = 25000;
55+
private static final long MIN_CONFIG_FETCH_DELAY_MS = 5000;
56+
private static final int RANDOM_CONFIG_FETCH_DELAY_MS = 25000;
57+
private final long rcmInitTimestamp = getCurrentSystemTimeMillis();
5958

6059
private final DeviceCacheManager cache;
6160
private final ConcurrentHashMap<String, FirebaseRemoteConfigValue> allRcConfigMap;
6261
private final Executor executor;
63-
private final long appStartTimeInMs;
6462
private final long appStartConfigFetchDelayInMs;
65-
6663
private long firebaseRemoteConfigLastFetchTimestampMs = FETCH_NEVER_HAPPENED_TIMESTAMP_MS;
6764

6865
@Nullable private Provider<RemoteConfigComponent> firebaseRemoteConfigProvider;
@@ -80,43 +77,22 @@ private RemoteConfigManager() {
8077
TimeUnit.SECONDS,
8178
new LinkedBlockingQueue<Runnable>()),
8279
/* firebaseRemoteConfig= */ null, // set once FirebaseRemoteConfig is initialized
83-
MIN_APP_START_CONFIG_FETCH_DELAY_MS
84-
+ new Random().nextInt(RANDOM_APP_START_CONFIG_FETCH_DELAY_MS),
85-
getInitialStartupMillis());
86-
}
87-
88-
@VisibleForTesting
89-
@SuppressWarnings("FirebaseUseExplicitDependencies")
90-
static long getInitialStartupMillis() {
91-
StartupTime startupTime = null;
92-
try {
93-
startupTime = FirebaseApp.getInstance().get(StartupTime.class);
94-
} catch (IllegalStateException ex) {
95-
// This can happen if you start a trace before Firebase is init
96-
logger.debug("Unable to get StartupTime instance.");
97-
}
98-
if (startupTime != null) {
99-
return startupTime.getEpochMillis();
100-
} else {
101-
return System.currentTimeMillis();
102-
}
80+
MIN_CONFIG_FETCH_DELAY_MS + new Random().nextInt(RANDOM_CONFIG_FETCH_DELAY_MS));
10381
}
10482

10583
@VisibleForTesting
10684
RemoteConfigManager(
10785
DeviceCacheManager cache,
10886
Executor executor,
10987
FirebaseRemoteConfig firebaseRemoteConfig,
110-
long appStartConfigFetchDelayInMs,
111-
long appStartTimeInMs) {
88+
long appStartConfigFetchDelayInMs) {
11289
this.cache = cache;
11390
this.executor = executor;
11491
this.firebaseRemoteConfig = firebaseRemoteConfig;
11592
this.allRcConfigMap =
11693
firebaseRemoteConfig == null
11794
? new ConcurrentHashMap<>()
11895
: new ConcurrentHashMap<>(firebaseRemoteConfig.getAll());
119-
this.appStartTimeInMs = appStartTimeInMs;
12096
this.appStartConfigFetchDelayInMs = appStartConfigFetchDelayInMs;
12197
}
12298

@@ -329,7 +305,7 @@ public boolean isLastFetchFailed() {
329305
*
330306
* <ol>
331307
* <li>Firebase Remote Config is available,
332-
* <li>Time-since-app-start has passed a randomized delay-time (b/187985523), and
308+
* <li>Time has passed a randomized delay-time (b/187985523), and
333309
* <li>At least 12 hours have passed since the previous fetch.
334310
* </ol>
335311
*/
@@ -408,17 +384,17 @@ public boolean isFirebaseRemoteConfigAvailable() {
408384
/** Returns true if a RC fetch should be made, false otherwise. */
409385
private boolean shouldFetchAndActivateRemoteConfigValues() {
410386
long currentTimeInMs = getCurrentSystemTimeMillis();
411-
return hasAppStartConfigFetchDelayElapsed(currentTimeInMs)
387+
return hasRemoteConfigFetchDelayElapsed(currentTimeInMs)
412388
&& hasLastFetchBecomeStale(currentTimeInMs);
413389
}
414390

415391
/**
416-
* Delay fetch by some random time since app start. This is to prevent b/187985523.
392+
* Delay fetch by some random time. This is to prevent b/187985523.
417393
*
418394
* @return true if the random delay has elapsed, false otherwise
419395
*/
420-
private boolean hasAppStartConfigFetchDelayElapsed(long currentTimeInMs) {
421-
return (currentTimeInMs - appStartTimeInMs) >= appStartConfigFetchDelayInMs;
396+
private boolean hasRemoteConfigFetchDelayElapsed(long currentTimeInMs) {
397+
return (currentTimeInMs - rcmInitTimestamp) >= appStartConfigFetchDelayInMs;
422398
}
423399

424400
// We want to fetch once when the app starts and every 12 hours after that.

firebase-perf/src/test/java/com/google/firebase/perf/config/RemoteConfigManagerTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.common.util.concurrent.MoreExecutors;
2929
import com.google.firebase.inject.Provider;
3030
import com.google.firebase.perf.FirebasePerformanceTestBase;
31+
import com.google.firebase.provider.FirebaseInitProvider;
3132
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
3233
import com.google.firebase.remoteconfig.FirebaseRemoteConfigInfo;
3334
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
@@ -846,7 +847,7 @@ public void triggerRemoteConfigFetchIfNecessary_doesNotFetchBeforeAppStartRandom
846847
appStartConfigFetchDelay));
847848

848849
// Simulate time fast forward to some time before fetch time is up
849-
long appStartTimeInMs = System.currentTimeMillis();
850+
long appStartTimeInMs = FirebaseInitProvider.getStartupTime().getEpochMillis();
850851
when(remoteConfigManagerPartialMock.getCurrentSystemTimeMillis())
851852
.thenReturn(appStartTimeInMs + appStartConfigFetchDelay - 2000);
852853

@@ -924,18 +925,13 @@ private RemoteConfigManager setupTestRemoteConfigManager(
924925
when(mockFirebaseRemoteConfig.getAll()).thenReturn(configs);
925926
if (initializeFrc) {
926927
return new RemoteConfigManager(
927-
cacheManager,
928-
fakeExecutor,
929-
mockFirebaseRemoteConfig,
930-
appStartConfigFetchDelayInMs,
931-
RemoteConfigManager.getInitialStartupMillis());
928+
cacheManager, fakeExecutor, mockFirebaseRemoteConfig, appStartConfigFetchDelayInMs);
932929
} else {
933930
return new RemoteConfigManager(
934931
cacheManager,
935932
fakeExecutor,
936933
/* firebaseRemoteConfig= */ null,
937-
appStartConfigFetchDelayInMs,
938-
RemoteConfigManager.getInitialStartupMillis());
934+
appStartConfigFetchDelayInMs);
939935
}
940936
}
941937

0 commit comments

Comments
 (0)