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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
|
// 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 "net/disk_cache/simple/simple_backend_impl.h"
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <limits>
#include "base/functional/callback_helpers.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_POSIX)
#include <sys/resource.h>
#endif
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/system/sys_info.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "net/base/net_errors.h"
#include "net/base/prioritized_task_runner.h"
#include "net/disk_cache/backend_cleanup_tracker.h"
#include "net/disk_cache/cache_util.h"
#include "net/disk_cache/simple/simple_entry_format.h"
#include "net/disk_cache/simple/simple_entry_impl.h"
#include "net/disk_cache/simple/simple_file_tracker.h"
#include "net/disk_cache/simple/simple_histogram_macros.h"
#include "net/disk_cache/simple/simple_index.h"
#include "net/disk_cache/simple/simple_index_file.h"
#include "net/disk_cache/simple/simple_synchronous_entry.h"
#include "net/disk_cache/simple/simple_util.h"
#include "net/disk_cache/simple/simple_version_upgrade.h"
using base::FilePath;
using base::Time;
namespace disk_cache {
namespace {
// Maximum fraction of the cache that one entry can consume.
constexpr int kMaxFileRatio = 8;
// Native code entries can be large. Rather than increasing the overall cache
// size, allow an individual entry to occupy up to half of the cache.
constexpr int kMaxNativeCodeFileRatio = 2;
// Overrides the above.
constexpr int64_t kMinFileSizeLimit = 5 * 1024 * 1024;
// Global context of all the files we have open --- this permits some to be
// closed on demand if too many FDs are being used, to avoid running out.
base::LazyInstance<SimpleFileTracker>::Leaky g_simple_file_tracker =
LAZY_INSTANCE_INITIALIZER;
// Detects if the files in the cache directory match the current disk cache
// backend type and version. If the directory contains no cache, occupies it
// with the fresh structure.
SimpleCacheConsistencyResult FileStructureConsistent(
BackendFileOperations* file_operations,
const base::FilePath& path) {
if (!file_operations->PathExists(path) &&
!file_operations->CreateDirectory(path)) {
LOG(ERROR) << "Failed to create directory: " << path.LossyDisplayName();
return SimpleCacheConsistencyResult::kCreateDirectoryFailed;
}
return disk_cache::UpgradeSimpleCacheOnDisk(file_operations, path);
}
// A context used by a BarrierCompletionCallback to track state.
struct BarrierContext {
explicit BarrierContext(net::CompletionOnceCallback final_callback,
int expected)
: final_callback_(std::move(final_callback)), expected(expected) {}
net::CompletionOnceCallback final_callback_;
const int expected;
int count = 0;
bool had_error = false;
};
void BarrierCompletionCallbackImpl(
BarrierContext* context,
int result) {
DCHECK_GT(context->expected, context->count);
if (context->had_error)
return;
if (result != net::OK) {
context->had_error = true;
std::move(context->final_callback_).Run(result);
return;
}
++context->count;
if (context->count == context->expected)
std::move(context->final_callback_).Run(net::OK);
}
// A barrier completion callback is a repeatable callback that waits for
// |count| successful results before invoking |final_callback|. In the case of
// an error, the first error is passed to |final_callback| and all others
// are ignored.
base::RepeatingCallback<void(int)> MakeBarrierCompletionCallback(
int count,
net::CompletionOnceCallback final_callback) {
BarrierContext* context =
new BarrierContext(std::move(final_callback), count);
return base::BindRepeating(&BarrierCompletionCallbackImpl,
base::Owned(context));
}
// A short bindable thunk that ensures a completion callback is always called
// after running an operation asynchronously. Checks for backend liveness first.
void RunOperationAndCallback(
base::WeakPtr<SimpleBackendImpl> backend,
base::OnceCallback<net::Error(net::CompletionOnceCallback)> operation,
net::CompletionOnceCallback operation_callback) {
if (!backend)
return;
auto split_callback = base::SplitOnceCallback(std::move(operation_callback));
const int operation_result =
std::move(operation).Run(std::move(split_callback.first));
if (operation_result != net::ERR_IO_PENDING && split_callback.second)
std::move(split_callback.second).Run(operation_result);
}
// Same but for things that work with EntryResult.
void RunEntryResultOperationAndCallback(
base::WeakPtr<SimpleBackendImpl> backend,
base::OnceCallback<EntryResult(EntryResultCallback)> operation,
EntryResultCallback operation_callback) {
if (!backend)
return;
auto split_callback = base::SplitOnceCallback(std::move(operation_callback));
EntryResult operation_result =
std::move(operation).Run(std::move(split_callback.first));
if (operation_result.net_error() != net::ERR_IO_PENDING &&
split_callback.second) {
std::move(split_callback.second).Run(std::move(operation_result));
}
}
void RecordIndexLoad(net::CacheType cache_type,
base::TimeTicks constructed_since,
int result) {
const base::TimeDelta creation_to_index = base::TimeTicks::Now() -
constructed_since;
if (result == net::OK) {
SIMPLE_CACHE_UMA(TIMES, "CreationToIndex", cache_type, creation_to_index);
} else {
SIMPLE_CACHE_UMA(TIMES,
"CreationToIndexFail", cache_type, creation_to_index);
}
}
SimpleEntryImpl::OperationsMode CacheTypeToOperationsMode(net::CacheType type) {
return (type == net::DISK_CACHE || type == net::GENERATED_BYTE_CODE_CACHE ||
type == net::GENERATED_NATIVE_CODE_CACHE ||
type == net::GENERATED_WEBUI_BYTE_CODE_CACHE)
? SimpleEntryImpl::OPTIMISTIC_OPERATIONS
: SimpleEntryImpl::NON_OPTIMISTIC_OPERATIONS;
}
} // namespace
class SimpleBackendImpl::ActiveEntryProxy
: public SimpleEntryImpl::ActiveEntryProxy {
public:
~ActiveEntryProxy() override {
if (backend_) {
DCHECK_EQ(1U, backend_->active_entries_.count(entry_hash_));
backend_->active_entries_.erase(entry_hash_);
}
}
static std::unique_ptr<SimpleEntryImpl::ActiveEntryProxy> Create(
int64_t entry_hash,
base::WeakPtr<SimpleBackendImpl> backend) {
return base::WrapUnique(
new ActiveEntryProxy(entry_hash, std::move(backend)));
}
private:
ActiveEntryProxy(uint64_t entry_hash,
base::WeakPtr<SimpleBackendImpl> backend)
: entry_hash_(entry_hash), backend_(std::move(backend)) {}
uint64_t entry_hash_;
base::WeakPtr<SimpleBackendImpl> backend_;
};
SimpleBackendImpl::SimpleBackendImpl(
scoped_refptr<BackendFileOperationsFactory> file_operations_factory,
const FilePath& path,
scoped_refptr<BackendCleanupTracker> cleanup_tracker,
SimpleFileTracker* file_tracker,
int64_t max_bytes,
net::CacheType cache_type,
net::NetLog* net_log)
: Backend(cache_type),
file_operations_factory_(
file_operations_factory
? std::move(file_operations_factory)
: base::MakeRefCounted<TrivialFileOperationsFactory>()),
cleanup_tracker_(std::move(cleanup_tracker)),
file_tracker_(file_tracker ? file_tracker
: g_simple_file_tracker.Pointer()),
path_(path),
orig_max_size_(max_bytes),
entry_operations_mode_(CacheTypeToOperationsMode(cache_type)),
post_doom_waiting_(
base::MakeRefCounted<SimplePostOperationWaiterTable>()),
post_open_by_hash_waiting_(
base::MakeRefCounted<SimplePostOperationWaiterTable>()),
net_log_(net_log) {
// Treat negative passed-in sizes same as in other backends, as default.
if (orig_max_size_ < 0)
orig_max_size_ = 0;
}
SimpleBackendImpl::~SimpleBackendImpl() {
// Write the index out if there is a pending write from a
// previous operation.
if (index_->HasPendingWrite())
index_->WriteToDisk(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN);
}
void SimpleBackendImpl::SetTaskRunnerForTesting(
scoped_refptr<base::SequencedTaskRunner> task_runner) {
prioritized_task_runner_ =
base::MakeRefCounted<net::PrioritizedTaskRunner>(kWorkerPoolTaskTraits);
prioritized_task_runner_->SetTaskRunnerForTesting( // IN-TEST
std::move(task_runner));
}
void SimpleBackendImpl::Init(CompletionOnceCallback completion_callback) {
auto index_task_runner = base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::WithBaseSyncPrimitives(),
base::TaskPriority::USER_BLOCKING,
base::TaskShutdownBehavior::BLOCK_SHUTDOWN});
prioritized_task_runner_ =
base::MakeRefCounted<net::PrioritizedTaskRunner>(kWorkerPoolTaskTraits);
index_ = std::make_unique<SimpleIndex>(
base::SequencedTaskRunner::GetCurrentDefault(), cleanup_tracker_.get(),
this, GetCacheType(),
std::make_unique<SimpleIndexFile>(
index_task_runner, file_operations_factory_, GetCacheType(), path_));
index_->ExecuteWhenReady(
base::BindOnce(&RecordIndexLoad, GetCacheType(), base::TimeTicks::Now()));
auto file_operations = file_operations_factory_->Create(index_task_runner);
index_task_runner->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&SimpleBackendImpl::InitCacheStructureOnDisk,
std::move(file_operations), path_, orig_max_size_,
GetCacheType()),
base::BindOnce(&SimpleBackendImpl::InitializeIndex,
weak_ptr_factory_.GetWeakPtr(),
std::move(completion_callback)));
}
int64_t SimpleBackendImpl::MaxFileSize() const {
uint64_t file_size_ratio = GetCacheType() == net::GENERATED_NATIVE_CODE_CACHE
? kMaxNativeCodeFileRatio
: kMaxFileRatio;
return std::max(
base::saturated_cast<int64_t>(index_->max_size() / file_size_ratio),
kMinFileSizeLimit);
}
scoped_refptr<SimplePostOperationWaiterTable> SimpleBackendImpl::OnDoomStart(
uint64_t entry_hash) {
post_doom_waiting_->OnOperationStart(entry_hash);
return post_doom_waiting_;
}
void SimpleBackendImpl::DoomEntries(std::vector<uint64_t>* entry_hashes,
net::CompletionOnceCallback callback) {
auto mass_doom_entry_hashes = std::make_unique<std::vector<uint64_t>>();
mass_doom_entry_hashes->swap(*entry_hashes);
std::vector<uint64_t> to_doom_individually_hashes;
// For each of the entry hashes, there are two cases:
// 1. There are corresponding entries in active set, pending doom, or both
// sets, and so the hash should be doomed individually to avoid flakes.
// 2. The hash is not in active use at all, so we can call
// SimpleSynchronousEntry::DeleteEntrySetFiles and delete the files en
// masse.
for (int i = mass_doom_entry_hashes->size() - 1; i >= 0; --i) {
const uint64_t entry_hash = (*mass_doom_entry_hashes)[i];
if (!active_entries_.count(entry_hash) &&
!post_doom_waiting_->Has(entry_hash)) {
continue;
}
to_doom_individually_hashes.push_back(entry_hash);
(*mass_doom_entry_hashes)[i] = mass_doom_entry_hashes->back();
mass_doom_entry_hashes->resize(mass_doom_entry_hashes->size() - 1);
}
base::RepeatingCallback<void(int)> barrier_callback =
MakeBarrierCompletionCallback(to_doom_individually_hashes.size() + 1,
std::move(callback));
for (std::vector<uint64_t>::const_iterator
it = to_doom_individually_hashes.begin(),
end = to_doom_individually_hashes.end();
it != end; ++it) {
const int doom_result = DoomEntryFromHash(*it, barrier_callback);
DCHECK_EQ(net::ERR_IO_PENDING, doom_result);
index_->Remove(*it);
}
for (std::vector<uint64_t>::const_iterator
it = mass_doom_entry_hashes->begin(),
end = mass_doom_entry_hashes->end();
it != end; ++it) {
index_->Remove(*it);
OnDoomStart(*it);
}
// Taking this pointer here avoids undefined behaviour from calling
// std::move() before mass_doom_entry_hashes.get().
std::vector<uint64_t>* mass_doom_entry_hashes_ptr =
mass_doom_entry_hashes.get();
// We don't use priorities (i.e., `prioritized_task_runner_`) here because
// we don't actually have them here (since this is for eviction based on
// index).
auto task_runner =
base::ThreadPool::CreateSequencedTaskRunner(kWorkerPoolTaskTraits);
task_runner->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&SimpleSynchronousEntry::DeleteEntrySetFiles,
mass_doom_entry_hashes_ptr, path_,
file_operations_factory_->CreateUnbound()),
base::BindOnce(&SimpleBackendImpl::DoomEntriesComplete,
weak_ptr_factory_.GetWeakPtr(),
std::move(mass_doom_entry_hashes), barrier_callback));
}
int32_t SimpleBackendImpl::GetEntryCount(
net::Int32CompletionOnceCallback callback) const {
// TODO(pasko): Use directory file count when index is not ready.
return index_->GetEntryCount();
}
EntryResult SimpleBackendImpl::OpenEntry(const std::string& key,
net::RequestPriority request_priority,
EntryResultCallback callback) {
const uint64_t entry_hash = simple_util::GetEntryHashKey(key);
std::vector<base::OnceClosure>* post_operation = nullptr;
PostOperationQueue post_operation_queue = PostOperationQueue::kNone;
scoped_refptr<SimpleEntryImpl> simple_entry = CreateOrFindActiveOrDoomedEntry(
entry_hash, key, request_priority, post_operation, post_operation_queue);
if (!simple_entry) {
if (post_operation_queue == PostOperationQueue::kPostDoom &&
post_operation->empty() &&
entry_operations_mode_ == SimpleEntryImpl::OPTIMISTIC_OPERATIONS) {
// The entry is doomed, and no other backend operations are queued for the
// entry, thus the open must fail and it's safe to return synchronously.
net::NetLogWithSource log_for_entry(net::NetLogWithSource::Make(
net_log_, net::NetLogSourceType::DISK_CACHE_ENTRY));
log_for_entry.AddEvent(
net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_CALL);
log_for_entry.AddEventWithNetErrorCode(
net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_END, net::ERR_FAILED);
return EntryResult::MakeError(net::ERR_FAILED);
}
base::OnceCallback<EntryResult(EntryResultCallback)> operation =
base::BindOnce(&SimpleBackendImpl::OpenEntry, base::Unretained(this),
key, request_priority);
post_operation->emplace_back(base::BindOnce(
&RunEntryResultOperationAndCallback, weak_ptr_factory_.GetWeakPtr(),
std::move(operation), std::move(callback)));
return EntryResult::MakeError(net::ERR_IO_PENDING);
}
return simple_entry->OpenEntry(std::move(callback));
}
EntryResult SimpleBackendImpl::CreateEntry(
const std::string& key,
net::RequestPriority request_priority,
EntryResultCallback callback) {
DCHECK_LT(0u, key.size());
const uint64_t entry_hash = simple_util::GetEntryHashKey(key);
std::vector<base::OnceClosure>* post_operation = nullptr;
PostOperationQueue post_operation_queue = PostOperationQueue::kNone;
scoped_refptr<SimpleEntryImpl> simple_entry = CreateOrFindActiveOrDoomedEntry(
entry_hash, key, request_priority, post_operation, post_operation_queue);
// If couldn't grab an entry object due to pending doom, see if circumstances
// are right for an optimistic create.
if (!simple_entry && post_operation_queue == PostOperationQueue::kPostDoom) {
simple_entry = MaybeOptimisticCreateForPostDoom(
entry_hash, key, request_priority, post_operation);
}
// If that doesn't work either, retry this once doom / open by hash is done.
if (!simple_entry) {
base::OnceCallback<EntryResult(EntryResultCallback)> operation =
base::BindOnce(&SimpleBackendImpl::CreateEntry, base::Unretained(this),
key, request_priority);
post_operation->emplace_back(base::BindOnce(
&RunEntryResultOperationAndCallback, weak_ptr_factory_.GetWeakPtr(),
std::move(operation), std::move(callback)));
return EntryResult::MakeError(net::ERR_IO_PENDING);
}
return simple_entry->CreateEntry(std::move(callback));
}
EntryResult SimpleBackendImpl::OpenOrCreateEntry(
const std::string& key,
net::RequestPriority request_priority,
EntryResultCallback callback) {
DCHECK_LT(0u, key.size());
const uint64_t entry_hash = simple_util::GetEntryHashKey(key);
std::vector<base::OnceClosure>* post_operation = nullptr;
PostOperationQueue post_operation_queue = PostOperationQueue::kNone;
scoped_refptr<SimpleEntryImpl> simple_entry = CreateOrFindActiveOrDoomedEntry(
entry_hash, key, request_priority, post_operation, post_operation_queue);
// If couldn't grab an entry object due to pending doom, see if circumstances
// are right for an optimistic create.
if (!simple_entry) {
if (post_operation_queue == PostOperationQueue::kPostDoom) {
simple_entry = MaybeOptimisticCreateForPostDoom(
entry_hash, key, request_priority, post_operation);
}
if (simple_entry) {
return simple_entry->CreateEntry(std::move(callback));
} else {
// If that doesn't work either, retry this once doom / open by hash is
// done.
base::OnceCallback<EntryResult(EntryResultCallback)> operation =
base::BindOnce(&SimpleBackendImpl::OpenOrCreateEntry,
base::Unretained(this), key, request_priority);
post_operation->emplace_back(base::BindOnce(
&RunEntryResultOperationAndCallback, weak_ptr_factory_.GetWeakPtr(),
std::move(operation), std::move(callback)));
return EntryResult::MakeError(net::ERR_IO_PENDING);
}
}
return simple_entry->OpenOrCreateEntry(std::move(callback));
}
scoped_refptr<SimpleEntryImpl>
SimpleBackendImpl::MaybeOptimisticCreateForPostDoom(
uint64_t entry_hash,
const std::string& key,
net::RequestPriority request_priority,
std::vector<base::OnceClosure>* post_doom) {
scoped_refptr<SimpleEntryImpl> simple_entry;
// We would like to optimistically have create go ahead, for benefit of
// HTTP cache use. This can only be sanely done if we are the only op
// serialized after doom's completion.
if (post_doom->empty() &&
entry_operations_mode_ == SimpleEntryImpl::OPTIMISTIC_OPERATIONS) {
simple_entry = base::MakeRefCounted<SimpleEntryImpl>(
GetCacheType(), path_, cleanup_tracker_.get(), entry_hash,
entry_operations_mode_, this, file_tracker_, file_operations_factory_,
net_log_, GetNewEntryPriority(request_priority));
simple_entry->SetKey(key);
simple_entry->SetActiveEntryProxy(
ActiveEntryProxy::Create(entry_hash, weak_ptr_factory_.GetWeakPtr()));
simple_entry->SetCreatePendingDoom();
std::pair<EntryMap::iterator, bool> insert_result = active_entries_.insert(
EntryMap::value_type(entry_hash, simple_entry.get()));
post_doom->emplace_back(base::BindOnce(
&SimpleEntryImpl::NotifyDoomBeforeCreateComplete, simple_entry));
DCHECK(insert_result.second);
}
return simple_entry;
}
net::Error SimpleBackendImpl::DoomEntry(const std::string& key,
net::RequestPriority priority,
CompletionOnceCallback callback) {
const uint64_t entry_hash = simple_util::GetEntryHashKey(key);
std::vector<base::OnceClosure>* post_operation = nullptr;
PostOperationQueue post_operation_queue = PostOperationQueue::kNone;
scoped_refptr<SimpleEntryImpl> simple_entry = CreateOrFindActiveOrDoomedEntry(
entry_hash, key, priority, post_operation, post_operation_queue);
if (!simple_entry) {
// At first glance, it appears exceedingly silly to queue up a doom when we
// get here with `post_operation_queue == PostOperationQueue::kPostDoom`,
// e.g. a doom already pending; but it's possible that the sequence of
// operations is Doom/Create/Doom, in which case the second Doom is not
// at all redundant.
base::OnceCallback<net::Error(CompletionOnceCallback)> operation =
base::BindOnce(&SimpleBackendImpl::DoomEntry, base::Unretained(this),
key, priority);
post_operation->emplace_back(
base::BindOnce(&RunOperationAndCallback, weak_ptr_factory_.GetWeakPtr(),
std::move(operation), std::move(callback)));
return net::ERR_IO_PENDING;
}
return simple_entry->DoomEntry(std::move(callback));
}
net::Error SimpleBackendImpl::DoomAllEntries(CompletionOnceCallback callback) {
return DoomEntriesBetween(Time(), Time(), std::move(callback));
}
net::Error SimpleBackendImpl::DoomEntriesBetween(
const Time initial_time,
const Time end_time,
CompletionOnceCallback callback) {
index_->ExecuteWhenReady(base::BindOnce(
&SimpleBackendImpl::IndexReadyForDoom, weak_ptr_factory_.GetWeakPtr(),
initial_time, end_time, std::move(callback)));
return net::ERR_IO_PENDING;
}
net::Error SimpleBackendImpl::DoomEntriesSince(
const Time initial_time,
CompletionOnceCallback callback) {
return DoomEntriesBetween(initial_time, Time(), std::move(callback));
}
int64_t SimpleBackendImpl::CalculateSizeOfAllEntries(
Int64CompletionOnceCallback callback) {
index_->ExecuteWhenReady(
base::BindOnce(&SimpleBackendImpl::IndexReadyForSizeCalculation,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
return net::ERR_IO_PENDING;
}
int64_t SimpleBackendImpl::CalculateSizeOfEntriesBetween(
base::Time initial_time,
base::Time end_time,
Int64CompletionOnceCallback callback) {
index_->ExecuteWhenReady(
base::BindOnce(&SimpleBackendImpl::IndexReadyForSizeBetweenCalculation,
weak_ptr_factory_.GetWeakPtr(), initial_time, end_time,
std::move(callback)));
return net::ERR_IO_PENDING;
}
class SimpleBackendImpl::SimpleIterator final : public Iterator {
public:
explicit SimpleIterator(base::WeakPtr<SimpleBackendImpl> backend)
: backend_(backend) {}
// From Backend::Iterator:
EntryResult OpenNextEntry(EntryResultCallback callback) override {
if (!backend_)
return EntryResult::MakeError(net::ERR_FAILED);
CompletionOnceCallback open_next_entry_impl =
base::BindOnce(&SimpleIterator::OpenNextEntryImpl,
weak_factory_.GetWeakPtr(), std::move(callback));
backend_->index_->ExecuteWhenReady(std::move(open_next_entry_impl));
return EntryResult::MakeError(net::ERR_IO_PENDING);
}
void OpenNextEntryImpl(EntryResultCallback callback,
int index_initialization_error_code) {
if (!backend_) {
std::move(callback).Run(EntryResult::MakeError(net::ERR_FAILED));
return;
}
if (index_initialization_error_code != net::OK) {
std::move(callback).Run(EntryResult::MakeError(
static_cast<net::Error>(index_initialization_error_code)));
return;
}
if (!hashes_to_enumerate_)
hashes_to_enumerate_ = backend_->index()->GetAllHashes();
while (!hashes_to_enumerate_->empty()) {
uint64_t entry_hash = hashes_to_enumerate_->back();
hashes_to_enumerate_->pop_back();
if (backend_->index()->Has(entry_hash)) {
auto split_callback = base::SplitOnceCallback(std::move(callback));
callback = std::move(split_callback.first);
EntryResultCallback continue_iteration = base::BindOnce(
&SimpleIterator::CheckIterationReturnValue,
weak_factory_.GetWeakPtr(), std::move(split_callback.second));
EntryResult open_result = backend_->OpenEntryFromHash(
entry_hash, std::move(continue_iteration));
if (open_result.net_error() == net::ERR_IO_PENDING)
return;
if (open_result.net_error() != net::ERR_FAILED) {
std::move(callback).Run(std::move(open_result));
return;
}
}
}
std::move(callback).Run(EntryResult::MakeError(net::ERR_FAILED));
}
void CheckIterationReturnValue(EntryResultCallback callback,
EntryResult result) {
if (result.net_error() == net::ERR_FAILED) {
OpenNextEntry(std::move(callback));
return;
}
std::move(callback).Run(std::move(result));
}
private:
base::WeakPtr<SimpleBackendImpl> backend_;
std::unique_ptr<std::vector<uint64_t>> hashes_to_enumerate_;
base::WeakPtrFactory<SimpleIterator> weak_factory_{this};
};
std::unique_ptr<Backend::Iterator> SimpleBackendImpl::CreateIterator() {
return std::make_unique<SimpleIterator>(weak_ptr_factory_.GetWeakPtr());
}
void SimpleBackendImpl::GetStats(base::StringPairs* stats) {
std::pair<std::string, std::string> item;
item.first = "Cache type";
item.second = "Simple Cache";
stats->push_back(item);
}
void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) {
index_->UseIfExists(simple_util::GetEntryHashKey(key));
}
uint8_t SimpleBackendImpl::GetEntryInMemoryData(const std::string& key) {
const uint64_t entry_hash = simple_util::GetEntryHashKey(key);
return index_->GetEntryInMemoryData(entry_hash);
}
void SimpleBackendImpl::SetEntryInMemoryData(const std::string& key,
uint8_t data) {
const uint64_t entry_hash = simple_util::GetEntryHashKey(key);
index_->SetEntryInMemoryData(entry_hash, data);
}
void SimpleBackendImpl::InitializeIndex(CompletionOnceCallback callback,
const DiskStatResult& result) {
if (result.net_error == net::OK) {
index_->SetMaxSize(result.max_size);
#if BUILDFLAG(IS_ANDROID)
if (app_status_listener_getter_) {
index_->set_app_status_listener_getter(
std::move(app_status_listener_getter_));
}
#endif
index_->Initialize(result.cache_dir_mtime);
}
std::move(callback).Run(result.net_error);
}
void SimpleBackendImpl::IndexReadyForDoom(Time initial_time,
Time end_time,
CompletionOnceCallback callback,
int result) {
if (result != net::OK) {
std::move(callback).Run(result);
return;
}
std::unique_ptr<std::vector<uint64_t>> removed_key_hashes(
index_->GetEntriesBetween(initial_time, end_time).release());
DoomEntries(removed_key_hashes.get(), std::move(callback));
}
void SimpleBackendImpl::IndexReadyForSizeCalculation(
Int64CompletionOnceCallback callback,
int result) {
int64_t rv = result == net::OK ? index_->GetCacheSize() : result;
std::move(callback).Run(rv);
}
void SimpleBackendImpl::IndexReadyForSizeBetweenCalculation(
base::Time initial_time,
base::Time end_time,
Int64CompletionOnceCallback callback,
int result) {
int64_t rv = result == net::OK
? index_->GetCacheSizeBetween(initial_time, end_time)
: result;
std::move(callback).Run(rv);
}
// static
SimpleBackendImpl::DiskStatResult SimpleBackendImpl::InitCacheStructureOnDisk(
std::unique_ptr<BackendFileOperations> file_operations,
const base::FilePath& path,
uint64_t suggested_max_size,
net::CacheType cache_type) {
DiskStatResult result;
result.max_size = suggested_max_size;
result.net_error = net::OK;
SimpleCacheConsistencyResult consistency =
FileStructureConsistent(file_operations.get(), path);
SIMPLE_CACHE_UMA(ENUMERATION, "ConsistencyResult", cache_type, consistency);
// If the cache structure is inconsistent make a single attempt at
// recovering it. Previously there were bugs that could cause a partially
// written fake index file to be left in an otherwise empty cache. In
// that case we can delete the index files and start over. Also, some
// consistency failures may leave an empty directory directly and we can
// retry those cases as well.
if (consistency != SimpleCacheConsistencyResult::kOK) {
bool deleted_files = disk_cache::DeleteIndexFilesIfCacheIsEmpty(path);
SIMPLE_CACHE_UMA(BOOLEAN, "DidDeleteIndexFilesAfterFailedConsistency",
cache_type, deleted_files);
if (base::IsDirectoryEmpty(path)) {
SimpleCacheConsistencyResult orig_consistency = consistency;
consistency = FileStructureConsistent(file_operations.get(), path);
SIMPLE_CACHE_UMA(ENUMERATION, "RetryConsistencyResult", cache_type,
consistency);
if (consistency == SimpleCacheConsistencyResult::kOK) {
SIMPLE_CACHE_UMA(ENUMERATION,
"OriginalConsistencyResultBeforeSuccessfulRetry",
cache_type, orig_consistency);
}
}
if (deleted_files) {
SIMPLE_CACHE_UMA(ENUMERATION, "ConsistencyResultAfterIndexFilesDeleted",
cache_type, consistency);
}
}
if (consistency != SimpleCacheConsistencyResult::kOK) {
LOG(ERROR) << "Simple Cache Backend: wrong file structure on disk: "
<< static_cast<int>(consistency)
<< " path: " << path.LossyDisplayName();
result.net_error = net::ERR_FAILED;
} else {
std::optional<base::File::Info> file_info =
file_operations->GetFileInfo(path);
if (!file_info.has_value()) {
// Something deleted the directory between when we set it up and the
// mstat; this is not uncommon on some test fixtures which erase their
// tempdir while some worker threads may still be running.
LOG(ERROR) << "Simple Cache Backend: cache directory inaccessible right "
"after creation; path: "
<< path.LossyDisplayName();
result.net_error = net::ERR_FAILED;
} else {
result.cache_dir_mtime = file_info->last_modified;
if (!result.max_size) {
int64_t available = base::SysInfo::AmountOfFreeDiskSpace(path);
result.max_size = disk_cache::PreferredCacheSize(available, cache_type);
DCHECK(result.max_size);
}
}
}
return result;
}
scoped_refptr<SimpleEntryImpl>
SimpleBackendImpl::CreateOrFindActiveOrDoomedEntry(
const uint64_t entry_hash,
const std::string& key,
net::RequestPriority request_priority,
std::vector<base::OnceClosure>*& post_operation,
PostOperationQueue& post_operation_queue) {
DCHECK_EQ(entry_hash, simple_util::GetEntryHashKey(key));
// If there is a doom pending, we would want to serialize after it.
std::vector<base::OnceClosure>* post_doom =
post_doom_waiting_->Find(entry_hash);
if (post_doom) {
post_operation = post_doom;
post_operation_queue = PostOperationQueue::kPostDoom;
return nullptr;
}
std::pair<EntryMap::iterator, bool> insert_result =
active_entries_.insert(EntryMap::value_type(entry_hash, nullptr));
EntryMap::iterator& it = insert_result.first;
const bool did_insert = insert_result.second;
if (did_insert) {
SimpleEntryImpl* entry = it->second = new SimpleEntryImpl(
GetCacheType(), path_, cleanup_tracker_.get(), entry_hash,
entry_operations_mode_, this, file_tracker_, file_operations_factory_,
net_log_, GetNewEntryPriority(request_priority));
entry->SetKey(key);
entry->SetActiveEntryProxy(
ActiveEntryProxy::Create(entry_hash, weak_ptr_factory_.GetWeakPtr()));
}
// TODO(jkarlin): In case of recycling a half-closed entry, we might want to
// update its priority.
DCHECK(it->second);
// It's possible, but unlikely, that we have an entry hash collision with a
// currently active entry, or we may not know the key of active entry yet,
// since it's being opened by hash.
if (key != it->second->key()) {
DCHECK(!did_insert);
if (it->second->key().has_value()) {
// Collision case.
it->second->Doom();
DCHECK_EQ(0U, active_entries_.count(entry_hash));
DCHECK(post_doom_waiting_->Has(entry_hash));
// Re-run ourselves to handle the now-pending doom.
return CreateOrFindActiveOrDoomedEntry(entry_hash, key, request_priority,
post_operation,
post_operation_queue);
} else {
// Open by hash case.
post_operation = post_open_by_hash_waiting_->Find(entry_hash);
CHECK(post_operation);
post_operation_queue = PostOperationQueue::kPostOpenByHash;
return nullptr;
}
}
return base::WrapRefCounted(it->second);
}
EntryResult SimpleBackendImpl::OpenEntryFromHash(uint64_t entry_hash,
EntryResultCallback callback) {
std::vector<base::OnceClosure>* post_doom =
post_doom_waiting_->Find(entry_hash);
if (post_doom) {
base::OnceCallback<EntryResult(EntryResultCallback)> operation =
base::BindOnce(&SimpleBackendImpl::OpenEntryFromHash,
base::Unretained(this), entry_hash);
// TODO(crbug.com/40105434) The cancellation behavior looks wrong.
post_doom->emplace_back(base::BindOnce(
&RunEntryResultOperationAndCallback, weak_ptr_factory_.GetWeakPtr(),
std::move(operation), std::move(callback)));
return EntryResult::MakeError(net::ERR_IO_PENDING);
}
std::pair<EntryMap::iterator, bool> insert_result =
active_entries_.insert(EntryMap::value_type(entry_hash, nullptr));
EntryMap::iterator& it = insert_result.first;
const bool did_insert = insert_result.second;
// This needs to be here to keep the new entry alive until ->OpenEntry.
scoped_refptr<SimpleEntryImpl> simple_entry;
if (did_insert) {
simple_entry = base::MakeRefCounted<SimpleEntryImpl>(
GetCacheType(), path_, cleanup_tracker_.get(), entry_hash,
entry_operations_mode_, this, file_tracker_, file_operations_factory_,
net_log_, GetNewEntryPriority(net::HIGHEST));
it->second = simple_entry.get();
simple_entry->SetActiveEntryProxy(
ActiveEntryProxy::Create(entry_hash, weak_ptr_factory_.GetWeakPtr()));
post_open_by_hash_waiting_->OnOperationStart(entry_hash);
callback = base::BindOnce(&SimpleBackendImpl::OnEntryOpenedFromHash,
weak_ptr_factory_.GetWeakPtr(), entry_hash,
std::move(callback));
}
// Note: the !did_insert case includes when another OpenEntryFromHash is
// pending; we don't care since that one will take care of the queue and we
// don't need to check for key collisions.
return it->second->OpenEntry(std::move(callback));
}
net::Error SimpleBackendImpl::DoomEntryFromHash(
uint64_t entry_hash,
CompletionOnceCallback callback) {
std::vector<base::OnceClosure>* post_doom =
post_doom_waiting_->Find(entry_hash);
if (post_doom) {
base::OnceCallback<net::Error(CompletionOnceCallback)> operation =
base::BindOnce(&SimpleBackendImpl::DoomEntryFromHash,
base::Unretained(this), entry_hash);
post_doom->emplace_back(
base::BindOnce(&RunOperationAndCallback, weak_ptr_factory_.GetWeakPtr(),
std::move(operation), std::move(callback)));
return net::ERR_IO_PENDING;
}
auto active_it = active_entries_.find(entry_hash);
if (active_it != active_entries_.end())
return active_it->second->DoomEntry(std::move(callback));
// There's no pending dooms, nor any open entry. We can make a trivial
// call to DoomEntries() to delete this entry.
std::vector<uint64_t> entry_hash_vector;
entry_hash_vector.push_back(entry_hash);
DoomEntries(&entry_hash_vector, std::move(callback));
return net::ERR_IO_PENDING;
}
void SimpleBackendImpl::OnEntryOpenedFromHash(
uint64_t hash,
EntryResultCallback callback,
EntryResult result) {
post_open_by_hash_waiting_->OnOperationComplete(hash);
std::move(callback).Run(std::move(result));
}
void SimpleBackendImpl::DoomEntriesComplete(
std::unique_ptr<std::vector<uint64_t>> entry_hashes,
CompletionOnceCallback callback,
int result) {
for (const uint64_t& entry_hash : *entry_hashes)
post_doom_waiting_->OnOperationComplete(entry_hash);
std::move(callback).Run(result);
}
uint32_t SimpleBackendImpl::GetNewEntryPriority(
net::RequestPriority request_priority) {
// Lower priority is better, so give high network priority the least bump.
return ((net::RequestPriority::MAXIMUM_PRIORITY - request_priority) * 10000) +
entry_count_++;
}
} // namespace disk_cache
|