• 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.VERBOSE_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_VERBOSE_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.util.JobLockHolder;
39 import com.android.adservices.service.stats.AdServicesLoggerImpl;
40 import com.android.adservices.spe.AdServicesJobServiceLogger;
41 import com.android.internal.annotations.VisibleForTesting;
42 
43 import com.google.common.util.concurrent.ListeningExecutorService;
44 
45 import java.time.Clock;
46 import java.time.Instant;
47 import java.util.concurrent.Future;
48 
49 /**
50  * Fallback service for scheduling debug reporting jobs. This runs periodically to handle any
51  * reports that the {@link DebugReportingJobService } failed/missed. The actual job execution logic
52  * is part of {@link DebugReportingJobHandler }.
53  */
54 // TODO(b/311183933): Remove passed in Context from static method.
55 @SuppressWarnings("AvoidStaticContext")
56 public class VerboseDebugReportingFallbackJobService extends JobService {
57 
58     private static final int MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID =
59             MEASUREMENT_VERBOSE_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 VerboseDebugReportingFallbackJobService 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_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID);
79 
80         if (FlagsFactory.getFlags().getMeasurementVerboseDebugReportingFallbackJobKillSwitch()) {
81             LoggerFactory.getMeasurementLogger()
82                     .e("VerboseDebugReportingFallbackJobService is disabled.");
83             return skipAndCancelBackgroundJob(
84                     params,
85                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON,
86                     /* doRecord */ true);
87         }
88 
89         Instant jobStartTime = Clock.systemUTC().instant();
90         LoggerFactory.getMeasurementLogger()
91                 .d(
92                         "VerboseDebugReportingFallbackJobService.onStartJob " + "at %s",
93                         jobStartTime.toString());
94         mExecutorFuture =
95                 sBlockingExecutor.submit(
96                         () -> {
97                             sendReports();
98                             boolean shouldRetry = false;
99                             AdServicesJobServiceLogger.getInstance()
100                                     .recordJobFinished(
101                                             MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID,
102                                             /* isSuccessful */ true,
103                                             shouldRetry);
104 
105                             jobFinished(params, false);
106                         });
107         return true;
108     }
109 
110     @Override
onStopJob(JobParameters params)111     public boolean onStopJob(JobParameters params) {
112         LoggerFactory.getMeasurementLogger().d("VerboseDebugReportingJobService.onStopJob");
113         boolean shouldRetry = true;
114         if (mExecutorFuture != null) {
115             shouldRetry = mExecutorFuture.cancel(/* mayInterruptIfRunning */ true);
116         }
117         AdServicesJobServiceLogger.getInstance()
118                 .recordOnStopJob(
119                         params, MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID, shouldRetry);
120         return shouldRetry;
121     }
122 
123     @VisibleForTesting
schedule(JobScheduler jobScheduler, JobInfo job)124     protected static void schedule(JobScheduler jobScheduler, JobInfo job) {
125         jobScheduler.schedule(job);
126     }
127 
128     /**
129      * Schedule Verbose Debug Reporting Fallback Job Service if it is not already scheduled.
130      *
131      * @param context the context
132      * @param forceSchedule flag to indicate whether to force rescheduling the job.
133      */
scheduleIfNeeded(Context context, boolean forceSchedule)134     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
135         Flags flags = FlagsFactory.getFlags();
136         if (flags.getMeasurementVerboseDebugReportingFallbackJobKillSwitch()) {
137             LoggerFactory.getMeasurementLogger()
138                     .e("VerboseDebugReportingFallbackJobService is disabled, skip scheduling");
139             return;
140         }
141 
142         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
143         if (jobScheduler == null) {
144             LoggerFactory.getMeasurementLogger().e("JobScheduler not found");
145             return;
146         }
147 
148         final JobInfo scheduledJob =
149                 jobScheduler.getPendingJob(MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID);
150         JobInfo jobInfo = buildJobInfo(context, flags);
151         // Schedule if it hasn't been scheduled already or force rescheduling
152         if (forceSchedule || !jobInfo.equals(scheduledJob)) {
153             schedule(jobScheduler, jobInfo);
154             LoggerFactory.getMeasurementLogger()
155                     .d("Scheduled VerboseDebugReportingFallbackJobService");
156         } else {
157             LoggerFactory.getMeasurementLogger()
158                     .d(
159                             "VerboseDebugReportingFallbackJobService already scheduled, skipping"
160                                     + " reschedule");
161         }
162     }
163 
buildJobInfo(Context context, Flags flags)164     private static JobInfo buildJobInfo(Context context, Flags flags) {
165         return new JobInfo.Builder(
166                         MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID,
167                         new ComponentName(context, VerboseDebugReportingFallbackJobService.class))
168                 .setRequiredNetworkType(
169                         flags.getMeasurementVerboseDebugReportingJobRequiredNetworkType())
170                 .setPeriodic(flags.getMeasurementVerboseDebugReportingFallbackJobPeriodMs())
171                 .setPersisted(flags.getMeasurementVerboseDebugReportingFallbackJobPersisted())
172                 .build();
173     }
174 
skipAndCancelBackgroundJob( final JobParameters params, int skipReason, boolean doRecord)175     private boolean skipAndCancelBackgroundJob(
176             final JobParameters params, int skipReason, boolean doRecord) {
177         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
178         if (jobScheduler != null) {
179             jobScheduler.cancel(MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID);
180         }
181 
182         if (doRecord) {
183             AdServicesJobServiceLogger.getInstance()
184                     .recordJobSkipped(
185                             MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID, skipReason);
186         }
187 
188         // Tell the JobScheduler that the job is done and does not need to be rescheduled
189         jobFinished(params, false);
190 
191         // Returning false to reschedule this job.
192         return false;
193     }
194 
195     @VisibleForTesting
sendReports()196     void sendReports() {
197         JobLockHolder.getInstance(VERBOSE_DEBUG_REPORTING)
198                 .runWithLock(
199                         "VerboseDebugReportingFallbackJobService",
200                         () -> {
201                             DatastoreManager datastoreManager =
202                                     DatastoreManagerFactory.getDatastoreManager();
203                             new DebugReportingJobHandler(
204                                             datastoreManager,
205                                             FlagsFactory.getFlags(),
206                                             AdServicesLoggerImpl.getInstance(),
207                                             ReportingStatus.UploadMethod.FALLBACK,
208                                             getApplicationContext())
209                                     .performScheduledPendingReports();
210                         });
211     }
212 
213     @VisibleForTesting
getFutureForTesting()214     Future getFutureForTesting() {
215         return mExecutorFuture;
216     }
217 }
218