Skip to content

Commit 3694549

Browse files
committed
DRIVERS-3032 Add retries to download attempts
1 parent 1513f49 commit 3694549

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

.evergreen/mongodl.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import sys
2929
import tarfile
3030
import textwrap
31+
import time
3132
import urllib.error
3233
import urllib.request
3334
import warnings
@@ -832,6 +833,7 @@ def _dl_component(
832833
test: bool,
833834
no_download: bool,
834835
latest_build_branch: "str|None",
836+
retry: int,
835837
) -> ExpandResult:
836838
LOGGER.info(f"Download {component} {version}-{edition} for {target}-{arch}")
837839
if version in ("latest-build", "latest"):
@@ -863,7 +865,20 @@ def _dl_component(
863865
# This must go to stdout to be consumed by the calling program.
864866
print(dl_url)
865867
return None
866-
cached = cache.download_file(dl_url).path
868+
retries = retry
869+
while True:
870+
try:
871+
cached = cache.download_file(dl_url).path
872+
break
873+
except ConnectionResetError:
874+
retries -= 1
875+
if retries == 0:
876+
raise
877+
attempt = retry - retries
878+
LOGGER.warning(
879+
f"Download attempt failed, retry attempt {attempt} of {retry}"
880+
)
881+
time.sleep(attempt**2)
867882
return _expand_archive(cached, out_dir, pattern, strip_components, test=test)
868883

869884

@@ -1145,6 +1160,7 @@ def main(argv=None):
11451160
'download the with "--version=latest-build"',
11461161
metavar="BRANCH_NAME",
11471162
)
1163+
dl_grp.add_argument("--retry", help="The number of times to retry", default=0)
11481164
args = parser.parse_args(argv)
11491165
cache = Cache.open_in(args.cache_dir)
11501166
cache.refresh_full_json()
@@ -1184,6 +1200,7 @@ def main(argv=None):
11841200
test=args.test,
11851201
no_download=args.no_download,
11861202
latest_build_branch=args.latest_build_branch,
1203+
retry=int(args.retry),
11871204
)
11881205
if result is ExpandResult.Empty and args.empty_is_error:
11891206
sys.exit(1)

.evergreen/mongosh_dl.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import subprocess
1515
import sys
1616
import tempfile
17+
import time
1718
import urllib.request
1819
from pathlib import Path
1920

@@ -75,6 +76,7 @@ def _download(
7576
strip_components: int,
7677
test: bool,
7778
no_download: bool,
79+
retry: int,
7880
) -> int:
7981
LOGGER.info(f"Download {version} mongosh for {target}-{arch}")
8082
if version == "latest":
@@ -101,7 +103,20 @@ def _download(
101103
return ExpandResult.Okay
102104

103105
req = urllib.request.Request(dl_url)
104-
resp = urllib.request.urlopen(req)
106+
retries = retry
107+
while True:
108+
try:
109+
resp = urllib.request.urlopen(req)
110+
break
111+
except ConnectionResetError:
112+
retries -= 1
113+
if retries == 0:
114+
raise
115+
attempt = retry - retries
116+
LOGGER.warning(
117+
f"Download attempt failed, retry attempt {attempt} of {retry}"
118+
)
119+
time.sleep(attempt**2)
105120

106121
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as fp:
107122
buf = resp.read(1024 * 1024 * 4)
@@ -189,6 +204,7 @@ def main(argv=None):
189204
help="Do not extract or place any files/directories. "
190205
"Only print what will be extracted without placing any files.",
191206
)
207+
dl_grp.add_argument("--retry", help="The number of times to retry", default=0)
192208
args = parser.parse_args(argv)
193209

194210
target = args.target
@@ -214,6 +230,7 @@ def main(argv=None):
214230
strip_components=args.strip_components,
215231
test=args.test,
216232
no_download=args.no_download,
233+
retry=int(args.retry),
217234
)
218235
if result is ExpandResult.Empty:
219236
sys.exit(1)

.evergreen/orchestration/drivers_orchestration.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def run(opts):
194194
version = opts.version
195195
cache_dir = DRIVERS_TOOLS / ".local/cache"
196196
cache_dir_str = cache_dir.as_posix()
197-
default_args = f"--out {mdb_binaries_str} --cache-dir {cache_dir_str}"
197+
default_args = f"--out {mdb_binaries_str} --cache-dir {cache_dir_str} --retries 3"
198198
if opts.quiet:
199199
default_args += " -q"
200200
elif opts.verbose:
@@ -243,7 +243,7 @@ def run(opts):
243243
expansion_sh.write_text(crypt_text.replace(": ", "="))
244244

245245
# Download mongosh
246-
args = f"--out {mdb_binaries_str} --strip-path-components 2"
246+
args = f"--out {mdb_binaries_str} --strip-path-components 2 --retries 3"
247247
if opts.verbose:
248248
args += " -v"
249249
elif opts.quiet:

0 commit comments

Comments
 (0)