2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/telegram/files/FileLoaderActor.h"
|
|
|
|
#include "td/telegram/files/ResourceState.h"
|
|
|
|
|
|
|
|
#include "td/utils/Container.h"
|
|
|
|
#include "td/utils/Heap.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2020-10-05 17:07:23 +02:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class ResourceManager final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2018-04-19 15:08:30 +02:00
|
|
|
enum class Mode : int32 { Baseline, Greedy };
|
2018-12-31 20:04:05 +01:00
|
|
|
explicit ResourceManager(Mode mode) : mode_(mode) {
|
|
|
|
}
|
|
|
|
// use through ActorShared
|
2018-01-20 12:47:53 +01:00
|
|
|
void update_priority(int8 priority);
|
2018-12-31 20:04:05 +01:00
|
|
|
void update_resources(const ResourceState &resource_state);
|
|
|
|
|
2018-01-20 12:47:53 +01:00
|
|
|
void register_worker(ActorShared<FileLoaderActor> callback, int8 priority);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-08-26 11:24:06 +02:00
|
|
|
static constexpr int64 MAX_RESOURCE_LIMIT = 1 << 21;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
Mode mode_;
|
|
|
|
using NodeId = uint64;
|
2021-07-04 04:58:54 +02:00
|
|
|
struct Node final : public HeapNode {
|
2018-12-31 20:04:05 +01:00
|
|
|
NodeId node_id;
|
|
|
|
|
|
|
|
ResourceState resource_state_;
|
|
|
|
ActorShared<FileLoaderActor> callback_;
|
|
|
|
|
|
|
|
HeapNode *as_heap_node() {
|
|
|
|
return static_cast<HeapNode *>(this);
|
|
|
|
}
|
|
|
|
static Node *from_heap_node(HeapNode *heap_node) {
|
|
|
|
return static_cast<Node *>(heap_node);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
Container<unique_ptr<Node>> nodes_container_;
|
2018-01-20 12:47:53 +01:00
|
|
|
vector<std::pair<int8, NodeId>> to_xload_;
|
2018-12-31 20:04:05 +01:00
|
|
|
KHeap<int64> by_estimated_extra_;
|
|
|
|
ResourceState resource_state_;
|
|
|
|
|
|
|
|
ActorShared<> parent_;
|
|
|
|
bool stop_flag_ = false;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup_shared() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void add_to_heap(Node *node);
|
|
|
|
bool satisfy_node(NodeId file_node_id);
|
2018-01-20 12:47:53 +01:00
|
|
|
void add_node(NodeId node_id, int8 priority);
|
2018-12-31 20:04:05 +01:00
|
|
|
bool remove_node(NodeId node_id);
|
|
|
|
};
|
2020-10-05 17:07:23 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|