diff options
| author | YDBot <[email protected]> | 2025-10-20 12:59:43 +0000 |
|---|---|---|
| committer | YDBot <[email protected]> | 2025-10-20 12:59:43 +0000 |
| commit | 7874efeb56a4beed3df6a5a5a413dc6583d23e7a (patch) | |
| tree | ddd3dc43ad00a7ea058cf35070b98e5d6475fdbb /library/cpp | |
| parent | b0dc87f6d3013af373dfd9a5c670acd18ea1dbe6 (diff) | |
| parent | 14e27f7771734f0b5d08cb303cc469bf64d6c772 (diff) | |
Merge pull request #26940 from ydb-platform/merge-rightlib-251016-0050
Diffstat (limited to 'library/cpp')
| -rw-r--r-- | library/cpp/cache/thread_safe_cache.h | 17 | ||||
| -rw-r--r-- | library/cpp/cache/ut/cache_ut.cpp | 51 | ||||
| -rw-r--r-- | library/cpp/testing/unittest/registar.h | 2 | ||||
| -rw-r--r-- | library/cpp/yt/logging/logger.h | 2 |
4 files changed, 67 insertions, 5 deletions
diff --git a/library/cpp/cache/thread_safe_cache.h b/library/cpp/cache/thread_safe_cache.h index b4bd4deedf2..045bea0c8c3 100644 --- a/library/cpp/cache/thread_safe_cache.h +++ b/library/cpp/cache/thread_safe_cache.h @@ -52,12 +52,19 @@ namespace NPrivate { const TPtr GetOrNull(TArgs... args) { Key key = Callbacks.GetKey(args...); - TReadGuard r(Mutex); - auto iter = Cache.Find(key); - if (iter == Cache.End()) { - return nullptr; + switch (GettersPromotionPolicy) { + case EGettersPromotionPolicy::Promoted: { + TWriteGuard r(Mutex); + if (auto iter = Cache.Find(key); iter != Cache.End()) + return iter.Value(); + } + case EGettersPromotionPolicy::Unpromoted: { + TReadGuard r(Mutex); + if (auto iter = Cache.Find(key); iter != Cache.End()) + return iter.Value(); + } } - return iter.Value(); + return nullptr; } const TPtr Get(TArgs... args) const { diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp index 16f29b29d1f..c4c829cdb7d 100644 --- a/library/cpp/cache/ut/cache_ut.cpp +++ b/library/cpp/cache/ut/cache_ut.cpp @@ -2,6 +2,10 @@ #include <library/cpp/cache/thread_safe_cache.h> #include <library/cpp/testing/unittest/registar.h> +#include <util/thread/pool.h> +#include <util/string/cast.h> +#include <util/random/random.h> + struct TStrokaWeighter { static size_t Weight(const TString& s) { return s.size(); @@ -536,6 +540,53 @@ Y_UNIT_TEST_SUITE(TThreadSafeCacheTest) { } } +Y_UNIT_TEST_SUITE(TThreadSafeLRUCacheMultiThreadTest) { + typedef TThreadSafeLRUCache<ui32, TString, ui32> TCache; + + class TSimpleCallbacks: public TCache::ICallbacks { + public: + TKey GetKey(ui32 i) const override { + return i; + } + TValue* CreateObject(ui32 i) const override { + Y_UNUSED(i); + return nullptr; + } + }; + + Y_UNIT_TEST(GetOrNullMultiThreadTest) { + const size_t poolSize = 8; + const size_t passCnt = 128; + const size_t tasksCnt = 128; + + TRWMutex lock; + TThreadPool pool; + TSimpleCallbacks callbacks; + TCache cache(callbacks, poolSize); + + for (size_t i = 0; i < poolSize; ++i) { + cache.Insert(i, MakeAtomicShared<TString>(ToString(i))); + } + + pool.Start(poolSize); + { + TWriteGuard wGruard(lock); + for (size_t i = 0; i < tasksCnt; ++i) { + pool.SafeAddFunc([&lock, &cache]() { + TReadGuard rGuard(lock); + for (size_t j = 0; j < passCnt; ++j) { + UNIT_ASSERT(cache.GetOrNull(RandomNumber<size_t>(poolSize)) != nullptr); + } + }); + } + } // start race + pool.Stop(); + for (size_t i = 0; i < cache.Size(); ++i) { + UNIT_ASSERT(cache.GetOrNull(i) != nullptr); + } + } +} + Y_UNIT_TEST_SUITE(TThreadSafeCacheUnsafeTest) { typedef TThreadSafeCache<ui32, TString, ui32> TCache; diff --git a/library/cpp/testing/unittest/registar.h b/library/cpp/testing/unittest/registar.h index c6c1281b4db..b3b206ec5c1 100644 --- a/library/cpp/testing/unittest/registar.h +++ b/library/cpp/testing/unittest/registar.h @@ -714,6 +714,7 @@ public: \ //values #define UNIT_ASSERT_VALUES_EQUAL_IMPL(A, B, C, EQflag, EQstr, NEQstr) \ do { \ + /* NOLINTBEGIN(bugprone-reserved-identifier, readability-identifier-naming) */ \ TString _as; \ TString _bs; \ TString _asInd; \ @@ -727,6 +728,7 @@ public: \ } \ UNIT_FAIL_IMPL("assertion failed", failMsg); \ } \ + /* NOLINTEND(bugprone-reserved-identifier, readability-identifier-naming) */ \ } while (false) #define UNIT_ASSERT_VALUES_EQUAL_C(A, B, C) \ diff --git a/library/cpp/yt/logging/logger.h b/library/cpp/yt/logging/logger.h index d46cf10d841..4f23c9e3caa 100644 --- a/library/cpp/yt/logging/logger.h +++ b/library/cpp/yt/logging/logger.h @@ -363,6 +363,7 @@ void LogStructuredEvent( #define YT_LOG_EVENT(logger, level, ...) \ do { \ + /* NOLINTBEGIN(bugprone-reserved-identifier, readability-identifier-naming) */ \ const auto& logger__ = (logger)(); \ auto level__ = (level); \ auto location__ = __LOCATION__; \ @@ -397,6 +398,7 @@ void LogStructuredEvent( location__, \ anchor__, \ std::move(message__.MessageRef)); \ + /* NOLINTEND(bugprone-reserved-identifier, readability-identifier-naming) */ \ } while (false) #define YT_LOG_EVENT_WITH_DYNAMIC_ANCHOR(logger, level, anchor, ...) \ |
