FileGcWorker and FileStatsWorker: better cancellation

GitOrigin-RevId: d3b251f19eea923906d8bf8c9144b18e8724cedb
This commit is contained in:
Arseny Smirnov 2019-05-16 15:05:22 +03:00
parent 11b0da92fe
commit 3ab3c699b7
2 changed files with 20 additions and 2 deletions

View File

@ -128,6 +128,9 @@ void FileGcWorker::run_gc(const FileGcParameters &parameters, std::vector<FullFi
return false; return false;
}), }),
files.end()); files.end());
if (token_) {
return promise.set_error(Status::Error(500, "Request aborted"));
}
// sort by max(atime, mtime) // sort by max(atime, mtime)
std::sort(files.begin(), files.end(), [](const auto &a, const auto &b) { return a.atime_nsec < b.atime_nsec; }); std::sort(files.begin(), files.end(), [](const auto &a, const auto &b) { return a.atime_nsec < b.atime_nsec; });
@ -146,8 +149,7 @@ void FileGcWorker::run_gc(const FileGcParameters &parameters, std::vector<FullFi
size_t pos = 0; size_t pos = 0;
while (pos < files.size() && (remove_count > 0 || remove_size > 0)) { while (pos < files.size() && (remove_count > 0 || remove_size > 0)) {
if (token_) { if (token_) {
promise.set_error(Status::Error(500, "Request aborted")); return promise.set_error(Status::Error(500, "Request aborted"));
return;
} }
if (remove_count > 0) { if (remove_count > 0) {
remove_by_count_cnt++; remove_by_count_cnt++;

View File

@ -156,6 +156,9 @@ void FileStatsWorker::get_stats(bool need_all_files, bool split_by_owner_dialog_
}); });
auto passed = Time::now() - start; auto passed = Time::now() - start;
LOG_IF(INFO, passed > 0.5) << "Get file stats took: " << format::as_time(passed); LOG_IF(INFO, passed > 0.5) << "Get file stats took: " << format::as_time(passed);
if (token_) {
return promise.set_error(Status::Error(500, "Request aborted"));
}
promise.set_value(std::move(file_stats)); promise.set_value(std::move(file_stats));
} else { } else {
auto start = Time::now(); auto start = Time::now();
@ -174,11 +177,18 @@ void FileStatsWorker::get_stats(bool need_all_files, bool split_by_owner_dialog_
full_infos.push_back(std::move(info)); full_infos.push_back(std::move(info));
}); });
if (token_) {
return promise.set_error(Status::Error(500, "Request aborted"));
}
std::unordered_map<size_t, size_t> hash_to_pos; std::unordered_map<size_t, size_t> hash_to_pos;
size_t pos = 0; size_t pos = 0;
for (auto &full_info : full_infos) { for (auto &full_info : full_infos) {
hash_to_pos[std::hash<std::string>()(full_info.path)] = pos; hash_to_pos[std::hash<std::string>()(full_info.path)] = pos;
pos++; pos++;
if (token_) {
return promise.set_error(Status::Error(500, "Request aborted"));
}
} }
scan_db(token_, [&](DbFileInfo &db_info) { scan_db(token_, [&](DbFileInfo &db_info) {
auto it = hash_to_pos.find(std::hash<std::string>()(db_info.path)); auto it = hash_to_pos.find(std::hash<std::string>()(db_info.path));
@ -188,12 +198,18 @@ void FileStatsWorker::get_stats(bool need_all_files, bool split_by_owner_dialog_
// LOG(INFO) << "Match! " << db_info.path << " from " << db_info.owner_dialog_id; // LOG(INFO) << "Match! " << db_info.path << " from " << db_info.owner_dialog_id;
full_infos[it->second].owner_dialog_id = db_info.owner_dialog_id; full_infos[it->second].owner_dialog_id = db_info.owner_dialog_id;
}); });
if (token_) {
return promise.set_error(Status::Error(500, "Request aborted"));
}
FileStats file_stats; FileStats file_stats;
file_stats.need_all_files = need_all_files; file_stats.need_all_files = need_all_files;
file_stats.split_by_owner_dialog_id = split_by_owner_dialog_id; file_stats.split_by_owner_dialog_id = split_by_owner_dialog_id;
for (auto &full_info : full_infos) { for (auto &full_info : full_infos) {
file_stats.add(std::move(full_info)); file_stats.add(std::move(full_info));
if (token_) {
return promise.set_error(Status::Error(500, "Request aborted"));
}
} }
auto passed = Time::now() - start; auto passed = Time::now() - start;
LOG_IF(INFO, passed > 0.5) << "Get file stats took: " << format::as_time(passed); LOG_IF(INFO, passed > 0.5) << "Get file stats took: " << format::as_time(passed);