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
|
|
|
|
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"
|
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);
|
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);
|
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_;
|
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();
|
|
|
|
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_;
|
|
|
|
|
|
|
|
uint32 last_gc_timestamp_ = 0;
|
|
|
|
double next_gc_at_ = 0;
|
|
|
|
|
2019-10-03 18:30:31 +02:00
|
|
|
void on_all_files(FileGcParameters gc_parameters, Result<FileStats> r_file_stats, bool dummy);
|
2018-12-31 20:04:05 +01:00
|
|
|
void create_gc_worker();
|
2019-10-03 18:30:31 +02:00
|
|
|
void on_gc_finished(int32 dialog_limit, Result<FileStats> r_file_stats, bool dummy);
|
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();
|
|
|
|
|
|
|
|
void timeout_expired() override;
|
|
|
|
};
|
2018-06-27 20:26:52 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|