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) { | |
tim (not reviewing)
2011/08/08 23:11:35
this should be in a namespace (possibly even anon
lipalani1
2011/08/09 01:39:02
Done.
| |
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 | |
tim (not reviewing)
2011/08/08 23:11:35
don't use acronyms in comments unless the acronym
lipalani1
2011/08/09 01:39:02
Done.
| |
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 success_(false), | |
69 done_(false) { | |
70 } | |
71 | |
72 RetryVerifier::~RetryVerifier() { | |
73 } | |
74 | |
75 // Initializes the state for verification. | |
76 void RetryVerifier::Initialize( | |
77 const browser_sync::sessions::SyncSessionSnapshot& snap) { | |
78 retry_count_ = 0; | |
79 last_sync_time_ = snap.sync_start_time; | |
80 FillDelayTable(delay_table_, kMaxRetry); | |
81 done_ = false; | |
82 success_ = false; | |
83 } | |
84 | |
85 void RetryVerifier::VerifyRetryInterval( | |
86 const browser_sync::sessions::SyncSessionSnapshot& snap) { | |
87 DCHECK(retry_count_ < kMaxRetry); | |
88 if (retry_count_ == 0) { | |
89 if (snap.sync_start_time != last_sync_time_) { | |
90 retry_count_++; | |
91 last_sync_time_ = snap.sync_start_time; | |
92 } | |
93 success_ = true; | |
94 return; | |
95 } | |
96 | |
97 // Check if the sync start time has changed. If so indicates a new sync | |
98 // has taken place. | |
99 if (snap.sync_start_time != last_sync_time_) { | |
100 base::TimeDelta delta = snap.sync_start_time - last_sync_time_; | |
101 success_ = IsRetryOnTime(delay_table_,retry_count_ -1, delta); | |
102 last_sync_time_ = snap.sync_start_time; | |
103 ++retry_count_; | |
104 done_ = (retry_count_ >= kMaxRetry); | |
105 return; | |
106 } | |
107 } | |
108 | |
OLD | NEW |