Fix tests

This commit is contained in:
kssenii 2024-12-02 14:57:27 +01:00
parent ede05be071
commit 3a4f2ab890
3 changed files with 31 additions and 12 deletions

View File

@ -151,7 +151,7 @@ FileSegment & FileSegmentRangeWriter::allocateFileSegment(size_t offset, FileSeg
* File segment capacity will equal `max_file_segment_size`, but actual size is 0.
*/
CreateFileSegmentSettings create_settings(segment_kind);
CreateFileSegmentSettings create_settings(segment_kind, /* write_through_cache */true);
/// We set max_file_segment_size to be downloaded,
/// if we have less size to write, file segment will be resized in complete() method.

View File

@ -64,6 +64,7 @@ FileSegment::FileSegment(
, segment_range(offset_, offset_ + size_ - 1)
, segment_kind(settings.kind)
, is_unbound(settings.unbounded)
, created_from_write_through_cache(settings.write_through_cache)
, background_download_enabled(background_download_enabled_)
, download_state(download_state_)
, key_metadata(key_metadata_)
@ -645,6 +646,9 @@ void FileSegment::completePartAndResetDownloader()
void FileSegment::shrinkFileSegmentToDownloadedSize(const LockedKey & locked_key, const FileSegmentGuard::Lock & lock)
{
chassert(downloaded_size);
chassert(fs::file_size(getPath()) > 0);
if (downloaded_size == range().size())
{
/// Nothing to resize;
@ -659,26 +663,39 @@ void FileSegment::shrinkFileSegmentToDownloadedSize(const LockedKey & locked_key
getInfoForLog());
}
size_t aligned_downloaded_size = FileCacheUtils::roundUpToMultiple(downloaded_size, cache->getBoundaryAlignment());
chassert(aligned_downloaded_size >= downloaded_size);
size_t result_size = downloaded_size;
if (aligned_downloaded_size == range().size())
const bool shrink_to_aligned_size = !created_from_write_through_cache;
if (shrink_to_aligned_size)
{
size_t aligned_downloaded_size = FileCacheUtils::roundUpToMultiple(downloaded_size, cache->getBoundaryAlignment());
chassert(aligned_downloaded_size >= downloaded_size);
if (aligned_downloaded_size < range().size()
&& aligned_downloaded_size != downloaded_size)
{
result_size = aligned_downloaded_size;
}
}
chassert(result_size <= range().size());
chassert(result_size >= downloaded_size);
if (result_size == range().size())
{
/// Nothing to resize;
return;
}
else if (aligned_downloaded_size > range().size()
|| downloaded_size == aligned_downloaded_size)
if (downloaded_size == result_size)
{
/// Does not make sense to resize upwords.
/// Resize to already downloaded size.
setDownloadState(State::DOWNLOADED, lock);
segment_range.right = segment_range.left + downloaded_size - 1;
}
else
{
setDownloadState(State::PARTIALLY_DOWNLOADED, lock);
segment_range.right = segment_range.left + aligned_downloaded_size - 1;
}
segment_range.right = segment_range.left + result_size - 1;
const size_t diff = reserved_size - downloaded_size;
chassert(reserved_size >= downloaded_size);

View File

@ -33,11 +33,12 @@ struct CreateFileSegmentSettings
{
FileSegmentKind kind = FileSegmentKind::Regular;
bool unbounded = false;
bool write_through_cache = false;
CreateFileSegmentSettings() = default;
explicit CreateFileSegmentSettings(FileSegmentKind kind_)
: kind(kind_), unbounded(kind == FileSegmentKind::Ephemeral) {}
explicit CreateFileSegmentSettings(FileSegmentKind kind_, bool write_through_cache_ = false)
: kind(kind_), unbounded(kind == FileSegmentKind::Ephemeral), write_through_cache(write_through_cache_) {}
};
class FileSegment : private boost::noncopyable
@ -260,6 +261,7 @@ private:
/// can be bigger than max_file_segment_size.
/// is_unbound == true for temporary data in cache.
const bool is_unbound;
const bool created_from_write_through_cache;
const bool background_download_enabled;
std::atomic<State> download_state;