-
Notifications
You must be signed in to change notification settings - Fork 661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
retry failed elevation tile download #4461
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7a8c714
retry elevation tile download if the download failed for some reason …
nilsnolde a5298f1
changelog
nilsnolde 861be71
if successful, break the loop
nilsnolde f3b661a
Merge branch 'master' into nn-elevation-download
nilsnolde 0eee347
max retry of 7.5 mins over 5 tries
nilsnolde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,10 @@ from multiprocessing.dummy import Pool as ThreadPool | |
import os | ||
from pathlib import Path | ||
import sys | ||
from time import sleep | ||
from typing import List, Iterable, Set | ||
from urllib import request | ||
from urllib.error import URLError | ||
from urllib.error import URLError, HTTPError | ||
|
||
# hack so ArgumentParser can accept negative numbers | ||
# see https://github.com/valhalla/valhalla/issues/3426 | ||
|
@@ -53,6 +54,12 @@ class TileCompression(Enum): | |
return ".lz4" | ||
|
||
|
||
class DownloadStatus(Enum): | ||
OK = 1 | ||
FAILED = 2 | ||
CORRUPTED = 3 | ||
|
||
|
||
parser = argparse.ArgumentParser(description=description) | ||
method = parser.add_mutually_exclusive_group() | ||
method.add_argument( | ||
|
@@ -225,7 +232,7 @@ def get_tiles_with_graph(graph_dir: Path) -> Set[Tile]: | |
return tile_infos | ||
|
||
|
||
def download(tile: Tile, output_dir, compression: TileCompression): | ||
def download(tile: Tile, output_dir, compression: TileCompression) -> bool: | ||
dest_directory = Path(output_dir, tile.dir) | ||
dest_directory.mkdir(parents=True, exist_ok=True) | ||
|
||
|
@@ -237,39 +244,71 @@ def download(tile: Tile, output_dir, compression: TileCompression): | |
url = f"https://elevation-tiles-prod.s3.us-east-1.amazonaws.com/skadi/{tile.dir}/{tile.name}.gz" | ||
|
||
LOGGER.info(f"Downloading tile {tile.name}") | ||
try: | ||
with request.urlopen(url) as res, open(filepath, "wb") as f: | ||
if compression is TileCompression.GZIP: | ||
f.write(res.read()) | ||
else: | ||
with gzip.GzipFile(fileobj=res, mode="rb") as gz: | ||
uncompressed = gz.read() | ||
if compression is TileCompression.UNCOMPRESSED: | ||
f.write(uncompressed) | ||
elif compression is TileCompression.LZ4: | ||
# Compression level 6 was chosen after some benchmarking as the approx efficient frontier | ||
# between compression time and space savings (decompression time is roughly constant regardless | ||
# of level). The end result is larger than the maximally gzipped tiles from AWS, but only | ||
# by around 12%. | ||
import lz4.frame | ||
|
||
with lz4.frame.LZ4FrameCompressor( | ||
block_size=lz4.frame.BLOCKSIZE_MAX4MB, compression_level=6 | ||
) as compressor: | ||
# Optimization: we know the exact size of every uncompressed hgt file | ||
f.write(compressor.begin(25934402)) | ||
f.write(compressor.compress(uncompressed)) | ||
f.write(compressor.flush()) | ||
|
||
LOGGER.debug(f"Successfully downloaded tile {tile.name}") | ||
|
||
return True | ||
except URLError as e: | ||
LOGGER.critical(f"Download failed of elevation tile {tile.dir}/{tile.name}: {e.reason}") | ||
|
||
download_status = DownloadStatus.FAILED | ||
for i in range(5): | ||
try: | ||
LOGGER.debug(f"Downloading tile {tile.dir}/{tile.name} for the {i}th time.") | ||
|
||
# tries up to 24 mins with exponentially increasing sleeps in between, starting with 5 seconds | ||
sleep((i**2) / 2 * 30) | ||
with request.urlopen(url) as res, open(filepath, "wb") as f: | ||
|
||
if compression is TileCompression.GZIP: | ||
f.write(res.read()) | ||
else: | ||
with gzip.GzipFile(fileobj=res, mode="rb") as gz: | ||
try: | ||
uncompressed = gz.read() | ||
except Exception as e: | ||
Comment on lines
+261
to
+263
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. protect for corrupt downloads |
||
download_status = DownloadStatus.CORRUPTED | ||
LOGGER.error( | ||
f"Decompression error on tile {tile.dir}/{tile.name}: {e}. Likely a corrupted download." | ||
) | ||
continue | ||
if compression is TileCompression.UNCOMPRESSED: | ||
f.write(uncompressed) | ||
elif compression is TileCompression.LZ4: | ||
# Compression level 6 was chosen after some benchmarking as the approx efficient frontier | ||
# between compression time and space savings (decompression time is roughly constant regardless | ||
# of level). The end result is larger than the maximally gzipped tiles from AWS, but only | ||
# by around 12%. | ||
import lz4.frame | ||
|
||
with lz4.frame.LZ4FrameCompressor( | ||
block_size=lz4.frame.BLOCKSIZE_MAX4MB, compression_level=6 | ||
) as compressor: | ||
# Optimization: we know the exact size of every uncompressed hgt file | ||
f.write(compressor.begin(25934402)) | ||
f.write(compressor.compress(uncompressed)) | ||
f.write(compressor.flush()) | ||
|
||
download_status = DownloadStatus.OK | ||
LOGGER.debug(f"Successfully downloaded tile {tile.dir}/{tile.name}") | ||
break | ||
except HTTPError as e: | ||
LOGGER.error(f"Download failed with HTTP error {e.code}: {e.reason}.\nTrying again...") | ||
continue | ||
except URLError as e: | ||
LOGGER.error( | ||
f"Download failed of elevation tile {tile.dir}/{tile.name}: {e.reason}.\nTrying again.." | ||
) | ||
continue | ||
except ImportError: | ||
LOGGER.critical( | ||
"Could not import lz4. Please install lz4 or use another compression format." | ||
) | ||
sys.exit(1) | ||
|
||
if download_status == DownloadStatus.CORRUPTED: | ||
LOGGER.error(f"Tile {tile.dir}/{tile.name} was corrupted, removing it...") | ||
filepath.unlink() | ||
Comment on lines
+303
to
+305
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it was corrupted in the end, let's remove that tile from the file system |
||
return False | ||
except ImportError: | ||
LOGGER.critical("Could not import lz4. Please install lz4 or use another compression format.") | ||
sys.exit(1) | ||
elif download_status == DownloadStatus.FAILED: | ||
LOGGER.error(f"Tile {tile.dir}/{tile.name} couldn't be downloaded...") | ||
return False | ||
|
||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
most of the diff is just wrapping the previous logic into a for loop, where we request 15 times maximum with increased sleeps in between (exponential back off), up to 4.5 hours until we give up. could give much earlier though :D