Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1080)

Side by Side Diff: components/precache/core/precache_fetcher.cc

Issue 1961153003: Add pause/resume functionality to precache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved members to proto and updated UMA Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/precache/core/precache_fetcher.h" 5 #include "components/precache/core/precache_fetcher.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/bind_helpers.h"
14 #include "base/callback.h" 15 #include "base/callback.h"
15 #include "base/command_line.h" 16 #include "base/command_line.h"
16 #include "base/compiler_specific.h" 17 #include "base/compiler_specific.h"
17 #include "base/containers/hash_tables.h" 18 #include "base/containers/hash_tables.h"
19 #include "base/location.h"
18 #include "base/logging.h" 20 #include "base/logging.h"
19 #include "base/memory/ptr_util.h" 21 #include "base/memory/ptr_util.h"
22 #include "base/memory/ref_counted.h"
20 #include "base/metrics/histogram_macros.h" 23 #include "base/metrics/histogram_macros.h"
21 #include "components/precache/core/precache_switches.h" 24 #include "components/precache/core/precache_switches.h"
22 #include "components/precache/core/proto/precache.pb.h" 25 #include "components/precache/core/proto/precache.pb.h"
26 #include "components/precache/core/proto/unfinished_work.pb.h"
23 #include "net/base/completion_callback.h" 27 #include "net/base/completion_callback.h"
24 #include "net/base/escape.h" 28 #include "net/base/escape.h"
25 #include "net/base/io_buffer.h" 29 #include "net/base/io_buffer.h"
26 #include "net/base/load_flags.h" 30 #include "net/base/load_flags.h"
27 #include "net/base/net_errors.h" 31 #include "net/base/net_errors.h"
28 #include "net/http/http_response_headers.h" 32 #include "net/http/http_response_headers.h"
29 #include "net/url_request/url_fetcher_response_writer.h" 33 #include "net/url_request/url_fetcher_response_writer.h"
30 #include "net/url_request/url_request_context_getter.h" 34 #include "net/url_request/url_request_context_getter.h"
31 #include "net/url_request/url_request_status.h" 35 #include "net/url_request/url_request_status.h"
32 36
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 // If any of: 253 // If any of:
250 // - The request was for a config or manifest. 254 // - The request was for a config or manifest.
251 // - The resource was a cache hit without validators. 255 // - The resource was a cache hit without validators.
252 // - The response came from the network. 256 // - The response came from the network.
253 // Then Fetcher is done with this URL and can return control to the caller. 257 // Then Fetcher is done with this URL and can return control to the caller.
254 response_bytes_ = source->GetReceivedResponseContentLength(); 258 response_bytes_ = source->GetReceivedResponseContentLength();
255 network_response_bytes_ = source->GetTotalReceivedBytes(); 259 network_response_bytes_ = source->GetTotalReceivedBytes();
256 callback_.Run(*this); 260 callback_.Run(*this);
257 } 261 }
258 262
263 void PrecacheFetcher::RecordCompletionStatistics(
sclittle 2016/05/20 21:57:07 nit: add a // static
bengr 2016/05/21 00:16:32 Done.
264 const PrecacheUnfinishedWork& unfinished_work,
265 size_t remaining_manifest_urls_to_fetch,
266 size_t remaining_resource_urls_to_fetch) {
267 // These may be unset in tests.
268 if (unfinished_work.has_start_time()) {
269 base::TimeDelta time_to_fetch =
270 base::Time::Now() -
271 base::Time::FromInternalValue(unfinished_work.start_time());
272 UMA_HISTOGRAM_CUSTOM_TIMES("Precache.Fetch.TimeToComplete", time_to_fetch,
273 base::TimeDelta::FromSeconds(1),
274 base::TimeDelta::FromHours(4), 50);
275 }
276
277 // Number of manifests for which we have downloaded all resources.
278 int manifests_completed =
279 unfinished_work.num_manifest_urls() - remaining_manifest_urls_to_fetch;
280
281 // If there are resource URLs left to fetch, the last manifest is not yet
282 // completed.
283 if (remaining_resource_urls_to_fetch > 0)
284 --manifests_completed;
285
286 DCHECK_GE(manifests_completed, 0);
287 int percent_completed = unfinished_work.num_manifest_urls() == 0
288 ? 0
289 : (static_cast<double>(manifests_completed) /
290 unfinished_work.num_manifest_urls() * 100);
291
292 UMA_HISTOGRAM_PERCENTAGE("Precache.Fetch.PercentCompleted",
293 percent_completed);
294 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Total",
295 unfinished_work.total_bytes(),
296 1, kMaxResponseBytes, 100);
297 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Network",
298 unfinished_work.network_bytes(),
299 1, kMaxResponseBytes,
300 100);
301 }
302
259 PrecacheFetcher::PrecacheFetcher( 303 PrecacheFetcher::PrecacheFetcher(
260 const std::vector<std::string>& starting_hosts,
261 net::URLRequestContextGetter* request_context, 304 net::URLRequestContextGetter* request_context,
262 const GURL& config_url, 305 const GURL& config_url,
263 const std::string& manifest_url_prefix, 306 const std::string& manifest_url_prefix,
307 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work,
264 PrecacheFetcher::PrecacheDelegate* precache_delegate) 308 PrecacheFetcher::PrecacheDelegate* precache_delegate)
265 : starting_hosts_(starting_hosts), 309 : request_context_(request_context),
266 request_context_(request_context),
267 config_url_(config_url), 310 config_url_(config_url),
268 manifest_url_prefix_(manifest_url_prefix), 311 manifest_url_prefix_(manifest_url_prefix),
269 precache_delegate_(precache_delegate), 312 precache_delegate_(precache_delegate),
270 config_(new PrecacheConfigurationSettings),
271 total_response_bytes_(0),
272 network_response_bytes_(0),
273 num_manifest_urls_to_fetch_(0),
274 pool_(kMaxParallelFetches) { 313 pool_(kMaxParallelFetches) {
275 DCHECK(request_context_.get()); // Request context must be non-NULL. 314 DCHECK(request_context_.get()); // Request context must be non-NULL.
276 DCHECK(precache_delegate_); // Precache delegate must be non-NULL. 315 DCHECK(precache_delegate_); // Precache delegate must be non-NULL.
277 316
278 DCHECK_NE(GURL(), GetDefaultConfigURL()) 317 DCHECK_NE(GURL(), GetDefaultConfigURL())
279 << "Could not determine the precache config settings URL."; 318 << "Could not determine the precache config settings URL.";
280 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix()) 319 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix())
281 << "Could not determine the default precache manifest URL prefix."; 320 << "Could not determine the default precache manifest URL prefix.";
321 DCHECK(unfinished_work);
322
323 // Copy manifests and resources to member variables as a convenience.
324 // TODO(bengr): Consider accessing these directly from the proto.
325 for (const auto& manifest : unfinished_work->manifest()) {
326 if (manifest.has_url())
327 manifest_urls_to_fetch_.push_back(GURL(manifest.url()));
328 }
329 for (const auto& resource : unfinished_work->resource()) {
330 if (resource.has_url())
331 resource_urls_to_fetch_.push_back(GURL(resource.url()));
332 }
333 unfinished_work_ = std::move(unfinished_work);
282 } 334 }
283 335
284 PrecacheFetcher::~PrecacheFetcher() { 336 PrecacheFetcher::~PrecacheFetcher() {
285 base::TimeDelta time_to_fetch = base::TimeTicks::Now() - start_time_; 337 }
286 UMA_HISTOGRAM_CUSTOM_TIMES("Precache.Fetch.TimeToComplete", time_to_fetch,
287 base::TimeDelta::FromSeconds(1),
288 base::TimeDelta::FromHours(4), 50);
289 338
290 // Number of manifests for which we have downloaded all resources. 339 std::unique_ptr<PrecacheUnfinishedWork> PrecacheFetcher::CancelPrecaching() {
291 int manifests_completed = 340 unfinished_work_->clear_manifest();
292 num_manifest_urls_to_fetch_ - manifest_urls_to_fetch_.size(); 341 unfinished_work_->clear_resource();
293 342 for (const auto& manifest : manifest_urls_to_fetch_)
294 // If there are resource URLs left to fetch, the last manifest is not yet 343 unfinished_work_->add_manifest()->set_url(manifest.spec());
295 // completed. 344 for (const auto& resource : resource_urls_to_fetch_)
296 if (!resource_urls_to_fetch_.empty()) 345 unfinished_work_->add_resource()->set_url(resource.spec());
297 --manifests_completed; 346 return std::move(unfinished_work_);
298
299 DCHECK_GE(manifests_completed, 0);
300 int percent_completed = num_manifest_urls_to_fetch_ == 0
301 ? 0
302 : (static_cast<double>(manifests_completed) /
303 num_manifest_urls_to_fetch_ * 100);
304 UMA_HISTOGRAM_PERCENTAGE("Precache.Fetch.PercentCompleted",
305 percent_completed);
306 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Total",
307 total_response_bytes_, 1, kMaxResponseBytes, 100);
308 UMA_HISTOGRAM_CUSTOM_COUNTS("Precache.Fetch.ResponseBytes.Network",
309 network_response_bytes_, 1, kMaxResponseBytes,
310 100);
311 } 347 }
312 348
313 void PrecacheFetcher::Start() { 349 void PrecacheFetcher::Start() {
350 if (unfinished_work_->has_config_settings()) {
351 DCHECK(unfinished_work_->has_start_time());
352 DetermineManifests();
353 return;
354 }
355
314 GURL config_url = 356 GURL config_url =
315 config_url_.is_empty() ? GetDefaultConfigURL() : config_url_; 357 config_url_.is_empty() ? GetDefaultConfigURL() : config_url_;
316 358
317 DCHECK(config_url.is_valid()) << "Config URL not valid: " 359 DCHECK(config_url.is_valid()) << "Config URL not valid: "
318 << config_url.possibly_invalid_spec(); 360 << config_url.possibly_invalid_spec();
319 361
320 start_time_ = base::TimeTicks::Now(); 362 unfinished_work_->set_start_time(base::Time::Now().ToInternalValue());
321 363
322 // Fetch the precache configuration settings from the server. 364 // Fetch the precache configuration settings from the server.
323 DCHECK(pool_.IsEmpty()) << "All parallel requests should be available"; 365 DCHECK(pool_.IsEmpty()) << "All parallel requests should be available";
324 VLOG(3) << "Fetching " << config_url; 366 VLOG(3) << "Fetching " << config_url;
325 pool_.Add(base::WrapUnique(new Fetcher( 367 pool_.Add(base::WrapUnique(new Fetcher(
326 request_context_.get(), config_url, 368 request_context_.get(), config_url,
327 base::Bind(&PrecacheFetcher::OnConfigFetchComplete, 369 base::Bind(&PrecacheFetcher::OnConfigFetchComplete,
328 base::Unretained(this)), 370 base::Unretained(this)),
329 false /* is_resource_request */, std::numeric_limits<int32_t>::max()))); 371 false /* is_resource_request */, std::numeric_limits<int32_t>::max())));
330 } 372 }
331 373
332 void PrecacheFetcher::StartNextResourceFetch() { 374 void PrecacheFetcher::StartNextResourceFetch() {
375 DCHECK(unfinished_work_->has_config_settings());
333 while (!resource_urls_to_fetch_.empty() && pool_.IsAvailable()) { 376 while (!resource_urls_to_fetch_.empty() && pool_.IsAvailable()) {
334 const size_t max_bytes = 377 const size_t max_bytes =
335 std::min(config_->max_bytes_per_resource(), 378 std::min(unfinished_work_->config_settings().max_bytes_per_resource(),
336 config_->max_bytes_total() - total_response_bytes_); 379 unfinished_work_->config_settings().max_bytes_total() -
380 unfinished_work_->total_bytes());
337 VLOG(3) << "Fetching " << resource_urls_to_fetch_.front(); 381 VLOG(3) << "Fetching " << resource_urls_to_fetch_.front();
338 pool_.Add(base::WrapUnique( 382 pool_.Add(base::WrapUnique(
339 new Fetcher(request_context_.get(), resource_urls_to_fetch_.front(), 383 new Fetcher(request_context_.get(), resource_urls_to_fetch_.front(),
340 base::Bind(&PrecacheFetcher::OnResourceFetchComplete, 384 base::Bind(&PrecacheFetcher::OnResourceFetchComplete,
341 base::Unretained(this)), 385 base::Unretained(this)),
342 true /* is_resource_request */, max_bytes))); 386 true /* is_resource_request */, max_bytes)));
343 387
344 resource_urls_to_fetch_.pop_front(); 388 resource_urls_to_fetch_.pop_front();
345 } 389 }
346 } 390 }
(...skipping 11 matching lines...) Expand all
358 pool_.Add(base::WrapUnique(new Fetcher( 402 pool_.Add(base::WrapUnique(new Fetcher(
359 request_context_.get(), manifest_urls_to_fetch_.front(), 403 request_context_.get(), manifest_urls_to_fetch_.front(),
360 base::Bind(&PrecacheFetcher::OnManifestFetchComplete, 404 base::Bind(&PrecacheFetcher::OnManifestFetchComplete,
361 base::Unretained(this)), 405 base::Unretained(this)),
362 false /* is_resource_request */, std::numeric_limits<int32_t>::max()))); 406 false /* is_resource_request */, std::numeric_limits<int32_t>::max())));
363 407
364 manifest_urls_to_fetch_.pop_front(); 408 manifest_urls_to_fetch_.pop_front();
365 } 409 }
366 410
367 void PrecacheFetcher::StartNextFetch() { 411 void PrecacheFetcher::StartNextFetch() {
412 DCHECK(unfinished_work_->has_config_settings());
368 // If over the precache total size cap, then stop prefetching. 413 // If over the precache total size cap, then stop prefetching.
369 if (total_response_bytes_ > config_->max_bytes_total()) { 414 if (unfinished_work_->total_bytes() >
415 unfinished_work_->config_settings().max_bytes_total()) {
416 pool_.DeleteAll();
417 RecordCompletionStatistics(
sclittle 2016/05/20 21:57:07 nit: would it make sense to pull the RecordComplet
bengr 2016/05/21 00:16:32 Done.
418 *(unfinished_work_.get()), manifest_urls_to_fetch_.size(),
sclittle 2016/05/20 21:57:07 nit: you don't need the .get() here
bengr 2016/05/21 00:16:32 Done.
419 resource_urls_to_fetch_.size());
370 precache_delegate_->OnDone(); 420 precache_delegate_->OnDone();
371 return; 421 return;
372 } 422 }
373 423
374 StartNextResourceFetch(); 424 StartNextResourceFetch();
375 StartNextManifestFetch(); 425 StartNextManifestFetch();
376
377 if (pool_.IsEmpty()) { 426 if (pool_.IsEmpty()) {
378 // There are no more URLs to fetch, so end the precache cycle. 427 // There are no more URLs to fetch, so end the precache cycle.
428 RecordCompletionStatistics(*(unfinished_work_.get()), 0, 0);
379 precache_delegate_->OnDone(); 429 precache_delegate_->OnDone();
380 // OnDone may have deleted this PrecacheFetcher, so don't do anything after 430 // OnDone may have deleted this PrecacheFetcher, so don't do anything after
381 // it is called. 431 // it is called.
382 } 432 }
383 } 433 }
384 434
385 void PrecacheFetcher::OnConfigFetchComplete(const Fetcher& source) { 435 void PrecacheFetcher::OnConfigFetchComplete(const Fetcher& source) {
386 UpdateStats(source.response_bytes(), source.network_response_bytes()); 436 UpdateStats(source.response_bytes(), source.network_response_bytes());
387 if (source.network_url_fetcher() == nullptr) { 437 if (source.network_url_fetcher() == nullptr) {
388 pool_.DeleteAll(); // Cancel any other ongoing request. 438 pool_.DeleteAll(); // Cancel any other ongoing request.
389 } else { 439 } else {
390 // Attempt to parse the config proto. On failure, continue on with the 440 // Attempt to parse the config proto. On failure, continue on with the
391 // default configuration. 441 // default configuration.
392 ParseProtoFromFetchResponse(*source.network_url_fetcher(), config_.get()); 442 ParseProtoFromFetchResponse(
443 *source.network_url_fetcher(),
444 unfinished_work_->mutable_config_settings());
445 pool_.Delete(source);
446 DetermineManifests();
447 }
448 }
393 449
450 void PrecacheFetcher::DetermineManifests() {
451 DCHECK(unfinished_work_->has_config_settings());
394 std::string prefix = manifest_url_prefix_.empty() 452 std::string prefix = manifest_url_prefix_.empty()
395 ? GetDefaultManifestURLPrefix() 453 ? GetDefaultManifestURLPrefix()
396 : manifest_url_prefix_; 454 : manifest_url_prefix_;
397 DCHECK_NE(std::string(), prefix) 455 DCHECK_NE(std::string(), prefix)
398 << "Could not determine the precache manifest URL prefix."; 456 << "Could not determine the precache manifest URL prefix.";
399 457
400 // Keep track of manifest URLs that are being fetched, in order to elide 458 // Keep track of manifest URLs that are being fetched, in order to elide
401 // duplicates. 459 // duplicates.
402 base::hash_set<std::string> seen_manifest_urls; 460 base::hash_set<std::string> seen_manifest_urls;
403 461
404 // Attempt to fetch manifests for starting hosts up to the maximum top sites 462 // Attempt to fetch manifests for starting hosts up to the maximum top sites
405 // count. If a manifest does not exist for a particular starting host, then 463 // count. If a manifest does not exist for a particular starting host, then
406 // the fetch will fail, and that starting host will be ignored. 464 // the fetch will fail, and that starting host will be ignored. Starting
407 int64_t rank = 0; 465 // hosts are not added if this is a continuation from a previous precache
408 for (const std::string& host : starting_hosts_) { 466 // session.
409 ++rank; 467 if (manifest_urls_to_fetch_.empty() &&
410 if (rank > config_->top_sites_count()) 468 resource_urls_to_fetch_.empty()) {
411 break; 469 int64_t rank = 0;
412 AppendManifestURLIfNew(prefix, host, &seen_manifest_urls, 470 for (const auto& host : unfinished_work_->top_host()) {
413 &manifest_urls_to_fetch_); 471 ++rank;
472 if (rank > unfinished_work_->config_settings().top_sites_count())
473 break;
474 AppendManifestURLIfNew(prefix, host.hostname(), &seen_manifest_urls,
475 &manifest_urls_to_fetch_);
476 }
477
478 for (const std::string& host
479 : unfinished_work_->config_settings().forced_site()) {
480 AppendManifestURLIfNew(prefix, host, &seen_manifest_urls,
481 &manifest_urls_to_fetch_);
482 }
414 } 483 }
415 484 unfinished_work_->set_num_manifest_urls(manifest_urls_to_fetch_.size());
416 for (const std::string& host : config_->forced_site()) 485 StartNextFetch();
417 AppendManifestURLIfNew(prefix, host, &seen_manifest_urls,
418 &manifest_urls_to_fetch_);
419
420 num_manifest_urls_to_fetch_ = manifest_urls_to_fetch_.size();
421 }
422 pool_.Delete(source);
423
424 StartNextFetch();
425 } 486 }
426 487
427 void PrecacheFetcher::OnManifestFetchComplete(const Fetcher& source) { 488 void PrecacheFetcher::OnManifestFetchComplete(const Fetcher& source) {
489 DCHECK(unfinished_work_->has_config_settings());
428 UpdateStats(source.response_bytes(), source.network_response_bytes()); 490 UpdateStats(source.response_bytes(), source.network_response_bytes());
429 if (source.network_url_fetcher() == nullptr) { 491 if (source.network_url_fetcher() == nullptr) {
430 pool_.DeleteAll(); // Cancel any other ongoing request. 492 pool_.DeleteAll(); // Cancel any other ongoing request.
431 } else { 493 } else {
432 PrecacheManifest manifest; 494 PrecacheManifest manifest;
433 495
434 if (ParseProtoFromFetchResponse(*source.network_url_fetcher(), &manifest)) { 496 if (ParseProtoFromFetchResponse(*source.network_url_fetcher(), &manifest)) {
435 const int32_t len = 497 const int32_t len =
436 std::min(manifest.resource_size(), config_->top_resources_count()); 498 std::min(manifest.resource_size(),
499 unfinished_work_->config_settings().top_resources_count());
437 for (int i = 0; i < len; ++i) { 500 for (int i = 0; i < len; ++i) {
438 if (manifest.resource(i).has_url()) { 501 if (manifest.resource(i).has_url())
439 resource_urls_to_fetch_.push_back(GURL(manifest.resource(i).url())); 502 resource_urls_to_fetch_.push_back(GURL(manifest.resource(i).url()));
440 }
441 } 503 }
442 } 504 }
443 } 505 }
444 506
445 pool_.Delete(source); 507 pool_.Delete(source);
446 StartNextFetch(); 508 StartNextFetch();
447 } 509 }
448 510
449 void PrecacheFetcher::OnResourceFetchComplete(const Fetcher& source) { 511 void PrecacheFetcher::OnResourceFetchComplete(const Fetcher& source) {
450 UpdateStats(source.response_bytes(), source.network_response_bytes()); 512 UpdateStats(source.response_bytes(), source.network_response_bytes());
451 pool_.Delete(source); 513 pool_.Delete(source);
452 // The resource has already been put in the cache during the fetch process, so 514 // The resource has already been put in the cache during the fetch process, so
453 // nothing more needs to be done for the resource. 515 // nothing more needs to be done for the resource.
454 StartNextFetch(); 516 StartNextFetch();
455 } 517 }
456 518
457 void PrecacheFetcher::UpdateStats(int64_t response_bytes, 519 void PrecacheFetcher::UpdateStats(int64_t response_bytes,
458 int64_t network_response_bytes) { 520 int64_t network_response_bytes) {
459 total_response_bytes_ += response_bytes; 521 unfinished_work_->set_total_bytes(
460 network_response_bytes_ += network_response_bytes; 522 unfinished_work_->total_bytes() + response_bytes);
523 unfinished_work_->set_network_bytes(
524 unfinished_work_->network_bytes() + network_response_bytes);
461 } 525 }
462 526
463 } // namespace precache 527 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698