Safer ConfigShared.

GitOrigin-RevId: 505e66d4341c29bbc0b8801073997fafd8ddf820
This commit is contained in:
levlam 2019-01-07 01:20:38 +03:00
parent 8e822fd44f
commit aaae105785
7 changed files with 25 additions and 7 deletions

View File

@ -13,8 +13,8 @@
namespace td {
ConfigShared::ConfigShared(BinlogPmcPtr config_pmc, unique_ptr<Callback> callback)
: config_pmc_(config_pmc), callback_(std::move(callback)) {
ConfigShared::ConfigShared(std::shared_ptr<BinlogKeyValue<ConcurrentBinlog>> config_pmc, unique_ptr<Callback> 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);
}

View File

@ -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 <memory>
#include <unordered_map>
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> callback);
ConfigShared(std::shared_ptr<BinlogKeyValue<ConcurrentBinlog>> config_pmc, unique_ptr<Callback> 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<td_api::OptionValue> get_option_value_object(Slice value);
private:
BinlogPmcPtr config_pmc_;
std::shared_ptr<BinlogKeyValue<ConcurrentBinlog>> config_pmc_;
unique_ptr<Callback> callback_;
bool set_option(Slice name, Slice value);

View File

@ -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()) {

View File

@ -102,6 +102,7 @@ class Global : public ActorContext {
void set_shared_config(unique_ptr<ConfigShared> shared_config);
ConfigShared &shared_config() {
CHECK(shared_config_ != nullptr);
return *shared_config_;
}

View File

@ -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<ConfigShared>(G()->td_db()->get_config_pmc(), make_unique<ConfigSharedCallback>()));
td::make_unique<ConfigShared>(G()->td_db()->get_config_pmc_shared(), make_unique<ConfigSharedCallback>()));
config_manager_ = create_actor<ConfigManager>("ConfigManager", create_reference());
G()->set_config_manager(config_manager_.get());

View File

@ -144,6 +144,10 @@ std::shared_ptr<KeyValueSyncInterface> TdDb::get_binlog_pmc_shared() {
return binlog_pmc_;
}
std::shared_ptr<BinlogKeyValue<ConcurrentBinlog>> TdDb::get_config_pmc_shared() {
return config_pmc_;
}
BinlogKeyValue<ConcurrentBinlog> *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(

View File

@ -69,6 +69,7 @@ class TdDb {
ConcurrentBinlog *get_binlog();
std::shared_ptr<KeyValueSyncInterface> get_binlog_pmc_shared();
std::shared_ptr<BinlogKeyValue<ConcurrentBinlog>> get_config_pmc_shared();
BinlogKeyValue<ConcurrentBinlog> *get_binlog_pmc();
BinlogKeyValue<ConcurrentBinlog> *get_config_pmc();