• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.adservices.service.measurement.reporting;
18 
19 import static com.android.adservices.service.measurement.util.JobLockHolder.Type.DEBUG_REPORTING;
20 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON;
21 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB;
22 
23 import android.app.job.JobInfo;
24 import android.app.job.JobParameters;
25 import android.app.job.JobScheduler;
26 import android.app.job.JobService;
27 import android.content.ComponentName;
28 import android.content.Context;
29 
30 import com.android.adservices.LogUtil;
31 import com.android.adservices.LoggerFactory;
32 import com.android.adservices.concurrency.AdServicesExecutors;
33 import com.android.adservices.data.measurement.DatastoreManager;
34 import com.android.adservices.data.measurement.DatastoreManagerFactory;
35 import com.android.adservices.service.Flags;
36 import com.android.adservices.service.FlagsFactory;
37 import com.android.adservices.service.common.compat.ServiceCompatUtils;
38 import com.android.adservices.service.measurement.aggregation.AggregateEncryptionKeyManager;
39 import com.android.adservices.service.measurement.util.JobLockHolder;
40 import com.android.adservices.service.stats.AdServicesLoggerImpl;
41 import com.android.adservices.spe.AdServicesJobServiceLogger;
42 import com.android.internal.annotations.VisibleForTesting;
43 
44 import com.google.android.libraries.mobiledatadownload.internal.AndroidTimeSource;
45 import com.google.common.util.concurrent.ListeningExecutorService;
46 
47 import java.time.Clock;
48 import java.time.Instant;
49 import java.util.concurrent.Future;
50 
51 /**
52  * Fallback service for scheduling debug reporting jobs. This runs periodically to handle any
53  * reports that the {@link DebugReportingJobService } failed/missed. The actual job execution logic
54  * is part of {@link EventReportingJobHandler } and {@link AggregateReportingJobHandler}.
55  */
56 public class DebugReportingFallbackJobService extends JobService {
57 
58     private static final int MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID =
59             MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB.getJobId();
60 
61     private static final ListeningExecutorService sBlockingExecutor =
62             AdServicesExecutors.getBlockingExecutor();
63 
64     private Future mExecutorFuture;
65 
66     @Override
onStartJob(JobParameters params)67     public boolean onStartJob(JobParameters params) {
68         // Always ensure that the first thing this job does is check if it should be running, and
69         // cancel itself if it's not supposed to be.
70         if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) {
71             LogUtil.d(
72                     "Disabling DebugReportingFallbackJobService job because it's running in"
73                             + " ExtServices on T+");
74             return skipAndCancelBackgroundJob(params, /* skipReason=*/ 0, /* doRecord=*/ false);
75         }
76 
77         AdServicesJobServiceLogger.getInstance()
78                 .recordOnStartJob(MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID);
79 
80         if (FlagsFactory.getFlags().getMeasurementDebugReportingFallbackJobKillSwitch()) {
81             LoggerFactory.getMeasurementLogger().e("DebugReportingFallbackJobService is disabled.");
82             return skipAndCancelBackgroundJob(
83                     params,
84                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON,
85                     /* doRecord */ true);
86         }
87 
88         Instant jobStartTime = Clock.systemUTC().instant();
89         LoggerFactory.getMeasurementLogger()
90                 .d(
91                         "DebugReportingFallbackJobService.onStartJob " + "at %s",
92                         jobStartTime.toString());
93         mExecutorFuture =
94                 sBlockingExecutor.submit(
95                         () -> {
96                             sendReports();
97                             boolean shouldRetry = false;
98                             AdServicesJobServiceLogger.getInstance()
99                                     .recordJobFinished(
100                                             MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID,
101                                             /* isSuccessful */ true,
102                                             shouldRetry);
103                             jobFinished(params, false);
104                         });
105         return true;
106     }
107 
108     @Override
onStopJob(JobParameters params)109     public boolean onStopJob(JobParameters params) {
110         LoggerFactory.getMeasurementLogger().d("DebugReportingJobService.onStopJob");
111         boolean shouldRetry = true;
112         if (mExecutorFuture != null) {
113             shouldRetry = mExecutorFuture.cancel(/* mayInterruptIfRunning */ true);
114         }
115         AdServicesJobServiceLogger.getInstance()
116                 .recordOnStopJob(params, MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID, shouldRetry);
117         return shouldRetry;
118     }
119 
120     @VisibleForTesting
schedule(JobScheduler jobScheduler, JobInfo job)121     protected static void schedule(JobScheduler jobScheduler, JobInfo job) {
122         jobScheduler.schedule(job);
123     }
124 
125     /**
126      * Schedule Debug Reporting Fallback Job Service if it is not already scheduled.
127      *
128      * @param context the context
129      * @param forceSchedule flag to indicate whether to force rescheduling the job.
130      */
131     // TODO(b/311183933): Remove passed in Context from static method.
132     @SuppressWarnings("AvoidStaticContext")
scheduleIfNeeded(Context context, boolean forceSchedule)133     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
134         Flags flags = FlagsFactory.getFlags();
135         if (flags.getMeasurementDebugReportingFallbackJobKillSwitch()) {
136             LoggerFactory.getMeasurementLogger()
137                     .e("DebugReportingFallbackJobService is disabled, skip scheduling");
138             return;
139         }
140 
141         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
142         if (jobScheduler == null) {
143             LoggerFactory.getMeasurementLogger().e("JobScheduler not found");
144             return;
145         }
146 
147         final JobInfo scheduledJob =
148                 jobScheduler.getPendingJob(MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID);
149         // Schedule if it hasn't been scheduled already or force rescheduling
150         JobInfo jobInfo = buildJobInfo(context, flags);
151         if (forceSchedule || !jobInfo.equals(scheduledJob)) {
152             schedule(jobScheduler, jobInfo);
153             LoggerFactory.getMeasurementLogger().d("Scheduled DebugReportingFallbackJobService");
154         } else {
155             LoggerFactory.getMeasurementLogger()
156                     .d("DebugReportingFallbackJobService already scheduled, skipping reschedule");
157         }
158     }
159 
skipAndCancelBackgroundJob( final JobParameters params, int skipReason, boolean doRecord)160     private boolean skipAndCancelBackgroundJob(
161             final JobParameters params, int skipReason, boolean doRecord) {
162         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
163         if (jobScheduler != null) {
164             jobScheduler.cancel(MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID);
165         }
166 
167         if (doRecord) {
168             AdServicesJobServiceLogger.getInstance()
169                     .recordJobSkipped(MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID, skipReason);
170         }
171 
172         // Tell the JobScheduler that the job is done and does not need to be rescheduled
173         jobFinished(params, false);
174 
175         // Returning false to reschedule this job.
176         return false;
177     }
178 
179     // TODO(b/311183933): Remove passed in Context from static method.
180     @SuppressWarnings("AvoidStaticContext")
buildJobInfo(Context context, Flags flags)181     private static JobInfo buildJobInfo(Context context, Flags flags) {
182         return new JobInfo.Builder(
183                         MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID,
184                         new ComponentName(context, DebugReportingFallbackJobService.class))
185                 .setRequiredNetworkType(
186                         flags.getMeasurementDebugReportingFallbackJobRequiredNetworkType())
187                 .setPeriodic(flags.getMeasurementDebugReportingFallbackJobPeriodMs())
188                 .setPersisted(flags.getMeasurementDebugReportingFallbackJobPersisted())
189                 .build();
190     }
191 
192     @VisibleForTesting
sendReports()193     void sendReports() {
194         JobLockHolder.getInstance(DEBUG_REPORTING)
195                 .runWithLock(
196                         "DebugReportingFallbackJobService",
197                         () -> {
198                             DatastoreManager datastoreManager =
199                                     DatastoreManagerFactory.getDatastoreManager();
200                             AndroidTimeSource timeSource = new AndroidTimeSource();
201                             new EventReportingJobHandler(
202                                             datastoreManager,
203                                             FlagsFactory.getFlags(),
204                                             AdServicesLoggerImpl.getInstance(),
205                                             ReportingStatus.ReportType.DEBUG_EVENT,
206                                             ReportingStatus.UploadMethod.FALLBACK,
207                                             getApplicationContext(),
208                                             timeSource)
209                                     .setIsDebugInstance(true)
210                                     .performScheduledPendingReportsInWindow(0, 0);
211                             new AggregateReportingJobHandler(
212                                             datastoreManager,
213                                             new AggregateEncryptionKeyManager(
214                                                     datastoreManager, getApplicationContext()),
215                                             FlagsFactory.getFlags(),
216                                             AdServicesLoggerImpl.getInstance(),
217                                             ReportingStatus.ReportType.DEBUG_AGGREGATE,
218                                             ReportingStatus.UploadMethod.FALLBACK,
219                                             getApplicationContext(),
220                                             timeSource)
221                                     .setIsDebugInstance(true)
222                                     .performScheduledPendingReportsInWindow(0, 0);
223                         });
224     }
225 
226     @VisibleForTesting
getFutureForTesting()227     Future getFutureForTesting() {
228         return mExecutorFuture;
229     }
230 }
231