Skip to content

Commit de4b02e

Browse files
committed
uploader: clarify worker and reporting models
Use domain-specific names for telemetry, result, resource, and legacy-report state. Remove a redundant report wrapper and unused telemetry property while preserving configuration precedence and output contracts. Validated with the 449-test Python tooling suite, all 199 //tools tests, template lint, compileall, and git diff checks.
1 parent d5e2a97 commit de4b02e

8 files changed

Lines changed: 194 additions & 189 deletions

File tree

tools/core/uploader_py/config.py

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ def parse_uploader_config(
161161
"""Resolve generated config, environment, and CLI using public precedence."""
162162
env = dict(os.environ if environ is None else environ)
163163
args = _parser().parse_args(list(argv))
164+
165+
def text_option(
166+
cli_value: str | None,
167+
variable: str,
168+
default: str = "",
169+
) -> str:
170+
"""Prefer an explicit CLI value to its environment fallback."""
171+
return cli_value if cli_value is not None else env.get(variable) or default
172+
164173
config_path = Path(args.config)
165174
rule = load_rule_config(config_path)
166175
invocation_cwd = Path(cwd or Path.cwd()).absolute()
@@ -207,25 +216,20 @@ def parse_uploader_config(
207216
if args.validate_enrichment and not args.dry_run:
208217
raise ConfigError("--validate-enrichment requires --dry-run")
209218

210-
freshness_source_value = (
211-
args.freshness_source
212-
if args.freshness_source is not None
213-
else env.get("DD_TEST_OPTIMIZATION_FRESHNESS_SOURCE") or "auto"
219+
freshness_source_value = text_option(
220+
args.freshness_source,
221+
"DD_TEST_OPTIMIZATION_FRESHNESS_SOURCE",
222+
"auto",
214223
)
215224
freshness_source = _choice(
216225
"--freshness-source/DD_TEST_OPTIMIZATION_FRESHNESS_SOURCE",
217226
freshness_source_value,
218227
VALID_FRESHNESS_SOURCES,
219228
)
220-
new_freshness_mode = (
221-
args.freshness_mode
222-
if args.freshness_mode is not None
223-
else env.get("DD_TEST_OPTIMIZATION_FRESHNESS_MODE") or None
224-
)
225-
legacy_freshness_mode = (
226-
args.execution_log_mode
227-
if args.execution_log_mode is not None
228-
else env.get("DD_TEST_OPTIMIZATION_EXECUTION_LOG_MODE") or None
229+
new_freshness_mode = text_option(args.freshness_mode, "DD_TEST_OPTIMIZATION_FRESHNESS_MODE")
230+
legacy_freshness_mode = text_option(
231+
args.execution_log_mode,
232+
"DD_TEST_OPTIMIZATION_EXECUTION_LOG_MODE",
229233
)
230234
freshness_mode = _choice(
231235
"--freshness-mode/DD_TEST_OPTIMIZATION_FRESHNESS_MODE",
@@ -235,40 +239,39 @@ def parse_uploader_config(
235239
if args.allow_cached_payload_uploads:
236240
freshness_mode = "disabled"
237241

238-
artifact_source_value = (
239-
args.artifact_source
240-
if args.artifact_source is not None
241-
else env.get("DD_TEST_OPTIMIZATION_ARTIFACT_SOURCE") or "local"
242+
artifact_source_value = text_option(
243+
args.artifact_source,
244+
"DD_TEST_OPTIMIZATION_ARTIFACT_SOURCE",
245+
"local",
242246
)
243247
artifact_source = _choice(
244248
"--artifact-source/DD_TEST_OPTIMIZATION_ARTIFACT_SOURCE",
245249
artifact_source_value,
246250
VALID_ARTIFACT_SOURCES,
247251
)
248-
remote_artifacts_value = (
249-
args.remote_artifacts
250-
if args.remote_artifacts is not None
251-
else env.get("DD_TEST_OPTIMIZATION_REMOTE_ARTIFACTS") or "disabled"
252+
remote_artifacts_value = text_option(
253+
args.remote_artifacts,
254+
"DD_TEST_OPTIMIZATION_REMOTE_ARTIFACTS",
255+
"disabled",
252256
)
253257
remote_artifacts = _choice(
254258
"--remote-artifacts/DD_TEST_OPTIMIZATION_REMOTE_ARTIFACTS",
255259
remote_artifacts_value,
256260
VALID_REMOTE_ARTIFACT_MODES,
257261
)
258-
downloader_timeout_value = (
259-
args.bep_artifact_downloader_timeout_sec
260-
if args.bep_artifact_downloader_timeout_sec is not None
261-
else env.get("DD_TEST_OPTIMIZATION_BEP_ARTIFACT_DOWNLOADER_TIMEOUT_SEC") or "300"
262+
downloader_timeout_value = text_option(
263+
args.bep_artifact_downloader_timeout_sec,
264+
"DD_TEST_OPTIMIZATION_BEP_ARTIFACT_DOWNLOADER_TIMEOUT_SEC",
265+
"300",
262266
)
263267
downloader_timeout = _positive_decimal(
264268
"--bep-artifact-downloader-timeout-sec",
265269
downloader_timeout_value,
266270
)
267271

268-
staging_text = (
269-
args.artifact_staging_dir
270-
if args.artifact_staging_dir is not None
271-
else env.get("DD_TEST_OPTIMIZATION_ARTIFACT_STAGING_DIR", "")
272+
staging_text = text_option(
273+
args.artifact_staging_dir,
274+
"DD_TEST_OPTIMIZATION_ARTIFACT_STAGING_DIR",
272275
)
273276
artifact_staging_dir = (
274277
Path(staging_text)
@@ -284,21 +287,17 @@ def parse_uploader_config(
284287
bep_json_values.append(environment_bep)
285288
bep_json_values.extend(args.bep_json)
286289

287-
expected_enriched_tags = tuple(args.expected_enriched_tag) or DEFAULT_EXPECTED_ENRICHED_TAGS
288-
report_text = (
289-
args.report_json
290-
if args.report_json is not None
291-
else env.get("DD_TEST_OPTIMIZATION_UPLOADER_REPORT_JSON", "")
290+
expected_enriched_tags = (
291+
tuple(args.expected_enriched_tag) or DEFAULT_EXPECTED_ENRICHED_TAGS
292292
)
293-
execution_log_text = (
294-
args.execution_log_json
295-
if args.execution_log_json is not None
296-
else env.get("DD_TEST_OPTIMIZATION_EXECUTION_LOG_JSON", "")
293+
report_text = text_option(args.report_json, "DD_TEST_OPTIMIZATION_UPLOADER_REPORT_JSON")
294+
execution_log_text = text_option(
295+
args.execution_log_json,
296+
"DD_TEST_OPTIMIZATION_EXECUTION_LOG_JSON",
297297
)
298-
downloader_text = (
299-
args.bep_artifact_downloader
300-
if args.bep_artifact_downloader is not None
301-
else env.get("DD_TEST_OPTIMIZATION_BEP_ARTIFACT_DOWNLOADER", "")
298+
downloader_text = text_option(
299+
args.bep_artifact_downloader,
300+
"DD_TEST_OPTIMIZATION_BEP_ARTIFACT_DOWNLOADER",
302301
)
303302

304303
return UploaderConfig(

tools/core/uploader_py/coordinator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
@dataclass(frozen=True)
4141
class CoordinatorSettings:
42+
"""Worker-facing configuration with lifecycle-only options removed."""
43+
4244
workspace: Path
4345
workers: int
4446
dry_run: bool

tools/core/uploader_py/file_worker.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ class _TestRequest:
111111
content_encoding: str | None = None
112112

113113

114+
@dataclass(frozen=True)
115+
class _TelemetryRequest:
116+
"""One telemetry body paired with the headers derived from that body."""
117+
118+
body: Path
119+
headers: dict[str, str]
120+
121+
114122
def common_headers(
115123
runtime: WorkerRuntime,
116124
payload: Mapping[str, Any] | None = None,
@@ -294,7 +302,7 @@ def _process_test(
294302
warnings,
295303
)
296304

297-
base = dict(
305+
test_result_fields = dict(
298306
task_id=task.task_id,
299307
source_path=task.display_path,
300308
payload_type=task.payload_type,
@@ -317,7 +325,7 @@ def _process_test(
317325
return FileResult(
318326
status=FileStatus.SUCCEEDED,
319327
warning_codes=_unique_codes(warnings),
320-
**base,
328+
**test_result_fields,
321329
)
322330

323331
requests_attempted = 0
@@ -366,7 +374,7 @@ def _process_test(
366374
warning_codes=_unique_codes(warnings),
367375
failure_code=failure_code,
368376
failure_message=failure_message,
369-
**base,
377+
**test_result_fields,
370378
)
371379
requests_succeeded += 1
372380

@@ -383,7 +391,7 @@ def _process_test(
383391
retries=retries,
384392
source_deleted=deleted,
385393
warning_codes=_unique_codes(final_warnings),
386-
**base,
394+
**test_result_fields,
387395
)
388396

389397

@@ -572,7 +580,7 @@ def _process_coverage(
572580
except OSError as exc:
573581
return _failed(task, "coverage_payload_read_failed", type(exc).__name__)
574582

575-
base = dict(
583+
coverage_result_fields = dict(
576584
task_id=task.task_id,
577585
source_path=task.display_path,
578586
payload_type=task.payload_type,
@@ -611,7 +619,7 @@ def _process_coverage(
611619
task,
612620
f"dry-run prepared {prepared.content_length}-byte coverage multipart body",
613621
)
614-
return FileResult(status=FileStatus.SUCCEEDED, **base)
622+
return FileResult(status=FileStatus.SUCCEEDED, **coverage_result_fields)
615623

616624
result = transport.post_prepared_multipart(
617625
runtime.endpoints.coverage_url,
@@ -635,7 +643,7 @@ def _process_coverage(
635643
retries=result.retries,
636644
failure_code=failure_code,
637645
failure_message=failure_message,
638-
**base,
646+
**coverage_result_fields,
639647
)
640648

641649
deleted, cleanup_warning = _cleanup_source(task.source_path, runtime.keep_payloads)
@@ -647,7 +655,7 @@ def _process_coverage(
647655
retries=result.retries,
648656
source_deleted=deleted,
649657
warning_codes=(cleanup_warning,) if cleanup_warning else (),
650-
**base,
658+
**coverage_result_fields,
651659
)
652660

653661

@@ -694,7 +702,7 @@ def _process_telemetry(
694702
failure_message=metadata_failure,
695703
)
696704

697-
bodies: list[tuple[Path, dict[str, Any]]] = [(source_body, payload)]
705+
requests = [_TelemetryRequest(source_body, _telemetry_headers(runtime, payload))]
698706
if directive.create_synthetic:
699707
synthetic = _build_synthetic_telemetry(payload, directive, warnings)
700708
if synthetic is not None:
@@ -704,58 +712,56 @@ def _process_telemetry(
704712
)
705713
synthetic_path = task_directory / "telemetry_synthetic.json"
706714
synthetic_path.write_bytes(_compact_json_line(synthetic))
707-
bodies.append((synthetic_path, synthetic))
708-
prepared_headers = tuple(
709-
_telemetry_headers(runtime, body_payload)
710-
for _body_path, body_payload in bodies
711-
)
712-
_debug(runtime, task, f"prepared {len(bodies)} telemetry request(s)")
715+
requests.append(
716+
_TelemetryRequest(synthetic_path, _telemetry_headers(runtime, synthetic))
717+
)
718+
_debug(runtime, task, f"prepared {len(requests)} telemetry request(s)")
713719
if _debug_enabled(runtime):
714-
for index, (body_path, _body_payload) in enumerate(bodies, start=1):
720+
for index, request in enumerate(requests, start=1):
715721
try:
716-
body_bytes = body_path.stat().st_size
722+
body_bytes = request.body.stat().st_size
717723
except OSError as exc:
718724
_debug(
719725
runtime,
720726
task,
721-
f"telemetry request={index}/{len(bodies)} size unavailable: "
727+
f"telemetry request={index}/{len(requests)} size unavailable: "
722728
f"{type(exc).__name__}",
723729
)
724730
else:
725731
_debug(
726732
runtime,
727733
task,
728-
f"telemetry request={index}/{len(bodies)} bytes={body_bytes}",
734+
f"telemetry request={index}/{len(requests)} bytes={body_bytes}",
729735
)
730736

731-
base = dict(
737+
telemetry_result_fields = dict(
732738
task_id=task.task_id,
733739
source_path=task.display_path,
734740
payload_type=task.payload_type,
735-
requests_planned=len(bodies),
741+
requests_planned=len(requests),
736742
)
737743
if runtime.dry_run:
738-
for (body_path, _body_payload), headers in zip(bodies, prepared_headers):
744+
for request in requests:
739745
prepare_json_request(
740746
runtime.endpoints.telemetry_url,
741-
headers,
742-
body_path,
747+
request.headers,
748+
request.body,
743749
)
744750
_debug(runtime, task, "dry-run completed telemetry preparation without network")
745751
return FileResult(
746752
status=FileStatus.SUCCEEDED,
747753
warning_codes=_unique_codes(warnings),
748-
**base,
754+
**telemetry_result_fields,
749755
)
750756

751757
requests_attempted = 0
752758
requests_succeeded = 0
753759
retries = 0
754-
for (body_path, _body_payload), headers in zip(bodies, prepared_headers):
760+
for request in requests:
755761
result = transport.post_json(
756762
runtime.endpoints.telemetry_url,
757-
headers,
758-
body_path,
763+
request.headers,
764+
request.body,
759765
)
760766
requests_attempted += result.attempts
761767
retries += result.retries
@@ -778,7 +784,7 @@ def _process_telemetry(
778784
warning_codes=_unique_codes(warnings),
779785
failure_code=failure_code,
780786
failure_message=failure_message,
781-
**base,
787+
**telemetry_result_fields,
782788
)
783789
requests_succeeded += 1
784790

@@ -793,7 +799,7 @@ def _process_telemetry(
793799
retries=retries,
794800
source_deleted=deleted,
795801
warning_codes=_unique_codes(warnings),
796-
**base,
802+
**telemetry_result_fields,
797803
)
798804

799805

tools/core/uploader_py/freshness.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""Pre-worker BEP staging and freshness selection.
88
99
The doctor remains the source of truth for the non-trivial BEP and artifact
10-
carrier formats. This module loads that runtime once, snapshots its result in
10+
carrier formats. This module loads that runtime once, snapshots its result in
1111
immutable uploader models, and keeps all filesystem and policy decisions out
1212
of worker threads.
1313
"""
@@ -48,6 +48,8 @@ class RemoteOutput:
4848

4949
@dataclass(frozen=True)
5050
class FreshnessPlan:
51+
"""Immutable authorization snapshot used by discovery and postflight checks."""
52+
5153
selected_source: str = "none"
5254
eligibility_enabled: bool = False
5355
eligible_outputs: frozenset[tuple[str, str]] = frozenset()
@@ -62,6 +64,8 @@ class FreshnessPlan:
6264

6365
@dataclass(frozen=True)
6466
class FreshnessPreparation:
67+
"""Freshness plan plus any staged artifacts owned by this invocation."""
68+
6569
plan: FreshnessPlan
6670
scan_roots: tuple[ScanRoot, ...]
6771
staged_roots: tuple[Path, ...] = ()
@@ -175,6 +179,7 @@ def prepare_freshness(
175179
except SystemExit as exc:
176180
raise FreshnessError(_doctor_failure(doctor, exc)) from exc
177181
except BaseException:
182+
# Staging must not survive interrupts or unexpected doctor errors.
178183
if staged and staging_base is not None:
179184
doctor._cleanup_staged_bep_run_roots(
180185
staged,
@@ -196,6 +201,7 @@ def prepare_freshness(
196201
},
197202
)
198203
except BaseException:
204+
# Planning failures must obey the same ownership cleanup contract.
199205
if staged and doctor is not None and staging_base is not None:
200206
doctor._cleanup_staged_bep_run_roots(
201207
staged,

0 commit comments

Comments
 (0)