8000 [BugFix] Fix the `disk_device_id` function to aquire the device_id of an non-existent entry by its ancestor entries. (backport #59919) by mergify[bot] · Pull Request #59968 · StarRocks/starrocks · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[BugFix] Fix the disk_device_id function to aquire the device_id of an non-existent entry by its ancestor entries. (backport #59919) #59968

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

Merged
merged 1 commit into from
Jun 17, 2025
Merged
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
13 changes: 12 additions & 1 deletion be/src/block_cache/datacache_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,19 @@ Status DataCacheUtils::change_disk_path(const std::string& old_disk_path, const
}

dev_t DataCacheUtils::disk_device_id(const std::string& disk_path) {
std::filesystem::path cur_path(disk_path);
cur_path = std::filesystem::absolute(cur_path);

// Traverse from the current path to the ancestor node and find the first existing path
while (!cur_path.empty()) {
if (std::filesystem::exists(cur_path) || cur_path == cur_path.root_path()) {
break;
}
cur_path = cur_path.parent_path();
}

struct stat s;
if (stat(disk_path.c_str(), &s) != 0) {
if (stat(cur_path.c_str(), &s) != 0) {
return 0;
}
return s.st_dev;
Expand Down
41 changes: 38 additions & 3 deletions be/test/block_cache/datacache_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ TEST_F(DataCacheUtilsTest, parse_cache_space_paths) {
fs::remove_all(cache_dir).ok();
}

TEST_F(DataCacheUtilsTest, get_device_id_func) {
ASSERT_GT(DataCacheUtils::disk_device_id("/"), 0);
ASSERT_GT(DataCacheUtils::disk_device_id("/not_exist1/not_exist2"), 0);
ASSERT_EQ(DataCacheUtils::disk_device_id("/"), DataCacheUtils::disk_device_id("/not_exist/not_exist2"));

// Get the device id for relative path
ASSERT_GT(DataCacheUtils::disk_device_id("not_exist"), 0);
ASSERT_GT(DataCacheUtils::disk_device_id("./not_exist"), 0);
ASSERT_GT(DataCacheUtils::disk_device_id("./not_exist1/not_exist2"), 0);
ASSERT_EQ(DataCacheUtils::disk_device_id("./not_exist"), DataCacheUtils::disk_device_id("./not_exist1/not_exist2"));
}

TEST_F(DataCacheUtilsTest, change_cache_path_suc) {
const std::string old_dir = "./old_disk_cache_path";
const std::string new_dir = "./new_disk_cache_path";
Expand All @@ -145,15 +157,38 @@ TEST_F(DataCacheUtilsTest, change_cache_path_suc) {
fs::remove_all(new_dir);
}

TEST_F(DataCacheUtilsTest, change_cache_path_fail) {
const std::string old_dir = "./old_disk_cache_path2";
const std::string new_dir = "./old_disk_cache_path2/subdir";
TEST_F(DataCacheUtilsTest, change_cache_path_to_sub_path) {
const std::string old_dir = "./old_disk_cache_path";
const std::string new_dir = "./old_disk_cache_path/subdir";
ASSERT_TRUE(fs::create_directories(old_dir).ok());
ASSERT_TRUE(fs::create_directories(new_dir).ok());

ASSERT_FALSE(DataCacheUtils::change_disk_path(old_dir, new_dir).ok());

fs::remove_all(old_dir);
fs::remove_all(new_dir);
}

TEST_F(DataCacheUtilsTest, change_cache_path_to_nonexist_dest) {
const std::string old_dir = "./old_disk_cache_path";
const std::string new_dir = "./new_disk_cache_path";
ASSERT_TRUE(fs::create_directories(old_dir).ok());

ASSERT_TRUE(DataCacheUtils::change_disk_path(old_dir, new_dir).ok());

fs::remove_all(old_dir);
fs::remove_all(new_dir);
}

TEST_F(DataCacheUtilsTest, change_cache_path_from_nonexist_src) {
const std::string old_dir = "./old_disk_cache_path";
const std::string new_dir = "./new_disk_cache_path";
ASSERT_TRUE(fs::create_directories(new_dir).ok());

ASSERT_TRUE(DataCacheUtils::change_disk_path(old_dir, new_dir).ok());

fs::remove_all(old_dir);
fs::remove_all(new_dir);
}

} // namespace starrocks
Loading
0