Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0fc16f3e authored by Xin Guan's avatar Xin Guan
Browse files

Count proxy jobs toward scheduling limit.

Limit schedule calls for persisted proxy jobs.

Bug: 377912323
Bug: 299930087
Test: atest FrameworksMockingServicesTests:com.android.server.job.JobSchedulerServiceTest
Flag: com.android.server.job.enforce_schedule_limit_to_proxy_jobs
Change-Id: I4ebcdd6ee299347d7d5cf6e228ba441b73db7481
parent 023c47db
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -96,3 +96,13 @@ flag {
   description: "Apply the quota policy to jobs started when the app was in TOP state"
   bug: "374323858"
}

flag {
    name: "enforce_schedule_limit_to_proxy_jobs"
    namespace: "backstage_power"
    description: "Limit the schedule calls towards the persisted proxy jobs"
    bug: "377912323"
    metadata {
        purpose: PURPOSE_BUGFIX
    }
}
 No newline at end of file
+3 −2
Original line number Diff line number Diff line
@@ -1718,8 +1718,9 @@ public class JobSchedulerService extends com.android.server.SystemService
            int userId, @Nullable String namespace, String tag) {
        // Rate limit excessive schedule() calls.
        final String servicePkg = job.getService().getPackageName();
        if (job.isPersisted() && (packageName == null || packageName.equals(servicePkg))) {
            // Only limit schedule calls for persisted jobs scheduled by the app itself.
        if (job.isPersisted() && (Flags.enforceScheduleLimitToProxyJobs()
                || (packageName == null || packageName.equals(servicePkg)))) {
            // limit excessive schedule calls for persisted jobs.
            final String pkg = packageName == null ? servicePkg : packageName;
            if (!mQuotaTracker.isWithinQuota(userId, pkg, QUOTA_TRACKER_SCHEDULE_PERSISTED_TAG)) {
                if (mQuotaTracker.isWithinQuota(userId, pkg, QUOTA_TRACKER_SCHEDULE_LOGGED)) {
+30 −2
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ import android.os.ServiceManager;
import android.os.SystemClock;
import android.os.WorkSource;
import android.os.WorkSource.WorkChain;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
@@ -2319,11 +2320,12 @@ public class JobSchedulerServiceTest {
    }

    /**
     * Tests that jobs scheduled through a proxy (eg. system server) don't count towards scheduling
     * Tests that jobs scheduled through a proxy (eg. system server) count towards scheduling
     * limits.
     */
    @Test
    public void testScheduleLimiting_Proxy() {
    @DisableFlags(Flags.FLAG_ENFORCE_SCHEDULE_LIMIT_TO_PROXY_JOBS)
    public void testScheduleLimiting_Proxy_NotCountTowardsLimit() {
        mService.mConstants.ENABLE_API_QUOTAS = true;
        mService.mConstants.API_QUOTA_SCHEDULE_COUNT = 300;
        mService.mConstants.API_QUOTA_SCHEDULE_WINDOW_MS = 300000;
@@ -2341,6 +2343,32 @@ public class JobSchedulerServiceTest {
        }
    }

    /**
     * Tests that jobs scheduled through a proxy (eg. system server) don't count towards scheduling
     * limits.
     */
    @Test
    @EnableFlags(Flags.FLAG_ENFORCE_SCHEDULE_LIMIT_TO_PROXY_JOBS)
    public void testScheduleLimiting_Proxy_CountTowardsLimit() {
        mService.mConstants.ENABLE_API_QUOTAS = true;
        mService.mConstants.API_QUOTA_SCHEDULE_COUNT = 300;
        mService.mConstants.API_QUOTA_SCHEDULE_WINDOW_MS = 300000;
        mService.mConstants.API_QUOTA_SCHEDULE_THROW_EXCEPTION = false;
        mService.mConstants.API_QUOTA_SCHEDULE_RETURN_FAILURE_RESULT = true;
        mService.updateQuotaTracker();
        mService.resetScheduleQuota();

        final JobInfo job = createJobInfo().setPersisted(true).build();
        for (int i = 0; i < 500; ++i) {
            final int expected =
                    i < 300 ? JobScheduler.RESULT_SUCCESS : JobScheduler.RESULT_FAILURE;
            assertEquals("Got unexpected result for schedule #" + (i + 1),
                    expected,
                    mService.scheduleAsPackage(job, null, TEST_UID, "proxied.package", 0, "JSSTest",
                            ""));
        }
    }

    /**
     * Tests that jobs scheduled by an app for itself as if through a proxy are counted towards
     * scheduling limits.