Bug 1291821 - Use sync deadline to decide of batching downloader should proceed r=rnewman

MozReview-Commit-ID: IDgIj9lBt61

--HG--
extra : rebase_source : a3d1773abb50748631e28c0aa14797b17b857def
This commit is contained in:
Grisha Kruglov 2016-11-01 18:52:18 -07:00
Родитель 2c49e9087a
Коммит dc5b52e557
7 изменённых файлов: 35 добавлений и 1 удалений

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

@ -24,6 +24,7 @@ public class ConstrainedServer11Repository extends Server11Repository {
public ConstrainedServer11Repository( public ConstrainedServer11Repository(
String collection, String collection,
long syncDeadline,
String storageURL, String storageURL,
AuthHeaderProvider authHeaderProvider, AuthHeaderProvider authHeaderProvider,
InfoCollections infoCollections, InfoCollections infoCollections,
@ -33,6 +34,7 @@ public class ConstrainedServer11Repository extends Server11Repository {
boolean allowMultipleBatches) throws URISyntaxException { boolean allowMultipleBatches) throws URISyntaxException {
super( super(
collection, collection,
syncDeadline,
storageURL, storageURL,
authHeaderProvider, authHeaderProvider,
infoCollections, infoCollections,

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

@ -25,7 +25,7 @@ import android.support.annotation.Nullable;
public class Server11Repository extends Repository { public class Server11Repository extends Repository {
public final AuthHeaderProvider authHeaderProvider; public final AuthHeaderProvider authHeaderProvider;
/* package-local */ final long syncDeadline; private final long syncDeadlineMillis;
/* package-local */ final URI collectionURI; /* package-local */ final URI collectionURI;
protected final String collection; protected final String collection;
@ -46,6 +46,7 @@ public class Server11Repository extends Repository {
*/ */
public Server11Repository( public Server11Repository(
@NonNull String collection, @NonNull String collection,
long syncDeadlineMillis,
@NonNull String storageURL, @NonNull String storageURL,
AuthHeaderProvider authHeaderProvider, AuthHeaderProvider authHeaderProvider,
@NonNull InfoCollections infoCollections, @NonNull InfoCollections infoCollections,
@ -60,6 +61,7 @@ public class Server11Repository extends Repository {
throw new IllegalArgumentException("infoCollections must not be null"); throw new IllegalArgumentException("infoCollections must not be null");
} }
this.collection = collection; this.collection = collection;
this.syncDeadlineMillis = syncDeadlineMillis;
this.collectionURI = new URI(storageURL + (storageURL.endsWith("/") ? collection : "/" + collection)); this.collectionURI = new URI(storageURL + (storageURL.endsWith("/") ? collection : "/" + collection));
this.authHeaderProvider = authHeaderProvider; this.authHeaderProvider = authHeaderProvider;
this.infoCollections = infoCollections; this.infoCollections = infoCollections;
@ -100,4 +102,13 @@ public class Server11Repository extends Repository {
public boolean getAllowMultipleBatches() { public boolean getAllowMultipleBatches() {
return true; return true;
} }
/**
* A point in time by which this repository's session must complete fetch and store operations.
* Particularly pertinent for batching downloads performed by the session (should we fetch
* another batch?) and buffered repositories (do we have enough time to merge what we've downloaded?).
*/
public long getSyncDeadline() {
return syncDeadlineMillis;
}
} }

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

@ -112,6 +112,7 @@ public class Server11RepositorySession extends RepositorySession {
return new BatchingDownloader( return new BatchingDownloader(
serverRepositorySession.serverRepository.authHeaderProvider, serverRepositorySession.serverRepository.authHeaderProvider,
Uri.parse(serverRepositorySession.serverRepository.collectionURI().toString()), Uri.parse(serverRepositorySession.serverRepository.collectionURI().toString()),
serverRepositorySession.serverRepository.getSyncDeadline(),
serverRepositorySession.serverRepository.getAllowMultipleBatches(), serverRepositorySession.serverRepository.getAllowMultipleBatches(),
serverRepositorySession); serverRepositorySession);
} }

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

@ -58,6 +58,7 @@ public class BatchingDownloader {
private final RepositorySession repositorySession; private final RepositorySession repositorySession;
private final DelayedWorkTracker workTracker = new DelayedWorkTracker(); private final DelayedWorkTracker workTracker = new DelayedWorkTracker();
private final Uri baseCollectionUri; private final Uri baseCollectionUri;
private final long fetchDeadline;
private final boolean allowMultipleBatches; private final boolean allowMultipleBatches;
/* package-local */ final AuthHeaderProvider authHeaderProvider; /* package-local */ final AuthHeaderProvider authHeaderProvider;
@ -70,12 +71,14 @@ public class BatchingDownloader {
public BatchingDownloader( public BatchingDownloader(
AuthHeaderProvider authHeaderProvider, AuthHeaderProvider authHeaderProvider,
Uri baseCollectionUri, Uri baseCollectionUri,
long fetchDeadline,
boolean allowMultipleBatches, boolean allowMultipleBatches,
RepositorySession repositorySession) { RepositorySession repositorySession) {
this.repositorySession = repositorySession; this.repositorySession = repositorySession;
this.authHeaderProvider = authHeaderProvider; this.authHeaderProvider = authHeaderProvider;
this.baseCollectionUri = baseCollectionUri; this.baseCollectionUri = baseCollectionUri;
this.allowMultipleBatches = allowMultipleBatches; this.allowMultipleBatches = allowMultipleBatches;
this.fetchDeadline = fetchDeadline;
} }
@VisibleForTesting @VisibleForTesting
@ -217,6 +220,12 @@ public class BatchingDownloader {
} }
}); });
// Should we proceed, however? Do we have enough time?
if (!mayProceedWithBatching(fetchDeadline)) {
this.abort(fetchRecordsDelegate, new Exception("Not enough time to complete next batch"));
return;
}
// Create and execute new batch request. // Create and execute new batch request.
try { try {
final SyncStorageCollectionRequest newRequest = makeSyncStorageCollectionRequest(newer, final SyncStorageCollectionRequest newRequest = makeSyncStorageCollectionRequest(newer,
@ -293,6 +302,14 @@ public class BatchingDownloader {
} }
}); });
} }
private static boolean mayProceedWithBatching(long deadline) {
// For simplicity, allow batching to proceed if there's at least a minute left for the sync.
// This should be enough to fetch and process records in the batch.
final long timeLeft = deadline - SystemClock.elapsedRealtime();
return timeLeft > TimeUnit.MINUTES.toMillis(1);
}
@VisibleForTesting @VisibleForTesting
public static URI buildCollectionURI(Uri baseCollectionUri, boolean full, long newer, long limit, String sort, String ids, String offset) throws URISyntaxException { public static URI buildCollectionURI(Uri baseCollectionUri, boolean full, long newer, long limit, String sort, String ids, String offset) throws URISyntaxException {
Uri.Builder uriBuilder = baseCollectionUri.buildUpon(); Uri.Builder uriBuilder = baseCollectionUri.buildUpon();

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

@ -41,6 +41,7 @@ public class AndroidBrowserBookmarksServerSyncStage extends ServerSyncStage {
protected Repository getRemoteRepository() throws URISyntaxException { protected Repository getRemoteRepository() throws URISyntaxException {
return new ConstrainedServer11Repository( return new ConstrainedServer11Repository(
getCollection(), getCollection(),
session.getSyncDeadline(),
session.config.storageURL(), session.config.storageURL(),
session.getAuthHeaderProvider(), session.getAuthHeaderProvider(),
session.config.infoCollections, session.config.infoCollections,

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

@ -46,6 +46,7 @@ public class AndroidBrowserHistoryServerSyncStage extends ServerSyncStage {
protected Repository getRemoteRepository() throws URISyntaxException { protected Repository getRemoteRepository() throws URISyntaxException {
return new ConstrainedServer11Repository( return new ConstrainedServer11Repository(
getCollection(), getCollection(),
session.getSyncDeadline(),
session.config.storageURL(), session.config.storageURL(),
session.getAuthHeaderProvider(), session.getAuthHeaderProvider(),
session.config.infoCollections, session.config.infoCollections,

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

@ -144,6 +144,7 @@ public abstract class ServerSyncStage extends AbstractSessionManagingSyncStage i
protected Repository getRemoteRepository() throws URISyntaxException { protected Repository getRemoteRepository() throws URISyntaxException {
String collection = getCollection(); String collection = getCollection();
return new Server11Repository(collection, return new Server11Repository(collection,
session.getSyncDeadline(),
session.config.storageURL(), session.config.storageURL(),
session.getAuthHeaderProvider(), session.getAuthHeaderProvider(),
session.config.infoCollections, session.config.infoCollections,