-
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 scheduler scheduling #31
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7b87f9c
Add creation time in job metadata
sitaowang1998 49c0ee3
Add clang format for tests
sitaowang1998 7e5eb95
Make SchedulerPolicy pure virtual
sitaowang1998 954416d
Fix bug in mysql table creation
sitaowang1998 2248eb1
Add fifo job scheduler
sitaowang1998 d826b4f
Add scheduler test
sitaowang1998 54e23a5
Fix scheduler
sitaowang1998 7e3dfb6
Fix clang tidy
sitaowang1998 cd01beb
Fix clang tidy
sitaowang1998 289a151
Fix clang tidy
sitaowang1998 d614d9b
Add error handle for mysql timestamp parsing
sitaowang1998 c10a764
Mark JobMetadata getter as const
sitaowang1998 93481f8
Add more check for scheduler test
sitaowang1998 7725428
Fix typo in comment
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef SPIDER_CORE_JOBMETADATA_HPP | ||
#define SPIDER_CORE_JOBMETADATA_HPP | ||
|
||
#include <chrono> | ||
|
||
#include <boost/uuid/uuid.hpp> | ||
|
||
namespace spider::core { | ||
|
||
class JobMetadata { | ||
public: | ||
JobMetadata() = default; | ||
|
||
JobMetadata( | ||
boost::uuids::uuid id, | ||
boost::uuids::uuid client_id, | ||
std::chrono::system_clock::time_point creation_time | ||
) | ||
: m_id{id}, | ||
m_client_id{client_id}, | ||
m_creation_time{creation_time} {} | ||
|
||
[[nodiscard]] auto get_id() -> boost::uuids::uuid { return m_id; } | ||
|
||
[[nodiscard]] auto get_client_id() -> boost::uuids::uuid { return m_client_id; } | ||
|
||
[[nodiscard]] auto get_creation_time() -> std::chrono::system_clock::time_point { | ||
return m_creation_time; | ||
} | ||
|
||
private: | ||
boost::uuids::uuid m_id; | ||
boost::uuids::uuid m_client_id; | ||
std::chrono::system_clock::time_point m_creation_time; | ||
}; | ||
|
||
} // namespace spider::core | ||
|
||
#endif // SPIDER_CORE_JOBMETADATA_HPP |
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,125 @@ | ||
#include "FifoPolicy.hpp" | ||
|
||
#include <algorithm> | ||
#include <chrono> | ||
#include <memory> | ||
#include <optional> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <absl/container/flat_hash_map.h> | ||
#include <boost/uuid/uuid.hpp> | ||
#include <boost/uuid/uuid_io.hpp> | ||
#include <fmt/format.h> | ||
|
||
#include "../core/Data.hpp" | ||
#include "../core/JobMetadata.hpp" | ||
#include "../core/Task.hpp" | ||
#include "../storage/DataStorage.hpp" | ||
#include "../storage/MetadataStorage.hpp" | ||
|
||
namespace { | ||
|
||
auto task_locality_satisfied( | ||
std::shared_ptr<spider::core::DataStorage> const& data_store, | ||
spider::core::Task const& task, | ||
std::string const& addr | ||
) -> bool { | ||
for (auto const& input : task.get_inputs()) { | ||
if (input.get_value().has_value()) { | ||
continue; | ||
} | ||
std::optional<boost::uuids::uuid> optional_data_id = input.get_data_id(); | ||
if (!optional_data_id.has_value()) { | ||
continue; | ||
} | ||
boost::uuids::uuid const data_id = optional_data_id.value(); | ||
spider::core::Data data; | ||
if (false == data_store->get_data(data_id, &data).success()) { | ||
throw std::runtime_error( | ||
fmt::format("Data with id {} not exists.", boost::uuids::to_string((data_id))) | ||
); | ||
} | ||
if (false == data.is_hard_locality()) { | ||
continue; | ||
} | ||
std::vector<std::string> const& locality = data.get_locality(); | ||
if (locality.empty()) { | ||
continue; | ||
} | ||
if (std::ranges::find(locality, addr) == locality.end()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} // namespace | ||
|
||
namespace spider::scheduler { | ||
|
||
auto FifoPolicy::schedule_next( | ||
std::shared_ptr<core::MetadataStorage> metadata_store, | ||
std::shared_ptr<core::DataStorage> data_store, | ||
boost::uuids::uuid const /*worker_id*/, | ||
std::string const& worker_addr | ||
) -> std::optional<boost::uuids::uuid> { | ||
std::vector<core::Task> ready_tasks; | ||
metadata_store->get_ready_tasks(&ready_tasks); | ||
|
||
std::erase_if(ready_tasks, [data_store, worker_addr](core::Task const& task) -> bool { | ||
return !task_locality_satisfied(data_store, task, worker_addr); | ||
}); | ||
|
||
if (ready_tasks.empty()) { | ||
return std::nullopt; | ||
} | ||
|
||
auto const earliest_task = std::ranges::min_element( | ||
ready_tasks, | ||
{}, | ||
[this, | ||
metadata_store](core::Task const& task) -> std::chrono::system_clock::time_point { | ||
boost::uuids::uuid const task_id = task.get_id(); | ||
boost::uuids::uuid job_id; | ||
if (m_task_job_map.contains(task_id)) { | ||
job_id = m_task_job_map[task_id]; | ||
} else { | ||
if (false == metadata_store->get_task_job_id(task_id, &job_id).success()) { | ||
throw std::runtime_error(fmt::format( | ||
"Task with id {} not exists.", | ||
boost::uuids::to_string(task_id) | ||
)); | ||
} | ||
m_task_job_map.emplace(task_id, job_id); | ||
} | ||
|
||
if (m_job_time_map.contains(job_id)) { | ||
return m_job_time_map[job_id]; | ||
} | ||
|
||
core::JobMetadata job_metadata; | ||
if (false == metadata_store->get_job_metadata(job_id, &job_metadata).success()) { | ||
throw std::runtime_error(fmt::format( | ||
"Job with id {} not exists.", | ||
boost::uuids::to_string(job_id) | ||
)); | ||
} | ||
m_job_time_map.emplace(job_id, job_metadata.get_creation_time()); | ||
return job_metadata.get_creation_time(); | ||
} | ||
); | ||
|
||
return earliest_task->get_id(); | ||
} | ||
|
||
auto FifoPolicy::cleanup_job(boost::uuids::uuid const job_id) -> void { | ||
absl::erase_if(m_task_job_map, [&job_id](auto const& item) -> bool { | ||
auto const& [item_task_id, item_job_id] = item; | ||
return item_job_id == job_id; | ||
}); | ||
m_job_time_map.erase(job_id); | ||
} | ||
|
||
} // namespace spider::scheduler |
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,35 @@ | ||
#ifndef SPIDER_SCHEDULER_FIFOPOLICY_HPP | ||
#define SPIDER_SCHEDULER_FIFOPOLICY_HPP | ||
|
||
#include <chrono> | ||
#include <memory> | ||
#include <optional> | ||
#include <string> | ||
|
||
#include <absl/container/flat_hash_map.h> | ||
#include <boost/uuid/uuid.hpp> | ||
|
||
#include "../storage/DataStorage.hpp" | ||
#include "../storage/MetadataStorage.hpp" | ||
#include "SchedulerPolicy.hpp" | ||
|
||
namespace spider::scheduler { | ||
|
||
class FifoPolicy final : public SchedulerPolicy { | ||
public: | ||
auto schedule_next( | ||
std::shared_ptr<core::MetadataStorage> metadata_store, | ||
std::shared_ptr<core::DataStorage> data_store, | ||
boost::uuids::uuid worker_id, | ||
std::string const& worker_addr | ||
) -> std::optional<boost::uuids::uuid> override; | ||
auto cleanup_job(boost::uuids::uuid job_id) -> void override; | ||
|
||
private: | ||
absl::flat_hash_map<boost::uuids::uuid, boost::uuids::uuid> m_task_job_map; | ||
absl::flat_hash_map<boost::uuids::uuid, std::chrono::system_clock::time_point> m_job_time_map; | ||
}; | ||
|
||
} // namespace spider::scheduler | ||
|
||
#endif // SPIDER_SCHEDULER_FIFOPOLICY_HPP |
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,35 @@ | ||
#ifndef SPIDER_SCHEDULER_SCHEDULERPOLICY_HPP | ||
#define SPIDER_SCHEDULER_SCHEDULERPOLICY_HPP | ||
|
||
#include <memory> | ||
#include <optional> | ||
#include <string> | ||
|
||
#include <boost/uuid/uuid.hpp> | ||
|
||
#include "../storage/DataStorage.hpp" | ||
#include "../storage/MetadataStorage.hpp" | ||
|
||
namespace spider::scheduler { | ||
class SchedulerPolicy { | ||
public: | ||
SchedulerPolicy() = default; | ||
SchedulerPolicy(SchedulerPolicy const&) = default; | ||
auto operator=(SchedulerPolicy const&) -> SchedulerPolicy& = default; | ||
SchedulerPolicy(SchedulerPolicy&&) = default; | ||
auto operator=(SchedulerPolicy&&) -> SchedulerPolicy& = default; | ||
virtual ~SchedulerPolicy() = default; | ||
|
||
virtual auto schedule_next( | ||
std::shared_ptr<core::MetadataStorage> metadata_store, | ||
std::shared_ptr<core::DataStorage> data_store, | ||
boost::uuids::uuid worker_id, | ||
std::string const& worker_addr | ||
) -> std::optional<boost::uuids::uuid> = 0; | ||
|
||
virtual auto cleanup_job(boost::uuids::uuid job_id) -> void = 0; | ||
}; | ||
|
||
} // namespace spider::scheduler | ||
|
||
#endif // SPIDER_SCHEDULER_SCHEDULERPOLICY_HPP |
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,2 @@ | ||
|
||
auto main(int argc, char** argv) -> int {} |
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
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
Mark getter methods as
const
Marking the getter methods as
const
ensures they do not modify the object's state and conveys the intended usage.Apply this diff to make the methods
const
:After modification: