2022-01-31 13:56:44 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2022-01-28 13:50:59 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/algorithm.h"
|
2022-01-31 13:56:44 +01:00
|
|
|
#include "td/utils/common.h"
|
2022-01-28 13:50:59 +01:00
|
|
|
#include "td/utils/Container.h"
|
|
|
|
#include "td/utils/List.h"
|
2022-02-01 16:25:02 +01:00
|
|
|
#include "td/utils/logging.h"
|
2022-01-28 13:50:59 +01:00
|
|
|
#include "td/utils/optional.h"
|
|
|
|
#include "td/utils/Span.h"
|
2022-01-31 13:56:44 +01:00
|
|
|
#include "td/utils/StringBuilder.h"
|
2022-01-28 13:50:59 +01:00
|
|
|
#include "td/utils/VectorQueue.h"
|
|
|
|
|
2022-01-31 13:56:44 +01:00
|
|
|
#include <functional>
|
2022-01-28 13:50:59 +01:00
|
|
|
#include <map>
|
2022-02-01 15:05:23 +01:00
|
|
|
#include <set>
|
2022-01-28 13:50:59 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
struct ChainSchedulerBase {
|
|
|
|
struct TaskWithParents {
|
|
|
|
uint64 task_id{};
|
|
|
|
vector<uint64> parents;
|
|
|
|
};
|
2022-01-28 17:12:20 +01:00
|
|
|
};
|
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT = Unit>
|
2022-02-01 16:25:02 +01:00
|
|
|
class ChainScheduler final : public ChainSchedulerBase {
|
2022-01-28 13:50:59 +01:00
|
|
|
public:
|
|
|
|
using TaskId = uint64;
|
|
|
|
using ChainId = uint64;
|
2022-01-31 13:56:44 +01:00
|
|
|
TaskId create_task(Span<ChainId> chains, ExtraT extra = {});
|
2022-01-28 13:50:59 +01:00
|
|
|
ExtraT *get_task_extra(TaskId task_id);
|
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
optional<TaskWithParents> start_next_task();
|
2022-02-01 14:01:17 +01:00
|
|
|
void pause_task(TaskId task_id);
|
2022-01-28 13:50:59 +01:00
|
|
|
void finish_task(TaskId task_id);
|
|
|
|
void reset_task(TaskId task_id);
|
|
|
|
template <class ExtraTT>
|
2022-01-28 17:12:20 +01:00
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, ChainScheduler<ExtraTT> &scheduler);
|
2022-01-28 13:50:59 +01:00
|
|
|
|
2022-01-28 15:43:22 +01:00
|
|
|
template <class F>
|
|
|
|
void for_each(F &&f) {
|
2022-01-31 13:56:44 +01:00
|
|
|
tasks_.for_each([&f](auto, Task &task) { f(task.extra); });
|
2022-01-28 15:43:22 +01:00
|
|
|
}
|
2022-02-01 14:01:17 +01:00
|
|
|
template <class F>
|
|
|
|
void for_each_dependent(TaskId task_id, F &&f) {
|
|
|
|
auto *task = tasks_.get(task_id);
|
|
|
|
CHECK(task);
|
|
|
|
std::set<TaskId> visited;
|
|
|
|
bool check_for_collisions = task->chains.size() > 1;
|
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
|
|
|
ChainInfo &chain_info = *task_chain_info.chain_info;
|
|
|
|
chain_info.chain.foreach_child(&task_chain_info.chain_node, [&](auto task_id, auto) {
|
|
|
|
if (check_for_collisions) {
|
|
|
|
auto it_ok = visited.insert(task_id);
|
|
|
|
if (!it_ok.second) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f(task_id);
|
|
|
|
});
|
|
|
|
auto o_child = chain_info.chain.get_child(&task_chain_info.chain_node);
|
|
|
|
if (o_child) {
|
|
|
|
f(o_child.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 15:43:22 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
private:
|
|
|
|
struct ChainNode : ListNode {
|
|
|
|
TaskId task_id{};
|
2022-02-01 09:13:59 +01:00
|
|
|
uint64 generation{};
|
2022-01-28 13:50:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class Chain {
|
|
|
|
public:
|
|
|
|
void add_task(ChainNode *node) {
|
|
|
|
head_.put_back(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<TaskId> get_first() {
|
|
|
|
if (head_.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return static_cast<ChainNode &>(*head_.get_next()).task_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<TaskId> get_child(ChainNode *chain_node) {
|
|
|
|
if (chain_node->get_next() == head_.end()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return static_cast<ChainNode &>(*chain_node->get_next()).task_id;
|
|
|
|
}
|
2022-02-01 16:25:02 +01:00
|
|
|
optional<ChainNode *> get_parent(ChainNode *chain_node) {
|
2022-01-28 13:50:59 +01:00
|
|
|
if (chain_node->get_prev() == head_.end()) {
|
|
|
|
return {};
|
|
|
|
}
|
2022-02-01 09:13:59 +01:00
|
|
|
return static_cast<ChainNode *>(chain_node->get_prev());
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void finish_task(ChainNode *node) {
|
|
|
|
node->remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return head_.empty();
|
|
|
|
}
|
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
void foreach(std::function<void(TaskId, uint64)> f) const {
|
2022-01-28 13:50:59 +01:00
|
|
|
for (auto it = head_.begin(); it != head_.end(); it = it->get_next()) {
|
2022-02-01 09:13:59 +01:00
|
|
|
auto &node = static_cast<const ChainNode &>(*it);
|
|
|
|
f(node.task_id, node.generation);
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-01 16:25:02 +01:00
|
|
|
void foreach_child(ListNode *start_node, std::function<void(TaskId, uint64)> f) const {
|
|
|
|
for (auto it = start_node; it != head_.end(); it = it->get_next()) {
|
2022-02-01 14:01:17 +01:00
|
|
|
auto &node = static_cast<const ChainNode &>(*it);
|
|
|
|
f(node.task_id, node.generation);
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 13:50:59 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
ListNode head_;
|
|
|
|
};
|
|
|
|
struct ChainInfo {
|
|
|
|
Chain chain;
|
|
|
|
uint32 active_tasks{};
|
2022-02-01 09:13:59 +01:00
|
|
|
uint64 generation{1};
|
2022-01-28 13:50:59 +01:00
|
|
|
};
|
|
|
|
struct TaskChainInfo {
|
|
|
|
ChainNode chain_node;
|
|
|
|
ChainId chain_id{};
|
|
|
|
ChainInfo *chain_info{};
|
|
|
|
};
|
|
|
|
struct Task {
|
2022-02-01 14:01:17 +01:00
|
|
|
enum class State { Pending, Active, Paused } state{State::Pending};
|
2022-01-31 13:56:44 +01:00
|
|
|
vector<TaskChainInfo> chains;
|
2022-01-28 13:50:59 +01:00
|
|
|
ExtraT extra;
|
|
|
|
};
|
|
|
|
std::map<ChainId, ChainInfo> chains_;
|
|
|
|
std::map<ChainId, TaskId> limited_tasks_;
|
|
|
|
Container<Task> tasks_;
|
|
|
|
VectorQueue<TaskId> pending_tasks_;
|
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
void try_start_task(TaskId task_id) {
|
|
|
|
auto *task = tasks_.get(task_id);
|
|
|
|
CHECK(task);
|
2022-01-28 13:50:59 +01:00
|
|
|
if (task->state != Task::State::Pending) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
2022-02-01 09:13:59 +01:00
|
|
|
auto o_parent = task_chain_info.chain_info->chain.get_parent(&task_chain_info.chain_node);
|
|
|
|
|
|
|
|
if (o_parent) {
|
|
|
|
if (o_parent.value()->generation != task_chain_info.chain_info->generation) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
2022-02-01 09:13:59 +01:00
|
|
|
|
|
|
|
if (task_chain_info.chain_info->active_tasks >= 10) {
|
2022-01-28 13:50:59 +01:00
|
|
|
limited_tasks_[task_chain_info.chain_id] = task_id;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_start_task(task_id, task);
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_start_task(TaskId task_id, Task *task) {
|
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
|
|
|
ChainInfo &chain_info = chains_[task_chain_info.chain_id];
|
|
|
|
chain_info.active_tasks++;
|
2022-02-01 09:13:59 +01:00
|
|
|
task_chain_info.chain_node.generation = chain_info.generation;
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
task->state = Task::State::Active;
|
|
|
|
|
|
|
|
pending_tasks_.push(task_id);
|
2022-02-01 16:25:02 +01:00
|
|
|
for_each_child(task, [&](auto task_id) { try_start_task(task_id); });
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
template <class F>
|
|
|
|
void for_each_child(Task *task, F &&f) {
|
2022-01-28 13:50:59 +01:00
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
2022-02-01 09:13:59 +01:00
|
|
|
ChainInfo &chain_info = *task_chain_info.chain_info;
|
2022-01-28 13:50:59 +01:00
|
|
|
auto o_child = chain_info.chain.get_child(&task_chain_info.chain_node);
|
|
|
|
if (o_child) {
|
2022-02-01 09:13:59 +01:00
|
|
|
f(o_child.value());
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
void inactivate_task(TaskId task_id, bool failed) {
|
2022-02-01 16:25:02 +01:00
|
|
|
LOG(DEBUG) << "Inactivate " << task_id << " " << (failed ? "failed" : "finished");
|
2022-02-01 14:01:17 +01:00
|
|
|
auto *task = tasks_.get(task_id);
|
|
|
|
CHECK(task);
|
2022-02-01 09:13:59 +01:00
|
|
|
bool was_active = task->state == Task::State::Active;
|
2022-01-28 13:50:59 +01:00
|
|
|
task->state = Task::State::Pending;
|
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
2022-02-01 09:13:59 +01:00
|
|
|
ChainInfo &chain_info = *task_chain_info.chain_info;
|
|
|
|
if (was_active) {
|
|
|
|
chain_info.active_tasks--;
|
|
|
|
}
|
2022-02-01 14:01:17 +01:00
|
|
|
if (was_active && failed) {
|
|
|
|
chain_info.generation = std::max(chain_info.generation, task_chain_info.chain_node.generation + 1);
|
|
|
|
}
|
2022-01-28 13:50:59 +01:00
|
|
|
|
|
|
|
auto it = limited_tasks_.find(task_chain_info.chain_id);
|
|
|
|
if (it != limited_tasks_.end()) {
|
|
|
|
auto limited_task_id = it->second;
|
|
|
|
limited_tasks_.erase(it);
|
|
|
|
if (limited_task_id != task_id) {
|
2022-02-01 09:13:59 +01:00
|
|
|
try_start_task_later(limited_task_id);
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-01 14:01:17 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
auto o_first = chain_info.chain.get_first();
|
|
|
|
if (o_first) {
|
|
|
|
auto first_task_id = o_first.unwrap();
|
|
|
|
if (first_task_id != task_id) {
|
2022-02-01 09:13:59 +01:00
|
|
|
try_start_task_later(first_task_id);
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void finish_chain_task(TaskChainInfo &task_chain_info) {
|
|
|
|
auto &chain = task_chain_info.chain_info->chain;
|
|
|
|
chain.finish_task(&task_chain_info.chain_node);
|
|
|
|
if (chain.empty()) {
|
|
|
|
chains_.erase(task_chain_info.chain_id);
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 09:13:59 +01:00
|
|
|
|
2022-02-01 16:25:02 +01:00
|
|
|
vector<TaskId> to_start_;
|
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
void try_start_task_later(TaskId task_id) {
|
2022-02-01 16:25:02 +01:00
|
|
|
LOG(DEBUG) << "Try to start later " << task_id;
|
|
|
|
to_start_.push_back(task_id);
|
2022-02-01 09:13:59 +01:00
|
|
|
}
|
2022-02-01 16:25:02 +01:00
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
void flush_try_start_task() {
|
2022-02-01 16:25:02 +01:00
|
|
|
auto moved_to_start = std::move(to_start_);
|
2022-02-01 09:13:59 +01:00
|
|
|
for (auto task_id : moved_to_start) {
|
2022-02-01 14:01:17 +01:00
|
|
|
try_start_task(task_id);
|
2022-02-01 09:13:59 +01:00
|
|
|
}
|
2022-02-01 16:25:02 +01:00
|
|
|
CHECK(to_start_.empty());
|
2022-02-01 09:13:59 +01:00
|
|
|
}
|
2022-01-28 13:50:59 +01:00
|
|
|
};
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
2022-01-31 13:56:44 +01:00
|
|
|
typename ChainScheduler<ExtraT>::TaskId ChainScheduler<ExtraT>::create_task(Span<ChainScheduler::ChainId> chains,
|
|
|
|
ExtraT extra) {
|
2022-01-28 13:50:59 +01:00
|
|
|
auto task_id = tasks_.create();
|
|
|
|
Task &task = *tasks_.get(task_id);
|
|
|
|
task.extra = std::move(extra);
|
|
|
|
task.chains = transform(chains, [&](auto chain_id) {
|
|
|
|
TaskChainInfo task_chain_info;
|
|
|
|
ChainInfo &chain_info = chains_[chain_id];
|
|
|
|
task_chain_info.chain_id = chain_id;
|
|
|
|
task_chain_info.chain_info = &chain_info;
|
|
|
|
task_chain_info.chain_node.task_id = task_id;
|
2022-02-01 14:01:17 +01:00
|
|
|
task_chain_info.chain_node.generation = 0;
|
2022-01-28 13:50:59 +01:00
|
|
|
return task_chain_info;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (TaskChainInfo &task_chain_info : task.chains) {
|
2022-02-01 09:13:59 +01:00
|
|
|
ChainInfo &chain_info = *task_chain_info.chain_info;
|
|
|
|
chain_info.chain.add_task(&task_chain_info.chain_node);
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
try_start_task(task_id);
|
2022-01-28 13:50:59 +01:00
|
|
|
return task_id;
|
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
// TODO: return reference
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
|
|
|
ExtraT *ChainScheduler<ExtraT>::get_task_extra(ChainScheduler::TaskId task_id) { // may return nullptr
|
|
|
|
auto *task = tasks_.get(task_id);
|
|
|
|
if (!task) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &task->extra;
|
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
2022-02-01 09:13:59 +01:00
|
|
|
optional<ChainSchedulerBase::TaskWithParents> ChainScheduler<ExtraT>::start_next_task() {
|
2022-01-28 13:50:59 +01:00
|
|
|
if (pending_tasks_.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto task_id = pending_tasks_.pop();
|
2022-02-01 09:13:59 +01:00
|
|
|
TaskWithParents res;
|
2022-01-28 13:50:59 +01:00
|
|
|
res.task_id = task_id;
|
|
|
|
auto *task = tasks_.get(task_id);
|
|
|
|
CHECK(task);
|
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
|
|
|
Chain &chain = task_chain_info.chain_info->chain;
|
|
|
|
auto o_parent = chain.get_parent(&task_chain_info.chain_node);
|
|
|
|
if (o_parent) {
|
2022-02-01 09:13:59 +01:00
|
|
|
res.parents.push_back(o_parent.value()->task_id);
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
|
|
|
void ChainScheduler<ExtraT>::finish_task(ChainScheduler::TaskId task_id) {
|
|
|
|
auto *task = tasks_.get(task_id);
|
|
|
|
CHECK(task);
|
2022-02-01 16:25:02 +01:00
|
|
|
CHECK(to_start_.empty());
|
2022-02-01 14:01:17 +01:00
|
|
|
|
|
|
|
inactivate_task(task_id, false);
|
|
|
|
|
2022-02-01 16:25:02 +01:00
|
|
|
for_each_child(task, [&](auto task_id) { try_start_task_later(task_id); });
|
2022-01-28 13:50:59 +01:00
|
|
|
|
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
|
|
|
finish_chain_task(task_chain_info);
|
|
|
|
}
|
2022-02-01 09:13:59 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
tasks_.erase(task_id);
|
2022-02-01 09:13:59 +01:00
|
|
|
flush_try_start_task();
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
|
|
|
void ChainScheduler<ExtraT>::reset_task(ChainScheduler::TaskId task_id) {
|
2022-02-01 16:25:02 +01:00
|
|
|
CHECK(to_start_.empty());
|
2022-01-28 13:50:59 +01:00
|
|
|
auto *task = tasks_.get(task_id);
|
|
|
|
CHECK(task);
|
2022-02-01 14:01:17 +01:00
|
|
|
inactivate_task(task_id, true);
|
2022-02-01 09:13:59 +01:00
|
|
|
try_start_task_later(task_id);
|
|
|
|
flush_try_start_task();
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
template <class ExtraT>
|
|
|
|
void ChainScheduler<ExtraT>::pause_task(TaskId task_id) {
|
|
|
|
auto *task = tasks_.get(task_id);
|
|
|
|
CHECK(task);
|
|
|
|
inactivate_task(task_id, true);
|
|
|
|
task->state = Task::State::Paused;
|
2022-02-01 15:05:23 +01:00
|
|
|
flush_try_start_task();
|
2022-02-01 14:01:17 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
2022-01-28 17:12:20 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &sb, ChainScheduler<ExtraT> &scheduler) {
|
2022-01-28 13:50:59 +01:00
|
|
|
// 1 print chains
|
|
|
|
sb << "\n";
|
|
|
|
for (auto &it : scheduler.chains_) {
|
|
|
|
sb << "ChainId{" << it.first << "} ";
|
|
|
|
sb << " active_cnt=" << it.second.active_tasks;
|
2022-02-01 09:13:59 +01:00
|
|
|
sb << " g=" << it.second.generation;
|
|
|
|
sb << " :";
|
2022-02-01 16:25:02 +01:00
|
|
|
it.second.chain.foreach(
|
|
|
|
[&](auto task_id, auto generation) { sb << " " << *scheduler.get_task_extra(task_id) << ":" << generation; });
|
2022-01-28 13:50:59 +01:00
|
|
|
sb << "\n";
|
|
|
|
}
|
|
|
|
scheduler.tasks_.for_each([&](auto id, auto &task) {
|
|
|
|
sb << "Task: " << task.extra;
|
|
|
|
sb << " state =" << static_cast<int>(task.state);
|
2022-02-01 16:25:02 +01:00
|
|
|
for (auto &task_chain_info : task.chains) {
|
2022-02-01 09:13:59 +01:00
|
|
|
sb << " g=" << task_chain_info.chain_node.generation;
|
|
|
|
if (task_chain_info.chain_info->generation != task_chain_info.chain_node.generation) {
|
|
|
|
sb << " chain_g=" << task_chain_info.chain_info->generation;
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sb << "\n";
|
|
|
|
});
|
|
|
|
return sb;
|
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
} // namespace td
|