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();
}
void StorageManager::on_new_file(int64 size) {
if (size > 0) {
fast_stat_.cnt++;
} else {
fast_stat_.cnt--;
}
void StorageManager::on_new_file(int64 size, int32 cnt) {
fast_stat_.cnt += cnt;
fast_stat_.size += size;
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();
}
save_fast_stat();
}
void StorageManager::get_storage_stats(int32 dialog_limit, Promise<FileStats> promise) {
if (pending_storage_stats_.size() != 0) {
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() {
G()->td_db()->get_binlog_pmc()->set("fast_file_stat", log_event_store(fast_stat_).as_slice().str());
}
void StorageManager::load_fast_stat() {
auto status = log_event_parse(fast_stat_, G()->td_db()->get_binlog_pmc()->get("fast_file_stat"));
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"));
return last_gc_timestamp_;
}
void StorageManager::save_last_gc_timestamp() {
last_gc_timestamp_ = static_cast<uint32>(Clocks::system());
G()->td_db()->get_binlog_pmc()->set("files_gc_ts", to_string(last_gc_timestamp_));
}
void StorageManager::schedule_next_gc() {
if (!G()->shared_config().get_option_boolean("use_storage_optimizer") &&
!G()->parameters().enable_storage_optimizer) {

View File

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

View File

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

View File

@ -989,6 +989,7 @@ static int merge_choose_size(int64 x, int64 y) {
}
return 2;
}
static int merge_choose_expected_size(int64 x, int64 y) {
if (x == 0) {
return 1;
@ -1554,7 +1555,7 @@ void FileManager::delete_file(FileId file_id, Promise<Unit> promise, const char
clear_from_pmc(node);
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();
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,
local_prefix_size, std::move(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),
std::move(promise));
@ -2384,13 +2386,14 @@ void FileManager::on_download_ok(QueryId query_id, const FullLocalFileLocation &
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;
auto r_new_file_id = register_local(local, DialogId(), size);
if (r_new_file_id.is_error()) {
LOG(ERROR) << "Can't register local file after download: " << r_new_file_id.error();
} 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));
}
}
@ -2554,7 +2557,7 @@ void FileManager::on_generate_ok(QueryId query_id, const FullLocalFileLocation &
}
CHECK(file_node);
context_->on_new_file(FileView(file_node).size());
context_->on_new_file(FileView(file_node).size(), 1);
run_upload(file_node, {});

View File

@ -309,7 +309,7 @@ class FileManager : public FileLoadManager::Callback {
class Context {
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 ActorShared<> create_reference() = 0;
Context() = default;