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

Commit dc47556c authored by Fred Quintana's avatar Fred Quintana
Browse files

fix expedited syncs. there were times when an expedited sync wouldn't

correctly preempt non-expedited syncs

Change-Id: Ia88ce6504c06d7c8e50e40362e8bf2b85bd0934b
parent d0c66f6a
Loading
Loading
Loading
Loading
+39 −19
Original line number Original line Diff line number Diff line
@@ -17,7 +17,6 @@
package android.content;
package android.content;


import com.android.internal.R;
import com.android.internal.R;
import com.android.internal.util.ArrayUtils;
import com.google.android.collect.Lists;
import com.google.android.collect.Lists;
import com.google.android.collect.Maps;
import com.google.android.collect.Maps;


@@ -32,7 +31,6 @@ import android.app.AppGlobals;
import android.app.Notification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.PendingIntent;
import android.app.DownloadManager.Request;
import android.content.SyncStorageEngine.OnSyncRequestListener;
import android.content.SyncStorageEngine.OnSyncRequestListener;
import android.content.pm.ApplicationInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
@@ -1998,6 +1996,7 @@ public class SyncManager implements OnAccountsUpdateListener {
                ActiveSyncContext conflict = null;
                ActiveSyncContext conflict = null;
                ActiveSyncContext longRunning = null;
                ActiveSyncContext longRunning = null;
                ActiveSyncContext toReschedule = null;
                ActiveSyncContext toReschedule = null;
                ActiveSyncContext oldestNonExpeditedRegular = null;


                for (ActiveSyncContext activeSyncContext : mActiveSyncContexts) {
                for (ActiveSyncContext activeSyncContext : mActiveSyncContexts) {
                    final SyncOperation activeOp = activeSyncContext.mSyncOperation;
                    final SyncOperation activeOp = activeSyncContext.mSyncOperation;
@@ -2005,6 +2004,13 @@ public class SyncManager implements OnAccountsUpdateListener {
                        numInit++;
                        numInit++;
                    } else {
                    } else {
                        numRegular++;
                        numRegular++;
                        if (!activeOp.isExpedited()) {
                            if (oldestNonExpeditedRegular == null
                                || (oldestNonExpeditedRegular.mStartTime
                                    > activeSyncContext.mStartTime)) {
                                oldestNonExpeditedRegular = activeSyncContext;
                            }
                        }
                    }
                    }
                    if (activeOp.account.type.equals(candidate.account.type)
                    if (activeOp.account.type.equals(candidate.account.type)
                            && activeOp.authority.equals(candidate.authority)
                            && activeOp.authority.equals(candidate.authority)
@@ -2027,8 +2033,13 @@ public class SyncManager implements OnAccountsUpdateListener {
                    Log.v(TAG, "  numActiveInit=" + numInit + ", numActiveRegular=" + numRegular);
                    Log.v(TAG, "  numActiveInit=" + numInit + ", numActiveRegular=" + numRegular);
                    Log.v(TAG, "  longRunning: " + longRunning);
                    Log.v(TAG, "  longRunning: " + longRunning);
                    Log.v(TAG, "  conflict: " + conflict);
                    Log.v(TAG, "  conflict: " + conflict);
                    Log.v(TAG, "  oldestNonExpeditedRegular: " + oldestNonExpeditedRegular);
                }
                }


                final boolean roomAvailable = candidateIsInitialization
                        ? numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS
                        : numRegular < MAX_SIMULTANEOUS_REGULAR_SYNCS;

                if (conflict != null) {
                if (conflict != null) {
                    if (candidateIsInitialization && !conflict.mSyncOperation.isInitialization()
                    if (candidateIsInitialization && !conflict.mSyncOperation.isInitialization()
                            && numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS) {
                            && numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS) {
@@ -2048,24 +2059,33 @@ public class SyncManager implements OnAccountsUpdateListener {
                    } else {
                    } else {
                        continue;
                        continue;
                    }
                    }
                } else {
                } else if (roomAvailable) {
                    final boolean roomAvailable = candidateIsInitialization
                            ? numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS
                            : numRegular < MAX_SIMULTANEOUS_REGULAR_SYNCS;
                    if (roomAvailable) {
                    // dispatch candidate
                    // dispatch candidate
                } else if (candidate.isExpedited() && oldestNonExpeditedRegular != null
                           && !candidateIsInitialization) {
                    // We found an active, non-expedited regular sync. We also know that the
                    // candidate doesn't conflict with this active sync since conflict
                    // is null. Reschedule the active sync and start the candidate.
                    toReschedule = oldestNonExpeditedRegular;
                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
                        Log.v(TAG, "canceling and rescheduling sync since an expedited is ready to run, "
                                + oldestNonExpeditedRegular);
                    }
                } else if (longRunning != null
                } else if (longRunning != null
                        && (candidateIsInitialization
                        && (candidateIsInitialization
                            == longRunning.mSyncOperation.isInitialization())) {
                            == longRunning.mSyncOperation.isInitialization())) {
                    // We found an active, long-running sync. Reschedule the active
                    // sync and start the candidate.
                    toReschedule = longRunning;
                    toReschedule = longRunning;
                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
                        Log.v(TAG, "canceling and rescheduling sync since it ran roo long, "
                        Log.v(TAG, "canceling and rescheduling sync since it ran roo long, "
                              + longRunning);
                              + longRunning);
                    }
                    }
                } else {
                } else {
                    // we were unable to find or make space to run this candidate, go on to
                    // the next one
                    continue;
                    continue;
                }
                }
                }


                if (toReschedule != null) {
                if (toReschedule != null) {
                    runSyncFinishedOrCanceledLocked(null, toReschedule);
                    runSyncFinishedOrCanceledLocked(null, toReschedule);
@@ -2516,7 +2536,7 @@ public class SyncManager implements OnAccountsUpdateListener {


            return mSyncStorageEngine.insertStartSyncEvent(
            return mSyncStorageEngine.insertStartSyncEvent(
                    syncOperation.account, syncOperation.userId, syncOperation.authority,
                    syncOperation.account, syncOperation.userId, syncOperation.authority,
                    now, source);
                    now, source, syncOperation.isInitialization());
        }
        }


        public void stopSyncEvent(long rowId, SyncOperation syncOperation, String resultMessage,
        public void stopSyncEvent(long rowId, SyncOperation syncOperation, String resultMessage,
+4 −0
Original line number Original line Diff line number Diff line
@@ -116,6 +116,10 @@ public class SyncOperation implements Comparable {
        return extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false);
        return extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false);
    }
    }


    public boolean isExpedited() {
        return extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
    }

    public boolean ignoreBackoff() {
    public boolean ignoreBackoff() {
        return extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false);
        return extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false);
    }
    }
+3 −1
Original line number Original line Diff line number Diff line
@@ -221,6 +221,7 @@ public class SyncStorageEngine extends Handler {
        long upstreamActivity;
        long upstreamActivity;
        long downstreamActivity;
        long downstreamActivity;
        String mesg;
        String mesg;
        boolean initialization;
    }
    }


    public static class DayStats {
    public static class DayStats {
@@ -1012,7 +1013,7 @@ public class SyncStorageEngine extends Handler {
     * Note that sync has started for the given account and authority.
     * Note that sync has started for the given account and authority.
     */
     */
    public long insertStartSyncEvent(Account accountName, int userId, String authorityName,
    public long insertStartSyncEvent(Account accountName, int userId, String authorityName,
            long now, int source) {
                                     long now, int source, boolean initialization) {
        long id;
        long id;
        synchronized (mAuthorities) {
        synchronized (mAuthorities) {
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
@@ -1025,6 +1026,7 @@ public class SyncStorageEngine extends Handler {
                return -1;
                return -1;
            }
            }
            SyncHistoryItem item = new SyncHistoryItem();
            SyncHistoryItem item = new SyncHistoryItem();
            item.initialization = initialization;
            item.authorityId = authority.ident;
            item.authorityId = authority.ident;
            item.historyId = mNextHistoryId++;
            item.historyId = mNextHistoryId++;
            if (mNextHistoryId < 0) mNextHistoryId = 0;
            if (mNextHistoryId < 0) mNextHistoryId = 0;
+2 −2
Original line number Original line Diff line number Diff line
@@ -20,7 +20,6 @@ import com.android.internal.os.AtomicFile;


import android.accounts.Account;
import android.accounts.Account;
import android.os.Bundle;
import android.os.Bundle;
import android.os.Debug;
import android.test.AndroidTestCase;
import android.test.AndroidTestCase;
import android.test.RenamingDelegatingContext;
import android.test.RenamingDelegatingContext;
import android.test.mock.MockContentResolver;
import android.test.mock.MockContentResolver;
@@ -57,7 +56,8 @@ public class SyncStorageEngineTest extends AndroidTestCase {


        long time0 = 1000;
        long time0 = 1000;
        long historyId = engine.insertStartSyncEvent(
        long historyId = engine.insertStartSyncEvent(
                account, 0, authority, time0, SyncStorageEngine.SOURCE_LOCAL);
                account, 0, authority, time0, SyncStorageEngine.SOURCE_LOCAL,
                false /* initialization */);
        long time1 = time0 + SyncStorageEngine.MILLIS_IN_4WEEKS * 2;
        long time1 = time0 + SyncStorageEngine.MILLIS_IN_4WEEKS * 2;
        engine.stopSyncEvent(historyId, time1 - time0, "yay", 0, 0);
        engine.stopSyncEvent(historyId, time1 - time0, "yay", 0, 0);
    }
    }