Skip to content

Commit 54d6570

Browse files
Get cache size from leveldb for mutable cache (#1215)
Old approach with evaluating cache size is quite inefficient and causes slowdowns for the users. Instead evaluating initial cache size with a leveldb routine GetApproximateSize(). Performance improvement here is open now up to x200-x300 times faster. Broken cache size could be also checked without any memory consumption spikes. Relates-To: OLPSUP-14421 Signed-off-by: Andrey Kashcheev <[email protected]>
1 parent 94277a5 commit 54d6570

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -501,16 +501,15 @@ void DefaultCacheImpl::InitializeLru() {
501501
if (!mutable_cache_) {
502502
return;
503503
}
504+
505+
OLP_SDK_LOG_INFO_F(kLogTag, "Initializing mutable LRU cache");
506+
504507
mutable_cache_data_size_ = 0;
505-
if (mutable_cache_ && settings_.max_disk_storage != kMaxDiskSize &&
506-
settings_.eviction_policy == EvictionPolicy::kLeastRecentlyUsed) {
507-
mutable_cache_lru_ =
508-
std::make_unique<DiskLruCache>(settings_.max_disk_storage);
509-
OLP_SDK_LOG_INFO_F(kLogTag, "Initializing mutable lru cache.");
510-
}
508+
509+
mutable_cache_lru_ =
510+
std::make_unique<DiskLruCache>(settings_.max_disk_storage);
511511

512512
const auto start = std::chrono::steady_clock::now();
513-
auto count = 0u;
514513
auto it = mutable_cache_->NewIterator(leveldb::ReadOptions());
515514

516515
for (it->SeekToFirst(); it->Valid(); it->Next()) {
@@ -519,14 +518,13 @@ void DefaultCacheImpl::InitializeLru() {
519518

520519
// Here we count both expiry keys and regular keys
521520
mutable_cache_data_size_ += key.size() + value.size();
522-
if (AddKeyLru(key, value)) {
523-
++count;
524-
}
521+
522+
AddKeyLru(key, value);
525523
}
526524

527525
OLP_SDK_LOG_INFO_F(
528-
kLogTag, "Cache initialized, items=%" PRIu32 ", time=%" PRId64 " ms",
529-
count, GetElapsedTime(start));
526+
kLogTag, "LRU cache initialized, items=%zu, time=%" PRId64 " ms",
527+
mutable_cache_lru_->Size(), GetElapsedTime(start));
530528
}
531529

532530
bool DefaultCacheImpl::RemoveKeyLru(const std::string& key) {
@@ -870,7 +868,12 @@ DefaultCache::StorageOpenResult DefaultCacheImpl::SetupMutableCache() {
870868
}
871869
}
872870

873-
InitializeLru();
871+
if (settings_.max_disk_storage != kMaxDiskSize &&
872+
settings_.eviction_policy == EvictionPolicy::kLeastRecentlyUsed) {
873+
InitializeLru();
874+
} else {
875+
mutable_cache_data_size_ = mutable_cache_->Size();
876+
}
874877

875878
return DefaultCache::Success;
876879
}

olp-cpp-sdk-core/tests/cache/DefaultCacheImplTest.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,27 @@ namespace cache = olp::cache;
3434
using CacheType = cache::DefaultCache::CacheType;
3535

3636
class DefaultCacheImplTest : public ::testing::Test {
37+
public:
3738
void SetUp() override {
38-
// Restore permisions in case if cache_path_ is not writable
39+
// Restore permissions in case if cache_path_ is not writable
3940
helpers::MakeDirectoryContentReadonly(cache_path_, false);
4041
}
4142

4243
void TearDown() override { olp::utils::Dir::Remove(cache_path_); }
4344

45+
uint64_t GetCacheSizeOnDisk() {
46+
const std::string ldb_ext = ".ldb";
47+
return olp::utils::Dir::Size(cache_path_, [&](const std::string& path) {
48+
// Taking into account only ldb files.
49+
// Other files: lock, logs, manifest are quite small (~100KB on >1GB db)
50+
// but on a compacted cache they could take more than a few megabytes
51+
if (path.length() <= ldb_ext.length()) {
52+
return false;
53+
}
54+
return path.substr(path.length() - ldb_ext.length()) == ldb_ext;
55+
});
56+
}
57+
4458
protected:
4559
const std::string cache_path_ =
4660
olp::utils::Dir::TempDirectory() + "/unittest";
@@ -570,12 +584,11 @@ TEST_F(DefaultCacheImplTest, MutableCacheSize) {
570584

571585
cache.Put(key, data_string, [=]() { return data_string; },
572586
(std::numeric_limits<time_t>::max)());
573-
const auto data_size = key.size() + data_string.size();
574587
cache.Close();
575588
EXPECT_EQ(0u, cache.Size(CacheType::kMutable));
576589

577590
cache.Open();
578-
EXPECT_EQ(data_size, cache.Size(CacheType::kMutable));
591+
EXPECT_EQ(GetCacheSizeOnDisk(), cache.Size(CacheType::kMutable));
579592

580593
cache.Clear();
581594
EXPECT_EQ(0u, cache.Size(CacheType::kMutable));
@@ -1283,16 +1296,7 @@ TEST_F(DefaultCacheImplTest, ProtectedCacheSize) {
12831296
settings.disk_path_mutable = boost::none;
12841297
}
12851298

1286-
const std::string ldb_ext = ".ldb";
1287-
const uint64_t actual_size_on_disk =
1288-
olp::utils::Dir::Size(cache_path_, [&](const std::string& path) {
1289-
// Taking into account only ldb files.
1290-
// Other files: lock, logs, manifest are quite small (~100KB on >1GB db)
1291-
if (path.length() <= ldb_ext.length()) {
1292-
return false;
1293-
}
1294-
return path.substr(path.length() - ldb_ext.length()) == ldb_ext;
1295-
});
1299+
const uint64_t actual_size_on_disk = GetCacheSizeOnDisk();
12961300
ASSERT_NE(actual_size_on_disk, 0);
12971301

12981302
settings.disk_path_protected = cache_path_;

0 commit comments

Comments
 (0)