2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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/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
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2019-05-01 16:15:54 +02:00
|
|
|
#include "td/utils/CancellationToken.h"
|
2019-05-22 20:17:24 +02:00
|
|
|
#include "td/utils/common.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.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;
|
2021-10-19 17:11:16 +02:00
|
|
|
explicit DatabaseStats(string debug) : debug(std::move(debug)) {
|
2019-04-17 11:17:51 +02:00
|
|
|
}
|
2020-06-22 03:28:03 +02:00
|
|
|
tl_object_ptr<td_api::databaseStatistics> get_database_statistics_object() const;
|
2019-04-17 11:17:51 +02:00
|
|
|
};
|
2018-06-27 20:26:52 +02:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class StorageManager final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
StorageManager(ActorShared<> parent, int32 scheduler_id);
|
2019-08-02 15:05:01 +02:00
|
|
|
void get_storage_stats(bool need_all_files, int32 dialog_limit, Promise<FileStats> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
void get_storage_stats_fast(Promise<FileStatsFast> promise);
|
2019-04-17 11:17:51 +02:00
|
|
|
void get_database_stats(Promise<DatabaseStats> promise);
|
2020-03-02 09:59:47 +01:00
|
|
|
void run_gc(FileGcParameters parameters, bool return_deleted_file_statistics, Promise<FileStats> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
void update_use_storage_optimizer();
|
2019-01-11 03:45:03 +01:00
|
|
|
|
2020-02-19 15:57:30 +01:00
|
|
|
void on_new_file(int64 size, int64 real_size, int32 cnt);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2020-06-26 01:24:13 +02:00
|
|
|
static constexpr int GC_EACH = 60 * 60 * 24; // 1 day
|
|
|
|
static constexpr int GC_DELAY = 60;
|
|
|
|
static constexpr int GC_RAND_DELAY = 60 * 15;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
ActorShared<> parent_;
|
|
|
|
|
|
|
|
int32 scheduler_id_;
|
|
|
|
|
|
|
|
// get stats
|
|
|
|
ActorOwn<FileStatsWorker> stats_worker_;
|
|
|
|
std::vector<Promise<FileStats>> pending_storage_stats_;
|
2019-08-02 15:05:01 +02:00
|
|
|
uint32 stats_generation_{0};
|
|
|
|
int32 stats_dialog_limit_{0};
|
|
|
|
bool stats_need_all_files_{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
FileTypeStat fast_stat_;
|
|
|
|
|
2019-08-02 15:05:01 +02:00
|
|
|
CancellationTokenSource stats_cancellation_token_source_;
|
|
|
|
CancellationTokenSource gc_cancellation_token_source_;
|
2019-05-01 16:15:54 +02:00
|
|
|
|
2019-08-02 15:05:01 +02:00
|
|
|
void on_file_stats(Result<FileStats> r_file_stats, uint32 generation);
|
2018-12-31 20:04:05 +01:00
|
|
|
void create_stats_worker();
|
2020-03-02 09:59:47 +01:00
|
|
|
void update_fast_stats(const FileStats &stats);
|
2021-10-19 17:11:16 +02:00
|
|
|
static void send_stats(FileStats &&stats, int32 dialog_limit, std::vector<Promise<FileStats>> &&promises);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
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();
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
|
|
|
void hangup_shared() final;
|
|
|
|
void hangup() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// Gc
|
|
|
|
ActorOwn<FileGcWorker> gc_worker_;
|
2020-03-02 09:59:47 +01:00
|
|
|
std::vector<Promise<FileStats>> pending_run_gc_[2];
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
uint32 last_gc_timestamp_ = 0;
|
|
|
|
double next_gc_at_ = 0;
|
|
|
|
|
2020-03-02 00:15:12 +01:00
|
|
|
void on_all_files(FileGcParameters gc_parameters, Result<FileStats> r_file_stats);
|
2018-12-31 20:04:05 +01:00
|
|
|
void create_gc_worker();
|
2020-03-02 09:59:47 +01:00
|
|
|
void on_gc_finished(int32 dialog_limit, Result<FileGcResult> r_file_gc_result);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-08-02 15:05:01 +02:00
|
|
|
void close_stats_worker();
|
|
|
|
void close_gc_worker();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
uint32 load_last_gc_timestamp();
|
|
|
|
void save_last_gc_timestamp();
|
|
|
|
void schedule_next_gc();
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void timeout_expired() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-06-27 20:26:52 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|