Fix system time it it goes backwards

GitOrigin-RevId: 3992a79df98fb164f5a410e756aa9e87ea853c38
This commit is contained in:
Arseny Smirnov 2019-12-24 19:51:41 +04:00
parent a14ee3e66c
commit 8045132040
5 changed files with 42 additions and 4 deletions

View File

@ -71,14 +71,30 @@ Status Global::init(const TdParameters &parameters, ActorId<Td> td, unique_ptr<T
td_db_ = std::move(td_db_ptr); td_db_ = std::move(td_db_ptr);
string save_diff_str = td_db()->get_binlog_pmc()->get("server_time_difference"); string save_diff_str = td_db()->get_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()) { if (save_diff_str.empty()) {
server_time_difference_ = default_time_difference; server_time_difference_ = default_time_difference;
server_time_difference_was_updated_ = false; server_time_difference_was_updated_ = false;
} else { } else {
double save_diff; double save_diff;
unserialize(save_diff, save_diff_str).ensure(); 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; 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); LOG(DEBUG) << "LOAD: " << tag("server_time_difference", diff);
server_time_difference_ = diff; server_time_difference_ = diff;
server_time_difference_was_updated_ = false; server_time_difference_was_updated_ = false;
@ -97,8 +113,19 @@ void Global::update_server_time_difference(double diff) {
// diff = server_time - Time::now // diff = server_time - Time::now
// save_diff = server_time - Clocks::system // save_diff = server_time - Clocks::system
double save_diff = diff + Time::now() - 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));
} }
} }

View File

@ -144,6 +144,7 @@ class Global : public ActorContext {
} }
void update_server_time_difference(double diff); void update_server_time_difference(double diff);
void save_system_time();
double get_server_time_difference() const { double get_server_time_difference() const {
return server_time_difference_.load(std::memory_order_relaxed); return server_time_difference_.load(std::memory_order_relaxed);
@ -407,6 +408,7 @@ class Global : public ActorContext {
std::atomic<double> dns_time_difference_{0.0}; std::atomic<double> dns_time_difference_{0.0};
std::atomic<bool> dns_time_difference_was_updated_{false}; std::atomic<bool> dns_time_difference_was_updated_{false};
std::atomic<bool> close_flag_{false}; std::atomic<bool> close_flag_{false};
std::atomic<double> system_time_saved_at_{0.0};
std::vector<std::shared_ptr<NetStatsCallback>> net_stats_file_callbacks_; std::vector<std::shared_ptr<NetStatsCallback>> net_stats_file_callbacks_;

View File

@ -42,6 +42,7 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <limits> #include <limits>
#include <numeric>
#include <tuple> #include <tuple>
#include <utility> #include <utility>
@ -1384,6 +1385,12 @@ Result<FileId> 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 " LOG(WARNING) << "File remote location was changed from " << y_node->remote_.full.value() << " to "
<< x_node->remote_.full.value(); << 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}; 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_}; FileNodeId node_ids[] = {get_file_id_info(x_file_id)->node_id_, get_file_id_info(y_file_id)->node_id_};

View File

@ -577,6 +577,7 @@ void ConnectionCreator::on_online(bool online_flag) {
} }
void ConnectionCreator::on_pong(size_t hash) { void ConnectionCreator::on_pong(size_t hash) {
G()->save_system_time();
if (active_proxy_id_ != 0) { if (active_proxy_id_ != 0) {
auto now = G()->unix_time(); auto now = G()->unix_time();
int32 &last_used = proxy_last_used_date_[active_proxy_id_]; int32 &last_used = proxy_last_used_date_[active_proxy_id_];
@ -1006,6 +1007,7 @@ void ConnectionCreator::client_add_connection(size_t hash, Result<unique_ptr<mtp
void ConnectionCreator::client_wakeup(size_t hash) { void ConnectionCreator::client_wakeup(size_t hash) {
LOG(INFO) << tag("hash", format::as_hex(hash)) << " wakeup"; LOG(INFO) << tag("hash", format::as_hex(hash)) << " wakeup";
G()->save_system_time();
client_loop(clients_[hash]); client_loop(clients_[hash]);
} }

View File

@ -119,7 +119,7 @@ Session::Session(unique_ptr<Callback> callback, std::shared_ptr<AuthDataShared>
int32 dc_id, bool is_main, bool use_pfs, bool is_cdn, bool need_destroy, int32 dc_id, bool is_main, bool use_pfs, bool is_cdn, bool need_destroy,
const mtproto::AuthKey &tmp_auth_key, std::vector<mtproto::ServerSalt> server_salts) const mtproto::AuthKey &tmp_auth_key, std::vector<mtproto::ServerSalt> server_salts)
: raw_dc_id_(raw_dc_id), dc_id_(dc_id), is_main_(is_main), is_cdn_(is_cdn) { : 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; need_destroy_ = need_destroy;
if (need_destroy) { if (need_destroy) {
use_pfs = false; use_pfs = false;