Skip to content

Commit c0b369f

Browse files
author
cherepashka
committed
YT-25865: Fix for TAsyncExpiringCache: separate expiration & refresh
* Changelog entry Type: fix Component: core Separated expiration and refresh in TAsyncExpiringCache commit_hash:759a12d941f29591611b67641c1b758010c03d72
1 parent 8ae54b4 commit c0b369f

File tree

7 files changed

+193
-109
lines changed

7 files changed

+193
-109
lines changed

yt/yt/client/driver/config.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void TDriverConfig::Register(TRegistrar registrar)
7070
registrar.Preprocessor([] (TThis* config) {
7171
config->ClientCache->Capacity = 1024_KB;
7272
config->ProxyDiscoveryCache->RefreshTime = TDuration::Seconds(15);
73+
config->ProxyDiscoveryCache->ExpirationPeriod = TDuration::Seconds(15);
7374
config->ProxyDiscoveryCache->ExpireAfterSuccessfulUpdateTime = TDuration::Seconds(15);
7475
config->ProxyDiscoveryCache->ExpireAfterFailedUpdateTime = TDuration::Seconds(15);
7576
});

yt/yt/core/misc/async_expiring_cache-inl.h

Lines changed: 142 additions & 92 deletions
Large diffs are not rendered by default.

yt/yt/core/misc/async_expiring_cache.h

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
#include "public.h"
44
#include "cache_config.h"
55

6-
#include <type_traits>
76
#include <yt/yt/core/actions/future.h>
87

98
#include <yt/yt/core/logging/log.h>
109

10+
// TODO(cherepashka): remove dependency.
11+
#include <yt/yt/core/rpc/dispatcher.h>
12+
1113
#include <yt/yt/library/profiling/sensor.h>
1214

1315
#include <library/cpp/yt/threading/spin_lock.h>
@@ -20,7 +22,7 @@ namespace NYT {
2022

2123
/*!
2224
* \note
23-
* Thread affinity: delayed executor's thread
25+
* Thread affinity: user defined invoker
2426
*/
2527
template <class TKey, class TValue>
2628
class TAsyncExpiringCache
@@ -39,7 +41,9 @@ class TAsyncExpiringCache
3941
explicit TAsyncExpiringCache(
4042
TAsyncExpiringCacheConfigPtr config,
4143
NLogging::TLogger logger = {},
42-
NProfiling::TProfiler profiler = {});
44+
NProfiling::TProfiler profiler = {},
45+
// TODO(cherepashka): remove default value and move upper.
46+
const IInvokerPtr& invoker = NYT::NRpc::TDispatcher::Get()->GetHeavyInvoker());
4347

4448
TFuture<TValue> Get(const TKey& key);
4549
TExtendedGetResult GetExtended(const TKey& key);
@@ -101,6 +105,8 @@ class TAsyncExpiringCache
101105

102106
private:
103107
const NLogging::TLogger Logger_;
108+
const NConcurrency::TPeriodicExecutorPtr ExpirationExecutor_;
109+
const NConcurrency::TPeriodicExecutorPtr RefreshExecutor_;
104110

105111
struct TEntry
106112
: public TRefCounted
@@ -131,7 +137,8 @@ class TAsyncExpiringCache
131137

132138
YT_DECLARE_SPIN_LOCK(NThreading::TReaderWriterSpinLock, SpinLock_);
133139
THashMap<TKey, TEntryPtr> Map_;
134-
TAsyncExpiringCacheConfigPtr Config_;
140+
141+
TAtomicIntrusivePtr<TAsyncExpiringCacheConfig> Config_;
135142

136143
NProfiling::TCounter HitCounter_;
137144
NProfiling::TCounter MissedCounter_;
@@ -152,22 +159,29 @@ class TAsyncExpiringCache
152159
TWeakPtr<TEntry> weakEntry,
153160
const TKey& key);
154161

162+
enum EEraseReason
163+
{
164+
Refresh,
165+
Expiration,
166+
};
167+
155168
bool TryEraseExpired(
156169
const TEntryPtr& Entry,
157-
const TKey& key);
170+
const TKey& key,
171+
EEraseReason reason);
158172

159173
void Erase(THashMap<TKey, TEntryPtr>::iterator it);
160174

161-
void UpdateAll();
175+
void DeleteExpiredItems();
176+
void RefreshAllItems();
162177

163-
void ScheduleEntryRefresh(
178+
// Schedules entry expiration and refresh.
179+
void ScheduleEntryUpdate(
164180
const TEntryPtr& entry,
165181
const TKey& key,
166-
std::optional<TDuration> refreshTime);
182+
const TAsyncExpiringCacheConfigPtr& config);
167183

168184
TPromise<TValue> GetPromise(const TEntryPtr& entry) noexcept;
169-
170-
const TAsyncExpiringCacheConfigPtr& Config() const;
171185
};
172186

173187
////////////////////////////////////////////////////////////////////////////////

yt/yt/core/misc/cache_config.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ void TAsyncExpiringCacheConfig::Register(TRegistrar registrar)
7373
registrar.Parameter("refresh_time", &TThis::RefreshTime)
7474
.Alias("success_probation_time")
7575
.Default(TDuration::Seconds(10));
76+
registrar.Parameter("expiration_period", &TThis::ExpirationPeriod)
77+
.Default(TDuration::Seconds(10));
7678
registrar.Parameter("batch_update", &TThis::BatchUpdate)
7779
.Default(false);
7880

@@ -88,13 +90,24 @@ void TAsyncExpiringCacheConfig::Register(TRegistrar registrar)
8890
void TAsyncExpiringCacheConfig::ApplyDynamicInplace(
8991
const TAsyncExpiringCacheDynamicConfigPtr& dynamicConfig)
9092
{
91-
ExpireAfterAccessTime = dynamicConfig->ExpireAfterAccessTime.value_or(ExpireAfterAccessTime);
92-
ExpireAfterSuccessfulUpdateTime = dynamicConfig->ExpireAfterSuccessfulUpdateTime.value_or(ExpireAfterSuccessfulUpdateTime);
93-
ExpireAfterFailedUpdateTime = dynamicConfig->ExpireAfterFailedUpdateTime.value_or(ExpireAfterFailedUpdateTime);
94-
RefreshTime = dynamicConfig->RefreshTime.has_value()
95-
? dynamicConfig->RefreshTime
96-
: RefreshTime;
97-
BatchUpdate = dynamicConfig->BatchUpdate.value_or(BatchUpdate);
93+
if (dynamicConfig->ExpireAfterAccessTime) {
94+
ExpireAfterAccessTime = *dynamicConfig->ExpireAfterAccessTime;
95+
}
96+
if (dynamicConfig->ExpireAfterSuccessfulUpdateTime) {
97+
ExpireAfterSuccessfulUpdateTime = *dynamicConfig->ExpireAfterSuccessfulUpdateTime;
98+
}
99+
if (dynamicConfig->ExpireAfterFailedUpdateTime) {
100+
ExpireAfterFailedUpdateTime = *dynamicConfig->ExpireAfterFailedUpdateTime;
101+
}
102+
if (dynamicConfig->RefreshTime) {
103+
RefreshTime = *dynamicConfig->RefreshTime;
104+
}
105+
if (dynamicConfig->ExpirationPeriod) {
106+
ExpirationPeriod = *dynamicConfig->ExpirationPeriod;
107+
}
108+
if (dynamicConfig->BatchUpdate) {
109+
BatchUpdate = *dynamicConfig->BatchUpdate;
110+
}
98111
}
99112

100113
TAsyncExpiringCacheConfigPtr TAsyncExpiringCacheConfig::ApplyDynamic(

yt/yt/core/misc/cache_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ struct TAsyncExpiringCacheConfig
9898
TDuration ExpireAfterFailedUpdateTime;
9999

100100
//! Time before next (background) update. If nullopt - background updates are disabled.
101+
// TODO(cherepashka): rename into RefreshPeriod.
101102
std::optional<TDuration> RefreshTime;
103+
//! Time before next (background) expiration. If nullopt - background expirations are disabled.
104+
std::optional<TDuration> ExpirationPeriod;
102105

103106
//! If set to true, cache will invoke DoGetMany once instead of DoGet on every entry during an update.
104107
bool BatchUpdate;
@@ -124,6 +127,7 @@ struct TAsyncExpiringCacheDynamicConfig
124127
std::optional<TDuration> ExpireAfterSuccessfulUpdateTime;
125128
std::optional<TDuration> ExpireAfterFailedUpdateTime;
126129
std::optional<TDuration> RefreshTime;
130+
std::optional<TDuration> ExpirationPeriod;
127131

128132
std::optional<bool> BatchUpdate;
129133

yt/yt/core/net/config.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void TAddressResolverConfig::Register(TRegistrar registrar)
4343

4444
registrar.Preprocessor([] (TThis* config) {
4545
config->RefreshTime = TDuration::Seconds(60);
46+
config->ExpirationPeriod = TDuration::Seconds(60);
4647
config->ExpireAfterSuccessfulUpdateTime = TDuration::Seconds(120);
4748
config->ExpireAfterFailedUpdateTime = TDuration::Seconds(30);
4849
});

yt/yt/core/service_discovery/yp/config.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void TServiceDiscoveryConfig::Register(TRegistrar registrar)
3232
config->ExpireAfterSuccessfulUpdateTime = TDuration::Days(1);
3333
config->ExpireAfterFailedUpdateTime = TDuration::Seconds(5);
3434
config->RefreshTime = TDuration::Seconds(5);
35+
config->ExpirationPeriod = TDuration::Seconds(5);
3536
});
3637
}
3738

0 commit comments

Comments
 (0)