1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.federatedcompute.services.common;
18 
19 import com.android.adservices.shared.common.flags.ConfigFlag;
20 import com.android.adservices.shared.common.flags.FeatureFlag;
21 import com.android.adservices.shared.common.flags.ModuleSharedFlags;
22 
23 import java.util.concurrent.TimeUnit;
24 
25 /** FederatedCompute feature flags interface. This Flags interface hold the default values */
26 public interface Flags extends ModuleSharedFlags {
27     /**
28      * Global FederatedCompute APK Kill Switch. This overrides all other killswitches under
29      * federatedcompute APK. The default value is false which means FederatedCompute is enabled.
30      * This flag is used for emergency turning off.
31      */
32     boolean FEDERATED_COMPUTE_GLOBAL_KILL_SWITCH = true;
33 
getGlobalKillSwitch()34     default boolean getGlobalKillSwitch() {
35         return FEDERATED_COMPUTE_GLOBAL_KILL_SWITCH;
36     }
37 
38     /**
39      * Flags for {@link
40      * com.android.federatedcompute.services.scheduling.FederatedComputeJobManager}.
41      */
42     long DEFAULT_SCHEDULING_PERIOD_SECS = 60 * 5; // 5 minutes
43 
getDefaultSchedulingPeriodSecs()44     default long getDefaultSchedulingPeriodSecs() {
45         return DEFAULT_SCHEDULING_PERIOD_SECS;
46     }
47 
48     long MIN_SCHEDULING_INTERVAL_SECS_FOR_FEDERATED_COMPUTATION = 1 * 60; // 1 min
49 
getMinSchedulingIntervalSecsForFederatedComputation()50     default long getMinSchedulingIntervalSecsForFederatedComputation() {
51         return MIN_SCHEDULING_INTERVAL_SECS_FOR_FEDERATED_COMPUTATION;
52     }
53 
54     long MAX_SCHEDULING_INTERVAL_SECS_FOR_FEDERATED_COMPUTATION =
55             6 * 24 * 60 * 60; // 6 days (< default ttl 7d)
56 
getMaxSchedulingIntervalSecsForFederatedComputation()57     default long getMaxSchedulingIntervalSecsForFederatedComputation() {
58         return MAX_SCHEDULING_INTERVAL_SECS_FOR_FEDERATED_COMPUTATION;
59     }
60 
61     long MAX_SCHEDULING_PERIOD_SECS = 60 * 60 * 24 * 2; // 2 days
62 
getMaxSchedulingPeriodSecs()63     default long getMaxSchedulingPeriodSecs() {
64         return MAX_SCHEDULING_PERIOD_SECS;
65     }
66 
67     long TRAINING_TIME_FOR_LIVE_SECONDS = 7 * 24 * 60 * 60; // one week
68 
getTrainingTimeForLiveSeconds()69     default long getTrainingTimeForLiveSeconds() {
70         return TRAINING_TIME_FOR_LIVE_SECONDS;
71     }
72 
73     long TRAINING_SERVICE_RESULT_CALLBACK_TIMEOUT_SEC =
74             60 * 9 + 45; // 9 minutes 45 seconds, leaving ~15 seconds to clean up.
75 
getTrainingServiceResultCallbackTimeoutSecs()76     default long getTrainingServiceResultCallbackTimeoutSecs() {
77         return TRAINING_SERVICE_RESULT_CALLBACK_TIMEOUT_SEC;
78     }
79 
80     float TRANSIENT_ERROR_RETRY_DELAY_JITTER_PERCENT = 0.2f;
81 
getTransientErrorRetryDelayJitterPercent()82     default float getTransientErrorRetryDelayJitterPercent() {
83         return TRANSIENT_ERROR_RETRY_DELAY_JITTER_PERCENT;
84     }
85 
86     long TRANSIENT_ERROR_RETRY_DELAY_SECS = 15 * 60; // 15 minutes
87 
getTransientErrorRetryDelaySecs()88     default long getTransientErrorRetryDelaySecs() {
89         return TRANSIENT_ERROR_RETRY_DELAY_SECS;
90     }
91 
92     /**
93      * The minimum percentage (expressed as an integer between 0 and 100) of battery charge that
94      * must be remaining in order start training as well as continue it once started.
95      */
96     int DEFAULT_TRAINING_MIN_BATTERY_LEVEL = 30;
97 
getTrainingMinBatteryLevel()98     default int getTrainingMinBatteryLevel() {
99         return DEFAULT_TRAINING_MIN_BATTERY_LEVEL;
100     }
101 
102     /**
103      * The thermal status reported by `PowerManager#getCurrentThermalStatus()` at which to interrupt
104      * training. Must be one of:
105      *
106      * <p>THERMAL_STATUS_NONE = 0;<br>
107      * THERMAL_STATUS_LIGHT = 1; <br>
108      * THERMAL_STATUS_MODERATE = 2; <br>
109      * THERMAL_STATUS_SEVERE = 3; <br>
110      * THERMAL_STATUS_CRITICAL = 4;<br>
111      * THERMAL_STATUS_EMERGENCY = 5; <br>
112      * THERMAL_STATUS_SHUTDOWN = 6; <br>
113      */
114     int DEFAULT_THERMAL_STATUS_TO_THROTTLE = 2;
115 
getThermalStatusToThrottle()116     default int getThermalStatusToThrottle() {
117         return DEFAULT_THERMAL_STATUS_TO_THROTTLE;
118     }
119 
120     /** The minimum duration between two training condition checks in milliseconds. */
121     long DEFAULT_TRAINING_CONDITION_CHECK_THROTTLE_PERIOD_MILLIS = 1000;
122 
getTrainingConditionCheckThrottlePeriodMillis()123     default long getTrainingConditionCheckThrottlePeriodMillis() {
124         return DEFAULT_TRAINING_CONDITION_CHECK_THROTTLE_PERIOD_MILLIS;
125     }
126 
127     String ENCRYPTION_KEY_FETCH_URL = "https://fake-coordinator/v1alpha/publicKeys";
128 
129     /**
130      * @return Url to fetch encryption key for federated compute.
131      */
getEncryptionKeyFetchUrl()132     default String getEncryptionKeyFetchUrl() {
133         return ENCRYPTION_KEY_FETCH_URL;
134     }
135 
136     Long FEDERATED_COMPUTE_ENCRYPTION_KEY_MAX_AGE_SECONDS =
137             TimeUnit.DAYS.toSeconds(14/* duration= */ );
138 
139     /**
140      * @return default max age in seconds for federated compute ecryption keys.
141      */
getFederatedComputeEncryptionKeyMaxAgeSeconds()142     default Long getFederatedComputeEncryptionKeyMaxAgeSeconds() {
143         return FEDERATED_COMPUTE_ENCRYPTION_KEY_MAX_AGE_SECONDS;
144     }
145 
146     Long ENCRYPTION_KEY_FETCH_PERIOD_SECONDS = 60 * 60 * 24L; // every 24 h
147 
getEncryptionKeyFetchPeriodSeconds()148     default Long getEncryptionKeyFetchPeriodSeconds() {
149         return ENCRYPTION_KEY_FETCH_PERIOD_SECONDS;
150     }
151 
152     Boolean USE_BACKGROUND_ENCRYPTION_KEY_FETCH = true;
153 
getEnableBackgroundEncryptionKeyFetch()154     default Boolean getEnableBackgroundEncryptionKeyFetch() {
155         return USE_BACKGROUND_ENCRYPTION_KEY_FETCH;
156     }
157 
158     Long ODP_AUTHORIZATION_TOKEN_DELETION_PERIOD_SECONDs =
159             TimeUnit.DAYS.toSeconds(1/* duration= */ );
160 
getAuthorizationTokenDeletionPeriodSeconds()161     default Long getAuthorizationTokenDeletionPeriodSeconds() {
162         return ODP_AUTHORIZATION_TOKEN_DELETION_PERIOD_SECONDs;
163     }
164 
165     int HTTP_REQUEST_RETRY_LIMIT = 3;
166 
getHttpRequestRetryLimit()167     default int getHttpRequestRetryLimit() {
168         return HTTP_REQUEST_RETRY_LIMIT;
169     }
170 
171     Boolean ENCRYPTION_ENABLED = true;
172 
173     /** Whether to enable encryption when uploading results. */
isEncryptionEnabled()174     default Boolean isEncryptionEnabled() {
175         return ENCRYPTION_ENABLED;
176     }
177 
178     boolean DEFAULT_ENABLE_ELIGIBILITY_TASK = true;
179 
isEligibilityTaskEnabled()180     default boolean isEligibilityTaskEnabled() {
181         return DEFAULT_ENABLE_ELIGIBILITY_TASK;
182     }
183 
184     int FCP_RESCHEDULE_LIMIT = 6;
185 
186     /**
187      * Limitation of how many times that FCP task job can be rescheduled if it failed, if federated
188      * compute job retry times exceeds this limit, the job will be canceled/abort.
189      */
getFcpRescheduleLimit()190     default int getFcpRescheduleLimit() {
191         return FCP_RESCHEDULE_LIMIT;
192     }
193 
194     int FCP_RECURRENT_RESCHEDULE_LIMIT = 100;
195 
196     /**
197      * Limitation of how much times can FCP task job can be rescheduled if it failed, if federated
198      * compute job retry times exceeds this limit, the job will be canceled/abort.
199      * This one is for recurrent jobs.
200      */
getFcpRecurrentRescheduleLimit()201     default int getFcpRecurrentRescheduleLimit() {
202         return FCP_RECURRENT_RESCHEDULE_LIMIT;
203     }
204 
205     // 7 days in milliseconds
206     long ODP_AUTHORIZATION_TOKEN_TTL = 7 * 24 * 60 * 60 * 1000L;
207 
getOdpAuthorizationTokenTtl()208     default long getOdpAuthorizationTokenTtl() {
209         return ODP_AUTHORIZATION_TOKEN_TTL;
210     }
211 
212     boolean ENABLE_CLIENT_ERROR_LOGGING = false;
213 
getEnableClientErrorLogging()214     default boolean getEnableClientErrorLogging() {
215         return ENABLE_CLIENT_ERROR_LOGGING;
216     }
217 
218     // 7 days in milliseconds
219     long DEFAULT_TASK_HISTORY_TTL_MILLIS = 7 * 24 * 60 * 60 * 1000L;
220 
getTaskHistoryTtl()221     default long getTaskHistoryTtl() {
222         return DEFAULT_TASK_HISTORY_TTL_MILLIS;
223     }
224 
225     int DEFAULT_EXAMPLE_STORE_SERVICE_CALLBACK_TIMEOUT_SEC = 10;
226 
getExampleStoreServiceCallbackTimeoutSec()227     default int getExampleStoreServiceCallbackTimeoutSec() {
228         return DEFAULT_EXAMPLE_STORE_SERVICE_CALLBACK_TIMEOUT_SEC;
229     }
230 
231     int DEFAULT_EXAMPLE_ITERATOR_NEXT_TIMEOUT_SEC = 2;
232 
getExampleIteratorNextTimeoutSec()233     default int getExampleIteratorNextTimeoutSec() {
234         return DEFAULT_EXAMPLE_ITERATOR_NEXT_TIMEOUT_SEC;
235     }
236 
237     long FCP_TF_ERROR_RESCHEDULE_SECONDS = 86400; // 24 hours in seconds
238 
239     /** Reschedule FCP jobs in case of TF failure. */
getFcpTfErrorRescheduleSeconds()240     default long getFcpTfErrorRescheduleSeconds() {
241         return FCP_TF_ERROR_RESCHEDULE_SECONDS;
242     }
243 
244     long FCP_DEFAULT_MEMORY_SIZE_LIMIT = 50000000L; // 50 MBs in bytes
245 
246     /** Provides upper limit for FCP temp files. */
getFcpMemorySizeLimit()247     default long getFcpMemorySizeLimit() {
248         return FCP_DEFAULT_MEMORY_SIZE_LIMIT;
249     }
250 
251     /** Default value for the base64 encoded Job Policy proto for FCP background jobs. */
252     @ConfigFlag String DEFAULT_FCP_MODULE_JOB_POLICY = "";
253 
getFcpModuleJobPolicy()254     default String getFcpModuleJobPolicy() {
255         return DEFAULT_FCP_MODULE_JOB_POLICY;
256     }
257 
258     /** Default value for SPE to be enabled for the pilot background jobs. */
259     @FeatureFlag boolean DEFAULT_SPE_PILOT_JOB_ENABLED = false;
260 
getSpePilotJobEnabled()261     default boolean getSpePilotJobEnabled() {
262         return DEFAULT_SPE_PILOT_JOB_ENABLED;
263     }
264 
265     @ConfigFlag int DEFAULT_FCP_TASK_LIMIT_PER_PACKAGE = 50;
266 
getFcpTaskLimitPerPackage()267     default int getFcpTaskLimitPerPackage() {
268         return DEFAULT_FCP_TASK_LIMIT_PER_PACKAGE;
269     }
270 }
271