Skip to content

GH-46217: [C++][Parquet] Update the timestamp of parquet::encryption::TwoLevelCacheWithExpiration correctly #46283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions cpp/src/parquet/encryption/two_level_cache_with_expiration.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,24 @@ class TwoLevelCacheWithExpiration {
if (external_cache_entry == cache_.end() ||
external_cache_entry->second.IsExpired()) {
cache_.insert({access_token, internal::ExpiringCacheMapEntry<V>(
std::shared_ptr<ConcurrentMap<std::string, V>>(
new ConcurrentMap<std::string, V>()),
std::make_shared<ConcurrentMap<std::string, V>>(),
cache_entry_lifetime_seconds)});
}

return cache_[access_token].cached_item();
}

void CheckCacheForExpiredTokens(double cache_cleanup_period_seconds) {
void CheckCacheForExpiredTokens(double cache_cleanup_period_seconds = 0.0) {
auto lock = mutex_.Lock();

const auto now = internal::CurrentTimePoint();
if (now > (last_cache_cleanup_timestamp_ +
std::chrono::duration<double>(cache_cleanup_period_seconds))) {
RemoveExpiredEntriesNoMutex();
last_cache_cleanup_timestamp_ =
now + std::chrono::duration<double>(cache_cleanup_period_seconds);
last_cache_cleanup_timestamp_ = now;
}
}

void RemoveExpiredEntriesFromCache() {
auto lock = mutex_.Lock();

RemoveExpiredEntriesNoMutex();
}

void Remove(const std::string& access_token) {
auto lock = mutex_.Lock();
cache_.erase(access_token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ TEST_F(TwoLevelCacheWithExpirationTest, RemoveExpiration) {
// lifetime2 will not be expired
SleepFor(0.3);
// now clear expired items from the cache
cache_.RemoveExpiredEntriesFromCache();
cache_.CheckCacheForExpiredTokens();

// lifetime1 (with 2 items) is expired and has been removed from the cache.
// Now the cache create a new object which has no item.
Expand Down Expand Up @@ -114,6 +114,14 @@ TEST_F(TwoLevelCacheWithExpirationTest, CleanupPeriodOk) {
// lifetime2 is not expired and still contains 2 items.
auto lifetime2 = cache_.GetOrCreateInternalCache("lifetime2", 3);
ASSERT_EQ(lifetime2->size(), 2);

// The further process is added to test whether the timestamp is set correctly.
// CheckCacheForExpiredTokens() should be called at least twice to verify the
// correctness.
SleepFor(0.3);
cache_.CheckCacheForExpiredTokens(0.2);
lifetime2 = cache_.GetOrCreateInternalCache("lifetime2", 0.5);
ASSERT_EQ(lifetime2->size(), 0);
}

TEST_F(TwoLevelCacheWithExpirationTest, RemoveByToken) {
Expand Down
Loading