2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/files/ResourceManager.h"
|
|
|
|
|
2020-10-05 17:07:23 +02:00
|
|
|
#include "td/telegram/files/FileLoaderUtils.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2018-01-20 12:47:53 +01:00
|
|
|
void ResourceManager::register_worker(ActorShared<FileLoaderActor> callback, int8 priority) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto node_id = nodes_container_.create();
|
|
|
|
auto *node_ptr = nodes_container_.get(node_id);
|
2018-09-27 03:19:03 +02:00
|
|
|
*node_ptr = make_unique<Node>();
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *node = (*node_ptr).get();
|
|
|
|
CHECK(node);
|
|
|
|
node->node_id = node_id;
|
|
|
|
node->callback_ = std::move(callback);
|
|
|
|
|
|
|
|
add_node(node_id, priority);
|
|
|
|
send_closure(node->callback_, &FileLoaderActor::set_resource_manager, actor_shared(this, node_id));
|
|
|
|
}
|
|
|
|
|
2018-01-20 12:47:53 +01:00
|
|
|
void ResourceManager::update_priority(int8 priority) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto node_id = get_link_token();
|
|
|
|
if (!remove_node(node_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
add_node(node_id, priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::update_resources(const ResourceState &resource_state) {
|
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto node_id = get_link_token();
|
|
|
|
auto node_ptr = nodes_container_.get(node_id);
|
|
|
|
if (node_ptr == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto node = (*node_ptr).get();
|
|
|
|
CHECK(node);
|
2020-10-05 21:08:24 +02:00
|
|
|
VLOG(file_loader) << "Before total: " << resource_state_ << "; node " << node_id << ": " << node->resource_state_;
|
2018-12-31 20:04:05 +01:00
|
|
|
resource_state_ -= node->resource_state_;
|
|
|
|
node->resource_state_.update_master(resource_state);
|
|
|
|
resource_state_ += node->resource_state_;
|
2020-10-05 21:08:24 +02:00
|
|
|
VLOG(file_loader) << "After total: " << resource_state_ << "; node " << node_id << ": " << node->resource_state_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (mode_ == Mode::Greedy) {
|
|
|
|
add_to_heap(node);
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::hangup_shared() {
|
|
|
|
auto node_id = get_link_token();
|
|
|
|
auto node_ptr = nodes_container_.get(node_id);
|
|
|
|
if (node_ptr == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto node = (*node_ptr).get();
|
|
|
|
CHECK(node);
|
|
|
|
if (node->in_heap()) {
|
|
|
|
by_estimated_extra_.erase(node->as_heap_node());
|
|
|
|
}
|
|
|
|
resource_state_ -= node->resource_state_;
|
|
|
|
remove_node(node_id);
|
|
|
|
nodes_container_.erase(node_id);
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::add_to_heap(Node *node) {
|
|
|
|
auto *heap_node = node->as_heap_node();
|
|
|
|
auto key = node->resource_state_.estimated_extra();
|
|
|
|
if (heap_node->in_heap()) {
|
|
|
|
if (key != 0) {
|
|
|
|
by_estimated_extra_.fix(key, heap_node);
|
|
|
|
} else {
|
|
|
|
by_estimated_extra_.erase(heap_node);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (key != 0) {
|
|
|
|
by_estimated_extra_.insert(key, heap_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceManager::satisfy_node(NodeId file_node_id) {
|
|
|
|
auto file_node_ptr = nodes_container_.get(file_node_id);
|
|
|
|
CHECK(file_node_ptr);
|
|
|
|
auto file_node = (*file_node_ptr).get();
|
|
|
|
CHECK(file_node);
|
|
|
|
auto part_size = narrow_cast<int64>(file_node->resource_state_.unit_size());
|
|
|
|
auto need = file_node->resource_state_.estimated_extra();
|
2020-10-05 21:08:24 +02:00
|
|
|
VLOG(file_loader) << tag("need", need) << tag("part_size", part_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
need = (need + part_size - 1) / part_size * part_size;
|
2020-10-05 21:08:24 +02:00
|
|
|
VLOG(file_loader) << tag("need", need);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (need == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
auto give = resource_state_.unused();
|
2018-02-12 11:37:54 +01:00
|
|
|
give = min(need, give);
|
2018-12-31 20:04:05 +01:00
|
|
|
give -= give % part_size;
|
2020-10-05 21:08:24 +02:00
|
|
|
VLOG(file_loader) << tag("give", give);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (give == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
resource_state_.start_use(give);
|
|
|
|
file_node->resource_state_.update_limit(give);
|
|
|
|
send_closure(file_node->callback_, &FileLoaderActor::update_resources, file_node->resource_state_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::loop() {
|
|
|
|
if (stop_flag_) {
|
|
|
|
if (nodes_container_.empty()) {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto active_limit = resource_state_.active_limit();
|
2022-05-20 15:11:31 +02:00
|
|
|
resource_state_.update_limit(max_resource_limit_ - active_limit);
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << tag("unused", resource_state_.unused());
|
|
|
|
|
|
|
|
if (mode_ == Mode::Greedy) {
|
|
|
|
std::vector<Node *> to_add;
|
|
|
|
while (!by_estimated_extra_.empty()) {
|
|
|
|
auto *node = Node::from_heap_node(by_estimated_extra_.pop());
|
|
|
|
SCOPE_EXIT {
|
|
|
|
to_add.push_back(node);
|
|
|
|
};
|
|
|
|
if (!satisfy_node(node->node_id)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto *node : to_add) {
|
|
|
|
add_to_heap(node);
|
|
|
|
}
|
|
|
|
} else if (mode_ == Mode::Baseline) {
|
|
|
|
// plain
|
|
|
|
for (auto &it : to_xload_) {
|
|
|
|
auto file_node_id = it.second;
|
|
|
|
if (!satisfy_node(file_node_id)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 17:07:23 +02:00
|
|
|
|
2018-01-20 12:47:53 +01:00
|
|
|
void ResourceManager::add_node(NodeId node_id, int8 priority) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (priority >= 0) {
|
|
|
|
auto it = std::find_if(to_xload_.begin(), to_xload_.end(), [&](auto &x) { return x.first <= priority; });
|
|
|
|
to_xload_.insert(it, std::make_pair(priority, node_id));
|
|
|
|
} else {
|
|
|
|
auto it = std::find_if(to_xload_.begin(), to_xload_.end(), [&](auto &x) { return x.first < -priority; });
|
2018-04-11 22:17:51 +02:00
|
|
|
to_xload_.insert(it, std::make_pair(narrow_cast<int8>(-priority), node_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-10-05 17:07:23 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool ResourceManager::remove_node(NodeId node_id) {
|
|
|
|
auto it = std::find_if(to_xload_.begin(), to_xload_.end(), [&](auto &x) { return x.second == node_id; });
|
|
|
|
if (it != to_xload_.end()) {
|
|
|
|
to_xload_.erase(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|