Repository navigation
[db] S3 results upload #1032
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
Merged
Merged
[db] S3 results upload #1032
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a6a35f8
Add generic ObjectStore interface with an S3 implementation
jj10306 aa152fa
Add ResultsUploadReporter to publish results to object storage
jj10306 41b74dd
Register results_upload and order it after every other report
jj10306 c8027f3
Test the object store and results upload reporter
jj10306 6b80da2
Document the results_upload report
jj10306 4b9feca
Drop exclude from ResultsUploadConfig
jj10306 273a836
Validate bucket accessibility before upload, tighten exists() error h…
jj10306 fc4f9ea
Scope uploaded S3 keys under the system name
jj10306 85fc94e
Upload tree files concurrently instead of one at a time
jj10306 3ec947b
add upload time to upload stats
jj10306 9c4797e
address comments: rename results_upload to s3, move s3 config and rep…
jj10306 bc785cf
clean up uv.lock, handle 403 and 404 differently and handle stale tar…
jj10306 299e97d
Fix copyright year in new S3 upload files
jj10306 c4a5b58
clean uv.lock
jj10306 ab99744
update docs with env var info and remove module level comment
jj10306 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| # SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES | ||
| # Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import logging | ||
| import os | ||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| from pydantic import Field, model_validator | ||
| from typing_extensions import Self | ||
|
|
||
| from .core import Reporter | ||
| from .models.scenario import ReportConfig | ||
| from .reporter import TarballReporter | ||
| from .util.object_store import S3ObjectStore, join_key | ||
|
|
||
|
|
||
| class S3UploadConfig(ReportConfig): | ||
| """ | ||
| Configuration for uploading a scenario results directory to object storage. | ||
|
|
||
| Destination fields fall back to environment variables when not set in TOML, so a | ||
| cluster-wide destination can be supplied by the environment while a scenario can | ||
| still override it. Credentials are never read from here; boto3 resolves them from | ||
| its standard chain. | ||
| """ | ||
|
|
||
| bucket: str = Field(default_factory=lambda: os.getenv("CLOUDAI_S3_BUCKET", "")) | ||
| prefix: str = Field(default_factory=lambda: os.getenv("CLOUDAI_S3_PREFIX", "")) | ||
| endpoint_url: Optional[str] = Field(default_factory=lambda: os.getenv("CLOUDAI_S3_ENDPOINT_URL") or None) | ||
|
jj10306 marked this conversation as resolved.
|
||
| region: Optional[str] = None | ||
| upload_tree: bool = True | ||
| upload_tarball: bool = False | ||
| upload_concurrency: int = Field(default=8, ge=1) | ||
|
|
||
| @model_validator(mode="after") | ||
| def at_least_one_upload_mode(self) -> Self: | ||
| if not (self.upload_tree or self.upload_tarball): | ||
| raise ValueError("At least one of 'upload_tree' or 'upload_tarball' must be enabled.") | ||
| return self | ||
|
|
||
|
|
||
| class S3UploadReporter(Reporter): | ||
| """Uploads the scenario results directory to object storage.""" | ||
|
|
||
| def generate(self) -> None: | ||
| config = self.config | ||
| if not isinstance(config, S3UploadConfig): | ||
| logging.warning(f"Expected S3UploadConfig, got {type(config).__name__}, skipping results upload.") | ||
| return | ||
|
|
||
| if not config.bucket: | ||
| logging.warning( | ||
| "S3 upload is enabled but no bucket is configured. " | ||
| "Set 'bucket' in the report config or the CLOUDAI_S3_BUCKET environment variable." | ||
| ) | ||
| return | ||
|
|
||
| if not self.results_root.exists(): | ||
| logging.warning(f"Results directory {self.results_root} does not exist, skipping results upload.") | ||
| return | ||
|
|
||
| store = S3ObjectStore(bucket=config.bucket, endpoint_url=config.endpoint_url, region=config.region) | ||
| if not store.bucket_exists(): | ||
| logging.warning(f"Bucket '{config.bucket}' does not exist, skipping results upload.") | ||
| return | ||
|
|
||
| key_prefix = join_key(config.prefix, self.system.name, self.results_root.name) | ||
|
|
||
| if config.upload_tree: | ||
| stats = store.upload_directory(self.results_root, key_prefix, max_workers=config.upload_concurrency) | ||
| logging.info( | ||
| f"Uploaded {stats.files_uploaded} file(s), {stats.bytes_uploaded} byte(s) to {store.uri(key_prefix)} " | ||
| f"in {stats.duration_seconds:.2f}s" | ||
| ) | ||
| if stats.failures: | ||
| logging.warning( | ||
| f"Failed to upload {len(stats.failures)} file(s) to {store.uri(key_prefix)}, " | ||
| "see debug log for details" | ||
| ) | ||
|
|
||
| if config.upload_tarball: | ||
| self.upload_tarball(store, key_prefix) | ||
|
|
||
| def upload_tarball(self, store: S3ObjectStore, key_prefix: str) -> None: | ||
| """ | ||
| Upload a tarball of the results directory, (re)creating it if it is missing or stale. | ||
|
|
||
| TarballReporter only produces a tarball when a test run failed, so we cannot | ||
| assume one is already present. A leftover tarball from an earlier run may predate | ||
| regenerated reports, so it is reused only if nothing in the directory is newer. | ||
| """ | ||
| tarball_path = Path(str(self.results_root) + ".tgz") | ||
| if not tarball_path.exists() or self._tarball_is_stale(tarball_path): | ||
| TarballReporter(self.system, self.test_scenario, self.results_root, self.config).create_tarball( | ||
| self.results_root | ||
| ) | ||
|
|
||
| key = join_key(key_prefix, tarball_path.name) | ||
| try: | ||
| store.upload_file(tarball_path, key) | ||
| except Exception as e: | ||
| logging.warning(f"Failed to upload tarball to {store.uri(key)}, see debug log for details") | ||
| logging.debug(e, exc_info=True) | ||
| return | ||
| logging.info(f"Uploaded tarball to {store.uri(key)}") | ||
|
|
||
| def _tarball_is_stale(self, tarball_path: Path) -> bool: | ||
| """Whether anything in the results directory was modified after the tarball was written.""" | ||
| tarball_mtime = tarball_path.stat().st_mtime | ||
| entries = [self.results_root, *self.results_root.rglob("*")] | ||
| return any(entry.stat().st_mtime > tarball_mtime for entry in entries) | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.