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.server.appsearch.external.localstorage.AppSearchConfig;
22 
23 /**
24  * An interface which exposes config flags to AppSearch.
25  *
26  * <p>This interface provides an abstraction for the AppSearch's flag mechanism and implements
27  * caching to avoid expensive lookups. This interface is only used by environments which have a
28  * running AppSearch service like Framework and GMSCore. JetPack uses {@link AppSearchConfig}
29  * directly instead.
30  *
31  * <p>Implementations of this interface must be thread-safe.
32  *
33  * @hide
34  */
35 public interface ServiceAppSearchConfig extends AppSearchConfig, AutoCloseable {
36     /**
37      * Default min time interval between samples in millis if there is no value set for {@link
38      * #getCachedMinTimeIntervalBetweenSamplesMillis()} in the flag system.
39      */
40     long DEFAULT_MIN_TIME_INTERVAL_BETWEEN_SAMPLES_MILLIS = 50;
41 
42     /**
43      * Default sampling interval if there is no value set for {@link
44      * #getCachedSamplingIntervalDefault()} in the flag system.
45      */
46     int DEFAULT_SAMPLING_INTERVAL = 10;
47 
48     int DEFAULT_LIMIT_CONFIG_MAX_DOCUMENT_SIZE_BYTES = 512 * 1024; // 512KiB
49     int DEFAULT_LIMIT_CONFIG_MAX_DOCUMENT_COUNT = 80_000;
50     int DEFAULT_LIMIT_CONFIG_MAX_SUGGESTION_COUNT = 20_000;
51     int DEFAULT_BYTES_OPTIMIZE_THRESHOLD = 10 * 1024 * 1024; // 10 MiB
52     int DEFAULT_TIME_OPTIMIZE_THRESHOLD_MILLIS = 7 * 24 * 60 * 60 * 1000; // 7 days in millis
53     int DEFAULT_DOC_COUNT_OPTIMIZE_THRESHOLD = 10_000;
54     int DEFAULT_MIN_TIME_OPTIMIZE_THRESHOLD_MILLIS = 0;
55     // Cached API Call Stats is disabled by default
56     int DEFAULT_API_CALL_STATS_LIMIT = 0;
57     boolean DEFAULT_RATE_LIMIT_ENABLED = false;
58 
59     /** This defines the task queue's total capacity for rate limiting. */
60     int DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY = Integer.MAX_VALUE;
61 
62     /**
63      * This defines the per-package capacity for rate limiting as a percentage of the total
64      * capacity.
65      */
66     float DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE = 1;
67 
68     /**
69      * This defines API costs used for AppSearch's task queue rate limit.
70      *
71      * <p>Each entry in the string should follow the format 'api_name:integer_cost', and each entry
72      * should be separated by a semi-colon. API names should follow the string definitions in {@link
73      * com.android.server.appsearch.external.localstorage.stats.CallStats}.
74      *
75      * <p>e.g. A valid string: "localPutDocuments:5;localSearch:1;localSetSchema:10"
76      */
77     String DEFAULT_RATE_LIMIT_API_COSTS_STRING = "";
78 
79     boolean DEFAULT_ICING_CONFIG_USE_READ_ONLY_SEARCH = true;
80     boolean DEFAULT_USE_FIXED_EXECUTOR_SERVICE = false;
81     long DEFAULT_APP_FUNCTION_CALL_TIMEOUT_MILLIS = 30_000;
82 
83     /** This flag value is true by default because the flag is intended as a kill-switch. */
84     boolean DEFAULT_SHOULD_RETRIEVE_PARENT_INFO = true;
85 
86     /** The default interval in millisecond to trigger fully persist job. */
87     long DEFAULT_FULLY_PERSIST_JOB_INTERVAL = DAY_IN_MILLIS;
88 
89     /** Returns cached value for minTimeIntervalBetweenSamplesMillis. */
getCachedMinTimeIntervalBetweenSamplesMillis()90     long getCachedMinTimeIntervalBetweenSamplesMillis();
91 
92     /**
93      * Returns cached value for default sampling interval for all the stats NOT listed in the
94      * configuration.
95      *
96      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
97      */
getCachedSamplingIntervalDefault()98     int getCachedSamplingIntervalDefault();
99 
100     /**
101      * Returns cached value for sampling interval for batch calls.
102      *
103      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
104      */
getCachedSamplingIntervalForBatchCallStats()105     int getCachedSamplingIntervalForBatchCallStats();
106 
107     /**
108      * Returns cached value for sampling interval for putDocument.
109      *
110      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
111      */
getCachedSamplingIntervalForPutDocumentStats()112     int getCachedSamplingIntervalForPutDocumentStats();
113 
114     /**
115      * Returns cached value for sampling interval for initialize.
116      *
117      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
118      */
getCachedSamplingIntervalForInitializeStats()119     int getCachedSamplingIntervalForInitializeStats();
120 
121     /**
122      * Returns cached value for sampling interval for search.
123      *
124      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
125      */
getCachedSamplingIntervalForSearchStats()126     int getCachedSamplingIntervalForSearchStats();
127 
128     /**
129      * Returns cached value for sampling interval for globalSearch.
130      *
131      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
132      */
getCachedSamplingIntervalForGlobalSearchStats()133     int getCachedSamplingIntervalForGlobalSearchStats();
134 
135     /**
136      * Returns cached value for sampling interval for optimize.
137      *
138      * <p>For example, sampling_interval=10 means that one out of every 10 stats was logged.
139      */
getCachedSamplingIntervalForOptimizeStats()140     int getCachedSamplingIntervalForOptimizeStats();
141 
142     /**
143      * Returns the cached optimize byte size threshold.
144      *
145      * <p>An AppSearch Optimize job will be triggered if the bytes size of garbage resource exceeds
146      * this threshold.
147      */
getCachedBytesOptimizeThreshold()148     int getCachedBytesOptimizeThreshold();
149 
150     /**
151      * Returns the cached optimize time interval threshold.
152      *
153      * <p>An AppSearch Optimize job will be triggered if the time since last optimize job exceeds
154      * this threshold.
155      */
getCachedTimeOptimizeThresholdMs()156     int getCachedTimeOptimizeThresholdMs();
157 
158     /**
159      * Returns the cached optimize document count threshold.
160      *
161      * <p>An AppSearch Optimize job will be triggered if the number of document of garbage resource
162      * exceeds this threshold.
163      */
getCachedDocCountOptimizeThreshold()164     int getCachedDocCountOptimizeThreshold();
165 
166     /**
167      * Returns the cached minimum optimize time interval threshold.
168      *
169      * <p>An AppSearch Optimize job will only be triggered if the time since last optimize job
170      * exceeds this threshold.
171      */
getCachedMinTimeOptimizeThresholdMs()172     int getCachedMinTimeOptimizeThresholdMs();
173 
174     /** Returns the maximum number of last API calls' statistics that can be included in dumpsys. */
getCachedApiCallStatsLimit()175     int getCachedApiCallStatsLimit();
176 
177     /** Returns the cached denylist. */
getCachedDenylist()178     Denylist getCachedDenylist();
179 
180     /** Returns whether to enable AppSearch rate limiting. */
getCachedRateLimitEnabled()181     boolean getCachedRateLimitEnabled();
182 
183     /** Returns the cached {@link AppSearchRateLimitConfig}. */
getCachedRateLimitConfig()184     AppSearchRateLimitConfig getCachedRateLimitConfig();
185 
186     /**
187      * Returns the maximum allowed duration for an app function call in milliseconds.
188      *
189      * @see android.app.appsearch.functions.AppFunctionManager#executeAppFunction
190      */
getAppFunctionCallTimeoutMillis()191     long getAppFunctionCallTimeoutMillis();
192 
193     /**
194      * Returns the time interval to schedule a full persist to disk back ground job in milliseconds.
195      */
getCachedFullyPersistJobIntervalMillis()196     long getCachedFullyPersistJobIntervalMillis();
197 
198     /**
199      * Closes this {@link AppSearchConfig}.
200      *
201      * <p>This close() operation does not throw an exception.
202      */
203     @Override
close()204     void close();
205 }
206