-
Notifications
You must be signed in to change notification settings - Fork 436
Add C++ builder pattern #1018
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
base: main
Are you sure you want to change the base?
Add C++ builder pattern #1018
Changes from 2 commits
47add82
9c644f0
58650f8
4fbe66a
2fb2b7e
a9aa54c
58766bc
8010e07
a6a7dc3
ff674c5
9ebae76
1daeb48
1780a31
67d1268
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 { | ||||||
| public: | ||||||
| MooncakeStoreBuilder& WithLocalHostname(std::string local_hostname); | ||||||
| MooncakeStoreBuilder& WithMetadataConnectionString( | ||||||
| std::string metadata_connstring); | ||||||
| MooncakeStoreBuilder& UsingProtocol(std::string protocol); | ||||||
JasonZhang517 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| MooncakeStoreBuilder& WithRdmaDeviceNames(std::string device_names); | ||||||
JasonZhang517 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| MooncakeStoreBuilder& WithMasterServerEntry( | ||||||
JasonZhang517 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| std::string master_server_entry); | ||||||
| MooncakeStoreBuilder& WithExistingTransferEngine( | ||||||
| std::shared_ptr<TransferEngine> transfer_engine); | ||||||
|
|
||||||
| [[nodiscard]] std::optional<std::shared_ptr<Client>> Build(); | ||||||
|
||||||
| [[nodiscard]] std::optional<std::shared_ptr<Client>> Build(); | |
| [[nodiscard]] std::optional<std::shared_ptr<Client>> Build() const; |
JasonZhang517 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||
| #include <chrono> | ||||||
| #include <cstdint> | ||||||
| #include <cstdlib> | ||||||
| #include <string_view> | ||||||
| #include <optional> | ||||||
| #include <ranges> | ||||||
| #include <thread> | ||||||
|
|
@@ -406,6 +407,74 @@ std::optional<std::shared_ptr<Client>> Client::Create( | |||||
| return client; | ||||||
| } | ||||||
|
|
||||||
| MooncakeStoreBuilder& MooncakeStoreBuilder::WithLocalHostname( | ||||||
| std::string local_hostname) { | ||||||
| local_hostname_ = std::move(local_hostname); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| MooncakeStoreBuilder& MooncakeStoreBuilder::WithMetadataConnectionString( | ||||||
| std::string metadata_connstring) { | ||||||
| metadata_connstring_ = std::move(metadata_connstring); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| MooncakeStoreBuilder& MooncakeStoreBuilder::UsingProtocol( | ||||||
| std::string protocol) { | ||||||
| protocol_ = std::move(protocol); | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| MooncakeStoreBuilder& MooncakeStoreBuilder::WithRdmaDeviceNames( | ||||||
| std::string device_names) { | ||||||
| if (device_names.empty()) { | ||||||
JasonZhang517 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| device_names_.reset(); | ||||||
| } else { | ||||||
| device_names_ = std::move(device_names); | ||||||
| } | ||||||
| return *this; | ||||||
| } | ||||||
|
|
||||||
| MooncakeStoreBuilder& MooncakeStoreBuilder::WithMasterServerEntry( | ||||||
| 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; | ||||||
| } | ||||||
|
|
||||||
| std::optional<std::shared_ptr<Client>> MooncakeStoreBuilder::Build() { | ||||||
|
||||||
| std::optional<std::shared_ptr<Client>> MooncakeStoreBuilder::Build() { | |
| std::optional<std::shared_ptr<Client>> MooncakeStoreBuilder::Build() const { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loop for joining strings with a separator can be made slightly more efficient and idiomatic by handling the first element outside the loop. This avoids the conditional check if (i != 0) in every iteration.
joined.append(missing[0]);
for (size_t i = 1; i < missing.size(); ++i) {
joined.append(", ");
joined.append(missing[i]);
}| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -178,9 +178,20 @@ 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; | ||
|
||
| builder.WithLocalHostname(this->local_hostname) | ||
| .WithMetadataConnectionString(metadata_server) | ||
| .UsingProtocol(protocol) | ||
| .WithMasterServerEntry(master_server_addr); | ||
|
|
||
| if (device_name) { | ||
| builder.WithRdmaDeviceNames(*device_name); | ||
| } | ||
| if (transfer_engine) { | ||
| builder.WithExistingTransferEngine(transfer_engine); | ||
| } | ||
|
|
||
| auto client_opt = builder.Build(); | ||
|
||
| if (!client_opt) { | ||
| LOG(ERROR) << "Failed to create client"; | ||
| return tl::unexpected(ErrorCode::INVALID_PARAMS); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
ClientBuildershould be a better name in the context, as we are infact building aClient, not aMooncakeStore.There was a problem hiding this comment.
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.