1 // Copyright (c) 2012 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 "sync/internal_api/public/engine/polling_constants.h" 11 #include "sync/internal_api/public/sessions/sync_session_snapshot.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 syncer::kBackoffRandomizationFactor); 21 22 DelayInfo delay_info; 23 delay_info.min_delay = backoff_s + (-1 * current_delay/ 24 syncer::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, syncer::kMaxBackoffSeconds)); 29 30 delay_info.max_delay = std::max(static_cast<int64>(1), 31 std::min(delay_info.max_delay, syncer::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 DVLOG(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 memset(&delay_table_, 0, sizeof(delay_table_)); 75 } 76 77 RetryVerifier::~RetryVerifier() { 78 } 79 80 // Initializes the state for verification. 81 void RetryVerifier::Initialize( 82 const syncer::sessions::SyncSessionSnapshot& snap) { 83 retry_count_ = 0; 84 last_sync_time_ = snap.sync_start_time(); 85 FillDelayTable(delay_table_, kMaxRetry); 86 done_ = false; 87 success_ = false; 88 } 89 90 void RetryVerifier::VerifyRetryInterval( 91 const syncer::sessions::SyncSessionSnapshot& snap) { 92 DCHECK(retry_count_ < kMaxRetry); 93 if (retry_count_ == 0) { 94 if (snap.sync_start_time() != last_sync_time_) { 95 retry_count_++; 96 last_sync_time_ = snap.sync_start_time(); 97 } 98 success_ = true; 99 return; 100 } 101 102 // Check if the sync start time has changed. If so indicates a new sync 103 // has taken place. 104 if (snap.sync_start_time() != last_sync_time_) { 105 base::TimeDelta delta = snap.sync_start_time() - last_sync_time_; 106 success_ = IsRetryOnTime(delay_table_,retry_count_ -1, delta); 107 last_sync_time_ = snap.sync_start_time(); 108 ++retry_count_; 109 done_ = (retry_count_ >= kMaxRetry); 110 return; 111 } 112 } 113 } // namespace browser_sync 114