From 8045132040f30daa7441f037c25281d4ffd75d9b Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Tue, 24 Dec 2019 19:51:41 +0400 Subject: [PATCH] Fix system time it it goes backwards GitOrigin-RevId: 3992a79df98fb164f5a410e756aa9e87ea853c38 --- td/telegram/Global.cpp | 33 ++++++++++++++++++++++++--- td/telegram/Global.h | 2 ++ td/telegram/files/FileManager.cpp | 7 ++++++ td/telegram/net/ConnectionCreator.cpp | 2 ++ td/telegram/net/Session.cpp | 2 +- 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/td/telegram/Global.cpp b/td/telegram/Global.cpp index 2fbfa7d6..3ccb0ec2 100644 --- a/td/telegram/Global.cpp +++ b/td/telegram/Global.cpp @@ -71,14 +71,30 @@ Status Global::init(const TdParameters ¶meters, ActorId td, unique_ptrget_binlog_pmc()->get("server_time_difference"); - auto default_time_difference = Clocks::system() - Time::now(); + string save_system_time_str = td_db()->get_binlog_pmc()->get("system_time"); + auto system_time = Clocks::system(); + auto default_time_difference = system_time - Time::now(); if (save_diff_str.empty()) { server_time_difference_ = default_time_difference; server_time_difference_was_updated_ = false; } else { double save_diff; unserialize(save_diff, save_diff_str).ensure(); + + double save_system_time; + if (save_system_time_str.empty()) { + save_system_time = 0; + } else { + unserialize(save_system_time, save_system_time_str).ensure(); + } + double diff = save_diff + default_time_difference; + if (save_system_time > system_time) { + double time_backwards_fix = save_system_time - system_time; + LOG(WARNING) << "Fix system time which went backwards: " << format::as_time(time_backwards_fix) << " " + << tag("saved_system_time", save_system_time) << tag("system_time", system_time); + diff += time_backwards_fix; + } LOG(DEBUG) << "LOAD: " << tag("server_time_difference", diff); server_time_difference_ = diff; server_time_difference_was_updated_ = false; @@ -97,8 +113,19 @@ void Global::update_server_time_difference(double diff) { // diff = server_time - Time::now // save_diff = server_time - Clocks::system double save_diff = diff + Time::now() - Clocks::system(); - auto str = serialize(save_diff); - td_db()->get_binlog_pmc()->set("server_time_difference", str); + + td_db()->get_binlog_pmc()->set("server_time_difference", serialize(save_diff)); + save_system_time(); + } +} + +void Global::save_system_time() { + auto t = Time::now(); + if (system_time_saved_at_.load(std::memory_order_relaxed) + 10 < t) { + system_time_saved_at_ = t; + double save_system_time = Clocks::system(); + LOG(INFO) << "Save system time"; + td_db()->get_binlog_pmc()->set("system_time", serialize(save_system_time)); } } diff --git a/td/telegram/Global.h b/td/telegram/Global.h index a70b592c..93007f77 100644 --- a/td/telegram/Global.h +++ b/td/telegram/Global.h @@ -144,6 +144,7 @@ class Global : public ActorContext { } void update_server_time_difference(double diff); + void save_system_time(); double get_server_time_difference() const { return server_time_difference_.load(std::memory_order_relaxed); @@ -407,6 +408,7 @@ class Global : public ActorContext { std::atomic dns_time_difference_{0.0}; std::atomic dns_time_difference_was_updated_{false}; std::atomic close_flag_{false}; + std::atomic system_time_saved_at_{0.0}; std::vector> net_stats_file_callbacks_; diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index d7857247..25f5132f 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -1384,6 +1385,12 @@ Result FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy LOG(WARNING) << "File remote location was changed from " << y_node->remote_.full.value() << " to " << x_node->remote_.full.value(); } + auto count_local = [](auto &node) { + return std::accumulate(node->file_ids_.begin(), node->file_ids_.end(), 0, + [](const auto &x, const auto &y) { return x + (y.get_remote() != 0); }); + }; + if (count_local(x_node) + count_local(y_node) > 100) { + } FileNodePtr nodes[] = {x_node, y_node, x_node}; FileNodeId node_ids[] = {get_file_id_info(x_file_id)->node_id_, get_file_id_info(y_file_id)->node_id_}; diff --git a/td/telegram/net/ConnectionCreator.cpp b/td/telegram/net/ConnectionCreator.cpp index 040a663e..8ae9e583 100644 --- a/td/telegram/net/ConnectionCreator.cpp +++ b/td/telegram/net/ConnectionCreator.cpp @@ -577,6 +577,7 @@ void ConnectionCreator::on_online(bool online_flag) { } void ConnectionCreator::on_pong(size_t hash) { + G()->save_system_time(); if (active_proxy_id_ != 0) { auto now = G()->unix_time(); int32 &last_used = proxy_last_used_date_[active_proxy_id_]; @@ -1006,6 +1007,7 @@ void ConnectionCreator::client_add_connection(size_t hash, Resultsave_system_time(); client_loop(clients_[hash]); } diff --git a/td/telegram/net/Session.cpp b/td/telegram/net/Session.cpp index 8fb4a542..455a4798 100644 --- a/td/telegram/net/Session.cpp +++ b/td/telegram/net/Session.cpp @@ -119,7 +119,7 @@ Session::Session(unique_ptr callback, std::shared_ptr int32 dc_id, bool is_main, bool use_pfs, bool is_cdn, bool need_destroy, const mtproto::AuthKey &tmp_auth_key, std::vector server_salts) : raw_dc_id_(raw_dc_id), dc_id_(dc_id), is_main_(is_main), is_cdn_(is_cdn) { - VLOG(dc) << "Start connection"; + VLOG(dc) << "Start connection " << tag("need_destroy", need_destroy); need_destroy_ = need_destroy; if (need_destroy) { use_pfs = false;