diff --git a/src/common/utils/MetaKeyUtils.cpp b/src/common/utils/MetaKeyUtils.cpp index b070cf40731..b9fffa0360e 100644 --- a/src/common/utils/MetaKeyUtils.cpp +++ b/src/common/utils/MetaKeyUtils.cpp @@ -1152,9 +1152,30 @@ GraphSpaceID MetaKeyUtils::parseLocalIdSpace(folly::StringPiece rawData) { return *reinterpret_cast(rawData.data() + offset); } -GraphSpaceID MetaKeyUtils::parseDiskPartsSpace(folly::StringPiece rawData) { +HostAddr MetaKeyUtils::parseDiskPartsHost(const folly::StringPiece& rawData) { auto offset = kDiskPartsTable.size(); - return *reinterpret_cast(rawData.data() + offset); + size_t len = *reinterpret_cast(rawData.begin() + offset); + std::string hostStr; + hostStr.reserve(sizeof(size_t) + len + sizeof(Port)); + hostStr.append(rawData.begin() + offset, sizeof(size_t) + len + sizeof(Port)); + return deserializeHostAddr(hostStr); +} + +GraphSpaceID MetaKeyUtils::parseDiskPartsSpace(const folly::StringPiece& rawData) { + auto offset = kDiskPartsTable.size(); + size_t len = *reinterpret_cast(rawData.begin() + offset); + offset += sizeof(size_t) + len + sizeof(Port); + return *reinterpret_cast(rawData.begin() + offset); +} + +std::string MetaKeyUtils::parseDiskPartsPath(const folly::StringPiece& rawData) { + auto offset = kDiskPartsTable.size(); + size_t len = *reinterpret_cast(rawData.begin() + offset); + offset += sizeof(size_t) + len + sizeof(Port) + sizeof(GraphSpaceID); + std::string path; + path.reserve(rawData.size() - offset); + path.append(rawData.begin() + offset, rawData.size() - offset); + return path; } std::string MetaKeyUtils::diskPartsPrefix() { return kDiskPartsTable; } @@ -1176,7 +1197,9 @@ std::string MetaKeyUtils::diskPartsPrefix(HostAddr addr, GraphSpaceID spaceId) { return key; } -std::string MetaKeyUtils::diskPartsKey(HostAddr addr, GraphSpaceID spaceId, std::string path) { +std::string MetaKeyUtils::diskPartsKey(HostAddr addr, + GraphSpaceID spaceId, + const std::string& path) { std::string key; std::string prefix = diskPartsPrefix(addr, spaceId); key.reserve(prefix.size() + path.size()); diff --git a/src/common/utils/MetaKeyUtils.h b/src/common/utils/MetaKeyUtils.h index 29fa715771b..ee698d3dffa 100644 --- a/src/common/utils/MetaKeyUtils.h +++ b/src/common/utils/MetaKeyUtils.h @@ -376,7 +376,11 @@ class MetaKeyUtils final { static std::unordered_map> getSystemTableMaps(); - static GraphSpaceID parseDiskPartsSpace(folly::StringPiece rawData); + static GraphSpaceID parseDiskPartsSpace(const folly::StringPiece& rawData); + + static HostAddr parseDiskPartsHost(const folly::StringPiece& rawData); + + static std::string parseDiskPartsPath(const folly::StringPiece& rawData); static std::string diskPartsPrefix(); @@ -384,7 +388,7 @@ class MetaKeyUtils final { static std::string diskPartsPrefix(HostAddr addr, GraphSpaceID spaceId); - static std::string diskPartsKey(HostAddr addr, GraphSpaceID spaceId, std::string path); + static std::string diskPartsKey(HostAddr addr, GraphSpaceID spaceId, const std::string& path); static std::string diskPartsVal(const meta::cpp2::PartitionList& partList); diff --git a/src/common/utils/test/MetaKeyUtilsTest.cpp b/src/common/utils/test/MetaKeyUtilsTest.cpp index 26945701112..a3bc910c80d 100644 --- a/src/common/utils/test/MetaKeyUtilsTest.cpp +++ b/src/common/utils/test/MetaKeyUtilsTest.cpp @@ -201,6 +201,17 @@ TEST(MetaKeyUtilsTest, ZoneTest) { ASSERT_EQ(nodes, MetaKeyUtils::parseZoneHosts(zoneValue)); } +TEST(MetaKeyUtilsTest, DiskPathsTest) { + HostAddr addr{"192.168.0.1", 1234}; + GraphSpaceID spaceId = 1; + std::string path = "/data/storage/test_part1"; + + auto diskPartsKey = MetaKeyUtils::diskPartsKey(addr, spaceId, path); + ASSERT_EQ(addr, MetaKeyUtils::parseDiskPartsHost(diskPartsKey)); + ASSERT_EQ(spaceId, MetaKeyUtils::parseDiskPartsSpace(diskPartsKey)); + ASSERT_EQ(path, MetaKeyUtils::parseDiskPartsPath(diskPartsKey)); +} + } // namespace nebula int main(int argc, char** argv) { diff --git a/src/kvstore/test/DiskManagerTest.cpp b/src/kvstore/test/DiskManagerTest.cpp index df95bb0aa0b..93435537b20 100644 --- a/src/kvstore/test/DiskManagerTest.cpp +++ b/src/kvstore/test/DiskManagerTest.cpp @@ -145,6 +145,39 @@ TEST(DiskManagerTest, WalNoSpaceTest) { } } +TEST(DiskManagerTest, GetDiskPartsTest) { + GraphSpaceID spaceId = 1; + fs::TempDir disk1("/tmp/get_disk_part_test.XXXXXX"); + auto path1 = folly::stringPrintf("%s/nebula/%d", disk1.path(), spaceId); + boost::filesystem::create_directories(path1); + fs::TempDir disk2("/tmp/get_disk_part_test.XXXXXX"); + auto path2 = folly::stringPrintf("%s/nebula/%d", disk2.path(), spaceId); + boost::filesystem::create_directories(path2); + GraphSpaceID spaceId2 = 2; + fs::TempDir disk3("/tmp/get_disk_part_test.XXXXXX"); + auto path3 = folly::stringPrintf("%s/nebula/%d", disk3.path(), spaceId2); + boost::filesystem::create_directories(path3); + + std::vector dataPaths = {disk1.path(), disk2.path(), disk3.path()}; + DiskManager diskMan(dataPaths); + for (PartitionID partId = 1; partId <= 10; partId++) { + diskMan.addPartToPath(spaceId, partId, path1); + } + for (PartitionID partId = 11; partId <= 20; partId++) { + diskMan.addPartToPath(spaceId, partId, path2); + } + for (PartitionID partId = 1; partId <= 10; partId++) { + diskMan.addPartToPath(spaceId2, partId, path3); + } + + SpaceDiskPartsMap diskParts; + diskMan.getDiskParts(diskParts); + ASSERT_EQ(2, diskParts.size()); + ASSERT_EQ(2, diskParts[spaceId].size()); + ASSERT_EQ(1, diskParts[spaceId2].size()); + ASSERT_EQ(10, diskParts[spaceId2][path3].get_part_list().size()); +} + } // namespace kvstore } // namespace nebula