From 4286794f557c65b8ad5462929b9428a499eced56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B3=8A=E7=85=A6?= Date: Tue, 17 Jun 2025 19:38:40 +0800 Subject: [PATCH 1/2] Python store API supports batch operation 1. expose batch operation via python api 2. add BatchIsExist support --- mooncake-integration/store/store_py.cpp | 248 ++++++++++++++++-- mooncake-integration/store/store_py.h | 26 ++ mooncake-store/include/client.h | 8 + mooncake-store/include/master_client.h | 8 + mooncake-store/include/master_service.h | 7 + mooncake-store/include/rpc_service.h | 23 +- mooncake-store/proto/master.proto | 19 ++ mooncake-store/src/client.cpp | 8 + mooncake-store/src/master_client.cpp | 25 ++ mooncake-store/src/master_service.cpp | 9 + .../tests/client_integration_test.cpp | 20 ++ 11 files changed, 377 insertions(+), 24 deletions(-) diff --git a/mooncake-integration/store/store_py.cpp b/mooncake-integration/store/store_py.cpp index f6339068c78..8c946239b4a 100644 --- a/mooncake-integration/store/store_py.cpp +++ b/mooncake-integration/store/store_py.cpp @@ -315,6 +315,64 @@ int DistributedObjectStore::allocateSlices( return 0; } +int DistributedObjectStore::allocateBatchedSlices( + std::vector &keys, + const std::unordered_map> &batches, + std::unordered_map> + &batched_slices) { + if (batches.empty()) return -1; + for (const auto &item : batches) { + uint64_t offset = 0; + auto key = item.first; + auto value = item.second; + std::vector slices; + while (offset < value.size()) { + auto chunk_size = std::min(value.size() - offset, kMaxSliceSize); + auto ptr = client_buffer_allocator_->allocate(chunk_size); + if (!ptr) { + return 1; + } + memcpy(ptr, value.data() + offset, chunk_size); + slices.emplace_back(Slice{ptr, chunk_size}); + offset += chunk_size; + } + keys.emplace_back(key); + batched_slices.emplace(key, std::move(slices)); + } + return 0; +} + +int DistributedObjectStore::allocateBatchedSlices( + const std::vector &keys, + std::unordered_map> + &batched_slices, + const mooncake::Client::BatchObjectInfo &batched_object_info, + std::unordered_map &str_length_map) { + if (batched_object_info.batch_replica_list.empty()) return -1; + for (const auto &key : keys) { + auto object_info_it = batched_object_info.batch_replica_list.find(key); + if (object_info_it == batched_object_info.batch_replica_list.end()) { + LOG(ERROR) << "Key not found: " << key; + return 1; + } + // Get first replica + auto &replica = object_info_it->second[0]; + uint64_t length = 0; + for (auto &handle : replica.buffer_descriptors) { + auto chunk_size = handle.size_; + assert(chunk_size <= kMaxSliceSize); + auto ptr = client_buffer_allocator_->allocate(chunk_size); + if (!ptr) { + return 1; + } + batched_slices[key].emplace_back(Slice{ptr, chunk_size}); + length += chunk_size; + } + str_length_map.emplace(key, length); + } + return 0; +} + char *DistributedObjectStore::exportSlices( const std::vector &slices, uint64_t length) { char *buf = new char[length + 1]; @@ -377,6 +435,36 @@ int DistributedObjectStore::put(const std::string &key, return 0; } +int DistributedObjectStore::put_batch( + const std::unordered_map> &batches) { + if (!client_) { + LOG(ERROR) << "Client is not initialized"; + return 1; + } + std::vector keys; + std::unordered_map> + batched_slices; + int ret = allocateBatchedSlices(keys, batches, batched_slices); + if (ret) { + LOG(ERROR) << "Failed to allocate slices for put_batch operation"; + return ret; + } + + ReplicateConfig config; + config.replica_num = 1; + ErrorCode error_code = client_->BatchPut(keys, batched_slices, config); + if (error_code != ErrorCode::OK) { + LOG(ERROR) << "BatchPut operation failed with error: " + << toString(error_code); + return toInt(error_code); + } + + for (auto &slice : batched_slices) { + freeSlices(slice.second); + } + return 0; +} + int DistributedObjectStore::put_parts( const std::string &key, std::vector> values) { if (!client_) { @@ -466,6 +554,76 @@ pybind11::bytes DistributedObjectStore::get(const std::string &key) { return result; } +std::vector DistributedObjectStore::get_batch( + const std::vector &keys) { + const auto kNullString = pybind11::bytes("\0", 0); + if (!client_) { + LOG(ERROR) << "Client is not initialized"; + return {kNullString}; + } + std::unordered_set seen; + for (const auto &key : keys) { + if (!seen.insert(key).second) { + LOG(ERROR) << "Duplicate key not supportted for Batch API, key: " + << key; + return {kNullString}; + } + } + + std::vector results; + mooncake::Client::BatchObjectInfo batched_object_info; + std::unordered_map> + batched_slices; + std::unordered_map str_length_map; + { + py::gil_scoped_release release_gil; + ErrorCode error_code = client_->BatchQuery(keys, batched_object_info); + if (error_code != ErrorCode::OK) { + py::gil_scoped_acquire acquire_gil; + return {kNullString}; + } else { + int ret = allocateBatchedSlices( + keys, batched_slices, batched_object_info, str_length_map); + if (ret) { + py::gil_scoped_acquire acquire_gil; + return {kNullString}; + } + error_code = + client_->BatchGet(keys, batched_object_info, batched_slices); + if (error_code != ErrorCode::OK) { + py::gil_scoped_acquire acquire_gil; + return {kNullString}; + } + } + for (const auto &key : keys) { + if (batched_slices[key].size() == 1 && + batched_slices[key][0].size == str_length_map[key]) { + results.push_back(pybind11::bytes( + static_cast(batched_slices[key][0].ptr), + str_length_map[key])); + } else { + char *exported_str_ptr = + exportSlices(batched_slices[key], str_length_map[key]); + if (!exported_str_ptr) { + return {kNullString}; + } else { + results.push_back( + pybind11::bytes(exported_str_ptr, str_length_map[key])); + delete[] exported_str_ptr; + } + } + } + if (results.size() != keys.size()) { + LOG(ERROR) << "Results size does not match keys size"; + return {kNullString}; + } + for (auto &slice : batched_slices) { + freeSlices(slice.second); + } + return results; + } +} + int DistributedObjectStore::remove(const std::string &key) { if (!client_) { LOG(ERROR) << "Client is not initialized"; @@ -523,6 +681,25 @@ int64_t DistributedObjectStore::getSize(const std::string &key) { return total_size; } +std::unordered_map DistributedObjectStore::BatchIsExist( + const std::vector &keys) { + std::unordered_map result_map; + if (!client_) { + LOG(ERROR) << "Client is not initialized"; + return result_map; + } + const auto existence_map = client_->BatchIsExist(keys); + if (existence_map.size() != keys.size()) { + LOG(ERROR) << "Internal error: existence_map.size() is not equal to " + "keys.size()"; + return result_map; + } + for (const auto &[key, err] : existence_map) { + result_map[key] = (err == ErrorCode::OK); + } + return result_map; +} + // SliceBuffer implementation SliceBuffer::SliceBuffer(DistributedObjectStore &store, void *buffer, uint64_t size, bool use_allocator_free) @@ -648,6 +825,7 @@ PYBIND11_MODULE(store, m) { .def("setup", &DistributedObjectStore::setup) .def("init_all", &DistributedObjectStore::initAll) .def("get", &DistributedObjectStore::get) + .def("get_batch", &DistributedObjectStore::get_batch) .def("get_buffer", &DistributedObjectStore::get_buffer, py::call_guard(), py::return_value_policy::take_ownership) @@ -657,6 +835,8 @@ PYBIND11_MODULE(store, m) { py::call_guard()) .def("is_exist", &DistributedObjectStore::isExist, py::call_guard()) + .def("is_batch_exist", &DistributedObjectStore::BatchIsExist, + py::call_guard()) .def("close", &DistributedObjectStore::tearDownAll) .def("get_size", &DistributedObjectStore::getSize, py::call_guard()) @@ -669,27 +849,49 @@ PYBIND11_MODULE(store, m) { static_cast(info.ptr), static_cast(info.size))); }) - .def("put_parts", [](DistributedObjectStore &self, - const std::string &key, py::args parts) { - // 1) Python buffer → span - std::vector infos; - std::vector> spans; - infos.reserve(parts.size()); - spans.reserve(parts.size()); - - for (auto &obj : parts) { - py::buffer buf = py::reinterpret_borrow(obj); - infos.emplace_back(buf.request(false)); - const auto &info = infos.back(); - if (info.ndim != 1 || info.itemsize != 1) - throw std::runtime_error("parts must be 1-D bytes-like"); - - spans.emplace_back(static_cast(info.ptr), - static_cast(info.size)); - } - - // 2) Call C++ function - py::gil_scoped_release unlock; - return self.put_parts(key, spans); - }); + .def("put_parts", + [](DistributedObjectStore &self, const std::string &key, + py::args parts) { + // 1) Python buffer → span + std::vector infos; + std::vector> spans; + infos.reserve(parts.size()); + spans.reserve(parts.size()); + + for (auto &obj : parts) { + py::buffer buf = py::reinterpret_borrow(obj); + infos.emplace_back(buf.request(false)); + const auto &info = infos.back(); + if (info.ndim != 1 || info.itemsize != 1) + throw std::runtime_error( + "parts must be 1-D bytes-like"); + + spans.emplace_back(static_cast(info.ptr), + static_cast(info.size)); + } + + // 2) Call C++ function + py::gil_scoped_release unlock; + return self.put_parts(key, spans); + }) + .def( + "put_batch", + [](DistributedObjectStore &self, + const std::unordered_map &batches) { + std::unordered_map> + batches_c; + std::vector> temp_buffers; + for (auto item : batches) { + const std::string &key = item.first; + const std::string &value = item.second; + temp_buffers.emplace_back(value.begin(), value.end()); + + batches_c.emplace( + key, std::span(temp_buffers.back().data(), + temp_buffers.back().size())); + } + + py::gil_scoped_release release; + return self.put_batch(batches_c); + }); } diff --git a/mooncake-integration/store/store_py.h b/mooncake-integration/store/store_py.h index c30a5c7c3e3..eb66fbee5df 100644 --- a/mooncake-integration/store/store_py.h +++ b/mooncake-integration/store/store_py.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -112,11 +113,15 @@ class DistributedObjectStore { int put(const std::string &key, std::span value); + int put_batch(const std::unordered_map> &batches); + int put_parts(const std::string &key, std::vector> values); pybind11::bytes get(const std::string &key); + std::vector get_batch( + const std::vector &keys); /** * @brief Get a buffer containing the data for a key * @param key Key to get data for @@ -138,6 +143,14 @@ class DistributedObjectStore { */ int isExist(const std::string &key); + /** + * @brief Check if a batch of objects exists + * @param keys Key to check + * @return Map of keys to booleans + */ + std::unordered_map BatchIsExist( + const std::vector &keys); + /** * @brief Get the size of an object * @param key Key of the object @@ -160,6 +173,19 @@ class DistributedObjectStore { int allocateSlicesPacked(std::vector &slices, const std::vector> &parts); + int allocateBatchedSlices( + const std::vector &keys, + std::unordered_map> + &batched_slices, + const mooncake::Client::BatchObjectInfo &batched_object_info, + std::unordered_map &str_length_map); + + int allocateBatchedSlices( + std::vector &keys, + const std::unordered_map> &batches, + std::unordered_map> + &batched_slices); + char *exportSlices(const std::vector &slices, uint64_t length); diff --git a/mooncake-store/include/client.h b/mooncake-store/include/client.h index 2f2abbf2120..90e22bd2cb0 100644 --- a/mooncake-store/include/client.h +++ b/mooncake-store/include/client.h @@ -191,6 +191,14 @@ class Client { */ ErrorCode IsExist(const std::string& key); + /** + * @brief Checks if a batch of objects exist + * @param keys Keys to check + * @return existence map, true if exists, false if not + */ + std::unordered_map BatchIsExist( + const std::vector& keys); + private: /** * @brief Private constructor to enforce creation through Create() method diff --git a/mooncake-store/include/master_client.h b/mooncake-store/include/master_client.h index 77b97c3a98b..d7aa1623d6c 100644 --- a/mooncake-store/include/master_client.h +++ b/mooncake-store/include/master_client.h @@ -41,6 +41,14 @@ class MasterClient { [[nodiscard]] ExistKeyResponse ExistKey( const std::string& object_key); + /** + * @brief Checks if a batch of objects exist + * @param object_keys Keys to query + * @return ErrorCode indicating success/failure + */ + [[nodiscard]] BatchExistKeyResponse BatchExistKey( + const std::vector& object_keys); + /** * @brief Gets object metadata without transferring data * @param object_key Key to query diff --git a/mooncake-store/include/master_service.h b/mooncake-store/include/master_service.h index f6a1bf29cfe..f55ba5b0220 100644 --- a/mooncake-store/include/master_service.h +++ b/mooncake-store/include/master_service.h @@ -117,6 +117,13 @@ class MasterService { */ ErrorCode ExistKey(const std::string& key); + /** + * @brief Check if an batch of objects exist + * @return existence map, key is the object name, value is the error code + */ + std::unordered_map BatchExistKey( + const std::vector& key); + /** * @brief Fetch all keys * @return ErrorCode::OK if exists diff --git a/mooncake-store/include/rpc_service.h b/mooncake-store/include/rpc_service.h index bef7981b19d..929f3a826dd 100644 --- a/mooncake-store/include/rpc_service.h +++ b/mooncake-store/include/rpc_service.h @@ -22,7 +22,11 @@ struct ExistKeyResponse { ErrorCode error_code = ErrorCode::OK; }; YLT_REFL(ExistKeyResponse, error_code) - +struct BatchExistKeyResponse { + std::unordered_map existence_map; + ErrorCode error_code = ErrorCode::OK; +}; +YLT_REFL(BatchExistKeyResponse, existence_map, error_code) struct GetReplicaListResponse { std::vector replica_list; ErrorCode error_code = ErrorCode::OK; @@ -262,6 +266,21 @@ class WrappedMasterService { return response; } + BatchExistKeyResponse BatchExistKey(const std::vector& keys) { + ScopedVLogTimer timer(1, "BatchExistKey"); + timer.LogRequest("key count=", keys.size()); + // Increment request metric + MasterMetricManager::instance().inc_exist_key_requests(keys.size()); + BatchExistKeyResponse response; + response.existence_map = master_service_.BatchExistKey(keys); + // Track failures if needed + if (response.error_code != ErrorCode::OK) { + MasterMetricManager::instance().inc_exist_key_failures(keys.size()); + } + timer.LogResponseJson(response); + return response; + } + GetReplicaListResponse GetReplicaList(const std::string& key) { ScopedVLogTimer timer(1, "GetReplicaList"); timer.LogRequest("key=", key); @@ -519,6 +538,8 @@ inline void RegisterRpcService( mooncake::WrappedMasterService& wrapped_master_service) { server.register_handler<&mooncake::WrappedMasterService::ExistKey>( &wrapped_master_service); + server.register_handler<&mooncake::WrappedMasterService::BatchExistKey>( + &wrapped_master_service); server.register_handler<&mooncake::WrappedMasterService::GetReplicaList>( &wrapped_master_service); server diff --git a/mooncake-store/proto/master.proto b/mooncake-store/proto/master.proto index 6c409ca636c..d775909a93b 100644 --- a/mooncake-store/proto/master.proto +++ b/mooncake-store/proto/master.proto @@ -47,6 +47,11 @@ message BatchSliceLength { repeated uint64 slice_lengths = 2; } +message ExistencePair { + required string key = 1; + required int32 status_code = 2 +} + // Request to check key existence. message ExistKeyRequest { required string key = 1; // Object key. @@ -57,6 +62,17 @@ message ExistKeyResponse { required int32 status_code = 1; // Status. } +// Request to check key existence. +message BatchExistKeyRequest { + repeated string key = 1; // Object key. +} +// Response to check key existence. +message BatchExistKeyResponse { + repeated ExistencePair existence_pair = 1; + required int32 status_code = 2; // Status. +} + + // Request to get replica list. message GetReplicaListRequest { required string key = 1; // Object key. @@ -222,4 +238,7 @@ service MasterService { // Check existence of a key. rpc ExistKey(ExistKeyRequest) returns (ExistKeyResponse); + + // Check existence of a batch of keys. + rpc BatchExistKey(BatchExistKeyRequest) returns (BatchExistKeyResponse); } diff --git a/mooncake-store/src/client.cpp b/mooncake-store/src/client.cpp index 6da5377ec05..22eff49f82f 100644 --- a/mooncake-store/src/client.cpp +++ b/mooncake-store/src/client.cpp @@ -628,6 +628,14 @@ ErrorCode Client::IsExist(const std::string& key) { return response.error_code; } +std::unordered_map Client::BatchIsExist( + const std::vector& keys) { + const auto& [existence_map, error_code] = + master_client_.BatchExistKey(keys); + return error_code == ErrorCode::OK ? existence_map + : decltype(existence_map){}; +} + ErrorCode Client::TransferData( const std::vector& handles, std::vector& slices, TransferRequest::OpCode op_code) { diff --git a/mooncake-store/src/master_client.cpp b/mooncake-store/src/master_client.cpp index 6cd3b83c162..03ebf0be3f3 100644 --- a/mooncake-store/src/master_client.cpp +++ b/mooncake-store/src/master_client.cpp @@ -60,6 +60,31 @@ ExistKeyResponse MasterClient::ExistKey(const std::string& object_key) { return result.value(); } +BatchExistKeyResponse MasterClient::BatchExistKey( + const std::vector& object_keys) { + ScopedVLogTimer timer(1, "MasterClient::BatchExistKey"); + timer.LogRequest("object_key count=", object_keys.size()); + auto request_result = + client_.send_request<&WrappedMasterService::BatchExistKey>(object_keys); + std::optional result = coro::syncAwait( + [&]() -> coro::Lazy> { + auto result = co_await co_await request_result; + if (!result) { + LOG(ERROR) << "Failed to check existence for a batch of key: " + << result.error().msg; + co_return std::nullopt; + } + co_return result->result(); + }()); + if (!result) { + auto response = BatchExistKeyResponse{{}, ErrorCode::RPC_FAIL}; + timer.LogResponseJson(response); + return response; + } + timer.LogResponseJson(result.value()); + return result.value(); +} + GetReplicaListResponse MasterClient::GetReplicaList( const std::string& object_key) { ScopedVLogTimer timer(1, "MasterClient::GetReplicaList"); diff --git a/mooncake-store/src/master_service.cpp b/mooncake-store/src/master_service.cpp index 102bdda241a..2cccd124170 100644 --- a/mooncake-store/src/master_service.cpp +++ b/mooncake-store/src/master_service.cpp @@ -170,6 +170,15 @@ ErrorCode MasterService::ExistKey(const std::string& key) { return ErrorCode::OK; } +std::unordered_map MasterService::BatchExistKey( + const std::vector& keys) { + std::unordered_map result; + for (const auto& key : keys) { + result.emplace(key, ExistKey(key)); + } + return result; +} + ErrorCode MasterService::GetAllKeys(std::vector & all_keys) { all_keys.clear(); for(int i = 0; i < kNumShards; i++) { diff --git a/mooncake-store/tests/client_integration_test.cpp b/mooncake-store/tests/client_integration_test.cpp index 473f5e4f7fd..e3a1dfec227 100644 --- a/mooncake-store/tests/client_integration_test.cpp +++ b/mooncake-store/tests/client_integration_test.cpp @@ -422,6 +422,26 @@ TEST_F(ClientIntegrationTest, BatchPutGetOperations) { .count() << "us"; + start = std::chrono::high_resolution_clock::now(); + for (int i = 0; i < batch_sz; i++) { + test_client_->IsExist(keys[i]); + } + end = std::chrono::high_resolution_clock::now(); + LOG(INFO) << "Time taken for single IsExist: " + << std::chrono::duration_cast(end - + start) + .count() + << "us"; + start = std::chrono::high_resolution_clock::now(); + std::unordered_map batched_existence = + test_client_->BatchIsExist(keys); + end = std::chrono::high_resolution_clock::now(); + LOG(INFO) << "Time taken for BatchIsExist: " + << std::chrono::duration_cast(end - + start) + .count() + << "us"; + start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < batch_sz; i++) { std::vector slices; From 2a4e2663e2bd7e0c598674d6bdc09cfa124f02c3 Mon Sep 17 00:00:00 2001 From: xinranwang17 <87713897+xinranwang17@users.noreply.github.com> Date: Tue, 17 Jun 2025 21:59:51 +0800 Subject: [PATCH 2/2] Update store_py.cpp --- mooncake-integration/store/store_py.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mooncake-integration/store/store_py.cpp b/mooncake-integration/store/store_py.cpp index 8c946239b4a..f27e7fda104 100644 --- a/mooncake-integration/store/store_py.cpp +++ b/mooncake-integration/store/store_py.cpp @@ -564,7 +564,7 @@ std::vector DistributedObjectStore::get_batch( std::unordered_set seen; for (const auto &key : keys) { if (!seen.insert(key).second) { - LOG(ERROR) << "Duplicate key not supportted for Batch API, key: " + LOG(ERROR) << "Duplicate key not supported for Batch API, key: " << key; return {kNullString}; }