Skip to content

Migrate RC to common executors. #4393

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 1 commit into from
Dec 2, 2022
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
1 change: 1 addition & 0 deletions firebase-config/firebase-config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ android {
}

dependencies {
implementation project(':firebase-annotations')
implementation project(':firebase-common')
implementation project(':firebase-abt')
implementation project(':firebase-components')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package com.google.firebase.remoteconfig;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
Expand All @@ -26,6 +25,7 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.abt.AbtException;
import com.google.firebase.abt.FirebaseABTesting;
import com.google.firebase.concurrent.FirebaseExecutors;
import com.google.firebase.installations.FirebaseInstallationsApi;
import com.google.firebase.installations.InstallationTokenResult;
import com.google.firebase.remoteconfig.internal.ConfigCacheClient;
Expand Down Expand Up @@ -280,14 +280,13 @@ public Task<Boolean> activate() {
*
* @return {@link Task} representing the {@code fetch} call.
*/
// TODO(b/258275481): Use an explicit executor in continuations.
@SuppressLint("TaskMainThread")
@NonNull
public Task<Void> fetch() {
Task<FetchResponse> fetchTask = fetchHandler.fetch();

// Convert Task type to Void.
return fetchTask.onSuccessTask((unusedFetchResponse) -> Tasks.forResult(null));
return fetchTask.onSuccessTask(
FirebaseExecutors.directExecutor(), (unusedFetchResponse) -> Tasks.forResult(null));
}

/**
Expand All @@ -309,14 +308,13 @@ public Task<Void> fetch() {
* this many seconds ago, configs are served from the backend instead of local storage.
* @return {@link Task} representing the {@code fetch} call.
*/
// TODO(b/258275481): Use an explicit executor in continuations.
@SuppressLint("TaskMainThread")
@NonNull
public Task<Void> fetch(long minimumFetchIntervalInSeconds) {
Task<FetchResponse> fetchTask = fetchHandler.fetch(minimumFetchIntervalInSeconds);

// Convert Task type to Void.
return fetchTask.onSuccessTask((unusedFetchResponse) -> Tasks.forResult(null));
return fetchTask.onSuccessTask(
FirebaseExecutors.directExecutor(), (unusedFetchResponse) -> Tasks.forResult(null));
}

/**
Expand Down Expand Up @@ -589,8 +587,6 @@ private boolean processActivatePutTask(Task<ConfigContainer> putTask) {
*
* @return A task with result {@code null} on failure.
*/
// TODO(b/258275481): Use an explicit executor in continuations.
@SuppressLint("TaskMainThread")
private Task<Void> setDefaultsWithStringsMapAsync(Map<String, String> defaultsStringMap) {
ConfigContainer defaultConfigs = null;
try {
Expand All @@ -602,7 +598,8 @@ private Task<Void> setDefaultsWithStringsMapAsync(Map<String, String> defaultsSt

Task<ConfigContainer> putTask = defaultConfigsCache.put(defaultConfigs);
// Convert Task type to Void.
return putTask.onSuccessTask((unusedContainer) -> Tasks.forResult(null));
return putTask.onSuccessTask(
FirebaseExecutors.directExecutor(), (unusedContainer) -> Tasks.forResult(null));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package com.google.firebase.remoteconfig;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.GuardedBy;
Expand All @@ -27,6 +26,7 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.abt.FirebaseABTesting;
import com.google.firebase.analytics.connector.AnalyticsConnector;
import com.google.firebase.annotations.concurrent.Background;
import com.google.firebase.inject.Provider;
import com.google.firebase.installations.FirebaseInstallationsApi;
import com.google.firebase.remoteconfig.internal.ConfigCacheClient;
Expand All @@ -40,8 +40,6 @@
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Component for providing multiple Firebase Remote Config (FRC) instances. Firebase Android
Expand Down Expand Up @@ -76,7 +74,7 @@ public class RemoteConfigComponent {
private final Map<String, FirebaseRemoteConfig> frcNamespaceInstances = new HashMap<>();

private final Context context;
private final ExecutorService executorService;
private final Executor executor;
private final FirebaseApp firebaseApp;
private final FirebaseInstallationsApi firebaseInstallations;
private final FirebaseABTesting firebaseAbt;
Expand All @@ -88,17 +86,16 @@ public class RemoteConfigComponent {
private Map<String, String> customHeaders = new HashMap<>();

/** Firebase Remote Config Component constructor. */
// TODO(b/258275481): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
RemoteConfigComponent(
Context context,
@Background Executor executor,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be @Blocking, too?

FirebaseApp firebaseApp,
FirebaseInstallationsApi firebaseInstallations,
FirebaseABTesting firebaseAbt,
Provider<AnalyticsConnector> analyticsConnector) {
this(
context,
Executors.newCachedThreadPool(),
executor,
firebaseApp,
firebaseInstallations,
firebaseAbt,
Expand All @@ -110,14 +107,14 @@ public class RemoteConfigComponent {
@VisibleForTesting
protected RemoteConfigComponent(
Context context,
ExecutorService executorService,
Executor executor,
FirebaseApp firebaseApp,
FirebaseInstallationsApi firebaseInstallations,
FirebaseABTesting firebaseAbt,
Provider<AnalyticsConnector> analyticsConnector,
boolean loadGetDefault) {
this.context = context;
this.executorService = executorService;
this.executor = executor;
this.firebaseApp = firebaseApp;
this.firebaseInstallations = firebaseInstallations;
this.firebaseAbt = firebaseAbt;
Expand All @@ -130,7 +127,7 @@ protected RemoteConfigComponent(
// while another test has already cleared the component but hasn't gotten a new one yet.
if (loadGetDefault) {
// Loads the default namespace's configs from disk on App startup.
Tasks.call(executorService, this::getDefault);
Tasks.call(executor, this::getDefault);
}
}

Expand Down Expand Up @@ -167,7 +164,7 @@ public synchronized FirebaseRemoteConfig get(String namespace) {
namespace,
firebaseInstallations,
firebaseAbt,
executorService,
executor,
fetchedCacheClient,
activatedCacheClient,
defaultsCacheClient,
Expand Down Expand Up @@ -214,15 +211,13 @@ public synchronized void setCustomHeaders(Map<String, String> customHeaders) {
this.customHeaders = customHeaders;
}

// TODO(b/258275481): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
private ConfigCacheClient getCacheClient(String namespace, String configStoreType) {
String fileName =
String.format(
"%s_%s_%s_%s.json",
FIREBASE_REMOTE_CONFIG_FILE_NAME_PREFIX, appId, namespace, configStoreType);
return ConfigCacheClient.getInstance(
Executors.newCachedThreadPool(), ConfigStorageClient.getInstance(context, fileName));
executor, ConfigStorageClient.getInstance(context, fileName));
}

@VisibleForTesting
Expand All @@ -244,7 +239,7 @@ synchronized ConfigFetchHandler getFetchHandler(
return new ConfigFetchHandler(
firebaseInstallations,
isPrimaryApp(firebaseApp) ? analyticsConnector : () -> null,
executorService,
executor,
DEFAULT_CLOCK,
DEFAULT_RANDOM,
fetchedCacheClient,
Expand All @@ -255,8 +250,7 @@ synchronized ConfigFetchHandler getFetchHandler(

private ConfigGetParameterHandler getGetHandler(
ConfigCacheClient activatedCacheClient, ConfigCacheClient defaultsCacheClient) {
return new ConfigGetParameterHandler(
executorService, activatedCacheClient, defaultsCacheClient);
return new ConfigGetParameterHandler(executor, activatedCacheClient, defaultsCacheClient);
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
import com.google.firebase.abt.FirebaseABTesting.OriginService;
import com.google.firebase.abt.component.AbtComponent;
import com.google.firebase.analytics.connector.AnalyticsConnector;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.components.Component;
import com.google.firebase.components.ComponentRegistrar;
import com.google.firebase.components.Dependency;
import com.google.firebase.components.Qualified;
import com.google.firebase.installations.FirebaseInstallationsApi;
import com.google.firebase.platforminfo.LibraryVersionComponent;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;

/**
* Registrar for setting up Firebase Remote Config's dependency injections in Firebase Android
Expand All @@ -41,10 +44,12 @@ public class RemoteConfigRegistrar implements ComponentRegistrar {

@Override
public List<Component<?>> getComponents() {
Qualified<Executor> blockingExecutor = Qualified.qualified(Blocking.class, Executor.class);
return Arrays.asList(
Component.builder(RemoteConfigComponent.class)
.name(LIBRARY_NAME)
.add(Dependency.required(Context.class))
.add(Dependency.required(blockingExecutor))
.add(Dependency.required(FirebaseApp.class))
.add(Dependency.required(FirebaseInstallationsApi.class))
.add(Dependency.required(AbtComponent.class))
Expand All @@ -53,6 +58,7 @@ public List<Component<?>> getComponents() {
container ->
new RemoteConfigComponent(
container.get(Context.class),
container.get(blockingExecutor),
container.get(FirebaseApp.class),
container.get(FirebaseInstallationsApi.class),
container.get(AbtComponent.class).get(OriginService.REMOTE_CONFIG),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

Expand All @@ -56,7 +55,7 @@ public class ConfigCacheClient {
@GuardedBy("ConfigCacheClient.class")
private static final Map<String, ConfigCacheClient> clientInstances = new HashMap<>();

private final ExecutorService executorService;
private final Executor executor;
private final ConfigStorageClient storageClient;

/**
Expand All @@ -71,8 +70,8 @@ public class ConfigCacheClient {
* Creates a new cache client that executes async calls through {@code executorService} and is
* backed by {@code storageClient}.
*/
private ConfigCacheClient(ExecutorService executorService, ConfigStorageClient storageClient) {
this.executorService = executorService;
private ConfigCacheClient(Executor executor, ConfigStorageClient storageClient) {
this.executor = executor;
this.storageClient = storageClient;

cachedContainerTask = null;
Expand Down Expand Up @@ -126,9 +125,9 @@ public Task<ConfigContainer> put(ConfigContainer configContainer) {
*/
public Task<ConfigContainer> put(
ConfigContainer configContainer, boolean shouldUpdateInMemoryContainer) {
return Tasks.call(executorService, () -> storageClient.write(configContainer))
return Tasks.call(executor, () -> storageClient.write(configContainer))
.onSuccessTask(
executorService,
executor,
(unusedVoid) -> {
if (shouldUpdateInMemoryContainer) {
updateInMemoryConfigContainer(configContainer);
Expand Down Expand Up @@ -165,7 +164,7 @@ public synchronized Task<ConfigContainer> get() {
*/
if (cachedContainerTask == null
|| (cachedContainerTask.isComplete() && !cachedContainerTask.isSuccessful())) {
cachedContainerTask = Tasks.call(executorService, storageClient::read);
cachedContainerTask = Tasks.call(executor, storageClient::read);
}
return cachedContainerTask;
}
Expand Down Expand Up @@ -200,10 +199,10 @@ synchronized Task<ConfigContainer> getCachedContainerTask() {
* underlying file name.
*/
public static synchronized ConfigCacheClient getInstance(
ExecutorService executorService, ConfigStorageClient storageClient) {
Executor executor, ConfigStorageClient storageClient) {
String fileName = storageClient.getFileName();
if (!clientInstances.containsKey(fileName)) {
clientInstances.put(fileName, new ConfigCacheClient(executorService, storageClient));
clientInstances.put(fileName, new ConfigCacheClient(executor, storageClient));
}
return clientInstances.get(fileName);
}
Expand Down