Commit bef7a422 authored by tby's avatar tby Committed by Commit Bot

[Dolphin] Add a sequence checker

The RecurrenceRanker isn't threadsafe, so we should formalize this.
This CL adds a sequence DCHECK to all public methods (and a couple
of private callbacks, for clarity).

Bug: 921444
Change-Id: Ib52f8e33be82de147efd23e67c424083ff980184
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1729991Reviewed-by: default avatarCharles . <charleszhao@chromium.org>
Commit-Queue: Tony Yeoman <tby@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683057}
parent 4146a8bd
...@@ -166,6 +166,7 @@ RecurrenceRanker::RecurrenceRanker(const std::string& model_identifier, ...@@ -166,6 +166,7 @@ RecurrenceRanker::RecurrenceRanker(const std::string& model_identifier,
TimeDelta::FromSeconds(config.min_seconds_between_saves())), TimeDelta::FromSeconds(config.min_seconds_between_saves())),
time_of_last_save_(Time::Now()), time_of_last_save_(Time::Now()),
weak_factory_(this) { weak_factory_(this) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
task_runner_ = base::CreateSequencedTaskRunnerWithTraits( task_runner_ = base::CreateSequencedTaskRunnerWithTraits(
{base::TaskPriority::BEST_EFFORT, base::MayBlock(), {base::TaskPriority::BEST_EFFORT, base::MayBlock(),
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}); base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN});
...@@ -201,6 +202,7 @@ RecurrenceRanker::~RecurrenceRanker() = default; ...@@ -201,6 +202,7 @@ RecurrenceRanker::~RecurrenceRanker() = default;
void RecurrenceRanker::OnLoadProtoFromDiskComplete( void RecurrenceRanker::OnLoadProtoFromDiskComplete(
std::unique_ptr<RecurrenceRankerProto> proto) { std::unique_ptr<RecurrenceRankerProto> proto) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
load_from_disk_completed_ = true; load_from_disk_completed_ = true;
LogInitializationStatus(model_identifier_, LogInitializationStatus(model_identifier_,
InitializationStatus::kInitialized); InitializationStatus::kInitialized);
...@@ -242,6 +244,7 @@ void RecurrenceRanker::OnLoadProtoFromDiskComplete( ...@@ -242,6 +244,7 @@ void RecurrenceRanker::OnLoadProtoFromDiskComplete(
void RecurrenceRanker::Record(const std::string& target, void RecurrenceRanker::Record(const std::string& target,
const std::string& condition) { const std::string& condition) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!load_from_disk_completed_) if (!load_from_disk_completed_)
return; return;
LogUsage(model_identifier_, Usage::kRecord); LogUsage(model_identifier_, Usage::kRecord);
...@@ -252,6 +255,7 @@ void RecurrenceRanker::Record(const std::string& target, ...@@ -252,6 +255,7 @@ void RecurrenceRanker::Record(const std::string& target,
void RecurrenceRanker::RenameTarget(const std::string& target, void RecurrenceRanker::RenameTarget(const std::string& target,
const std::string& new_target) { const std::string& new_target) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!load_from_disk_completed_) if (!load_from_disk_completed_)
return; return;
LogUsage(model_identifier_, Usage::kRenameTarget); LogUsage(model_identifier_, Usage::kRenameTarget);
...@@ -261,6 +265,7 @@ void RecurrenceRanker::RenameTarget(const std::string& target, ...@@ -261,6 +265,7 @@ void RecurrenceRanker::RenameTarget(const std::string& target,
} }
void RecurrenceRanker::RemoveTarget(const std::string& target) { void RecurrenceRanker::RemoveTarget(const std::string& target) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// TODO(tby): Find a solution to the edge case of a removal before disk // TODO(tby): Find a solution to the edge case of a removal before disk
// loading is complete, resulting in the remove getting dropped. // loading is complete, resulting in the remove getting dropped.
if (!load_from_disk_completed_) if (!load_from_disk_completed_)
...@@ -273,6 +278,7 @@ void RecurrenceRanker::RemoveTarget(const std::string& target) { ...@@ -273,6 +278,7 @@ void RecurrenceRanker::RemoveTarget(const std::string& target) {
void RecurrenceRanker::RenameCondition(const std::string& condition, void RecurrenceRanker::RenameCondition(const std::string& condition,
const std::string& new_condition) { const std::string& new_condition) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!load_from_disk_completed_) if (!load_from_disk_completed_)
return; return;
LogUsage(model_identifier_, Usage::kRenameCondition); LogUsage(model_identifier_, Usage::kRenameCondition);
...@@ -282,6 +288,7 @@ void RecurrenceRanker::RenameCondition(const std::string& condition, ...@@ -282,6 +288,7 @@ void RecurrenceRanker::RenameCondition(const std::string& condition,
} }
void RecurrenceRanker::RemoveCondition(const std::string& condition) { void RecurrenceRanker::RemoveCondition(const std::string& condition) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!load_from_disk_completed_) if (!load_from_disk_completed_)
return; return;
LogUsage(model_identifier_, Usage::kRemoveCondition); LogUsage(model_identifier_, Usage::kRemoveCondition);
...@@ -292,6 +299,7 @@ void RecurrenceRanker::RemoveCondition(const std::string& condition) { ...@@ -292,6 +299,7 @@ void RecurrenceRanker::RemoveCondition(const std::string& condition) {
std::map<std::string, float> RecurrenceRanker::Rank( std::map<std::string, float> RecurrenceRanker::Rank(
const std::string& condition) { const std::string& condition) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!load_from_disk_completed_) if (!load_from_disk_completed_)
return {}; return {};
LogUsage(model_identifier_, Usage::kRank); LogUsage(model_identifier_, Usage::kRank);
...@@ -313,6 +321,7 @@ std::map<std::string, float> RecurrenceRanker::Rank( ...@@ -313,6 +321,7 @@ std::map<std::string, float> RecurrenceRanker::Rank(
void RecurrenceRanker::MaybeCleanup(float proportion_valid, void RecurrenceRanker::MaybeCleanup(float proportion_valid,
const FrecencyStore::ScoreTable& targets) { const FrecencyStore::ScoreTable& targets) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (proportion_valid > kMinValidTargetProportionBeforeCleanup) if (proportion_valid > kMinValidTargetProportionBeforeCleanup)
return; return;
...@@ -325,6 +334,7 @@ void RecurrenceRanker::MaybeCleanup(float proportion_valid, ...@@ -325,6 +334,7 @@ void RecurrenceRanker::MaybeCleanup(float proportion_valid,
std::vector<std::pair<std::string, float>> RecurrenceRanker::RankTopN( std::vector<std::pair<std::string, float>> RecurrenceRanker::RankTopN(
int n, int n,
const std::string& condition) { const std::string& condition) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!load_from_disk_completed_) if (!load_from_disk_completed_)
return {}; return {};
...@@ -333,15 +343,18 @@ std::vector<std::pair<std::string, float>> RecurrenceRanker::RankTopN( ...@@ -333,15 +343,18 @@ std::vector<std::pair<std::string, float>> RecurrenceRanker::RankTopN(
std::map<std::string, FrecencyStore::ValueData>* std::map<std::string, FrecencyStore::ValueData>*
RecurrenceRanker::GetTargetData() { RecurrenceRanker::GetTargetData() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return targets_->get_mutable_values(); return targets_->get_mutable_values();
} }
std::map<std::string, FrecencyStore::ValueData>* std::map<std::string, FrecencyStore::ValueData>*
RecurrenceRanker::GetConditionData() { RecurrenceRanker::GetConditionData() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return conditions_->get_mutable_values(); return conditions_->get_mutable_values();
} }
void RecurrenceRanker::SaveToDisk() { void RecurrenceRanker::SaveToDisk() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (is_ephemeral_user_) if (is_ephemeral_user_)
return; return;
...@@ -354,12 +367,14 @@ void RecurrenceRanker::SaveToDisk() { ...@@ -354,12 +367,14 @@ void RecurrenceRanker::SaveToDisk() {
} }
void RecurrenceRanker::MaybeSave() { void RecurrenceRanker::MaybeSave() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (Time::Now() - time_of_last_save_ > min_seconds_between_saves_) { if (Time::Now() - time_of_last_save_ > min_seconds_between_saves_) {
SaveToDisk(); SaveToDisk();
} }
} }
void RecurrenceRanker::ToProto(RecurrenceRankerProto* proto) { void RecurrenceRanker::ToProto(RecurrenceRankerProto* proto) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
proto->set_config_hash(config_hash_); proto->set_config_hash(config_hash_);
predictor_->ToProto(proto->mutable_predictor()); predictor_->ToProto(proto->mutable_predictor());
targets_->ToProto(proto->mutable_targets()); targets_->ToProto(proto->mutable_targets());
......
...@@ -142,6 +142,8 @@ class RecurrenceRanker { ...@@ -142,6 +142,8 @@ class RecurrenceRanker {
const base::TimeDelta min_seconds_between_saves_; const base::TimeDelta min_seconds_between_saves_;
base::Time time_of_last_save_; base::Time time_of_last_save_;
SEQUENCE_CHECKER(sequence_checker_);
scoped_refptr<base::SequencedTaskRunner> task_runner_; scoped_refptr<base::SequencedTaskRunner> task_runner_;
base::WeakPtrFactory<RecurrenceRanker> weak_factory_; base::WeakPtrFactory<RecurrenceRanker> weak_factory_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment