Skip to content

Commit eda9942

Browse files
kapoisukou
andauthored
GH-46217: [C++][Parquet] Update the timestamp of parquet::encryption::TwoLevelCacheWithExpiration correctly (#46283)
### Rationale for this change Fix the bug mentioned in #46217 (comment) ### What changes are included in this PR? * Fix the logic of timestamp modification. * Extend the test in two_level_cache_with_expiration_test.cc (CleanupPeriodOk). * Replace the usage of std::make_unique's constructor with std::make_unique(). ### Are these changes tested? Yes. The original test didn't expose the problem because CheckCacheForExpiredTokens was only called once. ### Are there any user-facing changes? No. * GitHub Issue: #46217 Lead-authored-by: Eddie Chang <kalcifer7319@gmail.com> Co-authored-by: Sutou Kouhei <kou@cozmixng.org> Signed-off-by: Antoine Pitrou <pitrou@free.fr>
1 parent 58318ef commit eda9942

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

cpp/src/parquet/encryption/two_level_cache_with_expiration.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,24 @@ class TwoLevelCacheWithExpiration {
103103
if (external_cache_entry == cache_.end() ||
104104
external_cache_entry->second.IsExpired()) {
105105
cache_.insert({access_token, internal::ExpiringCacheMapEntry<V>(
106-
std::shared_ptr<ConcurrentMap<std::string, V>>(
107-
new ConcurrentMap<std::string, V>()),
106+
std::make_shared<ConcurrentMap<std::string, V>>(),
108107
cache_entry_lifetime_seconds)});
109108
}
110109

111110
return cache_[access_token].cached_item();
112111
}
113112

114-
void CheckCacheForExpiredTokens(double cache_cleanup_period_seconds) {
113+
void CheckCacheForExpiredTokens(double cache_cleanup_period_seconds = 0.0) {
115114
auto lock = mutex_.Lock();
116115

117116
const auto now = internal::CurrentTimePoint();
118117
if (now > (last_cache_cleanup_timestamp_ +
119118
std::chrono::duration<double>(cache_cleanup_period_seconds))) {
120119
RemoveExpiredEntriesNoMutex();
121-
last_cache_cleanup_timestamp_ =
122-
now + std::chrono::duration<double>(cache_cleanup_period_seconds);
120+
last_cache_cleanup_timestamp_ = now;
123121
}
124122
}
125123

126-
void RemoveExpiredEntriesFromCache() {
127-
auto lock = mutex_.Lock();
128-
129-
RemoveExpiredEntriesNoMutex();
130-
}
131-
132124
void Remove(const std::string& access_token) {
133125
auto lock = mutex_.Lock();
134126
cache_.erase(access_token);

cpp/src/parquet/encryption/two_level_cache_with_expiration_test.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ TEST_F(TwoLevelCacheWithExpirationTest, RemoveExpiration) {
7777
// lifetime2 will not be expired
7878
SleepFor(0.3);
7979
// now clear expired items from the cache
80-
cache_.RemoveExpiredEntriesFromCache();
80+
cache_.CheckCacheForExpiredTokens();
8181

8282
// lifetime1 (with 2 items) is expired and has been removed from the cache.
8383
// Now the cache create a new object which has no item.
@@ -114,6 +114,14 @@ TEST_F(TwoLevelCacheWithExpirationTest, CleanupPeriodOk) {
114114
// lifetime2 is not expired and still contains 2 items.
115115
auto lifetime2 = cache_.GetOrCreateInternalCache("lifetime2", 3);
116116
ASSERT_EQ(lifetime2->size(), 2);
117+
118+
// The further process is added to test whether the timestamp is set correctly.
119+
// CheckCacheForExpiredTokens() should be called at least twice to verify the
120+
// correctness.
121+
SleepFor(0.3);
122+
cache_.CheckCacheForExpiredTokens(0.2);
123+
lifetime2 = cache_.GetOrCreateInternalCache("lifetime2", 0.5);
124+
ASSERT_EQ(lifetime2->size(), 0);
117125
}
118126

119127
TEST_F(TwoLevelCacheWithExpirationTest, RemoveByToken) {

0 commit comments

Comments
 (0)