Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
30 changes: 30 additions & 0 deletions mooncake-store/include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,34 @@ class Client {
UUID client_id_;
};

/**
* @brief Fluent builder for configuring a mooncake::Client instance.
*
* Provides readable, type-safe setters with sensible defaults so callers can
* specify only the options they need while reusing existing Client::Create
* logic under the hood.
*/
class MooncakeStoreBuilder {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ClientBuilder should be a better name in the context, as we are infact building a Client, not a MooncakeStore.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about MooncakeStoreClientBuilder? Don't be afraid of a long name.

public:
MooncakeStoreBuilder& WithLocalHostname(std::string local_hostname);
MooncakeStoreBuilder& WithMetadataConnectionString(
std::string metadata_connstring);
MooncakeStoreBuilder& WithProtocol(std::string protocol);
MooncakeStoreBuilder& WithTransferEngineArgs(std::string engine_args);
MooncakeStoreBuilder& WithMasterEndpoint(std::string master_server_entry);
MooncakeStoreBuilder& WithExistingTransferEngine(
std::shared_ptr<TransferEngine> transfer_engine);

[[nodiscard]] tl::expected<std::shared_ptr<Client>, std::string> Build()
const;

private:
std::optional<std::string> local_hostname_;
std::optional<std::string> metadata_connstring_;
std::string protocol_ = "tcp";
std::optional<std::string> device_names_;
std::string master_server_entry_ = kDefaultMasterAddress;
std::shared_ptr<TransferEngine> transfer_engine_ = nullptr;
};

} // namespace mooncake
16 changes: 8 additions & 8 deletions mooncake-store/include/pybind_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ class PyClient {
// Factory to create shared instances and auto-register to ResourceTracker
static std::shared_ptr<PyClient> create();

int setup(const std::string &local_hostname,
const std::string &metadata_server,
size_t global_segment_size = 1024 * 1024 * 16,
size_t local_buffer_size = 1024 * 1024 * 16,
const std::string &protocol = "tcp",
const std::string &rdma_devices = "",
const std::string &master_server_addr = "127.0.0.1:50051",
const std::shared_ptr<TransferEngine> &transfer_engine = nullptr);
[[deprecated("Use MooncakeStoreBuilder instead")]] int setup(
const std::string &local_hostname, const std::string &metadata_server,
size_t global_segment_size = 1024 * 1024 * 16,
size_t local_buffer_size = 1024 * 1024 * 16,
const std::string &protocol = "tcp",
const std::string &rdma_devices = "",
const std::string &master_server_addr = "127.0.0.1:50051",
const std::shared_ptr<TransferEngine> &transfer_engine = nullptr);

int initAll(const std::string &protocol, const std::string &device_name,
size_t mount_segment_size = 1024 * 1024 * 16); // Default 16MB
Expand Down
75 changes: 75 additions & 0 deletions mooncake-store/src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <string_view>
#include <optional>
#include <ranges>
#include <thread>
Expand Down Expand Up @@ -406,6 +407,80 @@ std::optional<std::shared_ptr<Client>> Client::Create(
return client;
}

MooncakeStoreBuilder& MooncakeStoreBuilder::WithLocalHostname(
std::string local_hostname) {
local_hostname_ = std::move(local_hostname);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use const std::string& instead of std::string for the parameter, and don't move the parameter. Please also update the other "WithXXX" methods that taking strings as arguments.

return *this;
}

MooncakeStoreBuilder& MooncakeStoreBuilder::WithMetadataConnectionString(
std::string metadata_connstring) {
metadata_connstring_ = std::move(metadata_connstring);
return *this;
}

MooncakeStoreBuilder& MooncakeStoreBuilder::WithProtocol(std::string protocol) {
protocol_ = std::move(protocol);
return *this;
}

MooncakeStoreBuilder& MooncakeStoreBuilder::WithTransferEngineArgs(
std::string engine_args) {
// Can add some other engine arguments
device_names_ = std::move(engine_args);
return *this;
}

MooncakeStoreBuilder& MooncakeStoreBuilder::WithMasterEndpoint(
std::string master_server_entry) {
master_server_entry_ = std::move(master_server_entry);
return *this;
}

MooncakeStoreBuilder& MooncakeStoreBuilder::WithExistingTransferEngine(
std::shared_ptr<TransferEngine> transfer_engine) {
transfer_engine_ = std::move(transfer_engine);
return *this;
}

tl::expected<std::shared_ptr<Client>, std::string> MooncakeStoreBuilder::Build()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning string for errors is not a good practice. String is not structured and hard to be programmatically processed. You can see the bad result in the unit tests that you must search the returned string to check whether it is expected.

I suggest to return an ErrorCode for failure. For example, INVALID_PARAMS for missing required field, INTERNAL_ERROR for client creation failure. And provide a GetMissingFields() method to get the missing fields if needed.

const {
std::vector<std::string_view> missing;
if (!local_hostname_) {
missing.emplace_back("local_hostname");
}
if (!metadata_connstring_) {
missing.emplace_back("metadata_connstring");
}

if (!missing.empty()) {
std::string joined;
joined.reserve(missing.size() * 16);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reserve is not necessary because this is not a performance critical path and 16 is a magic number. It's fine to let the string allocate space as needed.

for (size_t i = 0; i < missing.size(); ++i) {
if (i != 0) {
joined.append(", ");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This for loop and the if (i != 0) is redundent and confusing, just remove them.

joined.append(missing[0]);
for (size_t i = 1; i < missing.size(); ++i) {
joined.append(", ");
joined.append(missing[i]);
}
}
auto error_msg =
"MooncakeStoreBuilder missing required fields: " + joined;
LOG(ERROR) << error_msg;
return tl::make_unexpected(std::move(error_msg));
}
auto client =
Client::Create(*local_hostname_, *metadata_connstring_, protocol_,
device_names_, master_server_entry_, transfer_engine_);
if (!client) {
std::string error_msg = "Client creation failed";
return tl::make_unexpected(std::move(error_msg));
}
return *client;
}

tl::expected<void, ErrorCode> Client::Get(const std::string& object_key,
std::vector<Slice>& slices) {
auto query_result = Query(object_key);
Expand Down
24 changes: 21 additions & 3 deletions mooncake-store/src/pybind_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,27 @@ tl::expected<void, ErrorCode> PyClient::setup_internal(
(rdma_devices.empty() ? std::nullopt
: std::make_optional(rdma_devices));

auto client_opt = mooncake::Client::Create(
this->local_hostname, metadata_server, protocol, device_name,
master_server_addr, transfer_engine);
MooncakeStoreBuilder builder;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this? just use create is good enough i think

builder.WithLocalHostname(this->local_hostname)
.WithMetadataConnectionString(metadata_server)
.WithProtocol(protocol)
.WithMasterEndpoint(master_server_addr);

if (device_name) {
builder.WithTransferEngineArgs(*device_name);
}
if (transfer_engine) {
builder.WithExistingTransferEngine(transfer_engine);
}

auto client_opt = MooncakeStoreBuilder()
.WithLocalHostname(this->local_hostname)
.WithMetadataConnectionString(metadata_server)
.WithProtocol(protocol)
.WithMasterEndpoint(master_server_addr)
.WithTransferEngineArgs(device_name.value_or(""))
.WithExistingTransferEngine(transfer_engine)
.Build();
if (!client_opt) {
LOG(ERROR) << "Failed to create client";
return tl::unexpected(ErrorCode::INVALID_PARAMS);
Expand Down
1 change: 1 addition & 0 deletions mooncake-store/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ add_store_test(serializer_test serializer_test.cpp)
add_store_test(non_ha_reconnect_test non_ha_reconnect_test.cpp)
add_store_test(storage_backend_test storage_backend_test.cpp)
add_store_test(mutex_test mutex_test.cpp)
add_store_test(builder_test builder_test.cpp)
add_subdirectory(e2e)

add_executable(high_availability_test high_availability_test.cpp)
Expand Down
70 changes: 70 additions & 0 deletions mooncake-store/tests/builder_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <glog/logging.h>
#include <gtest/gtest.h>

#include "client.h"

namespace mooncake::test {

class MooncakeStoreBuilderTest : public ::testing::Test {
protected:
void SetUp() override {
google::InitGoogleLogging("MooncakeStoreBuilderTest");
FLAGS_logtostderr = true;
}

void TearDown() override { google::ShutdownGoogleLogging(); }
};

TEST_F(MooncakeStoreBuilderTest, MissingAllRequiredFields) {
MooncakeStoreBuilder builder;

auto result = builder.Build();

ASSERT_FALSE(result.has_value());
const std::string& error = result.error();
EXPECT_NE(error.find("local_hostname"), std::string::npos);
EXPECT_NE(error.find("metadata_connstring"), std::string::npos);
}

TEST_F(MooncakeStoreBuilderTest, MissingLocalHostnameOnly) {
MooncakeStoreBuilder builder;
builder.WithMetadataConnectionString("metastore:1234");

auto result = builder.Build();

ASSERT_FALSE(result.has_value());
const std::string& error = result.error();
EXPECT_NE(error.find("local_hostname"), std::string::npos);
EXPECT_EQ(error.find("metadata_connstring"), std::string::npos);
}

TEST_F(MooncakeStoreBuilderTest, MissingMetadataConnectionStringOnly) {
MooncakeStoreBuilder builder;
builder.WithLocalHostname("localhost:1234");

auto result = builder.Build();

ASSERT_FALSE(result.has_value());
const std::string& error = result.error();
EXPECT_NE(error.find("metadata_connstring"), std::string::npos);
EXPECT_EQ(error.find("local_hostname"), std::string::npos);
}

TEST_F(MooncakeStoreBuilderTest, ProvidedAllRequiredFields) {
MooncakeStoreBuilder builder;
builder.WithLocalHostname("localhost:1234");
builder.WithMetadataConnectionString("metadata:5678");

auto result = builder.Build();

if (result.has_value()) {
SUCCEED();
} else {
const std::string& error = result.error();
EXPECT_EQ(error.find("missing required fields"), std::string::npos);
EXPECT_EQ(error.find("local_hostname"), std::string::npos);
EXPECT_EQ(error.find("metadata_connstring"), std::string::npos);
}
}

} // namespace mooncake::test
Loading