Skip to content

Commit 989c753

Browse files
fredquintanaAndroid (Google) Code Review
authored andcommitted
Merge "fix expedited syncs. there were times when an expedited sync wouldn't correctly preempt non-expedited syncs" into jb-dev
2 parents 2551e5a + dc47556 commit 989c753

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed

core/java/android/content/SyncManager.java

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package android.content;
1818

1919
import com.android.internal.R;
20-
import com.android.internal.util.ArrayUtils;
2120
import com.google.android.collect.Lists;
2221
import com.google.android.collect.Maps;
2322

@@ -32,7 +31,6 @@
3231
import android.app.Notification;
3332
import android.app.NotificationManager;
3433
import android.app.PendingIntent;
35-
import android.app.DownloadManager.Request;
3634
import android.content.SyncStorageEngine.OnSyncRequestListener;
3735
import android.content.pm.ApplicationInfo;
3836
import android.content.pm.PackageManager;
@@ -1998,13 +1996,21 @@ private long maybeStartNextSyncLocked() {
19981996
ActiveSyncContext conflict = null;
19991997
ActiveSyncContext longRunning = null;
20001998
ActiveSyncContext toReschedule = null;
1999+
ActiveSyncContext oldestNonExpeditedRegular = null;
20012000

20022001
for (ActiveSyncContext activeSyncContext : mActiveSyncContexts) {
20032002
final SyncOperation activeOp = activeSyncContext.mSyncOperation;
20042003
if (activeOp.isInitialization()) {
20052004
numInit++;
20062005
} else {
20072006
numRegular++;
2007+
if (!activeOp.isExpedited()) {
2008+
if (oldestNonExpeditedRegular == null
2009+
|| (oldestNonExpeditedRegular.mStartTime
2010+
> activeSyncContext.mStartTime)) {
2011+
oldestNonExpeditedRegular = activeSyncContext;
2012+
}
2013+
}
20082014
}
20092015
if (activeOp.account.type.equals(candidate.account.type)
20102016
&& activeOp.authority.equals(candidate.authority)
@@ -2027,8 +2033,13 @@ private long maybeStartNextSyncLocked() {
20272033
Log.v(TAG, " numActiveInit=" + numInit + ", numActiveRegular=" + numRegular);
20282034
Log.v(TAG, " longRunning: " + longRunning);
20292035
Log.v(TAG, " conflict: " + conflict);
2036+
Log.v(TAG, " oldestNonExpeditedRegular: " + oldestNonExpeditedRegular);
20302037
}
20312038

2039+
final boolean roomAvailable = candidateIsInitialization
2040+
? numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS
2041+
: numRegular < MAX_SIMULTANEOUS_REGULAR_SYNCS;
2042+
20322043
if (conflict != null) {
20332044
if (candidateIsInitialization && !conflict.mSyncOperation.isInitialization()
20342045
&& numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS) {
@@ -2048,23 +2059,32 @@ private long maybeStartNextSyncLocked() {
20482059
} else {
20492060
continue;
20502061
}
2051-
} else {
2052-
final boolean roomAvailable = candidateIsInitialization
2053-
? numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS
2054-
: numRegular < MAX_SIMULTANEOUS_REGULAR_SYNCS;
2055-
if (roomAvailable) {
2056-
// dispatch candidate
2057-
} else if (longRunning != null
2058-
&& (candidateIsInitialization
2059-
== longRunning.mSyncOperation.isInitialization())) {
2060-
toReschedule = longRunning;
2061-
if (Log.isLoggable(TAG, Log.VERBOSE)) {
2062-
Log.v(TAG, "canceling and rescheduling sync since it ran roo long, "
2063-
+ longRunning);
2064-
}
2065-
} else {
2066-
continue;
2062+
} else if (roomAvailable) {
2063+
// dispatch candidate
2064+
} else if (candidate.isExpedited() && oldestNonExpeditedRegular != null
2065+
&& !candidateIsInitialization) {
2066+
// We found an active, non-expedited regular sync. We also know that the
2067+
// candidate doesn't conflict with this active sync since conflict
2068+
// is null. Reschedule the active sync and start the candidate.
2069+
toReschedule = oldestNonExpeditedRegular;
2070+
if (Log.isLoggable(TAG, Log.VERBOSE)) {
2071+
Log.v(TAG, "canceling and rescheduling sync since an expedited is ready to run, "
2072+
+ oldestNonExpeditedRegular);
20672073
}
2074+
} else if (longRunning != null
2075+
&& (candidateIsInitialization
2076+
== longRunning.mSyncOperation.isInitialization())) {
2077+
// We found an active, long-running sync. Reschedule the active
2078+
// sync and start the candidate.
2079+
toReschedule = longRunning;
2080+
if (Log.isLoggable(TAG, Log.VERBOSE)) {
2081+
Log.v(TAG, "canceling and rescheduling sync since it ran roo long, "
2082+
+ longRunning);
2083+
}
2084+
} else {
2085+
// we were unable to find or make space to run this candidate, go on to
2086+
// the next one
2087+
continue;
20682088
}
20692089

20702090
if (toReschedule != null) {
@@ -2516,7 +2536,7 @@ public long insertStartSyncEvent(SyncOperation syncOperation) {
25162536

25172537
return mSyncStorageEngine.insertStartSyncEvent(
25182538
syncOperation.account, syncOperation.userId, syncOperation.authority,
2519-
now, source);
2539+
now, source, syncOperation.isInitialization());
25202540
}
25212541

25222542
public void stopSyncEvent(long rowId, SyncOperation syncOperation, String resultMessage,

core/java/android/content/SyncOperation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ public boolean isInitialization() {
116116
return extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false);
117117
}
118118

119+
public boolean isExpedited() {
120+
return extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
121+
}
122+
119123
public boolean ignoreBackoff() {
120124
return extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false);
121125
}

core/java/android/content/SyncStorageEngine.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ public static class SyncHistoryItem {
221221
long upstreamActivity;
222222
long downstreamActivity;
223223
String mesg;
224+
boolean initialization;
224225
}
225226

226227
public static class DayStats {
@@ -1012,7 +1013,7 @@ public void reportActiveChange() {
10121013
* Note that sync has started for the given account and authority.
10131014
*/
10141015
public long insertStartSyncEvent(Account accountName, int userId, String authorityName,
1015-
long now, int source) {
1016+
long now, int source, boolean initialization) {
10161017
long id;
10171018
synchronized (mAuthorities) {
10181019
if (Log.isLoggable(TAG, Log.VERBOSE)) {
@@ -1025,6 +1026,7 @@ public long insertStartSyncEvent(Account accountName, int userId, String authori
10251026
return -1;
10261027
}
10271028
SyncHistoryItem item = new SyncHistoryItem();
1029+
item.initialization = initialization;
10281030
item.authorityId = authority.ident;
10291031
item.historyId = mNextHistoryId++;
10301032
if (mNextHistoryId < 0) mNextHistoryId = 0;

core/tests/coretests/src/android/content/SyncStorageEngineTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import android.accounts.Account;
2222
import android.os.Bundle;
23-
import android.os.Debug;
2423
import android.test.AndroidTestCase;
2524
import android.test.RenamingDelegatingContext;
2625
import android.test.mock.MockContentResolver;
@@ -57,7 +56,8 @@ public void testPurgeActiveSync() throws Exception {
5756

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

0 commit comments

Comments
 (0)