2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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/PartsManager.h"
|
|
|
|
|
2020-10-05 17:07:23 +02:00
|
|
|
#include "td/telegram/files/FileLoaderUtils.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include <numeric>
|
|
|
|
|
|
|
|
namespace td {
|
2018-01-30 13:33:02 +01:00
|
|
|
|
|
|
|
namespace {
|
2018-04-26 22:41:34 +02:00
|
|
|
int64 calc_part_count(int64 size, int64 part_size) {
|
2018-01-30 13:33:02 +01:00
|
|
|
CHECK(part_size != 0);
|
|
|
|
return (size + part_size - 1) / part_size;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status PartsManager::init_known_prefix(int64 known_prefix, size_t part_size, const std::vector<int> &ready_parts) {
|
|
|
|
known_prefix_flag_ = true;
|
|
|
|
known_prefix_size_ = known_prefix;
|
|
|
|
return init_no_size(part_size, ready_parts);
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:58:37 +02:00
|
|
|
int32 PartsManager::set_streaming_offset(int64 offset, int64 limit) {
|
2020-08-21 14:53:11 +02:00
|
|
|
auto finish = [&] {
|
2020-08-25 17:58:37 +02:00
|
|
|
set_streaming_limit(limit);
|
2020-08-21 15:32:31 +02:00
|
|
|
update_first_not_ready_part();
|
|
|
|
return first_streaming_not_ready_part_;
|
2019-03-05 08:48:50 +01:00
|
|
|
};
|
2020-08-21 14:53:11 +02:00
|
|
|
|
2018-12-26 17:11:15 +01:00
|
|
|
if (offset < 0 || need_check_ || (!unknown_size_flag_ && get_size() < offset)) {
|
2018-11-11 12:38:04 +01:00
|
|
|
streaming_offset_ = 0;
|
2019-03-25 00:29:33 +01:00
|
|
|
LOG_IF(ERROR, offset != 0) << "Ignore streaming_offset " << offset << ", need_check_ = " << need_check_
|
|
|
|
<< ", unknown_size_flag_ = " << unknown_size_flag_ << ", size = " << get_size();
|
2020-08-21 14:53:11 +02:00
|
|
|
|
|
|
|
return finish();
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto part_i = offset / part_size_;
|
2022-05-11 16:17:20 +02:00
|
|
|
if (use_part_count_limit_ && part_i >= MAX_PART_COUNT_PREMIUM) {
|
2018-11-11 12:38:04 +01:00
|
|
|
streaming_offset_ = 0;
|
2019-03-16 21:13:18 +01:00
|
|
|
LOG(ERROR) << "Ignore streaming_offset " << offset << " in part " << part_i;
|
2020-08-21 14:53:11 +02:00
|
|
|
|
|
|
|
return finish();
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
streaming_offset_ = offset;
|
|
|
|
first_streaming_empty_part_ = narrow_cast<int>(part_i);
|
2019-02-26 15:14:07 +01:00
|
|
|
first_streaming_not_ready_part_ = narrow_cast<int>(part_i);
|
2018-12-26 22:41:52 +01:00
|
|
|
if (part_count_ < first_streaming_empty_part_) {
|
|
|
|
part_count_ = first_streaming_empty_part_;
|
2018-11-11 12:38:04 +01:00
|
|
|
part_status_.resize(part_count_, PartStatus::Empty);
|
|
|
|
}
|
2020-08-21 14:53:11 +02:00
|
|
|
|
|
|
|
return finish();
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:32:22 +02:00
|
|
|
int32 PartsManager::get_pending_count() const {
|
2020-08-21 14:53:11 +02:00
|
|
|
return pending_count_;
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
2019-03-16 21:13:18 +01:00
|
|
|
|
2019-02-18 20:08:05 +01:00
|
|
|
void PartsManager::set_streaming_limit(int64 limit) {
|
|
|
|
streaming_limit_ = limit;
|
2019-02-27 17:00:10 +01:00
|
|
|
streaming_ready_size_ = 0;
|
|
|
|
if (streaming_limit_ == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int part_i = 0; part_i < part_count_; part_i++) {
|
|
|
|
if (is_part_in_streaming_limit(part_i) && part_status_[part_i] == PartStatus::Ready) {
|
2019-03-03 22:37:25 +01:00
|
|
|
streaming_ready_size_ += get_part(part_i).size;
|
2019-02-27 17:00:10 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-18 20:08:05 +01:00
|
|
|
}
|
2018-11-11 12:38:04 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status PartsManager::init_no_size(size_t part_size, const std::vector<int> &ready_parts) {
|
|
|
|
unknown_size_flag_ = true;
|
|
|
|
size_ = 0;
|
|
|
|
min_size_ = 0;
|
|
|
|
max_size_ = std::numeric_limits<int64>::max();
|
|
|
|
|
|
|
|
if (part_size != 0) {
|
|
|
|
part_size_ = part_size;
|
|
|
|
} else {
|
2020-11-17 00:59:58 +01:00
|
|
|
part_size_ = 32 << 10;
|
2022-05-11 16:17:20 +02:00
|
|
|
while (part_size_ < MAX_PART_SIZE && calc_part_count(expected_size_, part_size_) > MAX_PART_COUNT) {
|
2018-01-30 13:33:02 +01:00
|
|
|
part_size_ *= 2;
|
|
|
|
}
|
|
|
|
// just in case if expected_size_ is wrong
|
|
|
|
if (part_size_ < MAX_PART_SIZE) {
|
|
|
|
part_size_ *= 2;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-26 22:41:52 +01:00
|
|
|
part_count_ =
|
|
|
|
std::accumulate(ready_parts.begin(), ready_parts.end(), 0, [](auto a, auto b) { return max(a, b + 1); });
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-07-31 15:48:59 +02:00
|
|
|
return init_common(ready_parts);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 13:33:02 +01:00
|
|
|
Status PartsManager::init(int64 size, int64 expected_size, bool is_size_final, size_t part_size,
|
2019-07-31 17:04:38 +02:00
|
|
|
const std::vector<int> &ready_parts, bool use_part_count_limit, bool is_upload) {
|
2018-01-30 13:33:02 +01:00
|
|
|
CHECK(expected_size >= size);
|
2019-07-31 17:04:38 +02:00
|
|
|
is_upload_ = is_upload;
|
2018-12-31 20:04:05 +01:00
|
|
|
use_part_count_limit_ = use_part_count_limit;
|
2018-01-30 13:33:02 +01:00
|
|
|
expected_size_ = expected_size;
|
|
|
|
if (expected_size_ > MAX_FILE_SIZE) {
|
|
|
|
return Status::Error("Too big file");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!is_size_final) {
|
|
|
|
return init_known_prefix(size, part_size, ready_parts);
|
|
|
|
}
|
|
|
|
if (size == 0) {
|
|
|
|
return init_no_size(part_size, ready_parts);
|
|
|
|
}
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(size > 0) << tag("size", size);
|
2018-12-31 20:04:05 +01:00
|
|
|
unknown_size_flag_ = false;
|
|
|
|
size_ = size;
|
|
|
|
|
|
|
|
if (part_size != 0) {
|
|
|
|
part_size_ = part_size;
|
2022-05-11 16:17:20 +02:00
|
|
|
if (use_part_count_limit_ && part_size_ < MAX_PART_SIZE &&
|
|
|
|
calc_part_count(expected_size_, part_size_) > MAX_PART_COUNT) {
|
2020-11-17 00:59:58 +01:00
|
|
|
CHECK(is_upload_);
|
2018-01-29 18:46:06 +01:00
|
|
|
return Status::Error("FILE_UPLOAD_RESTART");
|
2018-01-29 15:41:40 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2020-11-17 00:59:58 +01:00
|
|
|
part_size_ = 64 << 10;
|
2022-05-11 16:17:20 +02:00
|
|
|
while (part_size_ < MAX_PART_SIZE && calc_part_count(expected_size_, part_size_) > MAX_PART_COUNT) {
|
2018-12-31 20:04:05 +01:00
|
|
|
part_size_ *= 2;
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(1 <= size_) << tag("size_", size_);
|
2022-05-11 16:17:20 +02:00
|
|
|
LOG_CHECK(!use_part_count_limit || calc_part_count(expected_size_, part_size_) <= MAX_PART_COUNT_PREMIUM)
|
2018-01-30 13:33:02 +01:00
|
|
|
<< tag("size_", size_) << tag("expected_size", size_) << tag("is_size_final", is_size_final)
|
|
|
|
<< tag("part_size_", part_size_) << tag("ready_parts", ready_parts.size());
|
2018-04-26 22:41:34 +02:00
|
|
|
part_count_ = static_cast<int>(calc_part_count(size_, part_size_));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-07-31 15:48:59 +02:00
|
|
|
return init_common(ready_parts);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PartsManager::unchecked_ready() {
|
2020-10-05 21:08:24 +02:00
|
|
|
VLOG(file_loader) << "Check readiness. Ready size is " << ready_size_ << ", total size is " << size_
|
|
|
|
<< ", unknown_size_flag = " << unknown_size_flag_ << ", need_check = " << need_check_
|
|
|
|
<< ", checked_prefix_size = " << checked_prefix_size_;
|
2018-12-31 20:04:05 +01:00
|
|
|
return !unknown_size_flag_ && ready_size_ == size_;
|
|
|
|
}
|
2020-10-05 17:07:23 +02:00
|
|
|
|
2019-02-26 15:14:07 +01:00
|
|
|
bool PartsManager::may_finish() {
|
|
|
|
if (is_streaming_limit_reached()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return ready();
|
|
|
|
}
|
2020-10-05 17:07:23 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool PartsManager::ready() {
|
|
|
|
return unchecked_ready() && (!need_check_ || checked_prefix_size_ == size_);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status PartsManager::finish() {
|
2019-02-27 17:00:10 +01:00
|
|
|
if (ready()) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-02-26 15:14:07 +01:00
|
|
|
if (is_streaming_limit_reached()) {
|
|
|
|
return Status::Error("FILE_DOWNLOAD_LIMIT");
|
|
|
|
}
|
2019-02-27 17:00:10 +01:00
|
|
|
return Status::Error("File transferring not finished");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartsManager::update_first_empty_part() {
|
|
|
|
while (first_empty_part_ < part_count_ && part_status_[first_empty_part_] != PartStatus::Empty) {
|
|
|
|
first_empty_part_++;
|
|
|
|
}
|
2018-11-11 12:38:04 +01:00
|
|
|
|
|
|
|
if (streaming_offset_ == 0) {
|
|
|
|
first_streaming_empty_part_ = first_empty_part_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (first_streaming_empty_part_ < part_count_ && part_status_[first_streaming_empty_part_] != PartStatus::Empty) {
|
|
|
|
first_streaming_empty_part_++;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartsManager::update_first_not_ready_part() {
|
|
|
|
while (first_not_ready_part_ < part_count_ && part_status_[first_not_ready_part_] == PartStatus::Ready) {
|
|
|
|
first_not_ready_part_++;
|
|
|
|
}
|
2019-02-26 15:14:07 +01:00
|
|
|
if (streaming_offset_ == 0) {
|
|
|
|
first_streaming_not_ready_part_ = first_not_ready_part_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (first_streaming_not_ready_part_ < part_count_ &&
|
|
|
|
part_status_[first_streaming_not_ready_part_] == PartStatus::Ready) {
|
|
|
|
first_streaming_not_ready_part_++;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-05-19 23:56:40 +02:00
|
|
|
int32 PartsManager::get_unchecked_ready_prefix_count() {
|
2018-12-31 20:04:05 +01:00
|
|
|
update_first_not_ready_part();
|
2018-05-19 23:56:40 +02:00
|
|
|
return first_not_ready_part_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 PartsManager::get_ready_prefix_count() {
|
|
|
|
auto res = get_unchecked_ready_prefix_count();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (need_check_) {
|
|
|
|
auto checked_parts = narrow_cast<int32>(checked_prefix_size_ / part_size_);
|
|
|
|
if (checked_parts < res) {
|
2018-05-19 23:56:40 +02:00
|
|
|
return checked_parts;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2020-10-05 17:07:23 +02:00
|
|
|
|
2019-07-30 20:27:39 +02:00
|
|
|
int64 PartsManager::get_streaming_offset() const {
|
|
|
|
return streaming_offset_;
|
|
|
|
}
|
2020-10-05 17:07:23 +02:00
|
|
|
|
2018-12-27 19:06:30 +01:00
|
|
|
string PartsManager::get_bitmask() {
|
2018-12-27 10:12:24 +01:00
|
|
|
int32 prefix_count = -1;
|
|
|
|
if (need_check_) {
|
|
|
|
prefix_count = narrow_cast<int32>(checked_prefix_size_ / part_size_);
|
|
|
|
}
|
|
|
|
return bitmask_.encode(prefix_count);
|
2018-11-11 12:38:04 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-26 15:14:07 +01:00
|
|
|
bool PartsManager::is_part_in_streaming_limit(int part_i) const {
|
2020-08-21 14:54:18 +02:00
|
|
|
CHECK(part_i < part_count_);
|
2019-04-28 14:00:06 +02:00
|
|
|
auto offset_begin = static_cast<int64>(part_i) * static_cast<int64>(get_part_size());
|
|
|
|
auto offset_end = offset_begin + static_cast<int64>(get_part(part_i).size);
|
2019-02-26 15:14:07 +01:00
|
|
|
|
|
|
|
if (offset_begin >= get_expected_size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (streaming_limit_ == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-28 04:14:21 +02:00
|
|
|
auto is_intersect_with = [&](int64 begin, int64 end) {
|
|
|
|
return max(begin, offset_begin) < min(end, offset_end);
|
|
|
|
};
|
2019-02-26 15:14:07 +01:00
|
|
|
|
|
|
|
auto streaming_begin = streaming_offset_;
|
|
|
|
auto streaming_end = streaming_offset_ + streaming_limit_;
|
|
|
|
if (is_intersect_with(streaming_begin, streaming_end)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// wrap limit
|
|
|
|
if (!unknown_size_flag_ && streaming_end > get_size() && is_intersect_with(0, streaming_end - get_size())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PartsManager::is_streaming_limit_reached() {
|
|
|
|
if (streaming_limit_ == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
update_first_not_ready_part();
|
|
|
|
auto part_i = first_streaming_not_ready_part_;
|
|
|
|
|
|
|
|
// wrap
|
|
|
|
if (!unknown_size_flag_ && part_i == part_count_) {
|
|
|
|
part_i = first_not_ready_part_;
|
|
|
|
}
|
2020-08-21 14:54:18 +02:00
|
|
|
if (part_i == part_count_) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-26 15:14:07 +01:00
|
|
|
return !is_part_in_streaming_limit(part_i);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<Part> PartsManager::start_part() {
|
|
|
|
update_first_empty_part();
|
2018-11-11 12:38:04 +01:00
|
|
|
auto part_i = first_streaming_empty_part_;
|
2018-12-26 22:41:52 +01:00
|
|
|
if (known_prefix_flag_ && part_i >= static_cast<int>(known_prefix_size_ / part_size_)) {
|
2022-09-20 01:02:28 +02:00
|
|
|
return Status::Error(-1, "Wait for prefix to be known");
|
2018-12-26 22:41:52 +01:00
|
|
|
}
|
2018-11-11 12:38:04 +01:00
|
|
|
if (part_i == part_count_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (unknown_size_flag_) {
|
2018-12-26 22:41:52 +01:00
|
|
|
part_count_++;
|
2022-05-11 16:17:20 +02:00
|
|
|
if (part_count_ > MAX_PART_COUNT_PREMIUM + (use_part_count_limit_ ? 0 : 64)) {
|
2019-08-19 11:31:25 +02:00
|
|
|
if (!is_upload_) {
|
|
|
|
// Caller will try to increase part size if it is possible
|
|
|
|
return Status::Error("FILE_DOWNLOAD_RESTART_INCREASE_PART_SIZE");
|
|
|
|
}
|
2018-12-26 22:41:52 +01:00
|
|
|
return Status::Error("Too big file with unknown size");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-26 22:41:52 +01:00
|
|
|
part_status_.push_back(PartStatus::Empty);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2018-11-11 12:38:04 +01:00
|
|
|
if (first_empty_part_ < part_count_) {
|
|
|
|
part_i = first_empty_part_;
|
|
|
|
} else {
|
|
|
|
return get_empty_part();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-18 20:08:05 +01:00
|
|
|
|
2019-02-26 15:14:07 +01:00
|
|
|
if (!is_part_in_streaming_limit(part_i)) {
|
|
|
|
return get_empty_part();
|
2019-02-18 20:08:05 +01:00
|
|
|
}
|
2018-11-11 12:38:04 +01:00
|
|
|
CHECK(part_status_[part_i] == PartStatus::Empty);
|
|
|
|
on_part_start(part_i);
|
|
|
|
return get_part(part_i);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 13:33:02 +01:00
|
|
|
Status PartsManager::set_known_prefix(size_t size, bool is_ready) {
|
2018-12-27 07:45:19 +01:00
|
|
|
if (!known_prefix_flag_ || size < static_cast<size_t>(known_prefix_size_)) {
|
2019-07-31 17:04:38 +02:00
|
|
|
CHECK(is_upload_);
|
2018-12-26 22:41:52 +01:00
|
|
|
return Status::Error("FILE_UPLOAD_RESTART");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
known_prefix_size_ = narrow_cast<int64>(size);
|
2018-02-12 11:37:54 +01:00
|
|
|
expected_size_ = max(known_prefix_size_, expected_size_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
CHECK(static_cast<size_t>(part_count_) == part_status_.size());
|
|
|
|
if (is_ready) {
|
2018-04-26 22:41:34 +02:00
|
|
|
part_count_ = static_cast<int>(calc_part_count(size, part_size_));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
size_ = narrow_cast<int64>(size);
|
|
|
|
unknown_size_flag_ = false;
|
2018-12-26 22:41:52 +01:00
|
|
|
known_prefix_flag_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
part_count_ = static_cast<int>(size / part_size_);
|
|
|
|
}
|
2019-07-31 15:48:59 +02:00
|
|
|
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(static_cast<size_t>(part_count_) >= part_status_.size())
|
2018-01-28 21:38:55 +01:00
|
|
|
<< size << " " << is_ready << " " << part_count_ << " " << part_size_ << " " << part_status_.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
part_status_.resize(part_count_);
|
2022-05-11 16:17:20 +02:00
|
|
|
if (use_part_count_limit_ && part_size_ < MAX_PART_SIZE &&
|
|
|
|
calc_part_count(expected_size_, part_size_) > MAX_PART_COUNT) {
|
2019-07-31 17:04:38 +02:00
|
|
|
CHECK(is_upload_);
|
2018-01-30 13:33:02 +01:00
|
|
|
return Status::Error("FILE_UPLOAD_RESTART");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status PartsManager::on_part_ok(int32 id, size_t part_size, size_t actual_size) {
|
|
|
|
CHECK(part_status_[id] == PartStatus::Pending);
|
|
|
|
pending_count_--;
|
|
|
|
|
|
|
|
part_status_[id] = PartStatus::Ready;
|
2018-12-28 14:16:14 +01:00
|
|
|
if (actual_size != 0) {
|
|
|
|
bitmask_.set(id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
ready_size_ += narrow_cast<int64>(actual_size);
|
2019-02-27 17:00:10 +01:00
|
|
|
if (streaming_limit_ > 0 && is_part_in_streaming_limit(id)) {
|
|
|
|
streaming_ready_size_ += narrow_cast<int64>(actual_size);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-05 21:08:24 +02:00
|
|
|
VLOG(file_loader) << "Transferred part " << id << " of size " << part_size << ", total ready size = " << ready_size_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int64 offset = narrow_cast<int64>(part_size_) * id;
|
|
|
|
int64 end_offset = offset + narrow_cast<int64>(actual_size);
|
|
|
|
if (unknown_size_flag_) {
|
|
|
|
CHECK(part_size == part_size_);
|
|
|
|
if (actual_size < part_size_) {
|
2018-02-12 11:37:54 +01:00
|
|
|
max_size_ = min(max_size_, end_offset);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (actual_size) {
|
2018-02-12 11:37:54 +01:00
|
|
|
min_size_ = max(min_size_, end_offset);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (min_size_ > max_size_) {
|
|
|
|
auto status = Status::Error(PSLICE() << "Failed to transfer file: " << tag("min_size", min_size_)
|
|
|
|
<< tag("max_size", max_size_));
|
|
|
|
LOG(ERROR) << status;
|
|
|
|
return status;
|
|
|
|
} else if (min_size_ == max_size_) {
|
|
|
|
unknown_size_flag_ = false;
|
|
|
|
size_ = min_size_;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((actual_size < part_size && offset < size_) || (offset >= size_ && actual_size > 0)) {
|
|
|
|
auto status = Status::Error(PSLICE() << "Failed to transfer file: " << tag("size", size_) << tag("offset", offset)
|
|
|
|
<< tag("transferred size", actual_size) << tag("part size", part_size));
|
|
|
|
LOG(ERROR) << status;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PartsManager::on_part_failed(int32 id) {
|
|
|
|
CHECK(part_status_[id] == PartStatus::Pending);
|
|
|
|
pending_count_--;
|
|
|
|
part_status_[id] = PartStatus::Empty;
|
|
|
|
if (id < first_empty_part_) {
|
|
|
|
first_empty_part_ = id;
|
|
|
|
}
|
2018-11-11 12:38:04 +01:00
|
|
|
if (streaming_offset_ == 0) {
|
|
|
|
first_streaming_empty_part_ = id;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto part_i = narrow_cast<int>(streaming_offset_ / part_size_);
|
|
|
|
if (id >= part_i && id < first_streaming_empty_part_) {
|
|
|
|
first_streaming_empty_part_ = id;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int64 PartsManager::get_size() const {
|
|
|
|
CHECK(!unknown_size_flag_);
|
|
|
|
return size_;
|
|
|
|
}
|
2020-10-05 17:07:23 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 PartsManager::get_size_or_zero() const {
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
|
2019-02-18 20:08:05 +01:00
|
|
|
int64 PartsManager::get_estimated_extra() const {
|
|
|
|
auto total_estimated_extra = get_expected_size() - get_ready_size();
|
|
|
|
if (streaming_limit_ != 0) {
|
2019-02-26 15:14:07 +01:00
|
|
|
int64 expected_size = get_expected_size();
|
|
|
|
int64 streaming_begin = streaming_offset_ / get_part_size() * get_part_size();
|
|
|
|
int64 streaming_end =
|
|
|
|
(streaming_offset_ + streaming_limit_ + get_part_size() - 1) / get_part_size() * get_part_size();
|
|
|
|
int64 streaming_size = streaming_end - streaming_begin;
|
|
|
|
if (unknown_size_flag_) {
|
|
|
|
if (streaming_begin < expected_size) {
|
|
|
|
streaming_size = min(expected_size - streaming_begin, streaming_size);
|
|
|
|
} else {
|
|
|
|
streaming_size = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (streaming_end > expected_size) {
|
|
|
|
int64 total = streaming_limit_;
|
|
|
|
int64 suffix = 0;
|
|
|
|
if (streaming_offset_ < expected_size_) {
|
|
|
|
suffix = expected_size_ - streaming_begin;
|
|
|
|
total -= expected_size_ - streaming_offset_;
|
|
|
|
}
|
|
|
|
int64 prefix = (total + get_part_size() - 1) / get_part_size() * get_part_size();
|
|
|
|
streaming_size = min(expected_size, prefix + suffix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int64 res = streaming_size;
|
2019-02-27 17:00:10 +01:00
|
|
|
|
|
|
|
//TODO: delete this block if CHECK won't fail
|
|
|
|
int64 sub = 0;
|
2019-02-26 15:14:07 +01:00
|
|
|
for (int part_i = 0; part_i < part_count_; part_i++) {
|
|
|
|
if (is_part_in_streaming_limit(part_i) && part_status_[part_i] == PartStatus::Ready) {
|
2019-02-27 17:00:10 +01:00
|
|
|
sub += get_part(part_i).size;
|
2019-02-26 15:14:07 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-27 17:00:10 +01:00
|
|
|
CHECK(sub == streaming_ready_size_);
|
|
|
|
|
|
|
|
res -= streaming_ready_size_;
|
2019-02-26 15:14:07 +01:00
|
|
|
CHECK(res >= 0);
|
|
|
|
return res;
|
2019-02-18 20:08:05 +01:00
|
|
|
}
|
|
|
|
return total_estimated_extra;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 PartsManager::get_ready_size() const {
|
|
|
|
return ready_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 PartsManager::get_expected_size() const {
|
|
|
|
if (unknown_size_flag_) {
|
2018-02-12 11:37:54 +01:00
|
|
|
return max(static_cast<int64>(512 * (1 << 10)), get_ready_size() * 2);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return get_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t PartsManager::get_part_size() const {
|
|
|
|
return part_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 PartsManager::get_part_count() const {
|
|
|
|
return part_count_;
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:48:59 +02:00
|
|
|
Status PartsManager::init_common(const std::vector<int> &ready_parts) {
|
2018-12-31 20:04:05 +01:00
|
|
|
ready_size_ = 0;
|
2019-02-27 17:00:10 +01:00
|
|
|
streaming_ready_size_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
pending_count_ = 0;
|
|
|
|
first_empty_part_ = 0;
|
|
|
|
first_not_ready_part_ = 0;
|
|
|
|
part_status_ = vector<PartStatus>(part_count_);
|
|
|
|
|
|
|
|
for (auto i : ready_parts) {
|
2019-07-31 17:04:38 +02:00
|
|
|
if (known_prefix_flag_ && i >= static_cast<int>(known_prefix_size_ / part_size_)) {
|
|
|
|
CHECK(is_upload_);
|
|
|
|
return Status::Error("FILE_UPLOAD_RESTART");
|
|
|
|
}
|
|
|
|
if (is_upload_ && i >= part_count_) {
|
|
|
|
return Status::Error("FILE_UPLOAD_RESTART");
|
|
|
|
}
|
2019-07-30 20:27:39 +02:00
|
|
|
LOG_CHECK(0 <= i && i < part_count_) << tag("i", i) << tag("part_count", part_count_) << tag("size", size_)
|
|
|
|
<< tag("part_size", part_size_) << tag("known_prefix_flag", known_prefix_flag_)
|
2019-07-31 15:48:59 +02:00
|
|
|
<< tag("known_prefix_size", known_prefix_size_)
|
2019-07-30 20:27:39 +02:00
|
|
|
<< tag("real part_count",
|
|
|
|
std::accumulate(ready_parts.begin(), ready_parts.end(), 0,
|
|
|
|
[](auto a, auto b) { return max(a, b + 1); }));
|
2018-12-31 20:04:05 +01:00
|
|
|
part_status_[i] = PartStatus::Ready;
|
2018-11-11 12:38:04 +01:00
|
|
|
bitmask_.set(i);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto part = get_part(i);
|
|
|
|
ready_size_ += narrow_cast<int64>(part.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
checked_prefix_size_ = get_ready_prefix_count() * narrow_cast<int64>(part_size_);
|
2019-07-31 15:48:59 +02:00
|
|
|
|
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartsManager::set_need_check() {
|
|
|
|
need_check_ = true;
|
2020-08-25 17:58:37 +02:00
|
|
|
set_streaming_offset(0, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartsManager::set_checked_prefix_size(int64 size) {
|
|
|
|
checked_prefix_size_ = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 PartsManager::get_checked_prefix_size() const {
|
|
|
|
return checked_prefix_size_;
|
|
|
|
}
|
2020-10-05 17:07:23 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 PartsManager::get_unchecked_ready_prefix_size() {
|
|
|
|
update_first_not_ready_part();
|
|
|
|
auto count = first_not_ready_part_;
|
|
|
|
if (count == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto part = get_part(count - 1);
|
|
|
|
int64 res = part.offset;
|
|
|
|
if (!unknown_size_flag_) {
|
|
|
|
res += narrow_cast<int64>(part.size);
|
2018-02-12 11:37:54 +01:00
|
|
|
res = min(res, get_size());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-02-26 15:14:07 +01:00
|
|
|
Part PartsManager::get_part(int id) const {
|
2021-10-19 17:11:16 +02:00
|
|
|
auto size = narrow_cast<int64>(part_size_);
|
|
|
|
auto offset = size * id;
|
2019-02-27 17:00:10 +01:00
|
|
|
auto total_size = unknown_size_flag_ ? max_size_ : get_size();
|
|
|
|
if (total_size < offset) {
|
|
|
|
size = 0;
|
|
|
|
} else {
|
|
|
|
size = min(size, total_size - offset);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return Part{id, offset, static_cast<size_t>(size)};
|
|
|
|
}
|
|
|
|
|
|
|
|
Part PartsManager::get_empty_part() {
|
|
|
|
return Part{-1, 0, 0};
|
|
|
|
}
|
|
|
|
|
|
|
|
void PartsManager::on_part_start(int32 id) {
|
|
|
|
CHECK(part_status_[id] == PartStatus::Empty);
|
|
|
|
part_status_[id] = PartStatus::Pending;
|
|
|
|
pending_count_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|