Skip to content
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

refactor(ingest/s3): enhance readability #12686

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

eagle-25
Copy link
Contributor

@eagle-25 eagle-25 commented Feb 20, 2025

  • Refactor S3Source().get_folder_info() to enhance readability
  • Add a test to ensure get_folder_info() returns the expected result.

Checklist

  • The PR conforms to DataHub's Contributing Guideline (particularly Commit Message Format)
  • Links to related issues (if applicable)
  • Tests for the changes have been added/updated (if applicable)
  • Docs related to the changes have been added/updated (if applicable). If a new feature has been added a Usage Guide has been added for the same.
  • For any breaking change/potential downtime/deprecation/big changes an entry has been made in Updating DataHub

@github-actions github-actions bot added ingestion PR or Issue related to the ingestion of metadata community-contribution PR or Issue raised by member(s) of DataHub Community labels Feb 20, 2025
@eagle-25 eagle-25 force-pushed the refactor/ingest/s3/enhance-redability-get_folder_info branch from 0fc14e6 to cade3f4 Compare February 20, 2025 01:45
Copy link

codecov bot commented Feb 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Files with missing lines Coverage Δ
...ngestion/src/datahub/ingestion/source/s3/source.py 86.58% <100.00%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9e18fa0...5952e30. Read the comment docs.

@@ -847,7 +847,7 @@ def get_folder_info(
path_spec: PathSpec,
bucket: "Bucket",
prefix: str,
) -> List[Folder]:
) -> Iterable[Folder]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed for memory efficiency

Comment on lines +880 to +882
if _is_allowed_path(
path_spec, self.create_s3_path(obj.bucket_name, obj.key)
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref

Comment on lines -901 to -905
if modification_time is None:
logger.warning(
f"Unable to find any files in the folder {key}. Skipping..."
)
continue
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this(L901~ 905) because groupby_unsorted never returns an empty group.

Comment on lines +888 to +901
for _, group in grouped_s3_objects_by_dirname:
max_file = max(group, key=lambda x: x.last_modified)
max_file_s3_path = self.create_s3_path(max_file.bucket_name, max_file.key)

# If partition_id is None, it means the folder is not a partition
partition_id = path_spec.get_partition_from_path(max_file_s3_path)

yield Folder(
partition_id=partition_id,
is_partition=bool(partition_id),
creation_time=min(obj.last_modified for obj in group),
modification_time=max_file.last_modified,
sample_file=max_file_s3_path,
size=sum(obj.size for obj in group),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before After
O(n) O(3n)

We got some performance loss but it is minimal. The time complexity has only increased from O(n) to O(3n).

The reason of O(3n) is this calls max(), min() and sum() in the each loop.

Comment on lines -911 to -916
# If id is None, it means the folder is not a partition
partitions.append(
Folder(
partition_id=id,
is_partition=bool(id),
creation_time=creation_time if creation_time else None, # type: ignore[arg-type]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need null handling. The type of creation_time is datetime.

  1. creation_time is the value of item.last_modified. (L896)
  2. The type of item is ObjectSummary
  3. The type of ObjectSummary.last_modified is datetime.

@eagle-25 eagle-25 marked this pull request as ready for review February 20, 2025 15:00
- Refactor S3Source().get_folder_info() to enhance readability
- Add a test to ensure that get_folder_info() returns the expected result.
@eagle-25 eagle-25 force-pushed the refactor/ingest/s3/enhance-redability-get_folder_info branch from cade3f4 to 5952e30 Compare February 20, 2025 15:00
@datahub-cyborg datahub-cyborg bot added the needs-review Label for PRs that need review from a maintainer. label Feb 20, 2025
@eagle-25
Copy link
Contributor Author

eagle-25 commented Feb 20, 2025

@treff7es
Could you give review in this pr pls? 🙏 (history)

@treff7es
Copy link
Contributor

@treff7es Could you give review in this pr pls? 🙏 (history)

sure, I will check. Thanks!

@datahub-cyborg datahub-cyborg bot added pending-submitter-response Issue/request has been reviewed but requires a response from the submitter and removed needs-review Label for PRs that need review from a maintainer. labels Feb 20, 2025
@eagle-25
Copy link
Contributor Author

Comment to change the label

@datahub-cyborg datahub-cyborg bot added needs-review Label for PRs that need review from a maintainer. and removed pending-submitter-response Issue/request has been reviewed but requires a response from the submitter labels Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution PR or Issue raised by member(s) of DataHub Community ingestion PR or Issue related to the ingestion of metadata needs-review Label for PRs that need review from a maintainer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants