2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
|
|
|
#include "td/telegram/files/FileGcWorker.h"
|
|
|
|
#include "td/telegram/files/FileStats.h"
|
2019-01-11 03:45:03 +01:00
|
|
|
#include "td/telegram/files/FileStatsWorker.h"
|
2019-04-22 02:46:51 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2019-05-01 16:15:54 +02:00
|
|
|
#include "td/utils/CancellationToken.h"
|
2019-04-22 02:46:51 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
2019-04-17 11:17:51 +02:00
|
|
|
|
|
|
|
struct DatabaseStats {
|
|
|
|
string debug;
|
|
|
|
DatabaseStats() = default;
|
|
|
|
explicit DatabaseStats(string debug) : debug(debug) {
|
|
|
|
}
|
|
|
|
tl_object_ptr<td_api::databaseStatistics> as_td_api() const;
|
|
|
|
};
|
2018-06-27 20:26:52 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class StorageManager : public Actor {
|
|
|
|
public:
|
|
|
|
StorageManager(ActorShared<> parent, int32 scheduler_id);
|
|
|
|
void get_storage_stats(int32 dialog_limit, Promise<FileStats> promise);
|
|
|
|
void get_storage_stats_fast(Promise<FileStatsFast> promise);
|
2019-04-17 11:17:51 +02:00
|
|
|
void get_database_stats(Promise<DatabaseStats> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
void run_gc(FileGcParameters parameters, Promise<FileStats> promise);
|
|
|
|
void update_use_storage_optimizer();
|
2019-01-11 03:45:03 +01:00
|
|
|
|
|
|
|
void on_new_file(int64 size, int32 cnt);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr uint32 GC_EACH = 60 * 60 * 24; // 1 day
|
|
|
|
static constexpr uint32 GC_DELAY = 60;
|
|
|
|
static constexpr uint32 GC_RAND_DELAY = 60 * 15;
|
|
|
|
|
|
|
|
ActorShared<> parent_;
|
|
|
|
|
|
|
|
int32 scheduler_id_;
|
|
|
|
|
|
|
|
// get stats
|
|
|
|
ActorOwn<FileStatsWorker> stats_worker_;
|
|
|
|
std::vector<Promise<FileStats>> pending_storage_stats_;
|
|
|
|
int32 stats_dialog_limit_ = 0;
|
|
|
|
|
|
|
|
FileTypeStat fast_stat_;
|
|
|
|
|
2019-05-01 16:15:54 +02:00
|
|
|
CancellationTokenSource cancellation_token_source_;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_file_stats(Result<FileStats> r_file_stats, bool dummy);
|
|
|
|
void create_stats_worker();
|
|
|
|
void send_stats(FileStats &&stats, int32 dialog_limit, std::vector<Promise<FileStats>> promises);
|
|
|
|
|
|
|
|
void save_fast_stat();
|
|
|
|
void load_fast_stat();
|
2019-04-21 18:22:58 +02:00
|
|
|
static int64 get_database_size();
|
2019-04-26 00:47:25 +02:00
|
|
|
static int64 get_language_pack_database_size();
|
2019-04-21 18:22:58 +02:00
|
|
|
static int64 get_log_size();
|
|
|
|
static int64 get_file_size(CSlice path);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// RefCnt
|
|
|
|
int32 ref_cnt_{1};
|
2019-05-01 16:15:54 +02:00
|
|
|
bool is_closed_{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorShared<> create_reference();
|
|
|
|
void start_up() override;
|
|
|
|
void hangup_shared() override;
|
|
|
|
void hangup() override;
|
|
|
|
|
|
|
|
// Gc
|
|
|
|
ActorOwn<FileGcWorker> gc_worker_;
|
|
|
|
std::vector<Promise<FileStats>> pending_run_gc_;
|
|
|
|
FileGcParameters gc_parameters_;
|
|
|
|
|
|
|
|
uint32 last_gc_timestamp_ = 0;
|
|
|
|
double next_gc_at_ = 0;
|
|
|
|
|
|
|
|
void on_all_files(Result<FileStats> r_file_stats, bool dummy);
|
|
|
|
void create_gc_worker();
|
|
|
|
void on_gc_finished(Result<FileStats> r_file_stats, bool dummy);
|
|
|
|
|
|
|
|
uint32 load_last_gc_timestamp();
|
|
|
|
void save_last_gc_timestamp();
|
|
|
|
void schedule_next_gc();
|
|
|
|
|
|
|
|
void timeout_expired() override;
|
|
|
|
};
|
2018-06-27 20:26:52 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|