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.
5 #include "chrome/browser/sync/retry_verifier.h"
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"
13 namespace browser_sync
{
16 // Given the current delay calculate the minimum and maximum wait times for
18 DelayInfo
CalculateDelay(int64 current_delay
) {
19 int64 backoff_s
= std::max(static_cast<int64
>(1), current_delay
*
20 syncer::kBackoffRandomizationFactor
);
23 delay_info
.min_delay
= backoff_s
+ (-1 * current_delay
/
24 syncer::kBackoffRandomizationFactor
);
25 delay_info
.max_delay
= backoff_s
+ current_delay
/2;
27 delay_info
.min_delay
= std::max(static_cast<int64
>(1),
28 std::min(delay_info
.min_delay
, syncer::kMaxBackoffSeconds
));
30 delay_info
.max_delay
= std::max(static_cast<int64
>(1),
31 std::min(delay_info
.max_delay
, syncer::kMaxBackoffSeconds
));
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
) {
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);
45 for (int i
= 1 ; i
< count
; ++i
) {
46 delay_table
[i
].min_delay
= CalculateDelay(delay_table
[i
-1].min_delay
).
48 delay_table
[i
].max_delay
= CalculateDelay(delay_table
[i
-1].max_delay
).
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
));
71 RetryVerifier::RetryVerifier() : retry_count_(0),
74 memset(&delay_table_
, 0, sizeof(delay_table_
));
77 RetryVerifier::~RetryVerifier() {
80 // Initializes the state for verification.
81 void RetryVerifier::Initialize(
82 const syncer::sessions::SyncSessionSnapshot
& snap
) {
84 last_sync_time_
= snap
.sync_start_time();
85 FillDelayTable(delay_table_
, kMaxRetry
);
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_
) {
96 last_sync_time_
= snap
.sync_start_time();
102 // Check if the sync start time has changed. If so indicates a new sync
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();
109 done_
= (retry_count_
>= kMaxRetry
);
113 } // namespace browser_sync