6#include <userver/cache/base_postgres_cache_fwd.hpp>
12#include <unordered_map>
14#include <fmt/format.h>
16#include <userver/cache/cache_statistics.hpp>
17#include <userver/cache/caching_component_base.hpp>
18#include <userver/components/component_config.hpp>
19#include <userver/components/component_context.hpp>
21#include <userver/storages/postgres/cluster.hpp>
22#include <userver/storages/postgres/component.hpp>
23#include <userver/storages/postgres/io/chrono.hpp>
25#include <userver/compiler/demangle.hpp>
26#include <userver/engine/sleep.hpp>
27#include <userver/logging/log.hpp>
28#include <userver/tracing/span.hpp>
29#include <userver/utils/assert.hpp>
30#include <userver/utils/cpu_relax.hpp>
31#include <userver/utils/meta.hpp>
32#include <userver/utils/void_t.hpp>
33#include <userver/yaml_config/merge_schemas.hpp>
35USERVER_NAMESPACE_BEGIN
126namespace pg_cache::detail {
129using ValueType =
typename T::ValueType;
131inline constexpr bool kHasValueType = meta::IsDetected<ValueType, T>;
134using RawValueTypeImpl =
typename T::RawValueType;
136inline constexpr bool kHasRawValueType = meta::IsDetected<RawValueTypeImpl, T>;
138using RawValueType = meta::DetectedOr<ValueType<T>, RawValueTypeImpl, T>;
140template <
typename PostgreCachePolicy>
141auto ExtractValue(RawValueType<PostgreCachePolicy>&& raw) {
142 if constexpr (kHasRawValueType<PostgreCachePolicy>) {
143 return Convert(std::move(raw),
formats::
parse::
To<ValueType<PostgreCachePolicy>>());
145 return std::move(raw);
151using HasNameImpl = std::enable_if_t<!std::string_view{T::kName}.empty()>;
153inline constexpr bool kHasName = meta::IsDetected<HasNameImpl, T>;
157using HasQueryImpl =
decltype(T::kQuery);
159inline constexpr bool kHasQuery = meta::IsDetected<HasQueryImpl, T>;
163using HasGetQueryImpl =
decltype(T::GetQuery());
165inline constexpr bool kHasGetQuery = meta::IsDetected<HasGetQueryImpl, T>;
169using HasWhere =
decltype(T::kWhere);
171inline constexpr bool kHasWhere = meta::IsDetected<HasWhere, T>;
175using HasOrderBy =
decltype(T::kOrderBy);
177inline constexpr bool kHasOrderBy = meta::IsDetected<HasOrderBy, T>;
181using HasUpdatedField =
decltype(T::kUpdatedField);
183inline constexpr bool kHasUpdatedField = meta::IsDetected<HasUpdatedField, T>;
186using WantIncrementalUpdates = std::enable_if_t<!std::string_view{T::kUpdatedField}.empty()>;
188inline constexpr bool kWantIncrementalUpdates = meta::IsDetected<WantIncrementalUpdates, T>;
192using KeyMemberTypeImpl = std::decay_t<std::invoke_result_t<
decltype(T::kKeyMember), ValueType<T>>>;
194inline constexpr bool kHasKeyMember = meta::IsDetected<KeyMemberTypeImpl, T>;
196using KeyMemberType = meta::DetectedType<KeyMemberTypeImpl, T>;
200using SizeMethodInvokeResultImpl =
decltype(std::declval<T>().size());
202inline constexpr bool kHasSizeMethod =
203 meta::IsDetected<SizeMethodInvokeResultImpl, T> &&
204 std::is_convertible_v<SizeMethodInvokeResultImpl<T>, std::size_t>;
208using InsertOrAssignMethodInvokeResultImpl =
209 decltype(std::declval<
typename T::CacheContainer>()
210 .insert_or_assign(std::declval<KeyMemberTypeImpl<T>>(), std::declval<ValueType<T>>()));
212inline constexpr bool kHasInsertOrAssignMethod = meta::IsDetected<InsertOrAssignMethodInvokeResultImpl, T>;
216using CacheInsertOrAssignFunctionInvokeResultImpl =
decltype(CacheInsertOrAssign(
217 std::declval<
typename T::CacheContainer&>(),
218 std::declval<ValueType<T>>(),
219 std::declval<KeyMemberTypeImpl<T>>()
223 kHasCacheInsertOrAssignFunction = meta::IsDetected<CacheInsertOrAssignFunctionInvokeResultImpl, T>;
226template <
typename T,
typename = USERVER_NAMESPACE::utils::void_t<>>
227struct DataCacheContainer {
229 meta::kIsStdHashable<KeyMemberType<T>>,
230 "With default CacheContainer, key type must be std::hash-able"
233 using type = std::unordered_map<KeyMemberType<T>, ValueType<T>>;
237struct DataCacheContainer<T, USERVER_NAMESPACE::utils::void_t<
typename T::CacheContainer>> {
238 static_assert(kHasSizeMethod<
typename T::CacheContainer>,
"Custom CacheContainer must provide `size` method");
240 kHasInsertOrAssignMethod<T> || kHasCacheInsertOrAssignFunction<T>,
241 "Custom CacheContainer must provide `insert_or_assign` method similar to std::unordered_map's "
242 "one or CacheInsertOrAssign function"
245 using type =
typename T::CacheContainer;
249using DataCacheContainerType =
typename DataCacheContainer<T>::type;
254inline constexpr bool kIsContainerCopiedByElement =
255 meta::kIsInstantiationOf<std::unordered_map, T> || meta::kIsInstantiationOf<std::map, T>;
258std::unique_ptr<T> CopyContainer(
260 [[maybe_unused]] std::size_t cpu_relax_iterations,
263 if constexpr (kIsContainerCopiedByElement<T>) {
264 auto copy = std::make_unique<T>();
265 if constexpr (meta::kIsReservable<T>) {
266 copy->reserve(container.size());
269 utils::
CpuRelax relax
{cpu_relax_iterations
, &scope
};
270 for (
const auto& kv : container) {
276 return std::make_unique<T>(container);
280template <
typename Container,
typename Value,
typename KeyMember,
typename... Args>
281void CacheInsertOrAssign(Container& container, Value&& value,
const KeyMember& key_member, Args&&... ) {
283 static_assert(
sizeof...(Args) == 0);
285 auto key = std::invoke(key_member, value);
286 container.insert_or_assign(std::move(key), std::forward<Value>(value));
290using HasOnWritesDoneImpl =
decltype(std::declval<T&>().OnWritesDone());
293void OnWritesDone(T& container) {
294 if constexpr (meta::IsDetected<HasOnWritesDoneImpl, T>) {
295 container.OnWritesDone();
300using HasCustomUpdatedImpl =
decltype(T::GetLastKnownUpdated(std::declval<DataCacheContainerType<T>>()));
303inline constexpr bool kHasCustomUpdated = meta::IsDetected<HasCustomUpdatedImpl, T>;
306using UpdatedFieldTypeImpl =
typename T::UpdatedFieldType;
308inline constexpr bool kHasUpdatedFieldType = meta::IsDetected<UpdatedFieldTypeImpl, T>;
310using UpdatedFieldType = meta::DetectedOr<storages::
postgres::TimePointTz, UpdatedFieldTypeImpl, T>;
313constexpr bool CheckUpdatedFieldType() {
314 if constexpr (kHasUpdatedFieldType<T>) {
315#if USERVER_POSTGRES_ENABLE_LEGACY_TIMESTAMP
317 std::is_same_v<
typename T::UpdatedFieldType, storages::postgres::TimePointTz> ||
318 std::is_same_v<
typename T::UpdatedFieldType, storages::postgres::TimePointWithoutTz> ||
319 std::is_same_v<
typename T::UpdatedFieldType, storages::postgres::TimePoint> || kHasCustomUpdated<T>,
320 "Invalid UpdatedFieldType, must be either TimePointTz or "
322 "or (legacy) system_clock::time_point"
326 std::is_same_v<
typename T::UpdatedFieldType, storages::
postgres::TimePointTz> ||
327 std::is_same_v<
typename T::UpdatedFieldType, storages::
postgres::TimePointWithoutTz> ||
328 kHasCustomUpdated<T>,
329 "Invalid UpdatedFieldType, must be either TimePointTz or "
335 !kWantIncrementalUpdates<T>,
336 "UpdatedFieldType must be explicitly specified when using "
337 "incremental updates"
345using HasClusterHostTypeImpl =
decltype(T::kClusterHostType);
348constexpr storages::
postgres::ClusterHostTypeFlags ClusterHostType() {
349 if constexpr (meta::IsDetected<HasClusterHostTypeImpl, T>) {
350 return T::kClusterHostType;
358using HasMayReturnNull =
decltype(T::kMayReturnNull);
361constexpr bool MayReturnNull() {
362 if constexpr (meta::IsDetected<HasMayReturnNull, T>) {
363 return T::kMayReturnNull;
369template <
typename PostgreCachePolicy>
370struct PolicyChecker {
372 static_assert(kHasName<PostgreCachePolicy>,
"The PosgreSQL cache policy must contain a static member `kName`");
373 static_assert(kHasValueType<PostgreCachePolicy>,
"The PosgreSQL cache policy must define a type alias `ValueType`");
375 kHasKeyMember<PostgreCachePolicy>,
376 "The PostgreSQL cache policy must contain a static member `kKeyMember` "
377 "with a pointer to a data or a function member with the object's key"
380 kHasQuery<PostgreCachePolicy> || kHasGetQuery<PostgreCachePolicy>,
381 "The PosgreSQL cache policy must contain a static data member "
382 "`kQuery` with a select statement or a static member function "
383 "`GetQuery` returning the query"
386 !(kHasQuery<PostgreCachePolicy> && kHasGetQuery<PostgreCachePolicy>),
387 "The PosgreSQL cache policy must define `kQuery` or "
388 "`GetQuery`, not both"
391 kHasUpdatedField<PostgreCachePolicy>,
392 "The PosgreSQL cache policy must contain a static member "
393 "`kUpdatedField`. If you don't want to use incremental updates, "
394 "please set its value to `nullptr`"
396 static_assert(CheckUpdatedFieldType<PostgreCachePolicy>());
399 ClusterHostType<PostgreCachePolicy>() & storages::
postgres::kClusterHostRolesMask,
400 "Cluster host role must be specified for caching component, "
401 "please be more specific"
404 static storages::
postgres::Query GetQuery() {
405 if constexpr (kHasGetQuery<PostgreCachePolicy>) {
406 return PostgreCachePolicy::GetQuery();
408 return PostgreCachePolicy::kQuery;
415inline constexpr std::chrono::minutes kDefaultFullUpdateTimeout{1};
416inline constexpr std::chrono::seconds kDefaultIncrementalUpdateTimeout{1};
417inline constexpr std::chrono::milliseconds kStatementTimeoutOff{0};
418inline constexpr std::chrono::milliseconds kCpuRelaxThreshold{10};
419inline constexpr std::chrono::milliseconds kCpuRelaxInterval{2};
421inline constexpr std::string_view kCopyStage =
"copy_data";
422inline constexpr std::string_view kFetchStage =
"fetch";
423inline constexpr std::string_view kParseStage =
"parse";
425inline constexpr std::size_t kDefaultChunkSize = 1000;
426inline constexpr std::chrono::milliseconds kDefaultSleepBetweenChunks{0};
434template <
typename PostgreCachePolicy>
435class PostgreCache
final :
public pg_cache::detail::PolicyChecker<PostgreCachePolicy>::BaseType {
438 using PolicyType = PostgreCachePolicy;
439 using ValueType = pg_cache::detail::ValueType<PolicyType>;
440 using RawValueType = pg_cache::detail::RawValueType<PolicyType>;
441 using DataType = pg_cache::detail::DataCacheContainerType<PolicyType>;
442 using PolicyCheckerType = pg_cache::detail::PolicyChecker<PostgreCachePolicy>;
443 using UpdatedFieldType = pg_cache::detail::UpdatedFieldType<PostgreCachePolicy>;
444 using BaseType =
typename PolicyCheckerType::BaseType;
447 constexpr static bool kIncrementalUpdates = pg_cache::detail::kWantIncrementalUpdates<PolicyType>;
448 constexpr static auto kClusterHostTypeFlags = pg_cache::detail::ClusterHostType<PolicyType>();
449 constexpr static auto kName = PolicyType::kName;
451 PostgreCache(
const ComponentConfig&,
const ComponentContext&);
452 ~PostgreCache()
override;
454 static yaml_config::Schema GetStaticConfigSchema();
457 using CachedData = std::unique_ptr<DataType>;
459 UpdatedFieldType GetLastUpdated(std::chrono::system_clock::time_point last_update,
const DataType& cache)
const;
463 const std::chrono::system_clock::time_point& last_update,
464 const std::chrono::system_clock::time_point& now,
465 cache::UpdateStatisticsScope& stats_scope
468 bool MayReturnNull()
const override;
473 CachedData& data_cache,
474 cache::UpdateStatisticsScope& stats_scope,
478 static storages::
postgres::Query GetAllQuery();
479 static storages::
postgres::Query GetDeltaQuery();
480 static std::string GetWhereClause();
481 static std::string GetDeltaWhereClause();
482 static std::string GetOrderByClause();
484 std::chrono::milliseconds ParseCorrection(
const ComponentConfig& config);
486 std::vector<storages::
postgres::ClusterPtr> clusters_;
488 const std::chrono::system_clock::duration correction_;
489 const std::chrono::milliseconds full_update_timeout_;
490 const std::chrono::milliseconds incremental_update_timeout_;
491 const std::size_t chunk_size_;
492 const std::chrono::milliseconds sleep_between_chunks_;
493 std::size_t cpu_relax_iterations_parse_{0};
494 std::size_t cpu_relax_iterations_copy_{0};
497template <
typename PostgreCachePolicy>
498inline constexpr bool kHasValidate<PostgreCache<PostgreCachePolicy>> =
true;
500template <
typename PostgreCachePolicy>
501PostgreCache<PostgreCachePolicy>::PostgreCache(
const ComponentConfig& config,
const ComponentContext& context)
502 : BaseType{config, context},
503 correction_{ParseCorrection(config)},
504 full_update_timeout_{
505 config
["full-update-op-timeout"].As<std
::chrono
::milliseconds
>(pg_cache::detail::kDefaultFullUpdateTimeout
)
507 incremental_update_timeout_{
508 config
["incremental-update-op-timeout"]
509 .As<std
::chrono
::milliseconds
>(pg_cache::detail::kDefaultIncrementalUpdateTimeout
)
511 chunk_size_{config
["chunk-size"].As<size_t
>(pg_cache::detail::kDefaultChunkSize
)},
512 sleep_between_chunks_{
513 config
["sleep-between-chunks"].As<std
::chrono
::milliseconds
>(pg_cache::detail::kDefaultSleepBetweenChunks
)
518 "Either set 'chunk-size' to 0, or enable PostgreSQL portals by building "
519 "the framework with CMake option USERVER_FEATURE_PATCH_LIBPQ set to ON."
523 throw std::logic_error(
524 "Incremental update support is requested in config but no update field "
525 "name is specified in traits of '" +
529 if (correction_.count() < 0) {
530 throw std::logic_error(
531 "Refusing to set forward (negative) update correction requested in "
537 const auto pg_alias = config
["pgcomponent"].As<std
::string
>("");
538 if (pg_alias.empty()) {
543 clusters_.resize(shard_count);
544 for (size_t i = 0; i < shard_count; ++i) {
552 this->StartPeriodicUpdates();
555template <
typename PostgreCachePolicy>
556PostgreCache<PostgreCachePolicy>::~PostgreCache() {
557 this->StopPeriodicUpdates();
560template <
typename PostgreCachePolicy>
561std::string PostgreCache<PostgreCachePolicy>::GetWhereClause() {
562 if constexpr (pg_cache::detail::kHasWhere<PostgreCachePolicy>) {
563 return fmt::format(FMT_COMPILE(
"where {}"), PostgreCachePolicy::kWhere);
569template <
typename PostgreCachePolicy>
570std::string PostgreCache<PostgreCachePolicy>::GetDeltaWhereClause() {
571 if constexpr (pg_cache::detail::kHasWhere<PostgreCachePolicy>) {
573 FMT_COMPILE(
"where ({}) and {} >= $1"),
574 PostgreCachePolicy::kWhere,
575 PostgreCachePolicy::kUpdatedField
578 return fmt::format(FMT_COMPILE(
"where {} >= $1"), PostgreCachePolicy::kUpdatedField);
582template <
typename PostgreCachePolicy>
583std::string PostgreCache<PostgreCachePolicy>::GetOrderByClause() {
584 if constexpr (pg_cache::detail::kHasOrderBy<PostgreCachePolicy>) {
585 return fmt::format(FMT_COMPILE(
"order by {}"), PostgreCachePolicy::kOrderBy);
591template <
typename PostgreCachePolicy>
592storages::
postgres::Query PostgreCache<PostgreCachePolicy>::GetAllQuery() {
593 const storages::
postgres::Query query = PolicyCheckerType::GetQuery();
594 return fmt::format(
"{} {} {}", query
.GetStatementView(), GetWhereClause(), GetOrderByClause());
597template <
typename PostgreCachePolicy>
598storages::
postgres::Query PostgreCache<PostgreCachePolicy>::GetDeltaQuery() {
599 if constexpr (kIncrementalUpdates) {
600 const storages::
postgres::Query query = PolicyCheckerType::GetQuery();
602 fmt::format(
"{} {} {}", query
.GetStatementView(), GetDeltaWhereClause(), GetOrderByClause()),
606 return GetAllQuery();
610template <
typename PostgreCachePolicy>
611std::chrono::milliseconds PostgreCache<PostgreCachePolicy>::ParseCorrection(
const ComponentConfig& config) {
612 static constexpr std::string_view kUpdateCorrection =
"update-correction";
613 if (pg_cache::detail::kHasCustomUpdated<PostgreCachePolicy> ||
616 return config
[kUpdateCorrection
].As<std
::chrono
::milliseconds
>(0
);
618 return config
[kUpdateCorrection
].As<std
::chrono
::milliseconds
>();
622template <
typename PostgreCachePolicy>
623typename PostgreCache<PostgreCachePolicy>::UpdatedFieldType PostgreCache<PostgreCachePolicy>::GetLastUpdated(
624 [[maybe_unused]] std::chrono::system_clock::time_point last_update,
625 const DataType& cache
627 if constexpr (pg_cache::detail::kHasCustomUpdated<PostgreCachePolicy>) {
628 return PostgreCachePolicy::GetLastKnownUpdated(cache);
630 return UpdatedFieldType{last_update - correction_};
634template <
typename PostgreCachePolicy>
635void PostgreCache<PostgreCachePolicy>::Update(
637 const std::chrono::system_clock::time_point& last_update,
638 const std::chrono::system_clock::time_point& ,
639 cache::UpdateStatisticsScope& stats_scope
642 if constexpr (!kIncrementalUpdates) {
646 const std::chrono::milliseconds
651 auto data_cache = GetDataSnapshot(type, scope);
652 [[maybe_unused]]
const auto old_size = data_cache->size();
654 scope.Reset(std::string{pg_cache::detail::kFetchStage});
658 for (
auto& cluster : clusters_) {
659 if (chunk_size_ > 0) {
660 auto trx = cluster->Begin(
661 kClusterHostTypeFlags,
663 pg::
CommandControl{timeout, pg_cache::detail::kStatementTimeoutOff}
665 auto portal = trx.MakePortal(query, GetLastUpdated(last_update, *data_cache));
667 scope.Reset(std::string{pg_cache::detail::kFetchStage});
668 auto res = portal.Fetch(chunk_size_);
671 scope.Reset(std::string{pg_cache::detail::kParseStage});
672 CacheResults(res, data_cache, stats_scope, scope);
673 changes += res.Size();
674 if (sleep_between_chunks_.count() > 0) {
684 kClusterHostTypeFlags,
685 pg::
CommandControl{timeout, pg_cache::detail::kStatementTimeoutOff},
687 GetLastUpdated(last_update, *data_cache)
690 kClusterHostTypeFlags,
691 pg::
CommandControl{timeout, pg_cache::detail::kStatementTimeoutOff},
696 scope.Reset(std::string{pg_cache::detail::kParseStage});
697 CacheResults(res, data_cache, stats_scope, scope);
698 changes += res.Size();
704 if constexpr (pg_cache::detail::kIsContainerCopiedByElement<DataType>) {
706 const auto elapsed_copy = scope.ElapsedTotal(std::string{pg_cache::detail::kCopyStage});
707 if (elapsed_copy > pg_cache::detail::kCpuRelaxThreshold) {
708 cpu_relax_iterations_copy_ =
static_cast<
709 std::size_t>(
static_cast<
double>(old_size) / (elapsed_copy / pg_cache::detail::kCpuRelaxInterval));
711 <<
"Elapsed time for copying " << kName <<
" " << elapsed_copy.count() <<
" for " << changes
712 <<
" data items is over threshold. Will relax CPU every " << cpu_relax_iterations_parse_
719 const auto elapsed_parse = scope.ElapsedTotal(std::string{pg_cache::detail::kParseStage});
720 if (elapsed_parse > pg_cache::detail::kCpuRelaxThreshold) {
721 cpu_relax_iterations_parse_ =
static_cast<
722 std::size_t>(
static_cast<
double>(changes) / (elapsed_parse / pg_cache::detail::kCpuRelaxInterval));
724 <<
"Elapsed time for parsing " << kName <<
" " << elapsed_parse.count() <<
" for " << changes
725 <<
" data items is over threshold. Will relax CPU every " << cpu_relax_iterations_parse_
731 pg_cache::detail::OnWritesDone(*data_cache);
733 this->Set(std::move(data_cache));
739template <
typename PostgreCachePolicy>
740bool PostgreCache<PostgreCachePolicy>::MayReturnNull()
const {
741 return pg_cache::detail::MayReturnNull<PolicyType>();
744template <
typename PostgreCachePolicy>
745void PostgreCache<PostgreCachePolicy>::CacheResults(
747 CachedData& data_cache,
748 cache::UpdateStatisticsScope& stats_scope,
751 auto values = res.AsSetOf<RawValueType>(storages::
postgres::kRowTag);
752 utils::
CpuRelax relax
{cpu_relax_iterations_parse_
, &scope
};
753 for (
auto p = values.begin(); p != values.end(); ++p) {
756 using pg_cache::detail::CacheInsertOrAssign;
759 pg_cache::detail::ExtractValue<PostgreCachePolicy>(*p),
760 PostgreCachePolicy::kKeyMember
762 }
catch (
const std::exception& e) {
765 <<
"Error parsing data row in cache '" << kName <<
"' to '" <<
compiler::GetTypeName<ValueType>()
766 <<
"': " << e.what();
771template <
typename PostgreCachePolicy>
772typename PostgreCache<PostgreCachePolicy>::CachedData PostgreCache<
775 auto data =
this->Get();
777 return pg_cache::detail::CopyContainer(*data, cpu_relax_iterations_copy_, scope);
780 return std::make_unique<DataType>();
785std::string GetPostgreCacheSchema();
789template <
typename PostgreCachePolicy>
790yaml_config::Schema PostgreCache<PostgreCachePolicy>::GetStaticConfigSchema() {
791 using ParentType =
typename pg_cache::detail::PolicyChecker<PostgreCachePolicy>::BaseType;
792 return yaml_config::MergeSchemas<ParentType>(impl::GetPostgreCacheSchema());
797namespace utils::
impl::projected_set {
799template <
typename Set,
typename Value,
typename KeyMember>
800void CacheInsertOrAssign(Set& set, Value&& value,
const KeyMember& ) {
801 DoInsert(set, std::forward<Value>(value));