You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by tw...@apache.org on 2022/10/24 15:28:49 UTC

[incubator-kvrocks] branch unstable updated: Use a consistent way to get the current time (#1038)

This is an automated email from the ASF dual-hosted git repository.

twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new f5c4a0ab Use a consistent way to get the current time (#1038)
f5c4a0ab is described below

commit f5c4a0ab30decd1daa12d4eb9b3fecc74f7b59a2
Author: hulk <hu...@gmail.com>
AuthorDate: Mon Oct 24 23:28:43 2022 +0800

    Use a consistent way to get the current time (#1038)
    
    * Use a consistent way to get the current time
    
    * Update src/common/util.h
    
    Co-authored-by: Twice <tw...@apache.org>
    
    * Update src/common/util.h
    
    Co-authored-by: Twice <tw...@apache.org>
    
    * Update src/common/util.h
    
    * Update src/common/util.h
    
    * Fix compile error
    
    Co-authored-by: Twice <tw...@apache.org>
---
 src/cluster/cluster.cc            |  5 +++--
 src/commands/redis_cmd.cc         | 15 +++++++--------
 src/common/util.cc                | 13 -------------
 src/common/util.h                 |  9 +++++++--
 src/server/server.cc              | 12 ++++++------
 src/server/worker.cc              |  5 ++---
 src/storage/compaction_checker.cc |  4 ++--
 src/storage/redis_metadata.cc     | 16 +++++-----------
 src/storage/storage.cc            | 12 +++++++-----
 src/types/redis_string.cc         | 15 +++++----------
 10 files changed, 44 insertions(+), 62 deletions(-)

diff --git a/src/cluster/cluster.cc b/src/cluster/cluster.cc
index 3447e98c..36ce43f9 100644
--- a/src/cluster/cluster.cc
+++ b/src/cluster/cluster.cc
@@ -491,8 +491,9 @@ std::string Cluster::GenNodesDescription() {
     }
 
     // Ping sent, pong received, config epoch, link status
-    node_str.append(std::to_string(std::time(nullptr) * 1000 - 1) + " " + std::to_string(std::time(nullptr) * 1000) +
-                    " " + std::to_string(version_) + " " + "connected");
+    auto now = Util::GetTimeStampMS();
+    node_str.append(std::to_string(now - 1) + " " + std::to_string(now) + " " + std::to_string(version_) + " " +
+                    "connected");
 
     // Slots
     if (n->slots_info_.size() > 0) n->slots_info_.pop_back();  // Trim space
diff --git a/src/commands/redis_cmd.cc b/src/commands/redis_cmd.cc
index 233acada..37e90041 100644
--- a/src/commands/redis_cmd.cc
+++ b/src/commands/redis_cmd.cc
@@ -164,8 +164,7 @@ Status ParseTTL(const std::vector<std::string> &args, std::unordered_map<std::st
     return Status(Status::NotOK, errInvalidSyntax);
   }
   if (!ttl && expire) {
-    int64_t now;
-    rocksdb::Env::Default()->GetCurrentTime(&now);
+    int64_t now = Util::GetTimeStamp();
     *result = expire - now;
   } else {
     *result = ttl;
@@ -1178,8 +1177,7 @@ class CommandExists : public Commander {
 class CommandExpire : public Commander {
  public:
   Status Parse(const std::vector<std::string> &args) override {
-    int64_t now;
-    rocksdb::Env::Default()->GetCurrentTime(&now);
+    int64_t now = Util::GetTimeStamp();
     auto parse_result = ParseInt<int>(args[2], 10);
     if (!parse_result) {
       return Status(Status::RedisParseErr, errValueNotInteger);
@@ -1210,8 +1208,7 @@ class CommandExpire : public Commander {
 class CommandPExpire : public Commander {
  public:
   Status Parse(const std::vector<std::string> &args) override {
-    int64_t now;
-    rocksdb::Env::Default()->GetCurrentTime(&now);
+    int64_t now = Util::GetTimeStamp();
     auto ttl_ms = ParseInt<int64_t>(args[2], 10);
     if (!ttl_ms) {
       return Status(Status::RedisParseErr, errValueNotInteger);
@@ -4653,7 +4650,8 @@ class CommandFetchMeta : public Commander {
       } else {
         LOG(WARNING) << "[replication] Fail to send full data file info " << ip << ", error: " << strerror(errno);
       }
-      svr->storage_->SetCheckpointAccessTime(std::time(nullptr));
+      auto now = static_cast<time_t>(Util::GetTimeStamp());
+      svr->storage_->SetCheckpointAccessTime(now);
     });
     t.detach();
 
@@ -4714,7 +4712,8 @@ class CommandFetchFile : public Commander {
           usleep(shortest - duration);
         }
       }
-      svr->storage_->SetCheckpointAccessTime(std::time(nullptr));
+      auto now = static_cast<time_t>(Util::GetTimeStamp());
+      svr->storage_->SetCheckpointAccessTime(now);
       svr->DecrFetchFileThread();
     });
     t.detach();
diff --git a/src/common/util.cc b/src/common/util.cc
index 5c01ae14..2be0c10c 100644
--- a/src/common/util.cc
+++ b/src/common/util.cc
@@ -672,17 +672,4 @@ int aeWait(int fd, int mask, uint64_t timeout) {
     return retval;
   }
 }
-
-uint64_t GetTimeStampMS() {
-  auto tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
-  auto ts = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
-  return ts.count();
-}
-
-uint64_t GetTimeStampUS() {
-  auto tp = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
-  auto ts = std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch());
-  return ts.count();
-}
-
 }  // namespace Util
diff --git a/src/common/util.h b/src/common/util.h
index 7f789d3f..749ac0bc 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -68,7 +68,12 @@ std::vector<std::string> TokenizeRedisProtocol(const std::string &value);
 
 void ThreadSetName(const char *name);
 int aeWait(int fd, int mask, uint64_t milliseconds);
-uint64_t GetTimeStampMS();
-uint64_t GetTimeStampUS();
+
+template <typename Duration = std::chrono::seconds>
+auto GetTimeStamp() {
+  return std::chrono::duration_cast<Duration>(std::chrono::system_clock::now().time_since_epoch()).count();
+}
+inline uint64_t GetTimeStampMS() { return GetTimeStamp<std::chrono::milliseconds>(); }
+inline uint64_t GetTimeStampUS() { return GetTimeStamp<std::chrono::microseconds>(); }
 
 }  // namespace Util
diff --git a/src/server/server.cc b/src/server/server.cc
index 15799c17..29a2d028 100644
--- a/src/server/server.cc
+++ b/src/server/server.cc
@@ -167,7 +167,7 @@ Status Server::Start() {
 
       if (is_loading_ == false && ++counter % 600 == 0  // check every minute
           && config_->compaction_checker_range.Enabled()) {
-        auto now = std::time(nullptr);
+        auto now = static_cast<time_t>(Util::GetTimeStamp());
         std::tm local_time{};
         localtime_r(&now, &local_time);
         if (local_time.tm_hour >= config_->compaction_checker_range.Start &&
@@ -635,7 +635,7 @@ void Server::cron() {
     }
     // check every 20s (use 20s instead of 60s so that cron will execute in critical condition)
     if (counter != 0 && counter % 200 == 0) {
-      auto t = std::time(nullptr);
+      auto t = static_cast<time_t>(Util::GetTimeStamp());
       std::tm now{};
       localtime_r(&t, &now);
       // disable compaction cron when the compaction checker was enabled
@@ -667,8 +667,8 @@ void Server::cron() {
 
       if (storage_->ExistCheckpoint()) {
         // TODO(shooterit): support to config the alive time of checkpoint
-        if ((GetFetchFileThreadNum() == 0 && std::time(nullptr) - access_time > 30) ||
-            (std::time(nullptr) - create_time > 24 * 60 * 60)) {
+        auto now = static_cast<time_t>(Util::GetTimeStamp());
+        if ((GetFetchFileThreadNum() == 0 && now - access_time > 30) || (now - create_time > 24 * 60 * 60)) {
           auto s = rocksdb::DestroyDB(config_->checkpoint_dir, rocksdb::Options());
           if (!s.ok()) {
             LOG(WARNING) << "[server] Fail to clean checkpoint, error: " << s.ToString();
@@ -1157,9 +1157,9 @@ Status Server::AsyncBgsaveDB() {
   is_bgsave_in_progress_ = true;
 
   Task task = [this] {
-    auto start_bgsave_time = std::time(nullptr);
+    auto start_bgsave_time = static_cast<time_t>(Util::GetTimeStamp());
     Status s = storage_->CreateBackup();
-    auto stop_bgsave_time = std::time(nullptr);
+    auto stop_bgsave_time = static_cast<time_t>(Util::GetTimeStamp());
 
     std::lock_guard<std::mutex> lg(db_job_mu_);
     is_bgsave_in_progress_ = false;
diff --git a/src/server/worker.cc b/src/server/worker.cc
index 17bcd8c4..50fc1325 100644
--- a/src/server/worker.cc
+++ b/src/server/worker.cc
@@ -397,10 +397,9 @@ void Worker::BecomeMonitorConn(Redis::Connection *conn) {
 }
 
 void Worker::FeedMonitorConns(Redis::Connection *conn, const std::vector<std::string> &tokens) {
-  struct timeval tv;
-  gettimeofday(&tv, nullptr);
+  auto now = Util::GetTimeStampUS();
   std::string output;
-  output += std::to_string(tv.tv_sec) + "." + std::to_string(tv.tv_usec);
+  output += std::to_string(now / 1000000) + "." + std::to_string(now % 1000000);
   output += " [" + conn->GetNamespace() + " " + conn->GetAddr() + "]";
   for (const auto &token : tokens) {
     output += " \"" + token + "\"";
diff --git a/src/storage/compaction_checker.cc b/src/storage/compaction_checker.cc
index b22bf3e3..80af78be 100644
--- a/src/storage/compaction_checker.cc
+++ b/src/storage/compaction_checker.cc
@@ -54,8 +54,8 @@ void CompactionChecker::PickCompactionFiles(const std::string &cf_name) {
   if (props.size() / 360 > maxFilesToCompact) {
     maxFilesToCompact = props.size() / 360;
   }
-  int64_t now, forceCompactSeconds = 2 * 24 * 3600;
-  rocksdb::Env::Default()->GetCurrentTime(&now);
+  int64_t forceCompactSeconds = 2 * 24 * 3600;
+  int64_t now = Util::GetTimeStamp();
   std::string best_filename;
   double best_delete_ratio = 0;
   int64_t total_keys = 0, deleted_keys = 0;
diff --git a/src/storage/redis_metadata.cc b/src/storage/redis_metadata.cc
index 87a5f64e..ddf34069 100644
--- a/src/storage/redis_metadata.cc
+++ b/src/storage/redis_metadata.cc
@@ -29,6 +29,7 @@
 #include <vector>
 
 #include "cluster/redis_slot.h"
+#include "util.h"
 
 // 52 bit for microseconds and 11 bit for counter
 const int VersionCounterBits = 11;
@@ -194,19 +195,14 @@ void Metadata::Encode(std::string *dst) {
 }
 
 void Metadata::InitVersionCounter() {
-  struct timeval now;
-  gettimeofday(&now, nullptr);
   // use random position for initial counter to avoid conflicts,
   // when the slave was promoted as master and the system clock may backoff
-  srand(static_cast<unsigned>(now.tv_sec));
+  srand(static_cast<unsigned>(Util::GetTimeStamp()));
   version_counter_ = static_cast<uint64_t>(std::rand());
 }
 
 uint64_t Metadata::generateVersion() {
-  struct timeval now;
-  gettimeofday(&now, nullptr);
-  uint64_t version = static_cast<uint64_t>(now.tv_sec) * 1000000;
-  version += static_cast<uint64_t>(now.tv_usec);
+  uint64_t version = Util::GetTimeStampUS();
   uint64_t counter = version_counter_.fetch_add(1);
   return (version << VersionCounterBits) + (counter % (1 << VersionCounterBits));
 }
@@ -224,11 +220,10 @@ bool Metadata::operator==(const Metadata &that) const {
 RedisType Metadata::Type() const { return static_cast<RedisType>(flags & (uint8_t)0x0f); }
 
 int32_t Metadata::TTL() const {
-  int64_t now;
   if (expire <= 0) {
     return -1;
   }
-  rocksdb::Env::Default()->GetCurrentTime(&now);
+  auto now = Util::GetTimeStamp();
   if (expire < now) {
     return -2;
   }
@@ -252,8 +247,7 @@ bool Metadata::Expired() const {
     return false;
   }
 
-  int64_t now;
-  rocksdb::Env::Default()->GetCurrentTime(&now);
+  int64_t now = Util::GetTimeStamp();
   return expire < now;
 }
 
diff --git a/src/storage/storage.cc b/src/storage/storage.cc
index fa0d8f03..554af44c 100644
--- a/src/storage/storage.cc
+++ b/src/storage/storage.cc
@@ -71,7 +71,7 @@ Storage::Storage(Config *config) : env_(rocksdb::Env::Default()), config_(config
   Metadata::InitVersionCounter();
   SetCheckpointCreateTime(0);
   SetCheckpointAccessTime(0);
-  backup_creating_time_ = std::time(nullptr);
+  backup_creating_time_ = Util::GetTimeStamp();
   SetWriteOptions(config->RocksDB.write_options);
 }
 
@@ -368,7 +368,7 @@ Status Storage::CreateBackup() {
     return Status(Status::NotOK, s.ToString());
   }
   // 'backup_mu_' can guarantee 'backup_creating_time_' is thread-safe
-  backup_creating_time_ = std::time(nullptr);
+  backup_creating_time_ = static_cast<time_t>(Util::GetTimeStamp());
 
   LOG(INFO) << "[storage] Success to create new backup";
   return Status::OK();
@@ -744,8 +744,9 @@ Status Storage::ReplDataManager::GetFullReplDataInfo(Storage *storage, std::stri
 
     // Create checkpoint of rocksdb
     s = checkpoint->CreateCheckpoint(data_files_dir, storage->config_->RocksDB.write_buffer_size * MiB);
-    storage->SetCheckpointCreateTime(std::time(nullptr));
-    storage->SetCheckpointAccessTime(std::time(nullptr));
+    auto now = static_cast<time_t>(Util::GetTimeStamp());
+    storage->SetCheckpointCreateTime(now);
+    storage->SetCheckpointAccessTime(now);
     if (!s.ok()) {
       LOG(WARNING) << "[storage] Fail to create checkpoint, error:" << s.ToString();
       return Status(Status::NotOK, s.ToString());
@@ -757,7 +758,8 @@ Status Storage::ReplDataManager::GetFullReplDataInfo(Storage *storage, std::stri
     int64_t can_shared_time = storage->config_->RocksDB.WAL_ttl_seconds / 2;
     if (can_shared_time > 60 * 60) can_shared_time = 60 * 60;
     if (can_shared_time < 10 * 60) can_shared_time = 10 * 60;
-    if (std::time(nullptr) - storage->GetCheckpointCreateTime() > can_shared_time) {
+    auto now = static_cast<time_t>(Util::GetTimeStamp());
+    if (now - storage->GetCheckpointCreateTime() > can_shared_time) {
       LOG(WARNING) << "[storage] Can't use current checkpoint, waiting next checkpoint";
       return Status(Status::NotOK, "Can't use current checkpoint, waiting for next checkpoint");
     }
diff --git a/src/types/redis_string.cc b/src/types/redis_string.cc
index ae1aa4c5..82ff6cc9 100644
--- a/src/types/redis_string.cc
+++ b/src/types/redis_string.cc
@@ -153,8 +153,7 @@ rocksdb::Status String::Get(const std::string &user_key, std::string *value) {
 rocksdb::Status String::GetEx(const std::string &user_key, std::string *value, int ttl) {
   uint32_t expire = 0;
   if (ttl > 0) {
-    int64_t now;
-    rocksdb::Env::Default()->GetCurrentTime(&now);
+    int64_t now = Util::GetTimeStamp();
     expire = uint32_t(now) + ttl;
   }
   std::string ns_key;
@@ -225,8 +224,7 @@ rocksdb::Status String::SetXX(const std::string &user_key, const std::string &va
   int exists = 0;
   uint32_t expire = 0;
   if (ttl > 0) {
-    int64_t now;
-    rocksdb::Env::Default()->GetCurrentTime(&now);
+    int64_t now = Util::GetTimeStamp();
     expire = uint32_t(now) + ttl;
   }
 
@@ -365,8 +363,7 @@ rocksdb::Status String::IncrByFloat(const std::string &user_key, double incremen
 rocksdb::Status String::MSet(const std::vector<StringPair> &pairs, int ttl) {
   uint32_t expire = 0;
   if (ttl > 0) {
-    int64_t now;
-    rocksdb::Env::Default()->GetCurrentTime(&now);
+    int64_t now = Util::GetTimeStamp();
     expire = uint32_t(now) + ttl;
   }
 
@@ -396,8 +393,7 @@ rocksdb::Status String::MSetNX(const std::vector<StringPair> &pairs, int ttl, in
 
   uint32_t expire = 0;
   if (ttl > 0) {
-    int64_t now;
-    rocksdb::Env::Default()->GetCurrentTime(&now);
+    int64_t now = Util::GetTimeStamp();
     expire = uint32_t(now) + ttl;
   }
 
@@ -462,8 +458,7 @@ rocksdb::Status String::CAS(const std::string &user_key, const std::string &old_
     uint32_t expire = 0;
     Metadata metadata(kRedisString, false);
     if (ttl > 0) {
-      int64_t now;
-      rocksdb::Env::Default()->GetCurrentTime(&now);
+      int64_t now = Util::GetTimeStamp();
       expire = uint32_t(now) + ttl;
     }
     metadata.expire = expire;