-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: Add client driver implementation #42
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9ca2b7d
Add function ptr to name map in function manager
sitaowang1998 338c624
Add basic driver impl
sitaowang1998 ba34a8b
Add task binding
sitaowang1998 d166536
Add tests for driver and fix some bugs
sitaowang1998 9559e0c
Fix function pointer problem
sitaowang1998 b0467b5
Fix task graph template deduction
sitaowang1998 24142d8
Fix tests and task function matching
sitaowang1998 ad450a6
Bump boost version try to solve github action install crash
sitaowang1998 5486f02
Use boost official url for dependency
sitaowang1998 1a0358d
Add more print to debug github workflow
sitaowang1998 cbe9644
Remove z flag for tar
sitaowang1998 fd43074
Try fix the tar flags
sitaowang1998 0e418a3
Use github for boost download instead of packaged tarball
sitaowang1998 aa806dd
Bug fix
sitaowang1998 60a486b
Pull submodule for boost
sitaowang1998 e2ac3e0
Revert boost version back to 1.86.0
sitaowang1998 8d4f2b5
Fix test storage tag
sitaowang1998 58e85bd
Fix clang tidy
sitaowang1998 1769a95
Fix clang tidy
sitaowang1998 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 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 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 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,121 @@ | ||
#include "Driver.hpp" | ||
|
||
#include <chrono> | ||
#include <memory> | ||
#include <optional> | ||
#include <stop_token> | ||
#include <string> | ||
#include <thread> | ||
|
||
#include <boost/uuid/random_generator.hpp> | ||
#include <boost/uuid/uuid.hpp> | ||
|
||
#include "../core/Driver.hpp" | ||
#include "../core/Error.hpp" | ||
#include "../core/KeyValueData.hpp" | ||
#include "../io/BoostAsio.hpp" // IWYU pragma: keep | ||
#include "../storage/MysqlStorage.hpp" | ||
#include "Exception.hpp" | ||
|
||
namespace spider { | ||
|
||
Driver::Driver(std::string const& storage_url) { | ||
boost::uuids::random_generator gen; | ||
m_id = gen(); | ||
|
||
m_metadata_storage = std::make_shared<core::MySqlMetadataStorage>(); | ||
m_data_storage = std::make_shared<core::MySqlDataStorage>(); | ||
core::StorageErr err = m_metadata_storage->connect(storage_url); | ||
if (!err.success()) { | ||
throw ConnectionException(err.description); | ||
} | ||
err = m_data_storage->connect(storage_url); | ||
if (!err.success()) { | ||
throw ConnectionException(err.description); | ||
} | ||
|
||
std::optional<std::string> const optional_addr = core::get_address(); | ||
if (!optional_addr.has_value()) { | ||
throw ConnectionException("Cannot get machine address"); | ||
} | ||
std::string const& addr = optional_addr.value(); | ||
err = m_metadata_storage->add_driver(core::Driver{m_id, addr}); | ||
if (!err.success()) { | ||
if (core::StorageErrType::DuplicateKeyErr == err.type) { | ||
throw DriverIdInUseException(m_id); | ||
} | ||
throw ConnectionException(err.description); | ||
} | ||
|
||
// Start a thread to send heartbeats | ||
// NOLINTNEXTLINE(performance-unnecessary-value-param) | ||
m_heartbeat_thread = std::jthread([this](std::stop_token stoken) { | ||
while (!stoken.stop_requested()) { | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
core::StorageErr const err = m_metadata_storage->update_heartbeat(m_id); | ||
if (!err.success()) { | ||
throw ConnectionException(err.description); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
Driver::Driver(std::string const& storage_url, boost::uuids::uuid const id) : m_id{id} { | ||
m_metadata_storage = std::make_shared<core::MySqlMetadataStorage>(); | ||
m_data_storage = std::make_shared<core::MySqlDataStorage>(); | ||
core::StorageErr err = m_metadata_storage->connect(storage_url); | ||
if (!err.success()) { | ||
throw ConnectionException(err.description); | ||
} | ||
err = m_data_storage->connect(storage_url); | ||
if (!err.success()) { | ||
throw ConnectionException(err.description); | ||
} | ||
|
||
std::optional<std::string> const optional_addr = core::get_address(); | ||
if (!optional_addr.has_value()) { | ||
throw ConnectionException("Cannot get machine address"); | ||
} | ||
std::string const& addr = optional_addr.value(); | ||
err = m_metadata_storage->add_driver(core::Driver{m_id, addr}); | ||
if (!err.success()) { | ||
if (core::StorageErrType::DuplicateKeyErr == err.type) { | ||
throw DriverIdInUseException(m_id); | ||
} | ||
throw ConnectionException(err.description); | ||
} | ||
|
||
// Start a thread to send heartbeats | ||
// NOLINTNEXTLINE(performance-unnecessary-value-param) | ||
m_heartbeat_thread = std::jthread([this](std::stop_token stoken) { | ||
while (!stoken.stop_requested()) { | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
core::StorageErr const err = m_metadata_storage->update_heartbeat(m_id); | ||
if (!err.success()) { | ||
throw ConnectionException(err.description); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
auto Driver::kv_store_insert(std::string const& key, std::string const& value) -> void { | ||
core::KeyValueData const kv_data{key, value, m_id}; | ||
core::StorageErr const err = m_data_storage->add_client_kv_data(kv_data); | ||
if (!err.success()) { | ||
throw ConnectionException(err.description); | ||
} | ||
} | ||
|
||
auto Driver::kv_store_get(std::string const& key) -> std::optional<std::string> { | ||
std::string value; | ||
core::StorageErr const err = m_data_storage->get_client_kv_data(m_id, key, &value); | ||
if (!err.success()) { | ||
if (core::StorageErrType::KeyNotFoundErr == err.type) { | ||
return std::nullopt; | ||
} | ||
throw ConnectionException(err.description); | ||
} | ||
return value; | ||
} | ||
|
||
} // namespace spider |
This file contains 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 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 |
---|---|---|
@@ -1,17 +1,38 @@ | ||
#ifndef SPIDER_CLIENT_TASKGRAPH_HPP | ||
#define SPIDER_CLIENT_TASKGRAPH_HPP | ||
|
||
#include <memory> | ||
#include <utility> | ||
|
||
#include "task.hpp" | ||
|
||
namespace spider { | ||
namespace core { | ||
class TaskGraphImpl; | ||
} // namespace core | ||
|
||
class Driver; | ||
class TaskContext; | ||
|
||
/** | ||
* A TaskGraph represents a directed acyclic graph (DAG) of tasks. | ||
* | ||
* @tparam ReturnType | ||
* @tparam Params | ||
*/ | ||
template <TaskIo ReturnType, TaskIo... Params> | ||
class TaskGraph {}; | ||
class TaskGraph { | ||
private: | ||
explicit TaskGraph(std::unique_ptr<core::TaskGraphImpl> impl) : m_impl{std::move(impl)} {} | ||
|
||
[[nodiscard]] auto get_impl() const -> core::TaskGraphImpl const& { return *m_impl; } | ||
|
||
std::unique_ptr<core::TaskGraphImpl> m_impl; | ||
|
||
friend class core::TaskGraphImpl; | ||
friend class Driver; | ||
friend class TaskContext; | ||
}; | ||
} // namespace spider | ||
|
||
#endif // SPIDER_CLIENT_TASKGRAPH_HPP |
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.
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.
🛠️ Refactor suggestion
Heartbeat thread usage
Starting a continuous heartbeat in the constructor is helpful, but confirm that the destructor or move logic properly stops and joins the thread to prevent resource leaks.