File: retry_verifier.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (114 lines) | stat: -rw-r--r-- 4,123 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync/test/integration/retry_verifier.h"

#include <string.h>

#include <algorithm>

#include "base/logging.h"
#include "components/sync/engine/cycle/sync_cycle_snapshot.h"
#include "components/sync/engine/polling_constants.h"

namespace {
// Given the current delay calculate the minimum and maximum wait times for
// the next retry.
DelayInfo CalculateDelay(int64_t current_delay) {
  int64_t backoff_s =
      std::max(static_cast<int64_t>(1),
               current_delay * syncer::kBackoffRandomizationFactor);

  DelayInfo delay_info;
  delay_info.min_delay = backoff_s + (-1 * current_delay/
                             syncer::kBackoffRandomizationFactor);
  delay_info.max_delay = backoff_s + current_delay/2;

  delay_info.min_delay =
      std::max(static_cast<int64_t>(1),
               std::min(delay_info.min_delay, syncer::kMaxBackoffSeconds));

  delay_info.max_delay =
      std::max(static_cast<int64_t>(1),
               std::min(delay_info.max_delay, syncer::kMaxBackoffSeconds));

  return delay_info;
}

// Fills the table with the maximum and minimum values for each retry, upto
// |count| number of retries.
void FillDelayTable(DelayInfo* delay_table, int count) {
  DCHECK_GT(count, 1);

  // We start off with the minimum value of 2 seconds.
  delay_table[0].min_delay = static_cast<int64_t>(2);
  delay_table[0].max_delay = static_cast<int64_t>(2);

  for (int i = 1 ; i < count ; ++i) {
    delay_table[i].min_delay = CalculateDelay(delay_table[i-1].min_delay).
                               min_delay;
    delay_table[i].max_delay = CalculateDelay(delay_table[i-1].max_delay).
                               max_delay;
  }
}
}  // namespace

// Verifies if the current retry is on time. Note that we dont use the
// maximum value of the retry range in verifying, only the minimum. Reason
// being there is no guarantee that the retry will be on the dot. However in
// practice it is on the dot. But making that assumption for all the platforms
// would make the test flaky. However we have the global timeout for the
// verification which would make sure all retries take place in a reasonable
// amount of time. The global timeout is defined in profile sync service
// harness as |kExponentialBackoffVerificationTimeoutMs|.
bool IsRetryOnTime(DelayInfo* delay_table, int retry_count,
                   const base::TimeDelta& time_elapsed) {
  DVLOG(1) << "Retry Count : " << retry_count
           << " Time elapsed : " << time_elapsed.InSeconds()
           << " Retry table min: " << delay_table[retry_count].min_delay
           << " Retry table max: " << delay_table[retry_count].max_delay;
  return ((time_elapsed.InSeconds() >= delay_table[retry_count].min_delay));
}

RetryVerifier::RetryVerifier() : retry_count_(0),
                                 success_(false),
                                 done_(false) {
  memset(&delay_table_, 0, sizeof(delay_table_));
}

RetryVerifier::~RetryVerifier() {
}

// Initializes the state for verification.
void RetryVerifier::Initialize(const syncer::SyncCycleSnapshot& snap) {
  retry_count_ = 0;
  last_sync_time_ = snap.sync_start_time();
  FillDelayTable(delay_table_, kMaxRetry);
  done_ = false;
  success_ = false;
}

void RetryVerifier::VerifyRetryInterval(const syncer::SyncCycleSnapshot& snap) {
  DCHECK(retry_count_ < kMaxRetry);
  if (retry_count_ == 0) {
    if (snap.sync_start_time() != last_sync_time_) {
      retry_count_++;
      last_sync_time_ = snap.sync_start_time();
    }
    success_ = true;
    return;
  }

  // Check if the sync start time has changed. If so indicates a new sync
  // has taken place.
  if (snap.sync_start_time() != last_sync_time_) {
    base::TimeDelta delta = snap.sync_start_time() - last_sync_time_;
    success_ = IsRetryOnTime(delay_table_, retry_count_ - 1, delta);
    last_sync_time_ = snap.sync_start_time();
    ++retry_count_;
    done_ = (retry_count_ >= kMaxRetry);
    return;
  }
}