OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sync/retry_verifier.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "chrome/browser/sync/engine/polling_constants.h" |
| 11 #include "chrome/browser/sync/sessions/session_state.h" |
| 12 |
| 13 namespace browser_sync { |
| 14 |
| 15 namespace { |
| 16 // Given the current delay calculate the minimum and maximum wait times for |
| 17 // the next retry. |
| 18 DelayInfo CalculateDelay(int64 current_delay) { |
| 19 int64 backoff_s = std::max(static_cast<int64>(1), current_delay * |
| 20 browser_sync::kBackoffRandomizationFactor); |
| 21 |
| 22 DelayInfo delay_info; |
| 23 delay_info.min_delay = backoff_s + (-1 * current_delay/ |
| 24 browser_sync::kBackoffRandomizationFactor); |
| 25 delay_info.max_delay = backoff_s + current_delay/2; |
| 26 |
| 27 delay_info.min_delay = std::max(static_cast<int64>(1), |
| 28 std::min(delay_info.min_delay, browser_sync::kMaxBackoffSeconds)); |
| 29 |
| 30 delay_info.max_delay = std::max(static_cast<int64>(1), |
| 31 std::min(delay_info.max_delay, browser_sync::kMaxBackoffSeconds)); |
| 32 |
| 33 return delay_info; |
| 34 } |
| 35 |
| 36 // Fills the table with the maximum and minimum values for each retry, upto |
| 37 // |count| number of retries. |
| 38 void FillDelayTable(DelayInfo* delay_table, int count) { |
| 39 DCHECK(count > 1); |
| 40 |
| 41 // We start off with the minimum value of 2 seconds. |
| 42 delay_table[0].min_delay = static_cast<int64>(2); |
| 43 delay_table[0].max_delay = static_cast<int64>(2); |
| 44 |
| 45 for (int i = 1 ; i < count ; ++i) { |
| 46 delay_table[i].min_delay = CalculateDelay(delay_table[i-1].min_delay). |
| 47 min_delay; |
| 48 delay_table[i].max_delay = CalculateDelay(delay_table[i-1].max_delay). |
| 49 max_delay; |
| 50 } |
| 51 } |
| 52 } // namespace |
| 53 |
| 54 // Verifies if the current retry is on time. Note that we dont use the |
| 55 // maximum value of the retry range in verifying, only the minimum. Reason |
| 56 // being there is no guarantee that the retry will be on the dot. However in |
| 57 // practice it is on the dot. But making that assumption for all the platforms |
| 58 // would make the test flaky. However we have the global timeout for the |
| 59 // verification which would make sure all retries take place in a reasonable |
| 60 // amount of time. The global timeout is defined in profile sync service |
| 61 // harness as |kExponentialBackoffVerificationTimeoutMs|. |
| 62 bool IsRetryOnTime(DelayInfo* delay_table, int retry_count, |
| 63 const base::TimeDelta& time_elapsed) { |
| 64 VLOG(1) << "Retry Count : " << retry_count |
| 65 << " Time elapsed : " << time_elapsed.InSeconds() |
| 66 << " Retry table min: " << delay_table[retry_count].min_delay |
| 67 << " Retry table max: " << delay_table[retry_count].max_delay; |
| 68 return ((time_elapsed.InSeconds() >= delay_table[retry_count].min_delay)); |
| 69 } |
| 70 |
| 71 RetryVerifier::RetryVerifier() : retry_count_(0), |
| 72 success_(false), |
| 73 done_(false) { |
| 74 } |
| 75 |
| 76 RetryVerifier::~RetryVerifier() { |
| 77 } |
| 78 |
| 79 // Initializes the state for verification. |
| 80 void RetryVerifier::Initialize( |
| 81 const browser_sync::sessions::SyncSessionSnapshot& snap) { |
| 82 retry_count_ = 0; |
| 83 last_sync_time_ = snap.sync_start_time; |
| 84 FillDelayTable(delay_table_, kMaxRetry); |
| 85 done_ = false; |
| 86 success_ = false; |
| 87 } |
| 88 |
| 89 void RetryVerifier::VerifyRetryInterval( |
| 90 const browser_sync::sessions::SyncSessionSnapshot& snap) { |
| 91 DCHECK(retry_count_ < kMaxRetry); |
| 92 if (retry_count_ == 0) { |
| 93 if (snap.sync_start_time != last_sync_time_) { |
| 94 retry_count_++; |
| 95 last_sync_time_ = snap.sync_start_time; |
| 96 } |
| 97 success_ = true; |
| 98 return; |
| 99 } |
| 100 |
| 101 // Check if the sync start time has changed. If so indicates a new sync |
| 102 // has taken place. |
| 103 if (snap.sync_start_time != last_sync_time_) { |
| 104 base::TimeDelta delta = snap.sync_start_time - last_sync_time_; |
| 105 success_ = IsRetryOnTime(delay_table_,retry_count_ -1, delta); |
| 106 last_sync_time_ = snap.sync_start_time; |
| 107 ++retry_count_; |
| 108 done_ = (retry_count_ >= kMaxRetry); |
| 109 return; |
| 110 } |
| 111 } |
| 112 } // namespace browser_sync |
OLD | NEW |