2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
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/FileLoader.h"
|
|
|
|
|
|
|
|
#include "td/telegram/files/ResourceManager.h"
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
|
|
|
#include "td/telegram/UniqueId.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 <tuple>
|
2018-03-27 15:11:15 +02:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-12-26 17:11:15 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileLoader::set_resource_manager(ActorShared<ResourceManager> resource_manager) {
|
|
|
|
resource_manager_ = std::move(resource_manager);
|
|
|
|
send_closure(resource_manager_, &ResourceManager::update_resources, resource_state_);
|
|
|
|
}
|
2018-01-20 12:47:53 +01:00
|
|
|
void FileLoader::update_priority(int8 priority) {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(resource_manager_, &ResourceManager::update_priority, priority);
|
|
|
|
}
|
|
|
|
void FileLoader::update_resources(const ResourceState &other) {
|
|
|
|
resource_state_.update_slave(other);
|
2019-02-21 16:58:20 +01:00
|
|
|
VLOG(files) << "Update resources " << resource_state_;
|
2018-12-31 20:04:05 +01:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
void FileLoader::set_ordered_flag(bool flag) {
|
|
|
|
ordered_flag_ = flag;
|
|
|
|
}
|
|
|
|
size_t FileLoader::get_part_size() const {
|
|
|
|
return parts_manager_.get_part_size();
|
|
|
|
}
|
2020-08-26 23:52:01 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileLoader::hangup() {
|
2020-08-26 23:52:01 +02:00
|
|
|
delay_dispatcher_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoader::hangup_shared() {
|
|
|
|
if (get_link_token() == 1) {
|
|
|
|
stop();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoader::update_local_file_location(const LocalFileLocation &local) {
|
2018-12-26 17:11:15 +01:00
|
|
|
auto r_prefix_info = on_update_local_location(local, parts_manager_.get_size_or_zero());
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_prefix_info.is_error()) {
|
|
|
|
on_error(r_prefix_info.move_as_error());
|
|
|
|
stop_flag_ = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto prefix_info = r_prefix_info.move_as_ok();
|
2018-01-30 13:33:02 +01:00
|
|
|
auto status = parts_manager_.set_known_prefix(narrow_cast<size_t>(prefix_info.size), prefix_info.is_ready);
|
|
|
|
if (status.is_error()) {
|
|
|
|
on_error(std::move(status));
|
|
|
|
stop_flag_ = true;
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:58:37 +02:00
|
|
|
void FileLoader::update_downloaded_part(int64 offset, int64 limit) {
|
2019-07-30 20:27:39 +02:00
|
|
|
if (parts_manager_.get_streaming_offset() != offset) {
|
2020-08-25 17:58:37 +02:00
|
|
|
auto begin_part_id = parts_manager_.set_streaming_offset(offset, limit);
|
2020-08-26 11:24:06 +02:00
|
|
|
auto new_end_part_id = limit <= 0 ? parts_manager_.get_part_count()
|
|
|
|
: static_cast<int32>((offset + limit - 1) / parts_manager_.get_part_size()) + 1;
|
|
|
|
auto max_parts = static_cast<int32>(ResourceManager::MAX_RESOURCE_LIMIT / parts_manager_.get_part_size());
|
|
|
|
auto end_part_id = begin_part_id + td::min(max_parts, new_end_part_id - begin_part_id);
|
|
|
|
VLOG(files) << "Protect parts " << begin_part_id << " ... " << end_part_id - 1;
|
2019-07-30 20:27:39 +02:00
|
|
|
for (auto &it : part_map_) {
|
2020-08-26 11:33:48 +02:00
|
|
|
if (!it.second.second.empty() && !(begin_part_id <= it.second.first.id && it.second.first.id < end_part_id)) {
|
2020-08-21 15:32:31 +02:00
|
|
|
VLOG(files) << "Cancel part " << it.second.first.id;
|
2020-08-21 14:53:11 +02:00
|
|
|
it.second.second.reset(); // cancel_query(it.second.second);
|
|
|
|
}
|
2019-07-30 20:27:39 +02:00
|
|
|
}
|
2020-08-25 17:58:37 +02:00
|
|
|
} else {
|
|
|
|
parts_manager_.set_streaming_limit(limit);
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
2019-03-03 22:37:25 +01:00
|
|
|
update_estimated_limit();
|
2018-11-11 12:38:04 +01:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileLoader::start_up() {
|
|
|
|
auto r_file_info = init();
|
|
|
|
if (r_file_info.is_error()) {
|
|
|
|
on_error(r_file_info.move_as_error());
|
|
|
|
stop_flag_ = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto file_info = r_file_info.ok();
|
|
|
|
auto size = file_info.size;
|
2018-02-12 11:37:54 +01:00
|
|
|
auto expected_size = max(size, file_info.expected_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
bool is_size_final = file_info.is_size_final;
|
|
|
|
auto part_size = file_info.part_size;
|
|
|
|
auto &ready_parts = file_info.ready_parts;
|
|
|
|
auto use_part_count_limit = file_info.use_part_count_limit;
|
2019-07-31 17:04:38 +02:00
|
|
|
bool is_upload = file_info.is_upload;
|
2019-08-01 15:03:11 +02:00
|
|
|
|
|
|
|
// Two cases when FILE_UPLOAD_RESTART will happen
|
|
|
|
// 1. File is ready, size is final. But there are more uploaded parts, than size of a file
|
|
|
|
//pm.init(1, 100000, true, 10, {0, 1, 2}, false, true).ensure_error();
|
|
|
|
// This error is definitely ok, because we are using actual size of file on disk (mtime is checked by somebody
|
|
|
|
// else). And actual size could change arbitrarily.
|
|
|
|
//
|
|
|
|
// 2. size is unknown/zero, size is not final, some parts of file are already uploaded
|
|
|
|
// pm.init(0, 100000, false, 10, {0, 1, 2}, false, true).ensure_error();
|
|
|
|
// This case is more complicated
|
|
|
|
// It means that at some point we got inconsistent state. Like deleted local location, but left partial remote
|
|
|
|
// locaiton untouched. This is completely possible at this point, but probably should be fixed.
|
2019-07-31 17:04:38 +02:00
|
|
|
auto status =
|
|
|
|
parts_manager_.init(size, expected_size, is_size_final, part_size, ready_parts, use_part_count_limit, is_upload);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status.is_error()) {
|
|
|
|
on_error(std::move(status));
|
|
|
|
stop_flag_ = true;
|
|
|
|
return;
|
|
|
|
}
|
2018-11-11 12:38:04 +01:00
|
|
|
if (file_info.only_check) {
|
|
|
|
parts_manager_.set_checked_prefix_size(0);
|
|
|
|
}
|
2020-08-25 17:58:37 +02:00
|
|
|
parts_manager_.set_streaming_offset(file_info.offset, file_info.limit);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (ordered_flag_) {
|
|
|
|
ordered_parts_ = OrderedEventsProcessor<std::pair<Part, NetQueryPtr>>(parts_manager_.get_ready_prefix_count());
|
|
|
|
}
|
2018-03-16 13:33:44 +01:00
|
|
|
if (file_info.need_delay) {
|
2020-08-26 23:52:01 +02:00
|
|
|
delay_dispatcher_ = create_actor<DelayDispatcher>("DelayDispatcher", 0.003, actor_shared(this, 1));
|
2018-03-17 18:06:16 +01:00
|
|
|
next_delay_ = 0.05;
|
2018-03-16 10:31:23 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
resource_state_.set_unit_size(parts_manager_.get_part_size());
|
|
|
|
update_estimated_limit();
|
2018-12-26 22:42:26 +01:00
|
|
|
on_progress_impl();
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoader::loop() {
|
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto status = do_loop();
|
|
|
|
if (status.is_error()) {
|
|
|
|
if (status.code() == 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
on_error(std::move(status));
|
|
|
|
stop_flag_ = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-08-26 23:52:01 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status FileLoader::do_loop() {
|
|
|
|
TRY_RESULT(check_info,
|
|
|
|
check_loop(parts_manager_.get_checked_prefix_size(), parts_manager_.get_unchecked_ready_prefix_size(),
|
|
|
|
parts_manager_.unchecked_ready()));
|
2018-02-26 11:05:14 +01:00
|
|
|
if (check_info.changed) {
|
2018-12-26 22:42:26 +01:00
|
|
|
on_progress_impl();
|
2018-02-26 11:05:14 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &query : check_info.queries) {
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(
|
2018-04-19 15:08:30 +02:00
|
|
|
std::move(query), actor_shared(this, UniqueId::next(UniqueId::Type::Default, COMMON_QUERY_KEY)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (check_info.need_check) {
|
|
|
|
parts_manager_.set_need_check();
|
|
|
|
parts_manager_.set_checked_prefix_size(check_info.checked_prefix_size);
|
|
|
|
}
|
|
|
|
|
2019-02-26 15:14:07 +01:00
|
|
|
if (parts_manager_.may_finish()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(parts_manager_.finish());
|
|
|
|
TRY_STATUS(on_ok(parts_manager_.get_size()));
|
2018-03-16 13:33:44 +01:00
|
|
|
LOG(INFO) << "Bad download order rate: "
|
|
|
|
<< (debug_total_parts_ == 0 ? 0.0 : 100.0 * debug_bad_part_order_ / debug_total_parts_) << "% "
|
2018-03-17 18:06:16 +01:00
|
|
|
<< debug_bad_part_order_ << "/" << debug_total_parts_ << " " << format::as_array(debug_bad_parts_);
|
2018-12-31 20:04:05 +01:00
|
|
|
stop_flag_ = true;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
TRY_STATUS(before_start_parts());
|
|
|
|
SCOPE_EXIT {
|
|
|
|
after_start_parts();
|
|
|
|
};
|
|
|
|
while (true) {
|
|
|
|
if (blocking_id_ != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (resource_state_.unused() < static_cast<int64>(parts_manager_.get_part_size())) {
|
|
|
|
VLOG(files) << "Got only " << resource_state_.unused() << " resource";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
TRY_RESULT(part, parts_manager_.start_part());
|
|
|
|
if (part.size == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
VLOG(files) << "Start part " << tag("id", part.id) << tag("size", part.size);
|
|
|
|
resource_state_.start_use(static_cast<int64>(part.size));
|
|
|
|
|
2020-01-08 18:15:49 +01:00
|
|
|
TRY_RESULT(query_flag, start_part(part, parts_manager_.get_part_count(), parts_manager_.get_streaming_offset()));
|
2018-12-31 20:04:05 +01:00
|
|
|
NetQueryPtr query;
|
|
|
|
bool is_blocking;
|
|
|
|
std::tie(query, is_blocking) = std::move(query_flag);
|
|
|
|
uint64 id = UniqueId::next();
|
|
|
|
if (is_blocking) {
|
|
|
|
CHECK(blocking_id_ == 0);
|
|
|
|
blocking_id_ = id;
|
|
|
|
}
|
|
|
|
part_map_[id] = std::make_pair(part, query->cancel_slot_.get_signal_new());
|
|
|
|
// part_map_[id] = std::make_pair(part, query.get_weak());
|
2018-03-16 10:31:23 +01:00
|
|
|
|
|
|
|
auto callback = actor_shared(this, id);
|
|
|
|
if (delay_dispatcher_.empty()) {
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), std::move(callback));
|
|
|
|
} else {
|
2020-08-26 11:41:15 +02:00
|
|
|
query->debug("sent to DelayDispatcher");
|
2018-03-17 18:06:16 +01:00
|
|
|
send_closure(delay_dispatcher_, &DelayDispatcher::send_with_callback_and_delay, std::move(query),
|
|
|
|
std::move(callback), next_delay_);
|
2018-03-27 15:11:15 +02:00
|
|
|
next_delay_ = max(next_delay_ * 0.8, 0.003);
|
2018-03-16 10:31:23 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoader::tear_down() {
|
|
|
|
for (auto &it : part_map_) {
|
2018-03-16 10:31:23 +01:00
|
|
|
it.second.second.reset(); // cancel_query(it.second.second);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-09-07 15:17:09 +02:00
|
|
|
ordered_parts_.clear([](auto &&part) { part.second->clear(); });
|
2020-08-26 23:52:01 +02:00
|
|
|
if (!delay_dispatcher_.empty()) {
|
|
|
|
send_closure(std::move(delay_dispatcher_), &DelayDispatcher::close_silent);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-08-26 11:24:06 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileLoader::update_estimated_limit() {
|
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-18 20:08:05 +01:00
|
|
|
auto estimated_extra = parts_manager_.get_estimated_extra();
|
|
|
|
resource_state_.update_estimated_limit(estimated_extra);
|
2019-02-21 16:58:20 +01:00
|
|
|
VLOG(files) << "Update estimated limit " << estimated_extra;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!resource_manager_.empty()) {
|
|
|
|
keep_fd_flag(narrow_cast<uint64>(resource_state_.active_limit()) >= parts_manager_.get_part_size());
|
|
|
|
send_closure(resource_manager_, &ResourceManager::update_resources, resource_state_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoader::on_result(NetQueryPtr query) {
|
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto id = get_link_token();
|
|
|
|
if (id == blocking_id_) {
|
|
|
|
blocking_id_ = 0;
|
|
|
|
}
|
2018-04-19 15:08:30 +02:00
|
|
|
if (UniqueId::extract_key(id) == COMMON_QUERY_KEY) {
|
2018-12-31 20:04:05 +01:00
|
|
|
on_common_query(std::move(query));
|
|
|
|
return loop();
|
|
|
|
}
|
|
|
|
auto it = part_map_.find(id);
|
|
|
|
if (it == part_map_.end()) {
|
|
|
|
LOG(WARNING) << "Got result for unknown part";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Part part = it->second.first;
|
|
|
|
it->second.second.release();
|
|
|
|
CHECK(query->is_ready());
|
2020-08-21 14:53:11 +02:00
|
|
|
part_map_.erase(it);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool next = false;
|
|
|
|
auto status = [&] {
|
|
|
|
TRY_RESULT(should_restart, should_restart_part(part, query));
|
2018-12-28 07:42:36 +01:00
|
|
|
if (query->is_error() && query->error().code() == NetQuery::Error::Cancelled) {
|
|
|
|
should_restart = true;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (should_restart) {
|
|
|
|
VLOG(files) << "Restart part " << tag("id", part.id) << tag("size", part.size);
|
|
|
|
resource_state_.stop_use(static_cast<int64>(part.size));
|
|
|
|
parts_manager_.on_part_failed(part.id);
|
|
|
|
} else {
|
|
|
|
next = true;
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}();
|
|
|
|
if (status.is_error()) {
|
|
|
|
on_error(std::move(status));
|
|
|
|
stop_flag_ = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next) {
|
|
|
|
if (ordered_flag_) {
|
|
|
|
auto seq_no = part.id;
|
|
|
|
ordered_parts_.add(seq_no, std::make_pair(part, std::move(query)),
|
|
|
|
[this](auto seq_no, auto &&p) { this->on_part_query(p.first, std::move(p.second)); });
|
|
|
|
} else {
|
|
|
|
on_part_query(part, std::move(query));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update_estimated_limit();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoader::on_part_query(Part part, NetQueryPtr query) {
|
2019-07-30 20:27:39 +02:00
|
|
|
if (stop_flag_) {
|
2019-07-30 20:29:52 +02:00
|
|
|
// important for secret files
|
2019-07-30 20:27:39 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
auto status = try_on_part_query(part, std::move(query));
|
|
|
|
if (status.is_error()) {
|
|
|
|
on_error(std::move(status));
|
|
|
|
stop_flag_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoader::on_common_query(NetQueryPtr query) {
|
|
|
|
auto status = process_check_query(std::move(query));
|
|
|
|
if (status.is_error()) {
|
|
|
|
on_error(std::move(status));
|
|
|
|
stop_flag_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status FileLoader::try_on_part_query(Part part, NetQueryPtr query) {
|
|
|
|
TRY_RESULT(size, process_part(part, std::move(query)));
|
|
|
|
VLOG(files) << "Ok part " << tag("id", part.id) << tag("size", part.size);
|
|
|
|
resource_state_.stop_use(static_cast<int64>(part.size));
|
2018-05-19 23:56:40 +02:00
|
|
|
auto old_ready_prefix_count = parts_manager_.get_unchecked_ready_prefix_count();
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(parts_manager_.on_part_ok(part.id, part.size, size));
|
2018-05-19 23:56:40 +02:00
|
|
|
auto new_ready_prefix_count = parts_manager_.get_unchecked_ready_prefix_count();
|
2018-03-16 10:31:23 +01:00
|
|
|
debug_total_parts_++;
|
|
|
|
if (old_ready_prefix_count == new_ready_prefix_count) {
|
2018-03-17 18:06:16 +01:00
|
|
|
debug_bad_parts_.push_back(part.id);
|
2018-03-16 10:31:23 +01:00
|
|
|
debug_bad_part_order_++;
|
|
|
|
}
|
2018-12-26 22:42:26 +01:00
|
|
|
on_progress_impl();
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-12-26 22:42:26 +01:00
|
|
|
void FileLoader::on_progress_impl() {
|
|
|
|
Progress progress;
|
|
|
|
progress.part_count = parts_manager_.get_part_count();
|
|
|
|
progress.part_size = static_cast<int32>(parts_manager_.get_part_size());
|
|
|
|
progress.ready_part_count = parts_manager_.get_ready_prefix_count();
|
|
|
|
progress.ready_bitmask = parts_manager_.get_bitmask();
|
|
|
|
progress.is_ready = parts_manager_.ready();
|
|
|
|
progress.ready_size = parts_manager_.get_ready_size();
|
|
|
|
progress.size = parts_manager_.get_size_or_zero();
|
|
|
|
on_progress(std::move(progress));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|