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

Side by Side Diff: components/offline_pages/offline_page_storage_manager.cc

Issue 2006923005: [Offline Pages] Two-step expiration in storage manager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments. Created 4 years, 6 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/offline_pages/offline_page_storage_manager.h" 5 #include "components/offline_pages/offline_page_storage_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/time/clock.h" 10 #include "base/time/clock.h"
(...skipping 29 matching lines...) Expand all
40 40
41 void OfflinePageStorageManager::SetClockForTesting( 41 void OfflinePageStorageManager::SetClockForTesting(
42 std::unique_ptr<base::Clock> clock) { 42 std::unique_ptr<base::Clock> clock) {
43 clock_ = std::move(clock); 43 clock_ = std::move(clock);
44 } 44 }
45 45
46 void OfflinePageStorageManager::OnGetStorageStatsDone( 46 void OfflinePageStorageManager::OnGetStorageStatsDone(
47 const ClearPagesCallback& callback, 47 const ClearPagesCallback& callback,
48 const ArchiveManager::StorageStats& stats) { 48 const ArchiveManager::StorageStats& stats) {
49 DCHECK(in_progress_); 49 DCHECK(in_progress_);
50 ClearMode mode = ShouldClearPages(stats); 50 base::Time now = clock_->Now();
romax 2016/05/25 20:05:24 this time would be used throughout the whole clear
51 ClearMode mode = ShouldClearPages(stats, now);
51 if (mode == ClearMode::NOT_NEEDED) { 52 if (mode == ClearMode::NOT_NEEDED) {
52 in_progress_ = false; 53 in_progress_ = false;
53 last_clear_time_ = clock_->Now(); 54 last_clear_time_ = clock_->Now();
54 callback.Run(0, ClearStorageResult::UNNECESSARY); 55 callback.Run(0, ClearStorageResult::UNNECESSARY);
55 return; 56 return;
56 } 57 }
57 client_->GetAllPages(base::Bind(&OfflinePageStorageManager::OnGetAllPagesDone, 58 client_->GetAllPages(base::Bind(&OfflinePageStorageManager::OnGetAllPagesDone,
58 weak_ptr_factory_.GetWeakPtr(), callback, 59 weak_ptr_factory_.GetWeakPtr(), callback,
59 stats)); 60 stats, now));
60 } 61 }
61 62
62 void OfflinePageStorageManager::OnGetAllPagesDone( 63 void OfflinePageStorageManager::OnGetAllPagesDone(
63 const ClearPagesCallback& callback, 64 const ClearPagesCallback& callback,
64 const ArchiveManager::StorageStats& stats, 65 const ArchiveManager::StorageStats& stats,
66 const base::Time& now,
65 const MultipleOfflinePageItemResult& pages) { 67 const MultipleOfflinePageItemResult& pages) {
66 std::vector<int64_t> offline_ids; 68 std::vector<int64_t>* page_ids_to_expire = new std::vector<int64_t>();
romax 2016/05/25 20:05:24 Jian Please take a look about these 2 pointers.. I
67 GetExpiredPageIds(pages, stats, offline_ids); 69 std::vector<int64_t>* page_ids_to_remove = new std::vector<int64_t>();
68 client_->DeletePagesByOfflineId( 70 GetPageIdsToClear(pages, stats, now, page_ids_to_expire, page_ids_to_remove);
69 offline_ids, base::Bind(&OfflinePageStorageManager::OnExpiredPagesDeleted, 71 client_->ExpirePages(
70 weak_ptr_factory_.GetWeakPtr(), callback, 72 *(base::Owned(page_ids_to_expire).get()), now,
71 static_cast<int>(offline_ids.size()))); 73 base::Bind(&OfflinePageStorageManager::OnPagesExpired,
74 weak_ptr_factory_.GetWeakPtr(), callback,
75 page_ids_to_expire->size(), base::Owned(page_ids_to_remove)));
72 } 76 }
73 77
74 void OfflinePageStorageManager::OnExpiredPagesDeleted( 78 void OfflinePageStorageManager::OnPagesExpired(
75 const ClearPagesCallback& callback, 79 const ClearPagesCallback& callback,
76 int pages_cleared, 80 size_t pages_expired,
81 std::vector<int64_t>* page_ids_to_remove,
82 bool success) {
83 client_->DeletePagesByOfflineId(
84 *page_ids_to_remove,
85 base::Bind(&OfflinePageStorageManager::OnDeadPagesCleared,
86 weak_ptr_factory_.GetWeakPtr(), callback, pages_expired,
87 success));
88 }
89
90 void OfflinePageStorageManager::OnDeadPagesCleared(
91 const ClearPagesCallback& callback,
92 size_t pages_cleared,
93 bool success,
77 DeletePageResult result) { 94 DeletePageResult result) {
78 last_clear_time_ = clock_->Now(); 95 last_clear_time_ = clock_->Now();
79 ClearStorageResult clear_result = result == DeletePageResult::SUCCESS 96 ClearStorageResult clear_result;
80 ? ClearStorageResult::SUCCESS 97 if (!success) {
81 : ClearStorageResult::DELETE_FAILURE; 98 clear_result = ClearStorageResult::EXPIRE_FAILURE;
99 } else {
100 clear_result = result == DeletePageResult::SUCCESS
101 ? ClearStorageResult::SUCCESS
102 : ClearStorageResult::DELETE_FAILURE;
103 }
82 in_progress_ = false; 104 in_progress_ = false;
83 callback.Run(pages_cleared, clear_result); 105 callback.Run(pages_cleared, clear_result);
84 } 106 }
85 107
86 void OfflinePageStorageManager::GetExpiredPageIds( 108 void OfflinePageStorageManager::GetPageIdsToClear(
87 const MultipleOfflinePageItemResult& pages, 109 const MultipleOfflinePageItemResult& pages,
88 const ArchiveManager::StorageStats& stats, 110 const ArchiveManager::StorageStats& stats,
89 std::vector<int64_t>& offline_ids) { 111 const base::Time& now,
90 base::Time now = clock_->Now(); 112 std::vector<int64_t>* page_ids_to_expire,
91 113 std::vector<int64_t>* page_ids_to_remove) {
92 // Creating a map from namespace to a vector of page items. 114 // Creating a map from namespace to a vector of page items.
93 // Sort each vector based on last accessed time and all pages after index 115 // Sort each vector based on last accessed time and all pages after index
94 // min{size(), page_limit} should be expired. And then start iterating 116 // min{size(), page_limit} should be expired. And then start iterating
95 // backwards to expire pages. 117 // backwards to expire pages.
96 std::map<std::string, std::vector<OfflinePageItem>> pages_map; 118 std::map<std::string, std::vector<OfflinePageItem>> pages_map;
97 std::vector<OfflinePageItem> kept_pages; 119 std::vector<OfflinePageItem> kept_pages;
98 int64_t kept_pages_size = 0; 120 int64_t kept_pages_size = 0;
99 121
100 for (const auto& page : pages) 122 for (const auto& page : pages) {
101 pages_map[page.client_id.name_space].push_back(page); 123 if (!page.IsExpired()) {
124 pages_map[page.client_id.name_space].push_back(page);
125 } else if (now - page.expiration_time >= kRemovePageItemInterval) {
126 page_ids_to_remove->push_back(page.offline_id);
127 }
128 }
102 129
103 for (auto& iter : pages_map) { 130 for (auto& iter : pages_map) {
104 std::string name_space = iter.first; 131 std::string name_space = iter.first;
105 std::vector<OfflinePageItem>& page_list = iter.second; 132 std::vector<OfflinePageItem>& page_list = iter.second;
106 133
107 LifetimePolicy policy = 134 LifetimePolicy policy =
108 policy_controller_->GetPolicy(name_space).lifetime_policy; 135 policy_controller_->GetPolicy(name_space).lifetime_policy;
109 136
110 std::sort(page_list.begin(), page_list.end(), 137 std::sort(page_list.begin(), page_list.end(),
111 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { 138 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool {
112 return a.last_access_time > b.last_access_time; 139 return a.last_access_time > b.last_access_time;
113 }); 140 });
114 141
115 int page_list_size = static_cast<int>(page_list.size()); 142 size_t page_list_size = page_list.size();
116 int pos = 0; 143 size_t pos = 0;
117 while (pos < page_list_size && 144 while (pos < page_list_size &&
118 (policy.page_limit == kUnlimitedPages || pos < policy.page_limit) && 145 (policy.page_limit == kUnlimitedPages || pos < policy.page_limit) &&
119 !ShouldBeExpired(now, page_list.at(pos))) { 146 !ShouldBeExpired(now, page_list.at(pos))) {
120 kept_pages_size += page_list.at(pos).file_size; 147 kept_pages_size += page_list.at(pos).file_size;
121 kept_pages.push_back(page_list.at(pos)); 148 kept_pages.push_back(page_list.at(pos));
122 pos++; 149 pos++;
123 } 150 }
124 151
125 for (; pos < page_list_size; pos++) 152 for (; pos < page_list_size; pos++)
126 offline_ids.push_back(page_list.at(pos).offline_id); 153 page_ids_to_expire->push_back(page_list.at(pos).offline_id);
127 } 154 }
128 155
129 // If we're still over the clear threshold, we're going to clear remaining 156 // If we're still over the clear threshold, we're going to clear remaining
130 // pages from oldest last access time. 157 // pages from oldest last access time.
131 int64_t free_space = stats.free_disk_space; 158 int64_t free_space = stats.free_disk_space;
132 int64_t total_size = stats.total_archives_size; 159 int64_t total_size = stats.total_archives_size;
133 int64_t space_to_release = 160 int64_t space_to_release =
134 kept_pages_size - 161 kept_pages_size -
135 (total_size + free_space) * kOfflinePageStorageClearThreshold; 162 (total_size + free_space) * kOfflinePageStorageClearThreshold;
136 if (space_to_release > 0) { 163 if (space_to_release > 0) {
137 // Here we're sorting the |kept_pages| with oldest first. 164 // Here we're sorting the |kept_pages| with oldest first.
138 std::sort(kept_pages.begin(), kept_pages.end(), 165 std::sort(kept_pages.begin(), kept_pages.end(),
139 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool { 166 [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool {
140 return a.last_access_time < b.last_access_time; 167 return a.last_access_time < b.last_access_time;
141 }); 168 });
142 int kept_pages_size = static_cast<int>(kept_pages.size()); 169 size_t kept_pages_size = kept_pages.size();
143 int pos = 0; 170 size_t pos = 0;
144 while (pos < kept_pages_size && space_to_release > 0) { 171 while (pos < kept_pages_size && space_to_release > 0) {
145 space_to_release -= kept_pages.at(pos).file_size; 172 space_to_release -= kept_pages.at(pos).file_size;
146 offline_ids.push_back(kept_pages.at(pos).offline_id); 173 page_ids_to_expire->push_back(kept_pages.at(pos).offline_id);
147 pos++; 174 pos++;
148 } 175 }
149 } 176 }
150 } 177 }
151 178
152 OfflinePageStorageManager::ClearMode 179 OfflinePageStorageManager::ClearMode
153 OfflinePageStorageManager::ShouldClearPages( 180 OfflinePageStorageManager::ShouldClearPages(
154 const ArchiveManager::StorageStats& storage_stats) { 181 const ArchiveManager::StorageStats& storage_stats,
182 const base::Time& now) {
155 int64_t total_size = storage_stats.total_archives_size; 183 int64_t total_size = storage_stats.total_archives_size;
156 int64_t free_space = storage_stats.free_disk_space; 184 int64_t free_space = storage_stats.free_disk_space;
157 if (total_size == 0) 185 if (total_size == 0)
158 return ClearMode::NOT_NEEDED; 186 return ClearMode::NOT_NEEDED;
159 187
160 // If the size of all offline pages is more than limit, or it's larger than a 188 // If the size of all offline pages is more than limit, or it's larger than a
161 // specified percentage of all available storage space on the disk we'll clear 189 // specified percentage of all available storage space on the disk we'll clear
162 // all offline pages. 190 // all offline pages.
163 if (total_size >= (total_size + free_space) * kOfflinePageStorageLimit) 191 if (total_size >= (total_size + free_space) * kOfflinePageStorageLimit)
164 return ClearMode::DEFAULT; 192 return ClearMode::DEFAULT;
165 // If it's been more than the pre-defined interval since the last time we 193 // If it's been more than the pre-defined interval since the last time we
166 // clear the storage, we should clear pages. 194 // clear the storage, we should clear pages.
167 if (last_clear_time_ == base::Time() || 195 if (last_clear_time_ == base::Time() ||
168 clock_->Now() - last_clear_time_ >= kClearStorageInterval) { 196 now - last_clear_time_ >= kClearStorageInterval) {
169 return ClearMode::DEFAULT; 197 return ClearMode::DEFAULT;
170 } 198 }
171 199
172 // Otherwise there's no need to clear storage right now. 200 // Otherwise there's no need to clear storage right now.
173 return ClearMode::NOT_NEEDED; 201 return ClearMode::NOT_NEEDED;
174 } 202 }
175 203
176 bool OfflinePageStorageManager::ShouldBeExpired(const base::Time& now, 204 bool OfflinePageStorageManager::ShouldBeExpired(const base::Time& now,
177 const OfflinePageItem& page) { 205 const OfflinePageItem& page) {
178 const LifetimePolicy& policy = 206 const LifetimePolicy& policy =
179 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy; 207 policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy;
180 return now - page.last_access_time >= policy.expiration_period; 208 return now - page.last_access_time >= policy.expiration_period;
181 } 209 }
182 210
183 } // namespace offline_pages 211 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698