diff --git a/internal/core/src/storage/ChunkCache.cpp b/internal/core/src/storage/ChunkCache.cpp index 11a85c974b..1e40dfe850 100644 --- a/internal/core/src/storage/ChunkCache.cpp +++ b/internal/core/src/storage/ChunkCache.cpp @@ -20,7 +20,7 @@ namespace milvus::storage { std::shared_ptr ChunkCache::Read(const std::string& filepath) { - auto path = std::filesystem::path(path_prefix_) / filepath; + auto path = CachePath(filepath); { std::shared_lock lck(mutex_); @@ -46,14 +46,14 @@ ChunkCache::Read(const std::string& filepath) { void ChunkCache::Remove(const std::string& filepath) { - auto path = std::filesystem::path(path_prefix_) / filepath; + auto path = CachePath(filepath); std::unique_lock lck(mutex_); columns_.erase(path); } void ChunkCache::Prefetch(const std::string& filepath) { - auto path = std::filesystem::path(path_prefix_) / filepath; + auto path = CachePath(filepath); std::shared_lock lck(mutex_); auto it = columns_.find(path); @@ -68,7 +68,7 @@ ChunkCache::Prefetch(const std::string& filepath) { read_ahead_policy_); AssertInfo(ok == 0, "failed to madvise to the data file {}, err: {}", - path.c_str(), + path, strerror(errno)); } @@ -115,4 +115,20 @@ ChunkCache::Mmap(const std::filesystem::path& path, return column; } +std::string +ChunkCache::CachePath(const std::string& filepath) { + auto path = std::filesystem::path(filepath); + auto prefix = std::filesystem::path(path_prefix_); + + // Cache path shall not use absolute filepath direct, it shall always under path_prefix_ + if (path.is_absolute()) { + return (prefix / + filepath.substr(path.root_directory().string().length(), + filepath.length())) + .string(); + } + + return (prefix / filepath).string(); +} + } // namespace milvus::storage diff --git a/internal/core/src/storage/ChunkCache.h b/internal/core/src/storage/ChunkCache.h index 3ec7b7a9e4..2dd24496b9 100644 --- a/internal/core/src/storage/ChunkCache.h +++ b/internal/core/src/storage/ChunkCache.h @@ -56,6 +56,9 @@ class ChunkCache { std::shared_ptr Mmap(const std::filesystem::path& path, const FieldDataPtr& field_data); + std::string + CachePath(const std::string& filepath); + private: using ColumnTable = std::unordered_map>; diff --git a/internal/core/unittest/test_storage.cpp b/internal/core/unittest/test_storage.cpp index 1b0c06fc73..9500fe478e 100644 --- a/internal/core/unittest/test_storage.cpp +++ b/internal/core/unittest/test_storage.cpp @@ -21,6 +21,9 @@ #include "storage/RemoteChunkManagerSingleton.h" #include "storage/storage_c.h" +#define private public +#include "storage/ChunkCache.h" + using namespace std; using namespace milvus; using namespace milvus::storage; @@ -150,3 +153,13 @@ TEST_F(StorageTest, GetStorageMetrics) { 0, strncmp(currentLine, familyName.c_str(), familyName.length())); } } + +TEST_F(StorageTest, CachePath) { + auto rcm = + RemoteChunkManagerSingleton::GetInstance().GetRemoteChunkManager(); + auto cc_ = ChunkCache("tmp/mmap/chunk_cache", "willneed", rcm); + auto relative_result = cc_.CachePath("abc"); + EXPECT_EQ("tmp/mmap/chunk_cache/abc", relative_result); + auto absolute_result = cc_.CachePath("/var/lib/milvus/abc"); + EXPECT_EQ("tmp/mmap/chunk_cache/var/lib/milvus/abc", absolute_result); +} \ No newline at end of file