Skip to content

Commit

Permalink
fix: truncate job history dir path length
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Wong <[email protected]>
  • Loading branch information
joel-wong-aws committed Jan 17, 2025
1 parent e49965b commit a69e61a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/deadline/client/job_bundle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ def create_job_history_bundle_dir(submitter_name: str, job_name: str) -> str:
char for char in submitter_name if char.isalnum() or char in " -_"
)

# Clean the job_name's characters and truncate for the filename
job_name_cleaned = "".join(char for char in job_name if char.isalnum() or char in " -_")
job_name_cleaned = job_name_cleaned[:128]

timestamp = datetime.datetime.now()
month_tag = timestamp.strftime("%Y-%m")
date_tag = timestamp.strftime("%Y-%m-%d")
Expand All @@ -53,8 +49,17 @@ def create_job_history_bundle_dir(submitter_name: str, job_name: str) -> str:
latest_dir = existing_dirs[-1]
number = int(os.path.basename(latest_dir)[len(date_tag) + 1 :].split("-", 1)[0]) + 1

result = os.path.join(
month_dir, f"{date_tag}-{number:02}-{submitter_name_cleaned}-{job_name_cleaned}"
)
job_dir_prefix = f"{date_tag}-{number:02}-{submitter_name_cleaned}-"

# max path length - manifest file name
# 256 - len("\manifests\d2b2c3102af5a862db950a2e30255429_input")
# = 207
max_job_name_prefix = 207 - len(os.path.abspath(os.path.join(month_dir, job_dir_prefix)))

# Clean the job_name's characters and truncate for the filename
job_name_cleaned = "".join(char for char in job_name if char.isalnum() or char in " -_")
job_name_cleaned = job_name_cleaned[: min(128, max_job_name_prefix)]

result = os.path.join(month_dir, f"{job_dir_prefix}{job_name_cleaned}")
os.makedirs(result)
return result
58 changes: 58 additions & 0 deletions test/unit/deadline_client/job_bundle/test_job_history_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import os
import sys
import tempfile
import pytest

Expand Down Expand Up @@ -55,6 +56,63 @@ def test_create_job_bundle_dir(fresh_deadline_config):
assert sorted(os.listdir(os.path.join(tmpdir, "2023-04"))) == EXPECTED_DIRS[3:]


def test_job_name_truncation_from_long_job_history_dir(fresh_deadline_config):
# Use a temporary directory for the job history
with tempfile.TemporaryDirectory() as tmpdir, freeze_time("2024-12-27"):
long_tmpdir = os.path.join(
tmpdir,
"dir1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567",
)
config.set_setting("settings.job_history_dir", long_tmpdir)
long_tmpdir_path_length = len(os.path.abspath(long_tmpdir))
assert 128 < long_tmpdir_path_length < 196 # smoke test for path length
assert os.path.isdir(long_tmpdir) # smoke test that path was created

output_path = job_bundle.create_job_history_bundle_dir(
"MySubmitter",
"job123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
)

assert len(os.path.abspath(output_path)) == 207
assert str(os.path.abspath(output_path)).startswith(
os.path.abspath(os.path.join(long_tmpdir, "2024-12-27-MySubmitter-job1234567890"))
)
assert (
len(
os.path.abspath(
os.path.join(output_path, "manifests", "d2b2c3102af5a862db950a2e30255429_input")
)
)
== 256
)
assert os.path.isdir(output_path)


def test_job_name_truncation_from_long_job_name(fresh_deadline_config):
# Use a temporary directory for the job history
with tempfile.TemporaryDirectory() as tmpdir, freeze_time("2024-08-26"):
config.set_setting("settings.job_history_dir", tmpdir)

job_name_exceeding_128_chars = "test12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
output_path = job_bundle.create_job_history_bundle_dir(
"SubmitterFour",
job_name_exceeding_128_chars,
)
if sys.platform == "darwin":
assert (
str(output_path)
== str(
os.path.join(tmpdir, f"2024-12-27-SubmitterFour-{job_name_exceeding_128_chars}")
)[:207]
)
else:
assert output_path == os.path.join(
tmpdir,
"2024-12-27-SubmitterFour-test1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234",
)
assert os.path.isdir(output_path)


@pytest.mark.parametrize(
"submitter_name, job_name, freeze_date, expected_output_path",
[
Expand Down

0 comments on commit a69e61a

Please sign in to comment.