Use int8 for upload/download priority.

GitOrigin-RevId: 3cc1c006c5ab3989be920b4d6e7e5f49aeaa42ce
This commit is contained in:
levlam 2018-01-20 14:47:53 +03:00
parent 271ab50403
commit fbb2dc6ccb
12 changed files with 55 additions and 50 deletions

View File

@ -46,7 +46,7 @@ class FileFromBytes : public FileLoaderActor {
void wakeup() override; void wakeup() override;
void set_resource_manager(ActorShared<ResourceManager>) override { void set_resource_manager(ActorShared<ResourceManager>) override {
} }
void update_priority(int32) override { void update_priority(int8 priority) override {
} }
void update_resources(const ResourceState &other) override { void update_resources(const ResourceState &other) override {
} }

View File

@ -38,7 +38,7 @@ class FileHashUploader : public FileLoaderActor {
send_closure(resource_manager_, &ResourceManager::update_resources, resource_state_); send_closure(resource_manager_, &ResourceManager::update_resources, resource_state_);
} }
void update_priority(int32 priority) override { void update_priority(int8 priority) override {
send_closure(resource_manager_, &ResourceManager::update_priority, priority); send_closure(resource_manager_, &ResourceManager::update_priority, priority);
} }
void update_resources(const ResourceState &other) override { void update_resources(const ResourceState &other) override {

View File

@ -30,7 +30,7 @@ void FileLoadManager::start_up() {
void FileLoadManager::download(QueryId id, const FullRemoteFileLocation &remote_location, void FileLoadManager::download(QueryId id, const FullRemoteFileLocation &remote_location,
const LocalFileLocation &local, int64 size, string name, const LocalFileLocation &local, int64 size, string name,
const FileEncryptionKey &encryption_key, int priority) { const FileEncryptionKey &encryption_key, int8 priority) {
if (stop_flag_) { if (stop_flag_) {
return; return;
} }
@ -51,7 +51,7 @@ void FileLoadManager::download(QueryId id, const FullRemoteFileLocation &remote_
void FileLoadManager::upload(QueryId id, const LocalFileLocation &local_location, void FileLoadManager::upload(QueryId id, const LocalFileLocation &local_location,
const RemoteFileLocation &remote_location, int64 size, const RemoteFileLocation &remote_location, int64 size,
const FileEncryptionKey &encryption_key, int priority, vector<int> bad_parts) { const FileEncryptionKey &encryption_key, int8 priority, vector<int> bad_parts) {
if (stop_flag_) { if (stop_flag_) {
return; return;
} }
@ -69,7 +69,7 @@ void FileLoadManager::upload(QueryId id, const LocalFileLocation &local_location
} }
void FileLoadManager::upload_by_hash(QueryId id, const FullLocalFileLocation &local_location, int64 size, void FileLoadManager::upload_by_hash(QueryId id, const FullLocalFileLocation &local_location, int64 size,
int priority) { int8 priority) {
if (stop_flag_) { if (stop_flag_) {
return; return;
} }
@ -85,7 +85,7 @@ void FileLoadManager::upload_by_hash(QueryId id, const FullLocalFileLocation &lo
query_id_to_node_id_[id] = node_id; query_id_to_node_id_[id] = node_id;
} }
void FileLoadManager::update_priority(QueryId id, int priority) { void FileLoadManager::update_priority(QueryId id, int8 priority) {
if (stop_flag_) { if (stop_flag_) {
return; return;
} }

View File

@ -44,11 +44,11 @@ class FileLoadManager final : public Actor {
explicit FileLoadManager(ActorShared<Callback> callback, ActorShared<> parent); explicit FileLoadManager(ActorShared<Callback> callback, ActorShared<> parent);
void download(QueryId id, const FullRemoteFileLocation &remote_location, const LocalFileLocation &local, int64 size, void download(QueryId id, const FullRemoteFileLocation &remote_location, const LocalFileLocation &local, int64 size,
string name, const FileEncryptionKey &encryption_key, int priority); string name, const FileEncryptionKey &encryption_key, int8 priority);
void upload(QueryId id, const LocalFileLocation &local_location, const RemoteFileLocation &remote_location, void upload(QueryId id, const LocalFileLocation &local_location, const RemoteFileLocation &remote_location,
int64 size, const FileEncryptionKey &encryption_key, int priority, vector<int> bad_parts); int64 size, const FileEncryptionKey &encryption_key, int8 priority, vector<int> bad_parts);
void upload_by_hash(QueryId id, const FullLocalFileLocation &local_location, int64 size, int priority); void upload_by_hash(QueryId id, const FullLocalFileLocation &local_location, int64 size, int8 priority);
void update_priority(QueryId id, int priority); void update_priority(QueryId id, int8 priority);
void from_bytes(QueryId id, FileType type, BufferSlice bytes, string name); void from_bytes(QueryId id, FileType type, BufferSlice bytes, string name);
void cancel(QueryId id); void cancel(QueryId id);
void update_local_file_location(QueryId id, const LocalFileLocation &local); void update_local_file_location(QueryId id, const LocalFileLocation &local);

View File

@ -24,7 +24,7 @@ void FileLoader::set_resource_manager(ActorShared<ResourceManager> resource_mana
resource_manager_ = std::move(resource_manager); resource_manager_ = std::move(resource_manager);
send_closure(resource_manager_, &ResourceManager::update_resources, resource_state_); send_closure(resource_manager_, &ResourceManager::update_resources, resource_state_);
} }
void FileLoader::update_priority(int32 priority) { void FileLoader::update_priority(int8 priority) {
send_closure(resource_manager_, &ResourceManager::update_priority, priority); send_closure(resource_manager_, &ResourceManager::update_priority, priority);
} }
void FileLoader::update_resources(const ResourceState &other) { void FileLoader::update_resources(const ResourceState &other) {

View File

@ -33,7 +33,7 @@ class FileLoader : public FileLoaderActor {
virtual ~Callback() = default; virtual ~Callback() = default;
}; };
void set_resource_manager(ActorShared<ResourceManager> resource_manager) override; void set_resource_manager(ActorShared<ResourceManager> resource_manager) override;
void update_priority(int32 priority) override; void update_priority(int8 priority) override;
void update_resources(const ResourceState &other) override; void update_resources(const ResourceState &other) override;
void update_local_file_location(const LocalFileLocation &local) override; void update_local_file_location(const LocalFileLocation &local) override;

View File

@ -17,7 +17,7 @@ class ResourceManager;
class FileLoaderActor : public NetQueryCallback { class FileLoaderActor : public NetQueryCallback {
public: public:
virtual void set_resource_manager(ActorShared<ResourceManager>) = 0; virtual void set_resource_manager(ActorShared<ResourceManager>) = 0;
virtual void update_priority(int32 priority) = 0; virtual void update_priority(int8 priority) = 0;
virtual void update_resources(const ResourceState &other) = 0; virtual void update_resources(const ResourceState &other) = 0;
// TODO: existence of this function is a dirty hack. Refactoring is highly appreciated // TODO: existence of this function is a dirty hack. Refactoring is highly appreciated

View File

@ -124,7 +124,7 @@ void FileNode::set_encryption_key(FileEncryptionKey key) {
} }
} }
void FileNode::set_download_priority(int32 priority) { void FileNode::set_download_priority(int8 priority) {
if ((download_priority_ == 0) != (priority == 0)) { if ((download_priority_ == 0) != (priority == 0)) {
VLOG(update_file) << "File " << main_file_id_ << " has changed download priority to " << priority; VLOG(update_file) << "File " << main_file_id_ << " has changed download priority to " << priority;
on_info_changed(); on_info_changed();
@ -132,7 +132,7 @@ void FileNode::set_download_priority(int32 priority) {
download_priority_ = priority; download_priority_ = priority;
} }
void FileNode::set_upload_priority(int32 priority) { void FileNode::set_upload_priority(int8 priority) {
if ((upload_priority_ == 0) != (priority == 0)) { if ((upload_priority_ == 0) != (priority == 0)) {
VLOG(update_file) << "File " << main_file_id_ << " has changed upload priority to " << priority; VLOG(update_file) << "File " << main_file_id_ << " has changed upload priority to " << priority;
on_info_changed(); on_info_changed();
@ -140,7 +140,7 @@ void FileNode::set_upload_priority(int32 priority) {
upload_priority_ = priority; upload_priority_ = priority;
} }
void FileNode::set_generate_priority(int32 download_priority, int32 upload_priority) { void FileNode::set_generate_priority(int8 download_priority, int8 upload_priority) {
if ((download_priority_ == 0) != (download_priority == 0) || (upload_priority_ == 0) != (upload_priority == 0)) { if ((download_priority_ == 0) != (download_priority == 0) || (upload_priority_ == 0) != (upload_priority == 0)) {
VLOG(update_file) << "File " << main_file_id_ << " has changed generate priority to " << download_priority << "/" VLOG(update_file) << "File " << main_file_id_ << " has changed generate priority to " << download_priority << "/"
<< upload_priority; << upload_priority;
@ -265,7 +265,7 @@ int64 FileView::local_total_size() const {
return node_->size_; return node_->size_;
case LocalFileLocation::Type::Partial: case LocalFileLocation::Type::Partial:
return std::max( return std::max(
static_cast<int64>(node_->local_.partial().part_size_ * node_->local_.partial().ready_part_count_), static_cast<int64>(node_->local_.partial().part_size_) * node_->local_.partial().ready_part_count_,
node_->local_ready_size_); node_->local_ready_size_);
default: default:
UNREACHABLE(); UNREACHABLE();
@ -719,7 +719,7 @@ static int merge_choose(const LocalFileLocation &x, const LocalFileLocation &y)
return 2; return 2;
} }
static int merge_choose(const RemoteFileLocation &x, int x_priority, const RemoteFileLocation &y, int y_priority) { static int merge_choose(const RemoteFileLocation &x, int8 x_source, const RemoteFileLocation &y, int8 y_source) {
int32 x_type = static_cast<int32>(x.type_); int32 x_type = static_cast<int32>(x.type_);
int32 y_type = static_cast<int32>(y.type_); int32 y_type = static_cast<int32>(y.type_);
if (x_type != y_type) { if (x_type != y_type) {
@ -728,7 +728,7 @@ static int merge_choose(const RemoteFileLocation &x, int x_priority, const Remot
// If access_hash changed use a newer one // If access_hash changed use a newer one
if (x.type_ == RemoteFileLocation::Type::Full) { if (x.type_ == RemoteFileLocation::Type::Full) {
if (x.full().get_access_hash() != y.full().get_access_hash()) { if (x.full().get_access_hash() != y.full().get_access_hash()) {
return x_priority < y_priority; return x_source < y_source;
} }
} }
return 2; return 2;
@ -867,8 +867,8 @@ Result<FileId> FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy
FileNodeId node_ids[] = {x_node_id, y_node_id}; FileNodeId node_ids[] = {x_node_id, y_node_id};
int local_i = merge_choose(x_node->local_, y_node->local_); int local_i = merge_choose(x_node->local_, y_node->local_);
int remote_i = merge_choose(x_node->remote_, static_cast<int>(x_node->remote_source_), y_node->remote_, int remote_i = merge_choose(x_node->remote_, static_cast<int8>(x_node->remote_source_), y_node->remote_,
static_cast<int>(y_node->remote_source_)); static_cast<int8>(y_node->remote_source_));
int generate_i = merge_choose(x_node->generate_, y_node->generate_); int generate_i = merge_choose(x_node->generate_, y_node->generate_);
int size_i = merge_choose_size(x_node->size_, y_node->size_); int size_i = merge_choose_size(x_node->size_, y_node->size_);
int expected_size_i = merge_choose_expected_size(x_node->expected_size_, y_node->expected_size_); int expected_size_i = merge_choose_expected_size(x_node->expected_size_, y_node->expected_size_);
@ -1348,7 +1348,7 @@ void FileManager::download(FileId file_id, std::shared_ptr<DownloadCallback> cal
auto *file_info = get_file_id_info(file_id); auto *file_info = get_file_id_info(file_id);
CHECK(new_priority == 0 || callback); CHECK(new_priority == 0 || callback);
file_info->download_priority_ = new_priority; file_info->download_priority_ = narrow_cast<int8>(new_priority);
file_info->download_callback_ = std::move(callback); file_info->download_callback_ = std::move(callback);
// TODO: send current progress? // TODO: send current progress?
@ -1369,7 +1369,7 @@ void FileManager::run_download(FileNode *node) {
if (!file_view.can_download_from_server()) { if (!file_view.can_download_from_server()) {
return; return;
} }
int32 priority = 0; int8 priority = 0;
for (auto id : node->file_ids_) { for (auto id : node->file_ids_) {
auto *info = get_file_id_info(id); auto *info = get_file_id_info(id);
if (info->download_priority_ > priority) { if (info->download_priority_ > priority) {
@ -1443,7 +1443,7 @@ void FileManager::resume_upload(FileId file_id, std::vector<int> bad_parts, std:
auto *file_info = get_file_id_info(file_id); auto *file_info = get_file_id_info(file_id);
CHECK(new_priority == 0 || callback); CHECK(new_priority == 0 || callback);
file_info->upload_order_ = upload_order; file_info->upload_order_ = upload_order;
file_info->upload_priority_ = new_priority; file_info->upload_priority_ = narrow_cast<int8>(new_priority);
file_info->upload_callback_ = std::move(callback); file_info->upload_callback_ = std::move(callback);
// TODO: send current progress? // TODO: send current progress?
@ -1505,8 +1505,8 @@ void FileManager::run_generate(FileNode *node) {
return; return;
} }
int32 download_priority = 0; int8 download_priority = 0;
int32 upload_priority = 0; int8 upload_priority = 0;
FileId file_id; FileId file_id;
for (auto id : node->file_ids_) { for (auto id : node->file_ids_) {
auto *info = get_file_id_info(id); auto *info = get_file_id_info(id);
@ -1581,7 +1581,7 @@ void FileManager::run_upload(FileNode *node, std::vector<int> bad_parts) {
return; return;
} }
} }
int32 priority = 0; int8 priority = 0;
FileId file_id; FileId file_id;
for (auto id : node->file_ids_) { for (auto id : node->file_ids_) {
auto *info = get_file_id_info(id); auto *info = get_file_id_info(id);
@ -1614,7 +1614,7 @@ void FileManager::run_upload(FileNode *node, std::vector<int> bad_parts) {
if (old_priority != 0) { if (old_priority != 0) {
LOG(INFO) << "File " << file_id << " is already uploading"; LOG(INFO) << "File " << file_id << " is already uploading";
CHECK(node->upload_id_ != 0); CHECK(node->upload_id_ != 0);
send_closure(file_load_manager_, &FileLoadManager::update_priority, node->upload_id_, -priority); send_closure(file_load_manager_, &FileLoadManager::update_priority, node->upload_id_, narrow_cast<int8>(-priority));
return; return;
} }
@ -1623,14 +1623,16 @@ void FileManager::run_upload(FileNode *node, std::vector<int> bad_parts) {
QueryId id = queries_container_.create(Query{file_id, Query::UploadByHash}); QueryId id = queries_container_.create(Query{file_id, Query::UploadByHash});
node->upload_id_ = id; node->upload_id_ = id;
send_closure(file_load_manager_, &FileLoadManager::upload_by_hash, id, node->local_.full(), node->size_, -priority); send_closure(file_load_manager_, &FileLoadManager::upload_by_hash, id, node->local_.full(), node->size_,
narrow_cast<int8>(-priority));
return; return;
} }
QueryId id = queries_container_.create(Query{file_id, Query::Upload}); QueryId id = queries_container_.create(Query{file_id, Query::Upload});
node->upload_id_ = id; node->upload_id_ = id;
send_closure(file_load_manager_, &FileLoadManager::upload, id, node->local_, node->remote_, node->size_, send_closure(file_load_manager_, &FileLoadManager::upload, id, node->local_, node->remote_, node->size_,
node->encryption_key_, bad_parts.empty() ? -priority : priority, std::move(bad_parts)); node->encryption_key_, narrow_cast<int8>(bad_parts.empty() ? -priority : priority),
std::move(bad_parts));
LOG(INFO) << "File " << file_id << " upload request has sent to FileLoadManager"; LOG(INFO) << "File " << file_id << " upload request has sent to FileLoadManager";
} }
@ -2243,12 +2245,14 @@ std::pair<FileManager::Query, bool> FileManager::finish_query(QueryId query_id)
} }
return std::make_pair(res, was_active); return std::make_pair(res, was_active);
} }
void FileManager::hangup() { void FileManager::hangup() {
file_db_.reset(); file_db_.reset();
file_generate_manager_.reset(); file_generate_manager_.reset();
file_load_manager_.reset(); file_load_manager_.reset();
stop(); stop();
} }
void FileManager::tear_down() { void FileManager::tear_down() {
parent_.reset(); parent_.reset();
} }

View File

@ -59,9 +59,9 @@ class FileNode {
void set_owner_dialog_id(DialogId owner_id); void set_owner_dialog_id(DialogId owner_id);
void set_encryption_key(FileEncryptionKey key); void set_encryption_key(FileEncryptionKey key);
void set_download_priority(int32 priority); void set_download_priority(int8 priority);
void set_upload_priority(int32 priority); void set_upload_priority(int8 priority);
void set_generate_priority(int32 download_priority, int32 upload_priority); void set_generate_priority(int8 download_priority, int8 upload_priority);
void on_changed(); void on_changed();
void on_info_changed(); void on_info_changed();
@ -102,12 +102,12 @@ class FileNode {
FileId main_file_id_; FileId main_file_id_;
FileId upload_pause_; FileId upload_pause_;
int32 upload_priority_ = 0; int8 upload_priority_ = 0;
int32 download_priority_ = 0; int8 download_priority_ = 0;
int32 generate_priority_ = 0; int8 generate_priority_ = 0;
int32 generate_download_priority_ = 0; int8 generate_download_priority_ = 0;
int32 generate_upload_priority_ = 0; int8 generate_upload_priority_ = 0;
int8 main_file_id_priority_ = 0; int8 main_file_id_priority_ = 0;
@ -299,7 +299,7 @@ class FileManager : public FileLoadManager::Callback {
FileId register_url(string url, FileType file_type, DialogId owner_dialog_id); FileId register_url(string url, FileType file_type, DialogId owner_dialog_id);
static constexpr int32 FROM_BYTES_PRIORITY = 10; static constexpr int8 FROM_BYTES_PRIORITY = 10;
using FileNodeId = int32; using FileNodeId = int32;
using QueryId = FileLoadManager::QueryId; using QueryId = FileLoadManager::QueryId;
class Query { class Query {
@ -312,11 +312,12 @@ class FileManager : public FileLoadManager::Callback {
bool send_updates_flag_{false}; bool send_updates_flag_{false};
bool pin_flag_{false}; bool pin_flag_{false};
int32 download_priority_{0}; int8 download_priority_{0};
std::shared_ptr<DownloadCallback> download_callback_; int8 upload_priority_{0};
int32 upload_priority_{0};
uint64 upload_order_; uint64 upload_order_;
std::shared_ptr<DownloadCallback> download_callback_;
std::shared_ptr<UploadCallback> upload_callback_; std::shared_ptr<UploadCallback> upload_callback_;
}; };

View File

@ -15,7 +15,7 @@
namespace td { namespace td {
void ResourceManager::register_worker(ActorShared<FileLoaderActor> callback, int32 priority) { void ResourceManager::register_worker(ActorShared<FileLoaderActor> callback, int8 priority) {
auto node_id = nodes_container_.create(); auto node_id = nodes_container_.create();
auto *node_ptr = nodes_container_.get(node_id); auto *node_ptr = nodes_container_.get(node_id);
*node_ptr = std::make_unique<Node>(); *node_ptr = std::make_unique<Node>();
@ -28,7 +28,7 @@ void ResourceManager::register_worker(ActorShared<FileLoaderActor> callback, int
send_closure(node->callback_, &FileLoaderActor::set_resource_manager, actor_shared(this, node_id)); send_closure(node->callback_, &FileLoaderActor::set_resource_manager, actor_shared(this, node_id));
} }
void ResourceManager::update_priority(int32 priority) { void ResourceManager::update_priority(int8 priority) {
if (stop_flag_) { if (stop_flag_) {
return; return;
} }
@ -158,7 +158,7 @@ void ResourceManager::loop() {
} }
} }
} }
void ResourceManager::add_node(NodeId node_id, int32 priority) { void ResourceManager::add_node(NodeId node_id, int8 priority) {
if (priority >= 0) { if (priority >= 0) {
auto it = std::find_if(to_xload_.begin(), to_xload_.end(), [&](auto &x) { return x.first <= priority; }); 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)); to_xload_.insert(it, std::make_pair(priority, node_id));

View File

@ -24,10 +24,10 @@ class ResourceManager : public Actor {
explicit ResourceManager(Mode mode) : mode_(mode) { explicit ResourceManager(Mode mode) : mode_(mode) {
} }
// use through ActorShared // use through ActorShared
void update_priority(int32 priority); void update_priority(int8 priority);
void update_resources(const ResourceState &resource_state); void update_resources(const ResourceState &resource_state);
void register_worker(ActorShared<FileLoaderActor> callback, int32 priority); void register_worker(ActorShared<FileLoaderActor> callback, int8 priority);
private: private:
Mode mode_; Mode mode_;
@ -47,7 +47,7 @@ class ResourceManager : public Actor {
}; };
Container<std::unique_ptr<Node>> nodes_container_; Container<std::unique_ptr<Node>> nodes_container_;
vector<std::pair<int32, NodeId>> to_xload_; vector<std::pair<int8, NodeId>> to_xload_;
KHeap<int64> by_estimated_extra_; KHeap<int64> by_estimated_extra_;
ResourceState resource_state_; ResourceState resource_state_;
@ -60,7 +60,7 @@ class ResourceManager : public Actor {
void add_to_heap(Node *node); void add_to_heap(Node *node);
bool satisfy_node(NodeId file_node_id); bool satisfy_node(NodeId file_node_id);
void add_node(NodeId node_id, int32 priority); void add_node(NodeId node_id, int8 priority);
bool remove_node(NodeId node_id); bool remove_node(NodeId node_id);
}; };
} // namespace td } // namespace td