• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.server.appsearch;
18 
19 import static android.text.format.DateUtils.DAY_IN_MILLIS;
20 
21 import com.android.appsearch.flags.Flags;
22 import com.android.server.appsearch.external.localstorage.AppSearchConfig;
23 import com.android.server.appsearch.isolated_storage_service.IsolatedStorageServiceManager;
24 
25 import com.google.android.icing.proto.PersistType;
26 
27 import org.jspecify.annotations.NonNull;
28 
29 import java.util.concurrent.TimeUnit;
30 
31 /**
32  * An interface which exposes config flags to AppSearch.
33  *
34  * <p>This interface provides an abstraction for the AppSearch's flag mechanism and implements
35  * caching to avoid expensive lookups. This interface is only used by environments which have a
36  * running AppSearch service like Framework and GMSCore. JetPack uses {@link AppSearchConfig}
37  * directly instead.
38  *
39  * <p>Implementations of this interface must be thread-safe.
40  *
41  * @hide
42  */
43 public interface ServiceAppSearchConfig extends AppSearchConfig, AutoCloseable {
44     /**
45      * Default min time interval between samples in millis if there is no value set for {@link
46      * #getCachedMinTimeIntervalBetweenSamplesMillis()} in the flag system.
47      */
48     long DEFAULT_MIN_TIME_INTERVAL_BETWEEN_SAMPLES_MILLIS = 50;
49 
50     /**
51      * Default sampling interval if there is no value set for {@link
52      * #getCachedSamplingIntervalDefault()} in the flag system.
53      */
54     int DEFAULT_SAMPLING_INTERVAL = 10;
55 
56     int DEFAULT_LIMIT_CONFIG_MAX_DOCUMENT_SIZE_BYTES = 512 * 1024; // 512KiB
57     int DEFAULT_LIMIT_CONFIG_PER_PACKAGE_DOCUMENT_COUNT_LIMIT = 80_000;
58     int DEFAULT_LIMIT_CONFIG_DOCUMENT_COUNT_LIMIT_START_THRESHOLD = 2_000_000;
59     int DEFAULT_LIMIT_CONFIG_MAX_SUGGESTION_COUNT = 20_000;
60     int DEFAULT_BYTES_OPTIMIZE_THRESHOLD = 10 * 1024 * 1024; // 10 MiB
61     int DEFAULT_TIME_OPTIMIZE_THRESHOLD_MILLIS = (int) TimeUnit.DAYS.toMillis(7);
62     int DEFAULT_DOC_COUNT_OPTIMIZE_THRESHOLD = 10_000;
63     int DEFAULT_MIN_TIME_OPTIMIZE_THRESHOLD_MILLIS = 0;
64     int DEFAULT_FOUR_HOUR_MIN_TIME_OPTIMIZE_THRESHOLD_MILLIS = (int) TimeUnit.HOURS.toMillis(4);
65     // Cached API Call Stats is disabled by default
66     int DEFAULT_API_CALL_STATS_LIMIT = 0;
67     boolean DEFAULT_RATE_LIMIT_ENABLED = false;
68 
69     /** This defines the task queue's total capacity for rate limiting. */
70     int DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY = Integer.MAX_VALUE;
71 
72     /**
73      * This defines the per-package capacity for rate limiting as a percentage of the total
74      * capacity.
75      */
76     float DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE = 1;
77 
78     /**
79      * This defines API costs used for AppSearch's task queue rate limit.
80      *
81      * <p>Each entry in the string should follow the format 'api_name:integer_cost', and each entry
82      * should be separated by a semi-colon. API names should follow the string definitions in {@link
83      * com.android.server.appsearch.external.localstorage.stats.CallStats}.
84      *
85      * <p>e.g. A valid string: "localPutDocuments:5;localSearch:1;localSetSchema:10"
86      */
87     String DEFAULT_RATE_LIMIT_API_COSTS_STRING = "";
88 
89     boolean DEFAULT_ICING_CONFIG_USE_READ_ONLY_SEARCH = true;
90     boolean DEFAULT_USE_FIXED_EXECUTOR_SERVICE = false;
91     long DEFAULT_APP_FUNCTION_CALL_TIMEOUT_MILLIS = 30_000;
92 
93     /** This flag value is true by default because the flag is intended as a kill-switch. */
94     boolean DEFAULT_SHOULD_RETRIEVE_PARENT_INFO = true;
95 
96     /** The default interval in millisecond to trigger fully persist job. */
97     long DEFAULT_FULLY_PERSIST_JOB_INTERVAL = DAY_IN_MILLIS;
98 
99     /**
100      * The default number of active fds an app is allowed to open for read and write blob from
101      * AppSearch.
102      */
103     int DEFAULT_MAX_OPEN_BLOB_COUNT = 250;
104 
105     /** Returns cached value for minTimeIntervalBetweenSamplesMillis. */
getCachedMinTimeIntervalBetweenSamplesMillis()106     long getCachedMinTimeIntervalBetweenSamplesMillis();
107 
108     /**
109      * Returns cached value for default sampling interval for all the stats NOT listed in the
110      * configuration.
111      *
112      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
113      */
getCachedSamplingIntervalDefault()114     int getCachedSamplingIntervalDefault();
115 
116     /**
117      * Returns cached value for sampling interval for batch calls.
118      *
119      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
120      */
getCachedSamplingIntervalForBatchCallStats()121     int getCachedSamplingIntervalForBatchCallStats();
122 
123     /**
124      * Returns cached value for sampling interval for putDocument.
125      *
126      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
127      */
getCachedSamplingIntervalForPutDocumentStats()128     int getCachedSamplingIntervalForPutDocumentStats();
129 
130     /**
131      * Returns cached value for sampling interval for initialize.
132      *
133      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
134      */
getCachedSamplingIntervalForInitializeStats()135     int getCachedSamplingIntervalForInitializeStats();
136 
137     /**
138      * Returns cached value for sampling interval for search.
139      *
140      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
141      */
getCachedSamplingIntervalForSearchStats()142     int getCachedSamplingIntervalForSearchStats();
143 
144     /**
145      * Returns cached value for sampling interval for globalSearch.
146      *
147      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
148      */
getCachedSamplingIntervalForGlobalSearchStats()149     int getCachedSamplingIntervalForGlobalSearchStats();
150 
151     /**
152      * Returns cached value for sampling interval for optimize.
153      *
154      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
155      */
getCachedSamplingIntervalForOptimizeStats()156     int getCachedSamplingIntervalForOptimizeStats();
157 
158     /**
159      * Returns the cached optimize byte size threshold.
160      *
161      * <p>An AppSearch Optimize job will be triggered if the bytes size of garbage resource exceeds
162      * this threshold.
163      */
getCachedBytesOptimizeThreshold()164     int getCachedBytesOptimizeThreshold();
165 
166     /**
167      * Returns the cached optimize time interval threshold.
168      *
169      * <p>An AppSearch Optimize job will be triggered if the time since last optimize job exceeds
170      * this threshold.
171      */
getCachedTimeOptimizeThresholdMs()172     int getCachedTimeOptimizeThresholdMs();
173 
174     /**
175      * Returns the cached optimize document count threshold.
176      *
177      * <p>An AppSearch Optimize job will be triggered if the number of document of garbage resource
178      * exceeds this threshold.
179      */
getCachedDocCountOptimizeThreshold()180     int getCachedDocCountOptimizeThreshold();
181 
182     /**
183      * Returns the cached minimum optimize time interval threshold.
184      *
185      * <p>An AppSearch Optimize job will only be triggered if the time since last optimize job
186      * exceeds this threshold.
187      */
getCachedMinTimeOptimizeThresholdMs()188     int getCachedMinTimeOptimizeThresholdMs();
189 
190     /** Returns the maximum number of last API calls' statistics that can be included in dumpsys. */
getCachedApiCallStatsLimit()191     int getCachedApiCallStatsLimit();
192 
193     /** Returns the cached denylist. */
getCachedDenylist()194     Denylist getCachedDenylist();
195 
196     /** Returns whether to enable AppSearch rate limiting. */
getCachedRateLimitEnabled()197     boolean getCachedRateLimitEnabled();
198 
199     /** Returns the cached {@link AppSearchRateLimitConfig}. */
getCachedRateLimitConfig()200     AppSearchRateLimitConfig getCachedRateLimitConfig();
201 
202     /**
203      * Returns the maximum allowed duration for an app function call in milliseconds.
204      *
205      * @see android.app.appsearch.functions.AppFunctionManager#executeAppFunction
206      */
getAppFunctionCallTimeoutMillis()207     long getAppFunctionCallTimeoutMillis();
208 
209     /**
210      * Returns the time interval to schedule a full persist to disk back ground job in milliseconds.
211      */
getCachedFullyPersistJobIntervalMillis()212     long getCachedFullyPersistJobIntervalMillis();
213 
214     /** Returns the memory size in bytes for isolated storage. */
getIsolatedStorageMemoryBytes()215     default long getIsolatedStorageMemoryBytes() {
216         return IsolatedStorageServiceManager.DEFAULT_MEMORY_BYTES;
217     }
218 
219     /**
220      * Default min time interval between consecutive optimize calls in millis if there is no value
221      * set for {@link #getCachedMinTimeOptimizeThresholdMs()} in the flag system.
222      */
defaultMinTimeOptimizeThresholdMillis()223     default int defaultMinTimeOptimizeThresholdMillis() {
224         // TODO (b/385020106): figure out how to make the default 0 timeSinceLastOptimize work
225         //  with a higher threshold and return 4 hours when
226         //  Flags.enable_four_hour_min_optimize_threshold is true
227         return DEFAULT_MIN_TIME_OPTIMIZE_THRESHOLD_MILLIS;
228     }
229 
230     /**
231      * Default {@code PersistType.Code} that should be used to persist common mutations such as
232      * PUTs or DELETEs.
233      */
defaultLightweightPersistType()234     default PersistType.@NonNull Code defaultLightweightPersistType() {
235         return Flags.enableRecoveryProofPersistence()
236                 ? PersistType.Code.RECOVERY_PROOF
237                 : PersistType.Code.LITE;
238     }
239 
240     /**
241      * Closes this {@link AppSearchConfig}.
242      *
243      * <p>This close() operation does not throw an exception.
244      */
245     @Override
close()246     void close();
247 }
248