Skip to content

Commit

Permalink
Update retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
forsyth2 committed Sep 30, 2022
1 parent 4f54d5b commit 4b0a295
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions zstash/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,28 @@ def multiprocess_extract(
return failures


def check_sizes_match(cur, tfname):
match: bool
if cur and tars_table_exists(cur):
logger.info(f"{tfname} exists. Checking expected size matches actual size.")
actual_size = os.path.getsize(tfname)
name_only = os.path.split(tfname)[1]
cur.execute(f"select size from tars where name is '{name_only}';")
expected_size: int = cur.fetchall()[0][0]
if expected_size != actual_size:
logger.info(
f"{name_only}: expected size={expected_size} != {actual_size}=actual_size"
)
match = False
else:
# Sizes match
match = True
else:
# Cannot access size information; assume the sizes match.
match = True
return match


# FIXME: C901 'extractFiles' is too complex (33)
def extractFiles( # noqa: C901
files: List[FilesRow],
Expand Down Expand Up @@ -445,35 +467,30 @@ def extractFiles( # noqa: C901
hpss: str = config.hpss
else:
raise TypeError("Invalid config.hpss={}".format(config.hpss))
tries = args.retries + 1
tries: int = args.retries + 1
# Set to True to test the `--retries` option with a forced failure.
# Then run `python -m unittest tests.test_extract.TestExtract.testExtractRetries`
test_retry = False
test_retry: bool = False
while tries > 0:
tries -= 1
do_retrieve: bool

if not os.path.exists(tfname):
do_retrieve = True
else:
do_retrieve = not check_sizes_match(cur, tfname)

try:
if test_retry:
test_retry = False
raise RuntimeError
if not os.path.exists(tfname):
# Will need to retrieve from HPSS
if do_retrieve:
hpss_get(hpss, tfname, cache)
elif cur and tars_table_exists(cur):
logger.info(
f"{tfname} exists. Checking expected size matches actual size."
)
actual_size = os.path.getsize(tfname)
name_only = os.path.split(tfname)[1]
cur.execute(
f"select size from tars where name is '{name_only}';"
)
expected_size: int = cur.fetchall()[0][0]
if expected_size > actual_size:
logger.info(
f"{name_only}: expected size={expected_size} > {actual_size}=actual_size"
if not check_sizes_match(cur, tfname):
raise RuntimeError(
f"{tfname} size does not match expected size."
)
hpss_get(hpss, tfname, cache)
# `get` successful or not needed: no more tries needed
# `hpss_get` successful or not needed: no more tries needed
break
except RuntimeError as e:
if tries > 0:
Expand Down

0 comments on commit 4b0a295

Please sign in to comment.