-
Notifications
You must be signed in to change notification settings - Fork 3
Prod 325 databricks rollovers #8
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d97d00d
[PROD-325] Added archive handling, updated project toml, tests pass!
rmoneys ca9f43a
[PROD-325] Appease Sonar
rmoneys 5688ef7
[PROD-325] Bug fixin, test adding
rmoneys 618f168
[PROD-325] More bug fixin, test adding
rmoneys 65bc437
[PROD-325] Missed a test file, added makefile and linting
rmoneys 2710733
[PROD-325] Maybe appease Sonar?
rmoneys 0a0f211
[PROD-325] Added support for unzipped and raw logs, and added tests
rmoneys 340dc97
[PROD-325] Don't build the log if it's already there
rmoneys e5f1974
[PROD-325] Extract extraction code, remove support for parsed log
rmoneys 38cc094
[PROD-325] Refactor to remove pydantic model that wasn't doing much, …
rmoneys de97dce
[PROD-325] Small clean-up
rmoneys 3e664cb
[PROD-325] Handle corner case
rmoneys bf12e1a
[PROD-325] Tiny clean-up
rmoneys 80814e8
[PROD-325] Removed one line. I'll stop
rmoneys 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| FILES := $(shell git diff --name-only --diff-filter=AM $$(git merge-base origin/main HEAD) -- \*.py) | ||
|
|
||
|
|
||
| .PHONY: test | ||
| test: | ||
| pytest | ||
|
|
||
| .PHONY: lint | ||
| lint: | ||
| flake8 --filename ./$(FILES) --max-complexity=10 --ignore=E501,W503 | ||
|
|
||
| .PHONY: format | ||
| format: | ||
| ifneq ("$(FILES)"," ") | ||
| black $(FILES) | ||
| isort $(FILES) | ||
| endif | ||
|
|
||
| .PHONY: tidy | ||
| tidy: format lint | ||
|
|
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 |
|---|---|---|
| @@ -1,37 +1,42 @@ | ||
| import argparse | ||
| import logging | ||
| import os | ||
| import sys | ||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| logging.captureWarnings(True) | ||
|
|
||
| from spark_log_parser.parsing_models.application_model_v2 import sparkApplication | ||
|
|
||
| import os | ||
| import argparse | ||
| import sys | ||
| from spark_log_parser.eventlog import EventLogBuilder # noqa: E402 | ||
| from spark_log_parser.parsing_models.application_model_v2 import sparkApplication # noqa: E402 | ||
|
|
||
| logger = logging.getLogger("spark_log_parser") | ||
|
|
||
| parser = argparse.ArgumentParser("spark_log_parser") | ||
| parser.add_argument("-l", "--log-file", required=True, help="path to event log") | ||
| parser.add_argument("-r", "--result-dir", required=True, help="path to directory in which to save parsed logs") | ||
| args = parser.parse_args() | ||
|
|
||
| print("\n" + "*" * 12 + " Running the Log Parser for Spark Predictor " + "*" * 12 + "\n") | ||
|
|
||
| log_path = os.path.abspath(args.log_file) | ||
| parser = argparse.ArgumentParser("spark_log_parser") | ||
| parser.add_argument("-l", "--log-file", required=True, type=Path, help="path to event log") | ||
| parser.add_argument( | ||
| "-r", "--result-dir", required=True, help="path to directory in which to save parsed logs" | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| if not os.path.isdir(args.result_dir): | ||
| logger.error("%s is not a directory", args.result_dir) | ||
| sys.exit(1) | ||
|
|
||
| print("\n--Processing log file: " + log_path) | ||
| print("\n" + "*" * 12 + " Running the Log Parser for Spark Predictor " + "*" * 12 + "\n") | ||
| print("--Processing log file: " + str(args.log_file)) | ||
|
|
||
| log_name = os.path.basename(log_path) | ||
| result_path = os.path.join(args.result_dir, "parsed-" + log_name) | ||
| with tempfile.TemporaryDirectory() as work_dir: | ||
| event_log = EventLogBuilder(args.log_file.resolve().as_uri(), work_dir).build() | ||
| app = sparkApplication(eventlog=str(event_log)) | ||
|
|
||
| if os.path.exists(result_path): | ||
| os.remove(result_path) | ||
| if args.log_file.suffixes: | ||
| result_path = os.path.join( | ||
| args.result_dir, "parsed-" + args.log_file.name[: -len("".join(args.log_file.suffixes))] | ||
| ) | ||
| else: | ||
| result_path = os.path.join(args.result_dir, "parsed-" + args.log_file.name) | ||
|
|
||
| appobj = sparkApplication(eventlog=log_path) | ||
| appobj.save(result_path) | ||
| app.save(result_path) | ||
|
|
||
| print(f"--Log directory saved to: {result_path}") | ||
| print(f"--Result saved to: {result_path}.json") | ||
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,73 @@ | ||
| import json | ||
| import tempfile | ||
| from pathlib import Path | ||
| from urllib.parse import ParseResult | ||
|
|
||
| import pandas as pd | ||
|
|
||
| from spark_log_parser.extractor import Extractor | ||
|
|
||
|
|
||
| class EventLogBuilder: | ||
| def __init__(self, source_url: ParseResult | str, work_dir: Path | str, s3_client=None): | ||
| self.source_url = source_url | ||
| self.work_dir = self._validate_work_dir(work_dir) | ||
| self.s3_client = s3_client | ||
| self.extractor = Extractor(self.source_url, self.work_dir, self.s3_client) | ||
|
|
||
| def _validate_work_dir(self, work_dir: Path | str) -> Path: | ||
| work_dir_path = work_dir if isinstance(work_dir, Path) else Path(work_dir) | ||
| if not work_dir_path.is_dir(): | ||
| raise ValueError("Path is not a directory") | ||
|
|
||
| return work_dir_path | ||
|
|
||
| def build(self) -> Path: | ||
| event_logs = self.extractor.extract() | ||
|
|
||
| self.event_log = self._concat(event_logs) | ||
|
|
||
| return self.event_log | ||
|
|
||
| def _concat(self, event_logs: list[Path]) -> Path: | ||
| if len(event_logs) == 1: | ||
| return event_logs[0] | ||
|
|
||
| dat = [] | ||
| for log in event_logs: | ||
| with open(log) as log_file: | ||
| try: | ||
| line = json.loads(log_file.readline()) | ||
| except ValueError: | ||
| continue # Maybe a Databricks pricing file | ||
| if line["Event"] == "DBCEventLoggingListenerMetadata": | ||
| dat.append((line["Rollover Number"], line["SparkContext Id"], log)) | ||
| else: | ||
| raise ValueError("Expected DBC event not found") | ||
|
|
||
| df = pd.DataFrame(dat, columns=["rollover_index", "context_id", "path"]).sort_values( | ||
| "rollover_index" | ||
| ) | ||
|
|
||
| self._validate_rollover_logs(df) | ||
|
|
||
| event_log = Path(tempfile.mkstemp(suffix="-concatenated.json", dir=str(self.work_dir))[1]) | ||
| with open(event_log, "w") as fobj: | ||
| for path in df.path: | ||
| with open(path) as part_fobj: | ||
| for line in part_fobj: | ||
| fobj.write(line) | ||
|
|
||
| return event_log | ||
|
|
||
| def _validate_rollover_logs(self, df: pd.DataFrame): | ||
| if not len(df.context_id.unique()) == 1: | ||
| raise ValueError("Not all rollover files have the same Spark context ID") | ||
|
|
||
| diffs = df.rollover_index.diff()[1:] | ||
|
|
||
| if any(diffs > 1) or df.rollover_index[0] > 0: | ||
| raise ValueError("Rollover file appears to be missing") | ||
|
|
||
| if any(diffs < 1): | ||
| raise ValueError("Duplicate rollover file detected") |
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.