OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/translate/core/browser/ranker_model_loader.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" |
| 12 #include "base/files/important_file_writer.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/profiler/scoped_tracker.h" |
| 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_util.h" |
| 19 #include "base/task_runner.h" |
| 20 #include "base/threading/platform_thread.h" |
| 21 #include "components/translate/core/browser/translate_url_fetcher.h" |
| 22 #include "components/translate/core/common/translate_switches.h" |
| 23 #include "components/variations/variations_associated_data.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 class MyScopedHistogramTimer { |
| 28 public: |
| 29 MyScopedHistogramTimer(const base::StringPiece& name) |
| 30 : name_(name.begin(), name.end()), start_(base::TimeTicks::Now()) {} |
| 31 |
| 32 ~MyScopedHistogramTimer() { |
| 33 base::TimeDelta duration = base::TimeTicks::Now() - start_; |
| 34 base::HistogramBase* counter = base::Histogram::FactoryTimeGet( |
| 35 name_, base::TimeDelta::FromMilliseconds(10), |
| 36 base::TimeDelta::FromMilliseconds(200000), 100, |
| 37 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 38 if (counter) |
| 39 counter->AddTime(duration); |
| 40 } |
| 41 |
| 42 private: |
| 43 const std::string name_; |
| 44 const base::TimeTicks start_; |
| 45 }; |
| 46 |
| 47 bool IsExpired(const chrome_intelligence::RankerModel& model) { |
| 48 DCHECK(model.has_metadata()); |
| 49 const auto& metadata = model.metadata(); |
| 50 |
| 51 // If the age of the model cannot be determined, presume it to be expired. |
| 52 if (!metadata.has_last_modified_sec()) |
| 53 return true; |
| 54 |
| 55 // If the model has no set cache duration, then it never expires. |
| 56 if (!metadata.has_cache_duration_sec()) |
| 57 return false; |
| 58 |
| 59 // Otherwise, a model is expired if its age exceeds the cache duration. |
| 60 base::Time last_modified = |
| 61 base::Time() + base::TimeDelta::FromSeconds(metadata.last_modified_sec()); |
| 62 base::TimeDelta age = base::Time::Now() - last_modified; |
| 63 return age > base::TimeDelta::FromSeconds(metadata.cache_duration_sec()); |
| 64 } |
| 65 |
| 66 } // namespace |
| 67 |
| 68 namespace translate { |
| 69 |
| 70 const int kUrlFetcherId = 2; |
| 71 const char kWriteTimerHistogram[] = ".Timer.WriteModel"; |
| 72 const char kReadTimerHistogram[] = ".Timer.ReadModel"; |
| 73 const char kDownloadTimerHistogram[] = ".Timer.DownloadModel"; |
| 74 const char kParsetimerHistogram[] = ".Timer.ParseModel"; |
| 75 const char kModelStatusHistogram[] = ".Model.Status"; |
| 76 |
| 77 RankerModelLoader::RankerModelLoader(RankerModelObserver* observer, |
| 78 const base::FilePath& model_path, |
| 79 const GURL& model_url, |
| 80 const std::string& uma_prefix) |
| 81 : thread_(uma_prefix + ".LoaderThread"), |
| 82 observer_(observer), |
| 83 model_path_(model_path), |
| 84 model_url_(model_url), |
| 85 uma_prefix_(uma_prefix) { |
| 86 DCHECK(observer_ != nullptr); |
| 87 base::Thread::Options options; |
| 88 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 89 options.priority = base::ThreadPriority::BACKGROUND; |
| 90 thread_.StartWithOptions(options); |
| 91 } |
| 92 |
| 93 RankerModelLoader::~RankerModelLoader() {} |
| 94 |
| 95 void RankerModelLoader::Start() { |
| 96 thread_.task_runner()->PostTask( |
| 97 FROM_HERE, |
| 98 base::Bind(&RankerModelLoader::LoadFromFile, base::Unretained(this))); |
| 99 } |
| 100 |
| 101 void RankerModelLoader::NotifyOfNetworkAvailability() { |
| 102 if (url_fetcher_) { |
| 103 thread_.task_runner()->PostTask( |
| 104 FROM_HERE, |
| 105 base::Bind(&RankerModelLoader::LoadFromURL, base::Unretained(this))); |
| 106 } |
| 107 } |
| 108 |
| 109 void RankerModelLoader::ReportModelStatus(RankerModelStatus model_status) { |
| 110 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( |
| 111 uma_prefix_ + kModelStatusHistogram, 1, |
| 112 static_cast<int>(RankerModelStatus::MAX), |
| 113 static_cast<int>(RankerModelStatus::MAX) + 1, |
| 114 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 115 if (histogram) |
| 116 histogram->Add(static_cast<int>(model_status)); |
| 117 } |
| 118 |
| 119 std::unique_ptr<chrome_intelligence::RankerModel> RankerModelLoader::Parse( |
| 120 const std::string& data) { |
| 121 MyScopedHistogramTimer timer(uma_prefix_ + kParsetimerHistogram); |
| 122 |
| 123 auto model = base::MakeUnique<chrome_intelligence::RankerModel>(); |
| 124 |
| 125 if (!model->ParseFromString(data)) { |
| 126 ReportModelStatus(RankerModelStatus::PARSE_FAILED); |
| 127 return nullptr; |
| 128 } |
| 129 |
| 130 RankerModelStatus model_status = observer_->Validate(*model); |
| 131 ReportModelStatus(model_status); |
| 132 if (model_status != RankerModelStatus::OK) |
| 133 return nullptr; |
| 134 |
| 135 return model; |
| 136 } |
| 137 |
| 138 void RankerModelLoader::LoadFromFile() { |
| 139 // Attempt to read the model data from the cache file. |
| 140 std::string data; |
| 141 if (!model_path_.empty()) { |
| 142 DVLOG(2) << "Attempting to load model from: " << model_path_.value(); |
| 143 MyScopedHistogramTimer timer(uma_prefix_ + kReadTimerHistogram); |
| 144 if (!base::ReadFileToString(model_path_, &data)) { |
| 145 DVLOG(2) << "Failed to read model from: " << model_path_.value(); |
| 146 data.clear(); |
| 147 } |
| 148 } |
| 149 |
| 150 // If the model was successfully was read and is compatible, then notify |
| 151 // the "owner" of this model loader of the models availability (transferring |
| 152 // ownership of the model). If the model originated at the model_url_ then |
| 153 // the model is also up to date, no need to download a new one. |
| 154 if (!data.empty()) { |
| 155 std::unique_ptr<chrome_intelligence::RankerModel> model = Parse(data); |
| 156 if (model) { |
| 157 DVLOG(2) << "Model in '" << model_path_.value() |
| 158 << "'' was originally downloaded from '" << model_url_ << "'."; |
| 159 |
| 160 std::string source = model->metadata().source(); |
| 161 if (!IsExpired(*model)) { |
| 162 TransferModelToObserver(std::move(model)); |
| 163 if (source == model_url_.spec()) |
| 164 return; |
| 165 } |
| 166 } |
| 167 } |
| 168 |
| 169 // Reaching this point means that a model download is required. If there is |
| 170 // no download URL configured, then there is nothing further to do. |
| 171 if (!model_url_.is_valid()) |
| 172 return; |
| 173 |
| 174 // Otherwise, initialize the model fetcher to be non-null and trigger an |
| 175 // initial download attempt. |
| 176 url_fetcher_ = base::MakeUnique<TranslateURLFetcher>(kUrlFetcherId); |
| 177 url_fetcher_->set_max_retry_on_5xx(kMaxRetryOn5xx); |
| 178 LoadFromURL(); |
| 179 } |
| 180 |
| 181 void RankerModelLoader::LoadFromURL() { |
| 182 // Immediately exit if there is no download pending. |
| 183 if (!url_fetcher_) |
| 184 return; |
| 185 |
| 186 // If a request is already in flight, do not issue a new one. |
| 187 if (url_fetcher_->state() == TranslateURLFetcher::REQUESTING) { |
| 188 DVLOG(2) << "RankerModelLoader: Download is in progress."; |
| 189 return; |
| 190 } |
| 191 // Do nothing if the download attempts should be throttled. |
| 192 if (base::TimeTicks::Now() < next_earliest_download_time_) { |
| 193 DVLOG(2) << "ModelLoadser: Last download attempt was too recent."; |
| 194 return; |
| 195 } |
| 196 |
| 197 DVLOG(2) << "Downloading model from: " << model_url_; |
| 198 |
| 199 // Reset the time of the next earliest allowable download attempt. |
| 200 next_earliest_download_time_ = |
| 201 base::TimeTicks::Now() + |
| 202 base::TimeDelta::FromMinutes(kDownloadRefractoryPeriodMin); |
| 203 |
| 204 // Kick off the next download attempt. |
| 205 download_start_time_ = base::TimeTicks::Now(); |
| 206 bool result = url_fetcher_->Request( |
| 207 model_url_, base::Bind(&RankerModelLoader::OnDownloadComplete, |
| 208 base::Unretained(this))); |
| 209 |
| 210 // The maximum number of download attempts has been surpassed. Don't make |
| 211 // any further attempts. |
| 212 if (!result) { |
| 213 DVLOG(2) << "Model download abandoned."; |
| 214 ReportModelStatus(RankerModelStatus::DOWNLOAD_FAILED); |
| 215 url_fetcher_.reset(); |
| 216 } |
| 217 } |
| 218 |
| 219 void RankerModelLoader::OnDownloadComplete(int /* id */, |
| 220 bool success, |
| 221 const std::string& data) { |
| 222 // Record the duration of the download. |
| 223 base::TimeDelta duration = base::TimeTicks::Now() - download_start_time_; |
| 224 base::HistogramBase* counter = base::Histogram::FactoryTimeGet( |
| 225 uma_prefix_ + kDownloadTimerHistogram, |
| 226 base::TimeDelta::FromMilliseconds(10), |
| 227 base::TimeDelta::FromMilliseconds(200000), 100, |
| 228 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 229 if (counter) |
| 230 counter->AddTime(duration); |
| 231 |
| 232 // On failure, we just abort. The TranslateRanker will retry on a subsequent |
| 233 // translation opportunity. The TranslateURLFetcher enforces a limit for |
| 234 // retried requests. |
| 235 if (!success) { |
| 236 DVLOG(2) << "Download from '" << model_url_ << "'' failed."; |
| 237 return; |
| 238 } |
| 239 |
| 240 auto model = Parse(data); |
| 241 if (!model) { |
| 242 DVLOG(2) << "Model from '" << model_url_ << "'' failed to parse."; |
| 243 return; |
| 244 } |
| 245 url_fetcher_.reset(); |
| 246 |
| 247 auto* metadata = model->mutable_metadata(); |
| 248 metadata->set_source(model_url_.spec()); |
| 249 metadata->set_last_modified_sec( |
| 250 (base::Time::Now() - base::Time()).InSeconds()); |
| 251 |
| 252 if (!model_path_.empty()) { |
| 253 DVLOG(2) << "Saving model from '" << model_url_ << "'' to '" |
| 254 << model_path_.value() << "'."; |
| 255 MyScopedHistogramTimer timer(uma_prefix_ + kWriteTimerHistogram); |
| 256 base::ImportantFileWriter::WriteFileAtomically(model_path_, |
| 257 model->SerializeAsString()); |
| 258 } |
| 259 |
| 260 // Notify the owner that a compatible model is available. |
| 261 TransferModelToObserver(std::move(model)); |
| 262 } |
| 263 |
| 264 void RankerModelLoader::TransferModelToObserver( |
| 265 std::unique_ptr<chrome_intelligence::RankerModel> model) { |
| 266 observer_->OnModelAvailable(std::move(model)); |
| 267 } |
| 268 |
| 269 } // namespace translate |
OLD | NEW |