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