Better on_new_file.

GitOrigin-RevId: 0f102cc0c166bbde4b99e7c6735bd79b45f5748a
This commit is contained in:
levlam 2019-01-11 05:45:03 +03:00
parent 042c43ff46
commit 5ea6fda925
5 changed files with 21 additions and 20 deletions

View File

@ -35,20 +35,18 @@ void StorageManager::start_up() {
load_fast_stat(); load_fast_stat();
} }
void StorageManager::on_new_file(int64 size) {
if (size > 0) { void StorageManager::on_new_file(int64 size, int32 cnt) {
fast_stat_.cnt++; fast_stat_.cnt += cnt;
} else {
fast_stat_.cnt--;
}
fast_stat_.size += size; fast_stat_.size += size;
if (fast_stat_.cnt < 0 || fast_stat_.size < 0) { if (fast_stat_.cnt < 0 || fast_stat_.size < 0) {
LOG(ERROR) << "Wrong fast stat after adding size " << size; LOG(ERROR) << "Wrong fast stat after adding size " << size << " and cnt " << cnt;
fast_stat_ = FileTypeStat(); fast_stat_ = FileTypeStat();
} }
save_fast_stat(); save_fast_stat();
} }
void StorageManager::get_storage_stats(int32 dialog_limit, Promise<FileStats> promise) { void StorageManager::get_storage_stats(int32 dialog_limit, Promise<FileStats> promise) {
if (pending_storage_stats_.size() != 0) { if (pending_storage_stats_.size() != 0) {
promise.set_error(Status::Error(400, "Another storage stats is active")); promise.set_error(Status::Error(400, "Another storage stats is active"));
@ -167,6 +165,7 @@ void StorageManager::on_gc_finished(Result<FileStats> r_file_stats, bool dummy)
void StorageManager::save_fast_stat() { void StorageManager::save_fast_stat() {
G()->td_db()->get_binlog_pmc()->set("fast_file_stat", log_event_store(fast_stat_).as_slice().str()); G()->td_db()->get_binlog_pmc()->set("fast_file_stat", log_event_store(fast_stat_).as_slice().str());
} }
void StorageManager::load_fast_stat() { void StorageManager::load_fast_stat() {
auto status = log_event_parse(fast_stat_, G()->td_db()->get_binlog_pmc()->get("fast_file_stat")); auto status = log_event_parse(fast_stat_, G()->td_db()->get_binlog_pmc()->get("fast_file_stat"));
if (status.is_error()) { if (status.is_error()) {
@ -210,10 +209,12 @@ uint32 StorageManager::load_last_gc_timestamp() {
last_gc_timestamp_ = to_integer<uint32>(G()->td_db()->get_binlog_pmc()->get("files_gc_ts")); last_gc_timestamp_ = to_integer<uint32>(G()->td_db()->get_binlog_pmc()->get("files_gc_ts"));
return last_gc_timestamp_; return last_gc_timestamp_;
} }
void StorageManager::save_last_gc_timestamp() { void StorageManager::save_last_gc_timestamp() {
last_gc_timestamp_ = static_cast<uint32>(Clocks::system()); last_gc_timestamp_ = static_cast<uint32>(Clocks::system());
G()->td_db()->get_binlog_pmc()->set("files_gc_ts", to_string(last_gc_timestamp_)); G()->td_db()->get_binlog_pmc()->set("files_gc_ts", to_string(last_gc_timestamp_));
} }
void StorageManager::schedule_next_gc() { void StorageManager::schedule_next_gc() {
if (!G()->shared_config().get_option_boolean("use_storage_optimizer") && if (!G()->shared_config().get_option_boolean("use_storage_optimizer") &&
!G()->parameters().enable_storage_optimizer) { !G()->parameters().enable_storage_optimizer) {

View File

@ -11,15 +11,11 @@
#include "td/telegram/files/FileGcWorker.h" #include "td/telegram/files/FileGcWorker.h"
#include "td/telegram/files/FileStats.h" #include "td/telegram/files/FileStats.h"
#include "td/telegram/files/FileStatsWorker.h"
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/Status.h" #include "td/utils/Status.h"
namespace td {
class FileStatsWorker;
class FileGcWorker;
} // namespace td
namespace td { namespace td {
class StorageManager : public Actor { class StorageManager : public Actor {
@ -29,7 +25,8 @@ class StorageManager : public Actor {
void get_storage_stats_fast(Promise<FileStatsFast> promise); void get_storage_stats_fast(Promise<FileStatsFast> promise);
void run_gc(FileGcParameters parameters, Promise<FileStats> promise); void run_gc(FileGcParameters parameters, Promise<FileStats> promise);
void update_use_storage_optimizer(); void update_use_storage_optimizer();
void on_new_file(int64 size);
void on_new_file(int64 size, int32 cnt);
private: private:
static constexpr uint32 GC_EACH = 60 * 60 * 24; // 1 day static constexpr uint32 GC_EACH = 60 * 60 * 24; // 1 day

View File

@ -4118,8 +4118,8 @@ Status Td::init(DbKey key) {
public: public:
explicit FileManagerContext(Td *td) : td_(td) { explicit FileManagerContext(Td *td) : td_(td) {
} }
void on_new_file(int64 size) final { void on_new_file(int64 size, int32 cnt) final {
send_closure(G()->storage_manager(), &StorageManager::on_new_file, size); send_closure(G()->storage_manager(), &StorageManager::on_new_file, size, cnt);
} }
void on_file_updated(FileId file_id) final { void on_file_updated(FileId file_id) final {
send_closure(G()->td(), &Td::send_update, send_closure(G()->td(), &Td::send_update,

View File

@ -989,6 +989,7 @@ static int merge_choose_size(int64 x, int64 y) {
} }
return 2; return 2;
} }
static int merge_choose_expected_size(int64 x, int64 y) { static int merge_choose_expected_size(int64 x, int64 y) {
if (x == 0) { if (x == 0) {
return 1; return 1;
@ -1554,7 +1555,7 @@ void FileManager::delete_file(FileId file_id, Promise<Unit> promise, const char
clear_from_pmc(node); clear_from_pmc(node);
unlink(file_view.local_location().path_).ignore(); unlink(file_view.local_location().path_).ignore();
context_->on_new_file(-file_view.size()); context_->on_new_file(-file_view.size(), -1);
node->drop_local_location(); node->drop_local_location();
try_flush_node(node, "delete_file 1"); try_flush_node(node, "delete_file 1");
} }
@ -1780,6 +1781,7 @@ void FileManager::external_file_generate_progress(int64 id, int32 expected_size,
send_closure(file_generate_manager_, &FileGenerateManager::external_file_generate_progress, id, expected_size, send_closure(file_generate_manager_, &FileGenerateManager::external_file_generate_progress, id, expected_size,
local_prefix_size, std::move(promise)); local_prefix_size, std::move(promise));
} }
void FileManager::external_file_generate_finish(int64 id, Status status, Promise<> promise) { void FileManager::external_file_generate_finish(int64 id, Status status, Promise<> promise) {
send_closure(file_generate_manager_, &FileGenerateManager::external_file_generate_finish, id, std::move(status), send_closure(file_generate_manager_, &FileGenerateManager::external_file_generate_finish, id, std::move(status),
std::move(promise)); std::move(promise));
@ -2384,13 +2386,14 @@ void FileManager::on_download_ok(QueryId query_id, const FullLocalFileLocation &
return; return;
} }
auto file_id = finish_query(query_id).first.file_id_; auto query = finish_query(query_id).first;
auto file_id = query.file_id_;
LOG(INFO) << "ON DOWNLOAD OK file " << file_id << " of size " << size; LOG(INFO) << "ON DOWNLOAD OK file " << file_id << " of size " << size;
auto r_new_file_id = register_local(local, DialogId(), size); auto r_new_file_id = register_local(local, DialogId(), size);
if (r_new_file_id.is_error()) { if (r_new_file_id.is_error()) {
LOG(ERROR) << "Can't register local file after download: " << r_new_file_id.error(); LOG(ERROR) << "Can't register local file after download: " << r_new_file_id.error();
} else { } else {
context_->on_new_file(get_file_view(r_new_file_id.ok()).size()); context_->on_new_file(size, 1);
LOG_STATUS(merge(r_new_file_id.ok(), file_id)); LOG_STATUS(merge(r_new_file_id.ok(), file_id));
} }
} }
@ -2554,7 +2557,7 @@ void FileManager::on_generate_ok(QueryId query_id, const FullLocalFileLocation &
} }
CHECK(file_node); CHECK(file_node);
context_->on_new_file(FileView(file_node).size()); context_->on_new_file(FileView(file_node).size(), 1);
run_upload(file_node, {}); run_upload(file_node, {});

View File

@ -309,7 +309,7 @@ class FileManager : public FileLoadManager::Callback {
class Context { class Context {
public: public:
virtual void on_new_file(int64 size) = 0; virtual void on_new_file(int64 size, int32 cnt) = 0;
virtual void on_file_updated(FileId size) = 0; virtual void on_file_updated(FileId size) = 0;
virtual ActorShared<> create_reference() = 0; virtual ActorShared<> create_reference() = 0;
Context() = default; Context() = default;