Skip to content

Commit

Permalink
modify cached_gc_safe_point to atomic to prevent more request to PD (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shuke987 authored Jun 14, 2022
1 parent 123440c commit 4b3a2ce
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions dbms/src/Storages/Transaction/PDTiKVClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace ErrorCodes
extern const int LOGICAL_ERROR;
}

Timestamp PDClientHelper::cached_gc_safe_point = 0;
std::chrono::time_point<std::chrono::system_clock> PDClientHelper::safe_point_last_update_time;
std::atomic<Timestamp> PDClientHelper::cached_gc_safe_point = 0;
std::atomic<std::chrono::time_point<std::chrono::system_clock>> PDClientHelper::safe_point_last_update_time;

} // namespace DB
8 changes: 5 additions & 3 deletions dbms/src/Storages/Transaction/PDTiKVClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <Storages/Transaction/Types.h>
#include <common/logger_useful.h>

#include <atomic>

// We define a shared ptr here, because TMTContext / SchemaSyncer / IndexReader all need to
// `share` the resource of cluster.
using KVClusterPtr = std::shared_ptr<pingcap::kv::Cluster>;
Expand All @@ -49,7 +51,7 @@ struct PDClientHelper
{
// In case we cost too much to update safe point from PD.
std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
const auto duration = std::chrono::duration_cast<std::chrono::seconds>(now - safe_point_last_update_time);
const auto duration = std::chrono::duration_cast<std::chrono::seconds>(now - safe_point_last_update_time.load());
const auto min_interval = std::max(Int64(1), safe_point_update_interval_seconds); // at least one second
if (duration.count() < min_interval)
return cached_gc_safe_point;
Expand All @@ -73,8 +75,8 @@ struct PDClientHelper
}

private:
static Timestamp cached_gc_safe_point;
static std::chrono::time_point<std::chrono::system_clock> safe_point_last_update_time;
static std::atomic<Timestamp> cached_gc_safe_point;
static std::atomic<std::chrono::time_point<std::chrono::system_clock>> safe_point_last_update_time;
};


Expand Down

0 comments on commit 4b3a2ce

Please sign in to comment.