File: sync_process_runner.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (241 lines) | stat: -rw-r--r-- 7,653 bytes parent folder | download | duplicates (6)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync_file_system/sync_process_runner.h"

#include <memory>
#include <utility>

#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "chrome/browser/sync_file_system/logger.h"

namespace sync_file_system {

const int64_t SyncProcessRunner::kSyncDelayInMilliseconds =
    1 * base::Time::kMillisecondsPerSecond;  // 1 sec
const int64_t SyncProcessRunner::kSyncDelayWithSyncError =
    3 * base::Time::kMillisecondsPerSecond;                           // 3 sec
const int64_t SyncProcessRunner::kSyncDelayFastInMilliseconds = 100;  // 100 ms
const int SyncProcessRunner::kPendingChangeThresholdForFastSync = 10;
const int64_t SyncProcessRunner::kSyncDelaySlowInMilliseconds =
    30 * base::Time::kMillisecondsPerSecond;  // 30 sec
const int64_t SyncProcessRunner::kSyncDelayMaxInMilliseconds =
    30 * 60 * base::Time::kMillisecondsPerSecond;  // 30 min

namespace {

class BaseTimerHelper : public SyncProcessRunner::TimerHelper {
 public:
  BaseTimerHelper() = default;

  bool IsRunning() override { return timer_.IsRunning(); }

  void Start(const base::Location& from_here,
             const base::TimeDelta& delay,
             base::OnceClosure closure) override {
    timer_.Start(from_here, delay, std::move(closure));
  }

  base::TimeTicks Now() const override { return base::TimeTicks::Now(); }

  BaseTimerHelper(const BaseTimerHelper&) = delete;
  BaseTimerHelper& operator=(const BaseTimerHelper&) = delete;

  ~BaseTimerHelper() override = default;

 private:
  base::OneShotTimer timer_;
};

bool WasSuccessfulSync(SyncStatusCode status) {
  return status == SYNC_STATUS_OK ||
         status == SYNC_STATUS_HAS_CONFLICT ||
         status == SYNC_STATUS_NO_CONFLICT ||
         status == SYNC_STATUS_NO_CHANGE_TO_SYNC ||
         status == SYNC_STATUS_UNKNOWN_ORIGIN ||
         status == SYNC_STATUS_RETRY;
}

}  // namespace

SyncProcessRunner::SyncProcessRunner(const std::string& name,
                                     Client* client,
                                     std::unique_ptr<TimerHelper> timer_helper,
                                     size_t max_parallel_task)
    : name_(name),
      client_(client),
      max_parallel_task_(max_parallel_task),
      running_tasks_(0),
      timer_helper_(std::move(timer_helper)),
      service_state_(SYNC_SERVICE_RUNNING),
      pending_changes_(0) {
  DCHECK_LE(1u, max_parallel_task_);
  if (!timer_helper_)
    timer_helper_ = std::make_unique<BaseTimerHelper>();
}

SyncProcessRunner::~SyncProcessRunner() = default;

void SyncProcessRunner::Schedule() {
  if (pending_changes_ == 0) {
    ScheduleInternal(kSyncDelayMaxInMilliseconds);
    return;
  }

  SyncServiceState last_service_state = service_state_;
  service_state_ = GetServiceState();

  switch (service_state_) {
    case SYNC_SERVICE_RUNNING:
      ResetThrottling();
      if (pending_changes_ > kPendingChangeThresholdForFastSync)
        ScheduleInternal(kSyncDelayFastInMilliseconds);
      else
        ScheduleInternal(kSyncDelayInMilliseconds);
      return;

    case SYNC_SERVICE_TEMPORARY_UNAVAILABLE:
      if (last_service_state != service_state_)
        ThrottleSync(kSyncDelaySlowInMilliseconds);
      ScheduleInternal(kSyncDelaySlowInMilliseconds);
      return;

    case SYNC_SERVICE_AUTHENTICATION_REQUIRED:
    case SYNC_SERVICE_DISABLED:
      if (last_service_state != service_state_)
        ThrottleSync(kSyncDelaySlowInMilliseconds);
      ScheduleInternal(kSyncDelayMaxInMilliseconds);
      return;
  }

  NOTREACHED();
}

void SyncProcessRunner::ThrottleSync(int64_t base_delay) {
  base::TimeTicks now = timer_helper_->Now();
  base::TimeDelta elapsed = std::min(now, throttle_until_) - throttle_from_;
  DCHECK(base::TimeDelta() <= elapsed);

  throttle_from_ = now;
  // Extend throttling duration by twice the elapsed time.
  // That is, if the backoff repeats in a short period, the throttling period
  // doesn't grow exponentially.  If the backoff happens on the end of
  // throttling period, it causes another throttling period that is twice as
  // long as previous.
  base::TimeDelta base_delay_delta = base::Milliseconds(base_delay);
  const base::TimeDelta max_delay =
      base::Milliseconds(kSyncDelayMaxInMilliseconds);
  throttle_until_ =
      std::min(now + max_delay,
               std::max(now + base_delay_delta, throttle_until_ + 2 * elapsed));
}

void SyncProcessRunner::ResetOldThrottling() {
  if (throttle_until_ < base::TimeTicks::Now())
    ResetThrottling();
}

void SyncProcessRunner::ResetThrottling() {
  throttle_from_ = base::TimeTicks();
  throttle_until_ = base::TimeTicks();
}

SyncServiceState SyncProcessRunner::GetServiceState() {
  return client_->GetSyncServiceState();
}

void SyncProcessRunner::OnChangesUpdated(int64_t pending_changes) {
  DCHECK_GE(pending_changes, 0);
  int64_t old_pending_changes = pending_changes_;
  pending_changes_ = pending_changes;
  if (old_pending_changes != pending_changes) {
    CheckIfIdle();
    util::Log(logging::LOGGING_VERBOSE, FROM_HERE,
              "[%s] pending_changes updated: %" PRId64, name_.c_str(),
              pending_changes);
  }
  Schedule();
}

SyncFileSystemService* SyncProcessRunner::GetSyncService() {
  return client_->GetSyncService();
}

void SyncProcessRunner::Finished(const base::TimeTicks& start_time,
                                 SyncStatusCode status) {
  DCHECK_LT(0u, running_tasks_);
  DCHECK_LE(running_tasks_, max_parallel_task_);
  --running_tasks_;
  CheckIfIdle();
  util::Log(logging::LOGGING_VERBOSE, FROM_HERE,
            "[%s] * Finished (elapsed: %" PRId64 " ms)", name_.c_str(),
            (timer_helper_->Now() - start_time).InMilliseconds());

  if (status == SYNC_STATUS_NO_CHANGE_TO_SYNC ||
      status == SYNC_STATUS_FILE_BUSY) {
    ScheduleInternal(kSyncDelayMaxInMilliseconds);
    return;
  }

  if (WasSuccessfulSync(status))
    ResetOldThrottling();
  else
    ThrottleSync(kSyncDelayWithSyncError);

  Schedule();
}

void SyncProcessRunner::Run() {
  if (running_tasks_ >= max_parallel_task_)
    return;
  ++running_tasks_;
  base::TimeTicks now = timer_helper_->Now();
  last_run_ = now;

  util::Log(logging::LOGGING_VERBOSE, FROM_HERE, "[%s] * Started",
            name_.c_str());

  StartSync(
      base::BindOnce(&SyncProcessRunner::Finished, factory_.GetWeakPtr(), now));
  if (running_tasks_ < max_parallel_task_)
    Schedule();
}

void SyncProcessRunner::ScheduleInternal(int64_t delay) {
  base::TimeTicks now = timer_helper_->Now();
  base::TimeTicks next_scheduled;

  if (timer_helper_->IsRunning()) {
    next_scheduled = last_run_ + base::Milliseconds(delay);
    if (next_scheduled < now) {
      next_scheduled = now + base::Milliseconds(kSyncDelayFastInMilliseconds);
    }
  } else {
    next_scheduled = now + base::Milliseconds(delay);
  }

  if (next_scheduled < throttle_until_)
    next_scheduled = throttle_until_;

  if (timer_helper_->IsRunning() && last_scheduled_ == next_scheduled)
    return;

  util::Log(logging::LOGGING_VERBOSE, FROM_HERE,
            "[%s] Scheduling task in %" PRId64 " ms", name_.c_str(),
            (next_scheduled - now).InMilliseconds());

  last_scheduled_ = next_scheduled;

  timer_helper_->Start(
      FROM_HERE, next_scheduled - now,
      base::BindOnce(&SyncProcessRunner::Run, base::Unretained(this)));
}

void SyncProcessRunner::CheckIfIdle() {
  if (pending_changes_ == 0 && running_tasks_ == 0)
    client_->OnSyncIdle();
}

}  // namespace sync_file_system