From aaae105785f5d2449a13ee09c2d6238b839335ce Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 7 Jan 2019 01:20:38 +0300 Subject: [PATCH] Safer ConfigShared. GitOrigin-RevId: 505e66d4341c29bbc0b8801073997fafd8ddf820 --- td/telegram/ConfigShared.cpp | 4 ++-- td/telegram/ConfigShared.h | 10 ++++++---- td/telegram/Global.cpp | 1 + td/telegram/Global.h | 1 + td/telegram/Td.cpp | 6 +++++- td/telegram/TdDb.cpp | 9 +++++++++ td/telegram/TdDb.h | 1 + 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/td/telegram/ConfigShared.cpp b/td/telegram/ConfigShared.cpp index 9430f0e2..81cdbf32 100644 --- a/td/telegram/ConfigShared.cpp +++ b/td/telegram/ConfigShared.cpp @@ -13,8 +13,8 @@ namespace td { -ConfigShared::ConfigShared(BinlogPmcPtr config_pmc, unique_ptr callback) - : config_pmc_(config_pmc), callback_(std::move(callback)) { +ConfigShared::ConfigShared(std::shared_ptr> config_pmc, unique_ptr callback) + : config_pmc_(std::move(config_pmc)), callback_(std::move(callback)) { for (auto key_value : config_pmc_->get_all()) { on_option_updated(key_value.first); } diff --git a/td/telegram/ConfigShared.h b/td/telegram/ConfigShared.h index 74ebb225..4dd42c74 100644 --- a/td/telegram/ConfigShared.h +++ b/td/telegram/ConfigShared.h @@ -6,13 +6,15 @@ // #pragma once -#include "td/db/Pmc.h" - #include "td/telegram/td_api.h" +#include "td/db/binlog/ConcurrentBinlog.h" +#include "td/db/BinlogKeyValue.h" + #include "td/utils/common.h" #include "td/utils/Slice.h" +#include #include namespace td { @@ -28,7 +30,7 @@ class ConfigShared { virtual void on_option_updated(const string &name, const string &value) const = 0; }; - ConfigShared(BinlogPmcPtr config_pmc, unique_ptr callback); + ConfigShared(std::shared_ptr> config_pmc, unique_ptr callback); void set_option_boolean(Slice name, bool value); void set_option_empty(Slice name); @@ -49,7 +51,7 @@ class ConfigShared { static tl_object_ptr get_option_value_object(Slice value); private: - BinlogPmcPtr config_pmc_; + std::shared_ptr> config_pmc_; unique_ptr callback_; bool set_option(Slice name, Slice value); diff --git a/td/telegram/Global.cpp b/td/telegram/Global.cpp index b568854b..428cc5c7 100644 --- a/td/telegram/Global.cpp +++ b/td/telegram/Global.cpp @@ -101,6 +101,7 @@ void Global::update_server_time_difference(double diff) { } DcId Global::get_webfile_dc_id() const { + CHECK(shared_config_ != nullptr); int32 dc_id = shared_config_->get_option_integer("webfile_dc_id"); if (!DcId::is_valid(dc_id)) { if (is_test_dc()) { diff --git a/td/telegram/Global.h b/td/telegram/Global.h index d5c68d61..b1f5fe54 100644 --- a/td/telegram/Global.h +++ b/td/telegram/Global.h @@ -102,6 +102,7 @@ class Global : public ActorContext { void set_shared_config(unique_ptr shared_config); ConfigShared &shared_config() { + CHECK(shared_config_ != nullptr); return *shared_config_; } diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index de4abc18..3528064f 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3723,6 +3723,7 @@ void Td::dec_actor_refcnt() { LOG(DEBUG) << "WebPagesManager was cleared " << timer; Promise<> promise = PromiseCreator::lambda([actor_id = create_reference()](Unit) mutable { actor_id.reset(); }); + G()->set_shared_config(nullptr); if (destroy_flag_) { G()->close_and_destroy_all(std::move(promise)); } else { @@ -4055,10 +4056,13 @@ Status Td::init(DbKey key) { void on_option_updated(const string &name, const string &value) const override { send_closure(G()->td(), &Td::on_config_option_updated, name); } + ~ConfigSharedCallback() override { + LOG(INFO) << "Destroy ConfigShared"; + } }; G()->set_shared_config( - make_unique(G()->td_db()->get_config_pmc(), make_unique())); + td::make_unique(G()->td_db()->get_config_pmc_shared(), make_unique())); config_manager_ = create_actor("ConfigManager", create_reference()); G()->set_config_manager(config_manager_.get()); diff --git a/td/telegram/TdDb.cpp b/td/telegram/TdDb.cpp index 77dff539..5a0aa4b0 100644 --- a/td/telegram/TdDb.cpp +++ b/td/telegram/TdDb.cpp @@ -144,6 +144,10 @@ std::shared_ptr TdDb::get_binlog_pmc_shared() { return binlog_pmc_; } +std::shared_ptr> TdDb::get_config_pmc_shared() { + return config_pmc_; +} + BinlogKeyValue *TdDb::get_binlog_pmc() { CHECK(binlog_pmc_); return binlog_pmc_.get(); @@ -185,18 +189,23 @@ CSlice TdDb::sqlite_path() const { } void TdDb::flush_all() { + LOG(INFO) << "Flush all databases"; if (messages_db_async_) { messages_db_async_->force_flush(); } binlog_->force_flush(); } + void TdDb::close_all(Promise<> on_finished) { + LOG(INFO) << "Close all databases"; do_close(std::move(on_finished), false /*destroy_flag*/); } void TdDb::close_and_destroy_all(Promise<> on_finished) { + LOG(INFO) << "Destroy all databases"; do_close(std::move(on_finished), true /*destroy_flag*/); } + void TdDb::do_close(Promise<> on_finished, bool destroy_flag) { MultiPromiseActorSafe mpas{"TdDbCloseMultiPromiseActor"}; mpas.add_promise(PromiseCreator::lambda( diff --git a/td/telegram/TdDb.h b/td/telegram/TdDb.h index 4752d753..002b35b6 100644 --- a/td/telegram/TdDb.h +++ b/td/telegram/TdDb.h @@ -69,6 +69,7 @@ class TdDb { ConcurrentBinlog *get_binlog(); std::shared_ptr get_binlog_pmc_shared(); + std::shared_ptr> get_config_pmc_shared(); BinlogKeyValue *get_binlog_pmc(); BinlogKeyValue *get_config_pmc();