downloading header from S3 correctly tracks progress

This commit is contained in:
Lex Berezhny 2019-02-18 16:44:46 -05:00
parent 03396c1993
commit 461c3c8408

View file

@ -147,40 +147,31 @@ class HeadersComponent(Component):
} }
async def fetch_headers_from_s3(self): async def fetch_headers_from_s3(self):
def collector(d, h_file):
h_file.write(d)
local_size = float(h_file.tell())
final_size = float(final_size_after_download)
self._headers_progress_percent = math.ceil(local_size / final_size * 100)
local_header_size = self.local_header_file_size() local_header_size = self.local_header_file_size()
resume_header = {"Range": f"bytes={local_header_size}-"} resume_header = {"Range": f"bytes={local_header_size}-"}
async with aiohttp.request('get', HEADERS_URL, headers=resume_header) as response: async with aiohttp.request('get', HEADERS_URL, headers=resume_header) as response:
got_406 = response.status == 406 # our file is bigger if response.status == 406 or response.content_length < HEADER_SIZE: # our file is bigger
final_size_after_download = response.content_length + local_header_size
if got_406:
log.warning("s3 is more out of date than we are") log.warning("s3 is more out of date than we are")
# should have something to download and a final length divisible by the header size return
elif final_size_after_download and not final_size_after_download % HEADER_SIZE: if response.content_length % HEADER_SIZE != 0:
s3_height = (final_size_after_download / HEADER_SIZE) - 1 log.warning("s3 appears to have corrupted header")
local_height = self.local_header_file_height() return
if s3_height > local_height: final_size_after_download = response.content_length + local_header_size
data = await response.read() write_mode = "wb"
if local_header_size > 0:
if local_header_size: log.info("Resuming download of %i bytes from s3", response.content_length)
log.info("Resuming download of %i bytes from s3", response.content_length) write_mode = "a+b"
with open(self.headers_file, "a+b") as headers_file: with open(self.headers_file, write_mode) as fd:
collector(data, headers_file) while True:
else: chunk = await response.content.read(512)
with open(self.headers_file, "wb") as headers_file: if not chunk:
collector(data, headers_file) break
log.info("fetched headers from s3 (s3 height: %i), now verifying integrity after download.", fd.write(chunk)
s3_height) self._headers_progress_percent = math.ceil(
self._check_header_file_integrity() float(fd.tell()) / float(final_size_after_download) * 100
else: )
log.warning("s3 is more out of date than we are") log.info("fetched headers from s3, now verifying integrity after download.")
else: self._check_header_file_integrity()
log.error("invalid size for headers from s3")
def local_header_file_height(self): def local_header_file_height(self):
return max((self.local_header_file_size() / HEADER_SIZE) - 1, 0) return max((self.local_header_file_size() / HEADER_SIZE) - 1, 0)