-
Notifications
You must be signed in to change notification settings - Fork 228
Initial implementation of new task system #7269
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
Merged
Changes from 27 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
e1bf9a2
Initial commit of new task system + tests
eddyashton a80f015
Add some more basic unit tests
eddyashton cc68420
Merge branch 'main' of github.com:microsoft/CCF into task_system_impl
eddyashton 158b0c7
Add benchmark of contended enqueue/dequeue
eddyashton ddbdbab
Merge branch 'main' of github.com:microsoft/CCF into task_system_impl
eddyashton 8dc97d0
Oh. This sucks
eddyashton 0d12ea6
More
eddyashton b0f386b
This does not mean what you think it means
eddyashton 50148e1
Unit test fix
eddyashton c20b2eb
Remove unhelpful unit test
eddyashton c77fe58
Merge branch 'main' of github.com:microsoft/CCF into task_system_impl
eddyashton 2ab05d3
Uhh
eddyashton 3f80bdb
Add a FanInTasks container, to maintain keyed-order-execution of some…
eddyashton 7658671
Merge branch 'main' of github.com:microsoft/CCF into task_system_impl
eddyashton d1802e8
Final test
eddyashton 60a7916
Tidy
eddyashton 115a1fd
Comments to explain test
eddyashton bf56944
TO REVERT: Verbose logging, force repro loop
eddyashton 361bc6f
TO REVERT: Less aggressive yml changes, still verbose
eddyashton 1d4b209
Revert previous debugging commits
eddyashton 5e32d9f
At least nicer error message when this fails?
eddyashton 365b248
Use deterministic time to simulate worker thread in unit test
eddyashton 3e0fc19
Neater still
eddyashton a5a07c2
Verbose logging, just in case
eddyashton ae6ef24
Something subtle is broken with spans - fine, use owning vector
eddyashton 1122163
Merge branch 'main' of github.com:microsoft/CCF into task_system_impl
eddyashton fb9fa4f
Pffft
eddyashton b682ba7
Return string_view from get_name
eddyashton 6107b1a
Longer names in core task system
eddyashton ebd9581
Cleaner pattern to enforce shared ptr ownership
eddyashton 62f16d3
Renames and mutex comments
eddyashton 3c5424a
Merge branch 'main' of github.com:microsoft/CCF into task_system_impl
eddyashton 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
#pragma once | ||
|
||
#include "tasks/task.h" | ||
|
||
namespace ccf::tasks | ||
{ | ||
struct BasicTask : public BaseTask | ||
{ | ||
using Fn = std::function<void()>; | ||
|
||
Fn fn; | ||
const std::string name; | ||
|
||
BasicTask(const Fn& _fn, const std::string& s = "[Anon]") : fn(_fn), name(s) | ||
{} | ||
|
||
void do_task_implementation() override | ||
{ | ||
fn(); | ||
} | ||
|
||
std::string get_name() const override | ||
{ | ||
return name; | ||
} | ||
}; | ||
|
||
template <typename... Ts> | ||
Task make_basic_task(Ts&&... ts) | ||
{ | ||
return std::make_shared<BasicTask>(std::forward<Ts>(ts)...); | ||
} | ||
} |
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,138 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
|
||
#include "tasks/fan_in_tasks.h" | ||
|
||
#include <map> | ||
#include <mutex> | ||
#include <stdexcept> | ||
|
||
#define FMT_HEADER_ONLY | ||
#include <fmt/format.h> | ||
|
||
namespace ccf::tasks | ||
{ | ||
struct FanInTasks::PImpl | ||
{ | ||
std::string name; | ||
IJobBoard& job_board; | ||
|
||
std::mutex mutex; | ||
std::map<size_t, Task> pending_tasks; | ||
size_t next_expected_task_index = 0; | ||
std::atomic<bool> active = false; | ||
}; | ||
|
||
void FanInTasks::enqueue_on_board() | ||
{ | ||
pimpl->job_board.add_task(shared_from_this()); | ||
} | ||
|
||
void FanInTasks::do_task_implementation() | ||
{ | ||
std::vector<Task> current_batch; | ||
|
||
{ | ||
std::lock_guard<std::mutex> lock(pimpl->mutex); | ||
pimpl->active.store(true); | ||
|
||
auto it = pimpl->pending_tasks.find(pimpl->next_expected_task_index); | ||
while (it != pimpl->pending_tasks.end()) | ||
{ | ||
current_batch.push_back(it->second); | ||
pimpl->pending_tasks.erase(it); | ||
|
||
++pimpl->next_expected_task_index; | ||
it = pimpl->pending_tasks.find(pimpl->next_expected_task_index); | ||
} | ||
} | ||
|
||
for (auto& task : current_batch) | ||
{ | ||
task->do_task(); | ||
} | ||
|
||
{ | ||
std::lock_guard<std::mutex> lock(pimpl->mutex); | ||
pimpl->active.store(false); | ||
|
||
auto it = pimpl->pending_tasks.find(pimpl->next_expected_task_index); | ||
if (it != pimpl->pending_tasks.end()) | ||
{ | ||
// While we were executing the previous batch, a call to fan_in_task | ||
// provided the _next_ contiguous task. We're now responsible for | ||
// re-enqueuing this task | ||
enqueue_on_board(); | ||
} | ||
} | ||
} | ||
|
||
FanInTasks::FanInTasks(IJobBoard& jb, const std::string& s) : | ||
pimpl(std::make_unique<FanInTasks::PImpl>(s, jb)) | ||
{} | ||
|
||
FanInTasks::~FanInTasks() = default; | ||
|
||
std::string FanInTasks::get_name() const | ||
eddyashton marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
return pimpl->name; | ||
} | ||
|
||
void FanInTasks::add_task(size_t task_index, Task task) | ||
{ | ||
{ | ||
std::lock_guard<std::mutex> lock(pimpl->mutex); | ||
|
||
if (task_index < pimpl->next_expected_task_index) | ||
{ | ||
throw std::runtime_error(fmt::format( | ||
"[{}] Received task {} ({}) out-of-order - already advanced next " | ||
"expected " | ||
"to {}", | ||
get_name(), | ||
task_index, | ||
task->get_name(), | ||
pimpl->next_expected_task_index)); | ||
} | ||
|
||
auto it = pimpl->pending_tasks.find(task_index); | ||
if (it != pimpl->pending_tasks.end()) | ||
{ | ||
throw std::runtime_error(fmt::format( | ||
"[{}] Received duplicate task {} ({}) - already have pending task {}", | ||
get_name(), | ||
task_index, | ||
task->get_name(), | ||
it->second == nullptr ? std::string("nullptr") : | ||
it->second->get_name())); | ||
} | ||
|
||
pimpl->pending_tasks.emplace(task_index, task); | ||
|
||
if (!pimpl->active.load()) | ||
{ | ||
if (task_index == pimpl->next_expected_task_index) | ||
{ | ||
enqueue_on_board(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
namespace | ||
{ | ||
struct ConcreteFanInTasks : public FanInTasks | ||
{ | ||
public: | ||
ConcreteFanInTasks(IJobBoard& jb, const std::string& s) : | ||
FanInTasks(jb, s) | ||
{} | ||
}; | ||
} | ||
|
||
std::shared_ptr<FanInTasks> make_fan_in_tasks( | ||
IJobBoard& jb, const std::string& s) | ||
{ | ||
return std::make_shared<ConcreteFanInTasks>(jb, s); | ||
} | ||
} |
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,37 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
#pragma once | ||
|
||
#include "tasks/job_board_interface.h" | ||
#include "tasks/task.h" | ||
|
||
#include <memory> | ||
|
||
namespace ccf::tasks | ||
{ | ||
class FanInTasks : public BaseTask, | ||
public std::enable_shared_from_this<FanInTasks> | ||
{ | ||
protected: | ||
struct PImpl; | ||
std::unique_ptr<PImpl> pimpl = nullptr; | ||
|
||
void enqueue_on_board(); | ||
void do_task_implementation() override; | ||
|
||
// Constructor is protected, to ensure this is only created via the | ||
// make_fan_in_tasks factory function (ensuring this is always owned by | ||
// a shared_ptr) | ||
FanInTasks(IJobBoard& jb, const std::string& s); | ||
|
||
public: | ||
~FanInTasks(); | ||
|
||
std::string get_name() const override; | ||
|
||
void add_task(size_t task_index, Task task); | ||
}; | ||
|
||
std::shared_ptr<FanInTasks> make_fan_in_tasks( | ||
IJobBoard& jb, const std::string& s = "[FanIn]"); | ||
} |
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,53 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
#include "tasks/job_board.h" | ||
|
||
namespace ccf::tasks | ||
{ | ||
void JobBoard::add_task(Task&& t) | ||
{ | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex); | ||
queue.emplace(std::move(t)); | ||
} | ||
work_beacon.notify_work_available(); | ||
} | ||
|
||
Task JobBoard::get_task() | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex); | ||
if (queue.empty()) | ||
{ | ||
return nullptr; | ||
} | ||
|
||
Task t = queue.front(); | ||
queue.pop(); | ||
return t; | ||
} | ||
|
||
bool JobBoard::empty() | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex); | ||
return queue.empty(); | ||
} | ||
|
||
Task JobBoard::wait_for_task(const std::chrono::milliseconds& timeout) | ||
{ | ||
using TClock = std::chrono::system_clock; | ||
|
||
const auto start = TClock::now(); | ||
const auto until = start + timeout; | ||
|
||
while (true) | ||
{ | ||
auto task = get_task(); | ||
if (task != nullptr || TClock::now() >= until) | ||
{ | ||
return task; | ||
} | ||
|
||
work_beacon.wait_for_work_with_timeout(timeout); | ||
} | ||
} | ||
} |
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,25 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
#pragma once | ||
|
||
#include "ds/work_beacon.h" | ||
#include "tasks/job_board_interface.h" | ||
|
||
#include <mutex> | ||
#include <queue> | ||
|
||
namespace ccf::tasks | ||
{ | ||
struct JobBoard : public IJobBoard | ||
{ | ||
std::mutex mutex; | ||
std::queue<Task> queue; | ||
ccf::ds::WorkBeacon work_beacon; | ||
|
||
void add_task(Task&& t) override; | ||
Task get_task() override; | ||
bool empty() override; | ||
|
||
Task wait_for_task(const std::chrono::milliseconds& timeout) override; | ||
}; | ||
} |
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,21 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
#pragma once | ||
|
||
#include "tasks/task.h" | ||
|
||
#include <mutex> | ||
#include <queue> | ||
#include <thread> | ||
|
||
namespace ccf::tasks | ||
{ | ||
struct IJobBoard | ||
{ | ||
virtual void add_task(Task&& t) = 0; | ||
virtual Task get_task() = 0; | ||
achamayou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
virtual bool empty() = 0; | ||
|
||
virtual Task wait_for_task(const std::chrono::milliseconds& timeout) = 0; | ||
}; | ||
} |
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.