mobile/android/base/background/healthreport/upload/SubmissionPolicy.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.background.healthreport.upload;
michael@0 6
michael@0 7 import java.net.MalformedURLException;
michael@0 8 import java.net.SocketException;
michael@0 9 import java.net.UnknownHostException;
michael@0 10 import java.util.Collection;
michael@0 11
michael@0 12 import org.mozilla.gecko.background.common.log.Logger;
michael@0 13 import org.mozilla.gecko.background.healthreport.HealthReportConstants;
michael@0 14 import org.mozilla.gecko.background.healthreport.HealthReportUtils;
michael@0 15 import org.mozilla.gecko.background.healthreport.upload.SubmissionClient.Delegate;
michael@0 16
michael@0 17 import android.content.SharedPreferences;
michael@0 18
michael@0 19 /**
michael@0 20 * Manages scheduling of Firefox Health Report data submission.
michael@0 21 *
michael@0 22 * The rules of data submission are as follows:
michael@0 23 *
michael@0 24 * 1. Do not submit data more than once every 24 hours.
michael@0 25 *
michael@0 26 * 2. Try to submit as close to 24 hours apart as possible.
michael@0 27 *
michael@0 28 * 3. Do not submit too soon after application startup so as to not negatively
michael@0 29 * impact performance at startup.
michael@0 30 *
michael@0 31 * 4. Before first ever data submission, the user should be notified about data
michael@0 32 * collection practices.
michael@0 33 *
michael@0 34 * 5. User should have opportunity to react to this notification before data
michael@0 35 * submission.
michael@0 36 *
michael@0 37 * 6. Display of notification without any explicit user action constitutes
michael@0 38 * implicit consent after a certain duration of time.
michael@0 39 *
michael@0 40 * 7. If data submission fails, try at most 2 additional times before giving up
michael@0 41 * on that day's submission.
michael@0 42 *
michael@0 43 * On Android, items 4, 5, and 6 are addressed by displaying an Android
michael@0 44 * notification on first run.
michael@0 45 */
michael@0 46 public class SubmissionPolicy {
michael@0 47 public static final String LOG_TAG = SubmissionPolicy.class.getSimpleName();
michael@0 48
michael@0 49 protected final SharedPreferences sharedPreferences;
michael@0 50 protected final SubmissionClient client;
michael@0 51 protected final boolean uploadEnabled;
michael@0 52 protected final ObsoleteDocumentTracker tracker;
michael@0 53
michael@0 54 public SubmissionPolicy(final SharedPreferences sharedPreferences,
michael@0 55 final SubmissionClient client,
michael@0 56 final ObsoleteDocumentTracker tracker,
michael@0 57 boolean uploadEnabled) {
michael@0 58 if (sharedPreferences == null) {
michael@0 59 throw new IllegalArgumentException("sharedPreferences must not be null");
michael@0 60 }
michael@0 61 this.sharedPreferences = sharedPreferences;
michael@0 62 this.client = client;
michael@0 63 this.tracker = tracker;
michael@0 64 this.uploadEnabled = uploadEnabled;
michael@0 65 }
michael@0 66
michael@0 67 /**
michael@0 68 * Check what action must happen, advance counters and timestamps, and
michael@0 69 * possibly spawn a request to the server.
michael@0 70 *
michael@0 71 * @param localTime now.
michael@0 72 * @return true if a request was spawned; false otherwise.
michael@0 73 */
michael@0 74 public boolean tick(final long localTime) {
michael@0 75 final long nextUpload = getNextSubmission();
michael@0 76
michael@0 77 // If the system clock were ever set to a time in the distant future,
michael@0 78 // it's possible our next schedule date is far out as well. We know
michael@0 79 // we shouldn't schedule for more than a day out, so we reset the next
michael@0 80 // scheduled date appropriately. 3 days was chosen to match desktop's
michael@0 81 // arbitrary choice.
michael@0 82 if (nextUpload >= localTime + 3 * getMinimumTimeBetweenUploads()) {
michael@0 83 Logger.warn(LOG_TAG, "Next upload scheduled far in the future; system clock reset? " + nextUpload + " > " + localTime);
michael@0 84 // Things are strange, we want to start again but we don't want to stampede.
michael@0 85 editor()
michael@0 86 .setNextSubmission(localTime + getMinimumTimeBetweenUploads())
michael@0 87 .commit();
michael@0 88 return false;
michael@0 89 }
michael@0 90
michael@0 91 // Don't upload unless an interval has elapsed.
michael@0 92 if (localTime < nextUpload) {
michael@0 93 Logger.debug(LOG_TAG, "We uploaded less than an interval ago; skipping. " + nextUpload + " > " + localTime);
michael@0 94 return false;
michael@0 95 }
michael@0 96
michael@0 97 if (!uploadEnabled) {
michael@0 98 // We only delete (rather than mark as obsolete during upload) when
michael@0 99 // uploading is disabled. We try to delete aggressively, since the volume
michael@0 100 // of deletes should be very low. But we don't want to send too many
michael@0 101 // delete requests at the same time, so we process these one at a time. In
michael@0 102 // the future (Bug 872756), we will be able to delete multiple documents
michael@0 103 // with one request.
michael@0 104 final String obsoleteId = tracker.getNextObsoleteId();
michael@0 105 if (obsoleteId == null) {
michael@0 106 return false;
michael@0 107 }
michael@0 108
michael@0 109 Editor editor = editor();
michael@0 110 editor.setLastDeleteRequested(localTime); // Write committed by delegate.
michael@0 111 client.delete(localTime, obsoleteId, new DeleteDelegate(editor));
michael@0 112 return true;
michael@0 113 }
michael@0 114
michael@0 115 long firstRun = getFirstRunLocalTime();
michael@0 116 if (firstRun < 0) {
michael@0 117 firstRun = localTime;
michael@0 118 // Make sure we start clean and as soon as possible.
michael@0 119 editor()
michael@0 120 .setFirstRunLocalTime(firstRun)
michael@0 121 .setNextSubmission(localTime + getMinimumTimeBeforeFirstSubmission())
michael@0 122 .setCurrentDayFailureCount(0)
michael@0 123 .commit();
michael@0 124 }
michael@0 125
michael@0 126 // This case will occur if the nextSubmission time is not set (== -1) but firstRun is.
michael@0 127 if (localTime < firstRun + getMinimumTimeBeforeFirstSubmission()) {
michael@0 128 Logger.info(LOG_TAG, "Need to wait " + getMinimumTimeBeforeFirstSubmission() + " before first upload.");
michael@0 129 return false;
michael@0 130 }
michael@0 131
michael@0 132 // The first upload attempt for a given document submission begins a 24-hour period in which
michael@0 133 // the upload will retry upon a soft failure. At the end of this period, the submission
michael@0 134 // failure count is reset, ensuring each day's first submission attempt has a zeroed failure
michael@0 135 // count. A period may also end on upload success or hard failure.
michael@0 136 if (localTime >= getCurrentDayResetTime()) {
michael@0 137 editor()
michael@0 138 .setCurrentDayResetTime(localTime + getMinimumTimeBetweenUploads())
michael@0 139 .setCurrentDayFailureCount(0)
michael@0 140 .commit();
michael@0 141 }
michael@0 142
michael@0 143 String id = HealthReportUtils.generateDocumentId();
michael@0 144 Collection<String> oldIds = tracker.getBatchOfObsoleteIds();
michael@0 145 tracker.addObsoleteId(id);
michael@0 146
michael@0 147 Editor editor = editor();
michael@0 148 editor.setLastUploadRequested(localTime); // Write committed by delegate.
michael@0 149 client.upload(localTime, id, oldIds, new UploadDelegate(editor, oldIds));
michael@0 150 return true;
michael@0 151 }
michael@0 152
michael@0 153 /**
michael@0 154 * Return true if the upload that produced <code>e</code> definitely did not
michael@0 155 * produce a new record on the remote server.
michael@0 156 *
michael@0 157 * @param e
michael@0 158 * <code>Exception</code> that upload produced.
michael@0 159 * @return true if the server could not have a new record.
michael@0 160 */
michael@0 161 protected boolean isLocalException(Exception e) {
michael@0 162 return (e instanceof MalformedURLException) ||
michael@0 163 (e instanceof SocketException) ||
michael@0 164 (e instanceof UnknownHostException);
michael@0 165 }
michael@0 166
michael@0 167 protected class UploadDelegate implements Delegate {
michael@0 168 protected final Editor editor;
michael@0 169 protected final Collection<String> oldIds;
michael@0 170
michael@0 171 public UploadDelegate(Editor editor, Collection<String> oldIds) {
michael@0 172 this.editor = editor;
michael@0 173 this.oldIds = oldIds;
michael@0 174 }
michael@0 175
michael@0 176 @Override
michael@0 177 public void onSuccess(long localTime, String id) {
michael@0 178 long next = localTime + getMinimumTimeBetweenUploads();
michael@0 179 tracker.markIdAsUploaded(id);
michael@0 180 tracker.purgeObsoleteIds(oldIds);
michael@0 181 editor
michael@0 182 .setNextSubmission(next)
michael@0 183 .setLastUploadSucceeded(localTime)
michael@0 184 .setCurrentDayFailureCount(0)
michael@0 185 .clearCurrentDayResetTime() // Set again on the next submission's first upload attempt.
michael@0 186 .commit();
michael@0 187 if (Logger.LOG_PERSONAL_INFORMATION) {
michael@0 188 Logger.pii(LOG_TAG, "Successful upload with id " + id + " obsoleting "
michael@0 189 + oldIds.size() + " old records reported at " + localTime + "; next upload at " + next + ".");
michael@0 190 } else {
michael@0 191 Logger.info(LOG_TAG, "Successful upload obsoleting " + oldIds.size()
michael@0 192 + " old records reported at " + localTime + "; next upload at " + next + ".");
michael@0 193 }
michael@0 194 }
michael@0 195
michael@0 196 @Override
michael@0 197 public void onHardFailure(long localTime, String id, String reason, Exception e) {
michael@0 198 long next = localTime + getMinimumTimeBetweenUploads();
michael@0 199 if (isLocalException(e)) {
michael@0 200 Logger.info(LOG_TAG, "Hard failure caused by local exception; not tracking id and not decrementing attempts.");
michael@0 201 tracker.removeObsoleteId(id);
michael@0 202 } else {
michael@0 203 tracker.decrementObsoleteIdAttempts(oldIds);
michael@0 204 }
michael@0 205 editor
michael@0 206 .setNextSubmission(next)
michael@0 207 .setLastUploadFailed(localTime)
michael@0 208 .setCurrentDayFailureCount(0)
michael@0 209 .clearCurrentDayResetTime() // Set again on the next submission's first upload attempt.
michael@0 210 .commit();
michael@0 211 Logger.warn(LOG_TAG, "Hard failure reported at " + localTime + ": " + reason + " Next upload at " + next + ".", e);
michael@0 212 }
michael@0 213
michael@0 214 @Override
michael@0 215 public void onSoftFailure(long localTime, String id, String reason, Exception e) {
michael@0 216 int failuresToday = getCurrentDayFailureCount();
michael@0 217 Logger.warn(LOG_TAG, "Soft failure reported at " + localTime + ": " + reason + " Previously failed " + failuresToday + " time(s) today.");
michael@0 218
michael@0 219 if (failuresToday >= getMaximumFailuresPerDay()) {
michael@0 220 onHardFailure(localTime, id, "Reached the limit of daily upload attempts: " + failuresToday, e);
michael@0 221 return;
michael@0 222 }
michael@0 223
michael@0 224 long next = localTime + getMinimumTimeAfterFailure();
michael@0 225 if (isLocalException(e)) {
michael@0 226 Logger.info(LOG_TAG, "Soft failure caused by local exception; not tracking id and not decrementing attempts.");
michael@0 227 tracker.removeObsoleteId(id);
michael@0 228 } else {
michael@0 229 tracker.decrementObsoleteIdAttempts(oldIds);
michael@0 230 }
michael@0 231 editor
michael@0 232 .setNextSubmission(next)
michael@0 233 .setLastUploadFailed(localTime)
michael@0 234 .setCurrentDayFailureCount(failuresToday + 1)
michael@0 235 .commit();
michael@0 236 Logger.info(LOG_TAG, "Retrying upload at " + next + ".");
michael@0 237 }
michael@0 238 }
michael@0 239
michael@0 240 protected class DeleteDelegate implements Delegate {
michael@0 241 protected final Editor editor;
michael@0 242
michael@0 243 public DeleteDelegate(Editor editor) {
michael@0 244 this.editor = editor;
michael@0 245 }
michael@0 246
michael@0 247 @Override
michael@0 248 public void onSoftFailure(final long localTime, String id, String reason, Exception e) {
michael@0 249 long next = localTime + getMinimumTimeBetweenDeletes();
michael@0 250 if (isLocalException(e)) {
michael@0 251 Logger.info(LOG_TAG, "Soft failure caused by local exception; not decrementing attempts.");
michael@0 252 } else {
michael@0 253 tracker.decrementObsoleteIdAttempts(id);
michael@0 254 }
michael@0 255 editor
michael@0 256 .setNextSubmission(next)
michael@0 257 .setLastDeleteFailed(localTime)
michael@0 258 .commit();
michael@0 259
michael@0 260 if (Logger.LOG_PERSONAL_INFORMATION) {
michael@0 261 Logger.info(LOG_TAG, "Got soft failure at " + localTime + " deleting obsolete document with id " + id + ": " + reason + " Trying again later.");
michael@0 262 } else {
michael@0 263 Logger.info(LOG_TAG, "Got soft failure at " + localTime + " deleting obsolete document: " + reason + " Trying again later.");
michael@0 264 }
michael@0 265 }
michael@0 266
michael@0 267 @Override
michael@0 268 public void onHardFailure(final long localTime, String id, String reason, Exception e) {
michael@0 269 // We're never going to be able to delete this id, so don't keep trying.
michael@0 270 long next = localTime + getMinimumTimeBetweenDeletes();
michael@0 271 tracker.removeObsoleteId(id);
michael@0 272 editor
michael@0 273 .setNextSubmission(next)
michael@0 274 .setLastDeleteFailed(localTime)
michael@0 275 .commit();
michael@0 276
michael@0 277 if (Logger.LOG_PERSONAL_INFORMATION) {
michael@0 278 Logger.warn(LOG_TAG, "Got hard failure at " + localTime + " deleting obsolete document with id " + id + ": " + reason + " Abandoning delete request.", e);
michael@0 279 } else {
michael@0 280 Logger.warn(LOG_TAG, "Got hard failure at " + localTime + " deleting obsolete document: " + reason + " Abandoning delete request.", e);
michael@0 281 }
michael@0 282 }
michael@0 283
michael@0 284 @Override
michael@0 285 public void onSuccess(final long localTime, String id) {
michael@0 286 long next = localTime + getMinimumTimeBetweenDeletes();
michael@0 287 tracker.removeObsoleteId(id);
michael@0 288 editor
michael@0 289 .setNextSubmission(next)
michael@0 290 .setLastDeleteSucceeded(localTime)
michael@0 291 .commit();
michael@0 292
michael@0 293 if (Logger.LOG_PERSONAL_INFORMATION) {
michael@0 294 Logger.pii(LOG_TAG, "Deleted an obsolete document with id " + id + " at " + localTime + ".");
michael@0 295 } else {
michael@0 296 Logger.info(LOG_TAG, "Deleted an obsolete document at " + localTime + ".");
michael@0 297 }
michael@0 298 }
michael@0 299 }
michael@0 300
michael@0 301 public SharedPreferences getSharedPreferences() {
michael@0 302 return this.sharedPreferences;
michael@0 303 }
michael@0 304
michael@0 305 public long getMinimumTimeBetweenUploads() {
michael@0 306 return getSharedPreferences().getLong(HealthReportConstants.PREF_MINIMUM_TIME_BETWEEN_UPLOADS, HealthReportConstants.DEFAULT_MINIMUM_TIME_BETWEEN_UPLOADS);
michael@0 307 }
michael@0 308
michael@0 309 public long getMinimumTimeBeforeFirstSubmission() {
michael@0 310 return getSharedPreferences().getLong(HealthReportConstants.PREF_MINIMUM_TIME_BEFORE_FIRST_SUBMISSION, HealthReportConstants.DEFAULT_MINIMUM_TIME_BEFORE_FIRST_SUBMISSION);
michael@0 311 }
michael@0 312
michael@0 313 public long getMinimumTimeAfterFailure() {
michael@0 314 return getSharedPreferences().getLong(HealthReportConstants.PREF_MINIMUM_TIME_AFTER_FAILURE, HealthReportConstants.DEFAULT_MINIMUM_TIME_AFTER_FAILURE);
michael@0 315 }
michael@0 316
michael@0 317 public long getMaximumFailuresPerDay() {
michael@0 318 return getSharedPreferences().getLong(HealthReportConstants.PREF_MAXIMUM_FAILURES_PER_DAY, HealthReportConstants.DEFAULT_MAXIMUM_FAILURES_PER_DAY);
michael@0 319 }
michael@0 320
michael@0 321 // Authoritative.
michael@0 322 public long getFirstRunLocalTime() {
michael@0 323 return getSharedPreferences().getLong(HealthReportConstants.PREF_FIRST_RUN, -1);
michael@0 324 }
michael@0 325
michael@0 326 // Authoritative.
michael@0 327 public long getNextSubmission() {
michael@0 328 return getSharedPreferences().getLong(HealthReportConstants.PREF_NEXT_SUBMISSION, -1);
michael@0 329 }
michael@0 330
michael@0 331 // Authoritative.
michael@0 332 public int getCurrentDayFailureCount() {
michael@0 333 return getSharedPreferences().getInt(HealthReportConstants.PREF_CURRENT_DAY_FAILURE_COUNT, 0);
michael@0 334 }
michael@0 335
michael@0 336 // Authoritative.
michael@0 337 public long getCurrentDayResetTime() {
michael@0 338 return getSharedPreferences().getLong(HealthReportConstants.PREF_CURRENT_DAY_RESET_TIME, -1);
michael@0 339 }
michael@0 340
michael@0 341 /**
michael@0 342 * To avoid writing to disk multiple times, we encapsulate writes in a
michael@0 343 * helper class. Be sure to call <code>commit</code> to flush to disk!
michael@0 344 */
michael@0 345 protected Editor editor() {
michael@0 346 return new Editor(getSharedPreferences().edit());
michael@0 347 }
michael@0 348
michael@0 349 protected static class Editor {
michael@0 350 protected final SharedPreferences.Editor editor;
michael@0 351
michael@0 352 public Editor(SharedPreferences.Editor editor) {
michael@0 353 this.editor = editor;
michael@0 354 }
michael@0 355
michael@0 356 public void commit() {
michael@0 357 editor.commit();
michael@0 358 }
michael@0 359
michael@0 360 // Authoritative.
michael@0 361 public Editor setFirstRunLocalTime(long localTime) {
michael@0 362 editor.putLong(HealthReportConstants.PREF_FIRST_RUN, localTime);
michael@0 363 return this;
michael@0 364 }
michael@0 365
michael@0 366 // Authoritative.
michael@0 367 public Editor setNextSubmission(long localTime) {
michael@0 368 editor.putLong(HealthReportConstants.PREF_NEXT_SUBMISSION, localTime);
michael@0 369 return this;
michael@0 370 }
michael@0 371
michael@0 372 // Authoritative.
michael@0 373 public Editor setCurrentDayFailureCount(int failureCount) {
michael@0 374 editor.putInt(HealthReportConstants.PREF_CURRENT_DAY_FAILURE_COUNT, failureCount);
michael@0 375 return this;
michael@0 376 }
michael@0 377
michael@0 378 // Authoritative.
michael@0 379 public Editor setCurrentDayResetTime(long resetTime) {
michael@0 380 editor.putLong(HealthReportConstants.PREF_CURRENT_DAY_RESET_TIME, resetTime);
michael@0 381 return this;
michael@0 382 }
michael@0 383
michael@0 384 // Authoritative.
michael@0 385 public Editor clearCurrentDayResetTime() {
michael@0 386 editor.putLong(HealthReportConstants.PREF_CURRENT_DAY_RESET_TIME, -1);
michael@0 387 return this;
michael@0 388 }
michael@0 389
michael@0 390 // Authoritative.
michael@0 391 public Editor setLastUploadRequested(long localTime) {
michael@0 392 editor.putLong(HealthReportConstants.PREF_LAST_UPLOAD_REQUESTED, localTime);
michael@0 393 return this;
michael@0 394 }
michael@0 395
michael@0 396 // Forensics only.
michael@0 397 public Editor setLastUploadSucceeded(long localTime) {
michael@0 398 editor.putLong(HealthReportConstants.PREF_LAST_UPLOAD_SUCCEEDED, localTime);
michael@0 399 return this;
michael@0 400 }
michael@0 401
michael@0 402 // Forensics only.
michael@0 403 public Editor setLastUploadFailed(long localTime) {
michael@0 404 editor.putLong(HealthReportConstants.PREF_LAST_UPLOAD_FAILED, localTime);
michael@0 405 return this;
michael@0 406 }
michael@0 407
michael@0 408 // Forensics only.
michael@0 409 public Editor setLastDeleteRequested(long localTime) {
michael@0 410 editor.putLong(HealthReportConstants.PREF_LAST_DELETE_REQUESTED, localTime);
michael@0 411 return this;
michael@0 412 }
michael@0 413
michael@0 414 // Forensics only.
michael@0 415 public Editor setLastDeleteSucceeded(long localTime) {
michael@0 416 editor.putLong(HealthReportConstants.PREF_LAST_DELETE_SUCCEEDED, localTime);
michael@0 417 return this;
michael@0 418 }
michael@0 419
michael@0 420 // Forensics only.
michael@0 421 public Editor setLastDeleteFailed(long localTime) {
michael@0 422 editor.putLong(HealthReportConstants.PREF_LAST_DELETE_FAILED, localTime);
michael@0 423 return this;
michael@0 424 }
michael@0 425 }
michael@0 426
michael@0 427 // Authoritative.
michael@0 428 public long getLastUploadRequested() {
michael@0 429 return getSharedPreferences().getLong(HealthReportConstants.PREF_LAST_UPLOAD_REQUESTED, -1);
michael@0 430 }
michael@0 431
michael@0 432 // Forensics only.
michael@0 433 public long getLastUploadSucceeded() {
michael@0 434 return getSharedPreferences().getLong(HealthReportConstants.PREF_LAST_UPLOAD_SUCCEEDED, -1);
michael@0 435 }
michael@0 436
michael@0 437 // Forensics only.
michael@0 438 public long getLastUploadFailed() {
michael@0 439 return getSharedPreferences().getLong(HealthReportConstants.PREF_LAST_UPLOAD_FAILED, -1);
michael@0 440 }
michael@0 441
michael@0 442 // Forensics only.
michael@0 443 public long getLastDeleteRequested() {
michael@0 444 return getSharedPreferences().getLong(HealthReportConstants.PREF_LAST_DELETE_REQUESTED, -1);
michael@0 445 }
michael@0 446
michael@0 447 // Forensics only.
michael@0 448 public long getLastDeleteSucceeded() {
michael@0 449 return getSharedPreferences().getLong(HealthReportConstants.PREF_LAST_DELETE_SUCCEEDED, -1);
michael@0 450 }
michael@0 451
michael@0 452 // Forensics only.
michael@0 453 public long getLastDeleteFailed() {
michael@0 454 return getSharedPreferences().getLong(HealthReportConstants.PREF_LAST_DELETE_FAILED, -1);
michael@0 455 }
michael@0 456
michael@0 457 public long getMinimumTimeBetweenDeletes() {
michael@0 458 return getSharedPreferences().getLong(HealthReportConstants.PREF_MINIMUM_TIME_BETWEEN_DELETES, HealthReportConstants.DEFAULT_MINIMUM_TIME_BETWEEN_DELETES);
michael@0 459 }
michael@0 460 }

mercurial