Repository navigation
[Store] Reuse an in-process master for tests and evaluation #3665
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
stmatengss
merged 11 commits into
kvcache-ai:main
from
stmatengss:stmatengss/store-standalone-no-master-311c
Sep 30, 2026
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c153e28
[Store] Add standalone mode that embeds master in-process
cursoragent ca38a7f
[Store] Fix EmbeddedMaster incomplete types for client builds
cursoragent f86546a
[Store] Fix standalone service tests for mocked CLI args
cursoragent 96f399f
[Store] Honor MOONCAKE_ENABLE_STANDALONE for HiCache setup()
cursoragent 460cc40
[CI/Build] Add standalone store e2e tests and fix docs xref
cursoragent 5b00417
[CI/Build] Use memory replicas for standalone store_client e2e
cursoragent e6c2a48
[Doc] Note standalone store_client e2e uses memory replicas
cursoragent 842dc49
[Store] Shrink standalone mode to in-process master only
cursoragent 95ffa8d
[Store] Keep in-process master as test plumbing, not a deployment mode
cursoragent 49c2a59
[Store] Merge main and address embedded master review
stmatengss 5b3925f
[Store] Preserve embedded master restart and engine metadata semantics
stmatengss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include <ylt/coro_rpc/coro_rpc_server.hpp> | ||
|
|
||
| #include "http_metadata_server.h" | ||
| #include "master_admin_service.h" | ||
| #include "master_config.h" | ||
| #include "rpc_service.h" | ||
|
|
||
| namespace mooncake { | ||
|
|
||
| // In-process master used by tests and by opt-in enable_embedded_master. | ||
| // When enabled, a store client starts this master in the same process so no | ||
| // external mooncake_master is required. This is not a user-facing deployment | ||
| // mode. All listeners bind to the IPv4 loopback address. | ||
| class EmbeddedMaster { | ||
| public: | ||
| EmbeddedMaster() = default; | ||
| ~EmbeddedMaster() { Stop(); } | ||
|
|
||
| EmbeddedMaster(const EmbeddedMaster&) = delete; | ||
| EmbeddedMaster& operator=(const EmbeddedMaster&) = delete; | ||
| EmbeddedMaster(EmbeddedMaster&&) = delete; | ||
| EmbeddedMaster& operator=(EmbeddedMaster&&) = delete; | ||
|
|
||
| bool Start(InProcMasterConfig config); | ||
| void Stop(); | ||
|
|
||
| int rpc_port() const { return rpc_port_; } | ||
| int http_metrics_port() const { return http_metrics_port_; } | ||
| int http_metadata_port() const { return http_metadata_port_; } | ||
| std::string master_address() const { | ||
| return std::string("127.0.0.1:") + std::to_string(rpc_port_); | ||
| } | ||
| std::string metadata_url() const { | ||
| if (http_metadata_port_ <= 0) return {}; | ||
| return std::string("http://127.0.0.1:") + | ||
| std::to_string(http_metadata_port_) + "/metadata"; | ||
| } | ||
| std::string http_metrics_base() const { | ||
| return std::string("http://127.0.0.1:") + | ||
| std::to_string(http_metrics_port_); | ||
| } | ||
| std::shared_ptr<WrappedMasterService> service() const { return wrapped_; } | ||
|
|
||
| private: | ||
| std::unique_ptr<coro_rpc::coro_rpc_server> server_; | ||
| std::shared_ptr<WrappedMasterService> wrapped_; | ||
| std::unique_ptr<MasterAdminServer> admin_server_; | ||
| std::unique_ptr<HttpMetadataServer> meta_server_; | ||
| int rpc_port_ = 0; | ||
| int http_metrics_port_ = 0; | ||
| int http_metadata_port_ = 0; | ||
| }; | ||
|
|
||
| } // namespace mooncake |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| #include "embedded_master.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <chrono> | ||
| #include <cstdlib> | ||
| #include <exception> | ||
| #include <string_view> | ||
| #include <thread> | ||
| #include <vector> | ||
|
|
||
| #include <glog/logging.h> | ||
| #include <ylt/coro_rpc/coro_rpc_server.hpp> | ||
|
|
||
| #include "common/network.h" | ||
| #include "http_metadata_server.h" | ||
| #include "master_admin_service.h" | ||
| #include "types.h" | ||
|
|
||
| namespace mooncake { | ||
|
|
||
| bool EmbeddedMaster::Start(InProcMasterConfig config) { | ||
| if (server_) { | ||
| LOG(ERROR) << "Embedded master is already running on port " | ||
| << rpc_port_; | ||
| return false; | ||
| } | ||
| try { | ||
| // Allocate all needed ports atomically to avoid collisions from | ||
| // rapid sequential getFreeTcpPort() calls (TOCTOU). | ||
| int needed = (!config.rpc_port.has_value()) + | ||
| (!config.http_metrics_port.has_value()) + | ||
| (!config.http_metadata_port.has_value()); | ||
| std::vector<int> available_ports; | ||
| if (needed > 0) { | ||
| std::vector<int> reserved_ports; | ||
| if (config.rpc_port.has_value()) { | ||
| reserved_ports.push_back(config.rpc_port.value()); | ||
| } | ||
| if (config.http_metrics_port.has_value()) { | ||
| reserved_ports.push_back(config.http_metrics_port.value()); | ||
| } | ||
| if (config.http_metadata_port.has_value()) { | ||
| reserved_ports.push_back(config.http_metadata_port.value()); | ||
| } | ||
| auto free_ports = getFreeTcpPorts( | ||
| needed + static_cast<int>(reserved_ports.size())); | ||
| available_ports.reserve(needed); | ||
| for (int port : free_ports) { | ||
| if (std::find(reserved_ports.begin(), reserved_ports.end(), | ||
| port) == reserved_ports.end()) { | ||
| available_ports.push_back(port); | ||
| if (static_cast<int>(available_ports.size()) == needed) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (static_cast<int>(available_ports.size()) < needed) { | ||
| LOG(ERROR) << "Failed to reserve " << needed | ||
| << " free TCP ports for embedded master"; | ||
| return false; | ||
| } | ||
| } | ||
| int idx = 0; | ||
| rpc_port_ = config.rpc_port.has_value() ? config.rpc_port.value() | ||
| : available_ports[idx++]; | ||
| http_metrics_port_ = config.http_metrics_port.has_value() | ||
| ? config.http_metrics_port.value() | ||
| : available_ports[idx++]; | ||
| http_metadata_port_ = config.http_metadata_port.has_value() | ||
| ? config.http_metadata_port.value() | ||
| : available_ports[idx++]; | ||
|
|
||
| if (http_metadata_port_ > 0) { | ||
| meta_server_ = std::make_unique<HttpMetadataServer>( | ||
| static_cast<uint16_t>(http_metadata_port_), "127.0.0.1"); | ||
| if (!meta_server_->start()) { | ||
| LOG(ERROR) << "Failed to start embedded HTTP metadata server " | ||
| "on port " | ||
| << http_metadata_port_; | ||
| Stop(); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| server_ = std::make_unique<coro_rpc::coro_rpc_server>( | ||
| /*thread_num=*/4, /*port=*/rpc_port_, /*address=*/"127.0.0.1", | ||
| std::chrono::seconds(0), /*tcp_no_delay=*/true); | ||
| const char* value = std::getenv("MC_RPC_PROTOCOL"); | ||
| if (value && std::string_view(value) == "rdma") { | ||
| #ifdef YLT_ENABLE_IBV | ||
| server_->init_ibv(); | ||
| #else | ||
| LOG(WARNING) | ||
| << "RDMA RPC is disabled at compile time; using TCP RPC"; | ||
| #endif | ||
| } | ||
|
|
||
| uint64_t default_kv_lease_ttl = DEFAULT_DEFAULT_KV_LEASE_TTL; | ||
| if (config.default_kv_lease_ttl.has_value()) { | ||
| default_kv_lease_ttl = config.default_kv_lease_ttl.value(); | ||
| } else if (const char* ttl_env = std::getenv("DEFAULT_KV_LEASE_TTL")) { | ||
| char* endptr = nullptr; | ||
| unsigned long parsed = std::strtoul(ttl_env, &endptr, 10); | ||
| if (endptr != ttl_env && endptr && *endptr == '\0') { | ||
| default_kv_lease_ttl = static_cast<uint64_t>(parsed); | ||
| } | ||
| } | ||
|
|
||
| WrappedMasterServiceConfig wms_cfg; | ||
| wms_cfg.default_kv_lease_ttl = default_kv_lease_ttl; | ||
| wms_cfg.default_kv_soft_pin_ttl = DEFAULT_KV_SOFT_PIN_TTL_MS; | ||
| wms_cfg.allow_evict_soft_pinned_objects = true; | ||
| wms_cfg.enable_metric_reporting = false; | ||
| wms_cfg.enable_offload = config.enable_offload.has_value() | ||
| ? config.enable_offload.value() | ||
| : false; | ||
| wms_cfg.eviction_ratio = DEFAULT_EVICTION_RATIO; | ||
| wms_cfg.eviction_high_watermark_ratio = | ||
| config.eviction_high_watermark_ratio.has_value() | ||
| ? config.eviction_high_watermark_ratio.value() | ||
| : DEFAULT_EVICTION_HIGH_WATERMARK_RATIO; | ||
| wms_cfg.view_version = 0; | ||
| wms_cfg.enable_ha = false; | ||
| wms_cfg.http_port = static_cast<uint16_t>(http_metrics_port_); | ||
| wms_cfg.cluster_id = DEFAULT_CLUSTER_ID; | ||
| wms_cfg.root_fs_dir = config.root_fs_dir.has_value() | ||
| ? config.root_fs_dir.value() | ||
| : DEFAULT_ROOT_FS_DIR; | ||
| wms_cfg.memory_allocator = BufferAllocatorType::OFFSET; | ||
| if (config.enable_disk_eviction.has_value()) { | ||
| wms_cfg.enable_disk_eviction = config.enable_disk_eviction.value(); | ||
| } | ||
| if (config.quota_bytes.has_value()) { | ||
| wms_cfg.quota_bytes = config.quota_bytes.value(); | ||
| } | ||
|
|
||
| wms_cfg.enable_cxl = | ||
| config.enable_cxl.has_value() ? config.enable_cxl.value() : false; | ||
| if (config.cxl_path.has_value()) { | ||
| wms_cfg.cxl_path = config.cxl_path.value(); | ||
| } else if (const char* cxl_path_env = std::getenv("MC_CXL_DEV_PATH")) { | ||
| wms_cfg.cxl_path = cxl_path_env; | ||
| } | ||
|
|
||
| if (config.cxl_size.has_value()) { | ||
| wms_cfg.cxl_size = config.cxl_size.value(); | ||
| } else if (const char* cxl_size_env = std::getenv("MC_CXL_DEV_SIZE")) { | ||
| char* endptr = nullptr; | ||
| unsigned long long val = std::strtoull(cxl_size_env, &endptr, 10); | ||
| if (endptr != cxl_size_env && *endptr == '\0') { | ||
| wms_cfg.cxl_size = static_cast<size_t>(val); | ||
| } | ||
| } | ||
|
|
||
| wrapped_ = std::make_shared<WrappedMasterService>(wms_cfg); | ||
| admin_server_ = std::make_unique<MasterAdminServer>( | ||
| static_cast<uint16_t>(http_metrics_port_), | ||
| /*enable_metric_reporting=*/false, /*http_host=*/"127.0.0.1"); | ||
| if (!admin_server_->Start()) { | ||
| LOG(ERROR) << "Failed to start embedded master admin server on " | ||
| "port " | ||
| << http_metrics_port_; | ||
| Stop(); | ||
| return false; | ||
| } | ||
| admin_server_->SetRuntimeState(ha::MasterRuntimeState::kServing); | ||
| admin_server_->SetServiceDelegate(wrapped_); | ||
| admin_server_->SetServiceAvailable(true); | ||
| RegisterRpcService(*server_, *wrapped_); | ||
|
|
||
| auto ec = server_->async_start(); | ||
| if (ec.hasResult()) { | ||
| LOG(ERROR) << "Failed to start embedded master RPC server on port " | ||
| << rpc_port_; | ||
| Stop(); | ||
| return false; | ||
| } | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(200)); | ||
| LOG(INFO) << "Embedded master started on " << master_address() | ||
| << ", metrics_port=" << http_metrics_port_ | ||
| << ", metadata_port=" << http_metadata_port_; | ||
| return true; | ||
| } catch (const std::exception& e) { | ||
| LOG(ERROR) << "Failed to start embedded master: " << e.what(); | ||
| Stop(); | ||
| return false; | ||
| } catch (...) { | ||
| LOG(ERROR) << "Failed to start embedded master"; | ||
| Stop(); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| void EmbeddedMaster::Stop() { | ||
| if (admin_server_) { | ||
| admin_server_->Stop(); | ||
| admin_server_.reset(); | ||
| } | ||
| if (server_) { | ||
| server_->stop(); | ||
| server_.reset(); | ||
| } | ||
| wrapped_.reset(); | ||
| if (meta_server_) { | ||
| meta_server_->stop(); | ||
| meta_server_.reset(); | ||
| } | ||
| rpc_port_ = 0; | ||
| http_metrics_port_ = 0; | ||
| http_metadata_port_ = 0; | ||
| } | ||
|
|
||
| } // namespace mooncake | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.