Bug 1291821 - Keep track of sync deadline r=rnewman

MozReview-Commit-ID: Fvuc05K1arV

--HG--
extra : rebase_source : 74b0d4dc58c0cd2c1113253eee28ba783e114803
This commit is contained in:
Grisha Kruglov 2016-11-08 12:38:00 -08:00
Родитель 2b78e4d632
Коммит 2c49e9087a
2 изменённых файлов: 30 добавлений и 7 удалений

Просмотреть файл

@ -60,6 +60,7 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter { public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
private static final String LOG_TAG = FxAccountSyncAdapter.class.getSimpleName(); private static final String LOG_TAG = FxAccountSyncAdapter.class.getSimpleName();
@ -74,6 +75,11 @@ public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
private static final int MINIMUM_SYNC_DELAY_MILLIS = 15 * 1000; // 15 seconds. private static final int MINIMUM_SYNC_DELAY_MILLIS = 15 * 1000; // 15 seconds.
private volatile long lastSyncRealtimeMillis; private volatile long lastSyncRealtimeMillis;
// Non-user initiated sync can't take longer than 30 minutes.
// To ensure we're not churning through device's battery/resources, we limit sync to 10 minutes,
// and request a re-sync if we hit that deadline.
private static final long SYNC_DEADLINE_DELTA_MILLIS = TimeUnit.MINUTES.toMillis(10);
protected final ExecutorService executor; protected final ExecutorService executor;
protected final FxAccountNotificationManager notificationManager; protected final FxAccountNotificationManager notificationManager;
@ -231,8 +237,7 @@ public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
return forced; return forced;
} }
protected void syncWithAssertion(final String audience, protected void syncWithAssertion(final String assertion,
final String assertion,
final URI tokenServerEndpointURI, final URI tokenServerEndpointURI,
final BackoffHandler tokenBackoffHandler, final BackoffHandler tokenBackoffHandler,
final SharedPreferences sharedPrefs, final SharedPreferences sharedPrefs,
@ -240,7 +245,8 @@ public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
final String clientState, final String clientState,
final SessionCallback callback, final SessionCallback callback,
final Bundle extras, final Bundle extras,
final AndroidFxAccount fxAccount) { final AndroidFxAccount fxAccount,
final long syncDeadline) {
final TokenServerClientDelegate delegate = new TokenServerClientDelegate() { final TokenServerClientDelegate delegate = new TokenServerClientDelegate() {
private boolean didReceiveBackoff = false; private boolean didReceiveBackoff = false;
@ -321,7 +327,7 @@ public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
syncConfig.setClusterURL(storageServerURI); syncConfig.setClusterURL(storageServerURI);
globalSession = new GlobalSession(syncConfig, callback, context, clientsDataDelegate); globalSession = new GlobalSession(syncConfig, callback, context, clientsDataDelegate);
globalSession.start(); globalSession.start(syncDeadline);
} catch (Exception e) { } catch (Exception e) {
callback.handleError(globalSession, e); callback.handleError(globalSession, e);
return; return;
@ -386,6 +392,10 @@ public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
final Context context = getContext(); final Context context = getContext();
final AndroidFxAccount fxAccount = new AndroidFxAccount(context, account); final AndroidFxAccount fxAccount = new AndroidFxAccount(context, account);
// NB: we use elapsedRealtime which is time since boot, to ensure our clock is monotonic and isn't
// paused while CPU is in the power-saving mode.
final long syncDeadline = SystemClock.elapsedRealtime() + SYNC_DEADLINE_DELTA_MILLIS;
Logger.info(LOG_TAG, "Syncing FxAccount" + Logger.info(LOG_TAG, "Syncing FxAccount" +
" account named like " + Utils.obfuscateEmail(account.name) + " account named like " + Utils.obfuscateEmail(account.name) +
" for authority " + authority + " for authority " + authority +
@ -536,7 +546,9 @@ public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
final SessionCallback sessionCallback = new SessionCallback(syncDelegate, schedulePolicy); final SessionCallback sessionCallback = new SessionCallback(syncDelegate, schedulePolicy);
final KeyBundle syncKeyBundle = married.getSyncKeyBundle(); final KeyBundle syncKeyBundle = married.getSyncKeyBundle();
final String clientState = married.getClientState(); final String clientState = married.getClientState();
syncWithAssertion(audience, assertion, tokenServerEndpointURI, tokenBackoffHandler, sharedPrefs, syncKeyBundle, clientState, sessionCallback, extras, fxAccount); syncWithAssertion(
assertion, tokenServerEndpointURI, tokenBackoffHandler, sharedPrefs,
syncKeyBundle, clientState, sessionCallback, extras, fxAccount, syncDeadline);
// Register the device if necessary (asynchronous, in another thread) // Register the device if necessary (asynchronous, in another thread)
if (fxAccount.getDeviceRegistrationVersion() != FxAccountDeviceRegistrator.DEVICE_REGISTRATION_VERSION if (fxAccount.getDeviceRegistrationVersion() != FxAccountDeviceRegistrator.DEVICE_REGISTRATION_VERSION

Просмотреть файл

@ -74,6 +74,8 @@ public class GlobalSession implements HttpResponseObserver {
protected final Context context; protected final Context context;
protected final ClientsDataDelegate clientsDelegate; protected final ClientsDataDelegate clientsDelegate;
private long syncDeadline;
/** /**
* Map from engine name to new settings for an updated meta/global record. * Map from engine name to new settings for an updated meta/global record.
* Engines to remove will have <code>null</code> EngineSettings. * Engines to remove will have <code>null</code> EngineSettings.
@ -234,6 +236,10 @@ public class GlobalSession implements HttpResponseObserver {
return out; return out;
} }
public long getSyncDeadline() {
return syncDeadline;
}
/** /**
* Advance and loop around the stages of a sync. * Advance and loop around the stages of a sync.
* @param current * @param current
@ -293,10 +299,14 @@ public class GlobalSession implements HttpResponseObserver {
* *
* @throws AlreadySyncingException * @throws AlreadySyncingException
*/ */
public void start() throws AlreadySyncingException { public void start(final long syncDeadline) throws AlreadySyncingException {
if (this.currentState != GlobalSyncStage.Stage.idle) { if (this.currentState != GlobalSyncStage.Stage.idle) {
throw new AlreadySyncingException(this.currentState); throw new AlreadySyncingException(this.currentState);
} }
// Make the deadline value available to stages via its getter.
this.syncDeadline = syncDeadline;
installAsHttpResponseObserver(); // Uninstalled by completeSync or abort. installAsHttpResponseObserver(); // Uninstalled by completeSync or abort.
this.advance(); this.advance();
} }
@ -311,7 +321,8 @@ public class GlobalSession implements HttpResponseObserver {
this.callback.handleAborted(this, "Told to back off."); this.callback.handleAborted(this, "Told to back off.");
return; return;
} }
this.start(); // Restart with the same deadline as before.
this.start(syncDeadline);
} }
/** /**