-
Notifications
You must be signed in to change notification settings - Fork 2
[PROD-1832] Run submit airflow preexecute call #113
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
Open
singhals
wants to merge
9
commits into
main
Choose a base branch
from
singhals/PROD-1832-airflow-operator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
49a8407
Run submit airflow preexecute call
singhals 138622d
Fix linting
singhals 0a80f8f
Add version
singhals c486234
Add task id to app id
singhals 1c88052
Support multiple tasks in the operator def
singhals ef2aac7
Fix syntax
singhals fd1eb1e
Fix formatting
singhals e1be2f9
Merge cluster configurations properly without override params that ar…
singhals a3ed855
Add version
singhals 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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| """Library for leveraging the power of Sync""" | ||
|
|
||
| __version__ = "1.6.0" | ||
| __version__ = "1.7.0-alpha" | ||
|
|
||
| TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" |
Empty file.
Empty file.
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,111 @@ | ||
| import logging | ||
|
|
||
| from sync import _databricks as databricks | ||
| from sync.api import projects | ||
| from sync.models import Platform | ||
| from sync.api import workspace | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| compute_provider_platform_dict = { | ||
| "aws": Platform.AWS_DATABRICKS, | ||
| "azure": Platform.AZURE_DATABRICKS | ||
| } | ||
|
|
||
|
|
||
| def apply_sync_gradient_cluster_recommendation( | ||
| run_submit_task: dict, | ||
| gradient_app_id: str, | ||
| auto_apply: bool, | ||
| cluster_log_url: str, | ||
| workspace_id: str | ||
| ) -> dict: | ||
| response = workspace.get_workspace_config(workspace_id) | ||
| workspace_config = response.result | ||
|
|
||
| project_id = create_or_fetch_project(gradient_app_id, cluster_log_url, workspace_config.get("compute_provider")) | ||
|
|
||
| # if recommendation exists apply the configuration or apply default configuration with project settings | ||
| if auto_apply: | ||
| logger.info(f"Generating recommendation - app_id: {gradient_app_id}, project_id: {project_id}, " | ||
| f"auto_apply: {auto_apply}") | ||
| rec = get_gradient_recommendation(project_id) | ||
| if rec: | ||
| logger.info(f"Recommendation generated - app_id: {gradient_app_id}, project_id: {project_id}, " | ||
| f"recommendation: {rec}") | ||
| updated_cluster = rec | ||
| else: | ||
| logger.warning(f"Unable to generate recommendation. Falling back to original cluster - " | ||
| f"app_id: {gradient_app_id}, project_id: {project_id}, " | ||
| f"auto_apply: {auto_apply}, cluster: {run_submit_task['new_cluster']}") | ||
| updated_cluster = run_submit_task["new_cluster"] | ||
| else: | ||
| updated_cluster = run_submit_task["new_cluster"] | ||
|
|
||
| resp = databricks.get_project_cluster(updated_cluster, project_id) | ||
| if resp.result: | ||
| configured_cluster = resp.result | ||
| run_submit_task["new_cluster"] = configured_cluster | ||
| run_submit_task = apply_webhook_notification(workspace_config, run_submit_task) | ||
| else: | ||
| logger.error("Unable to apply gradient configuration to databricks run submit call. " | ||
| "Submitting original run submit call.") | ||
|
|
||
| return run_submit_task | ||
|
|
||
|
|
||
| def create_or_fetch_project(app_id: str, cluster_log_url: str, compute_provider: str) -> str: | ||
| resp = projects.get_project_by_app_id(app_id) | ||
| if resp.result is None: | ||
| logger.info(f"Project with app_id does not exist. Creating project - app_id:{app_id}, " | ||
| f"cluster_log_url:{cluster_log_url}, compute_provider:{compute_provider}") | ||
| resp = projects.create_project( | ||
| name=app_id, | ||
| app_id=app_id, | ||
| cluster_log_url=cluster_log_url, | ||
| product_code=compute_provider_platform_dict.get(compute_provider) or Platform.AWS_DATABRICKS | ||
| ) | ||
| else: | ||
| logger.info(f"Found project with app_id - app_id:{app_id}") | ||
|
|
||
| return resp.result['id'] | ||
|
|
||
|
|
||
| def get_gradient_recommendation(project_id: str) -> dict: | ||
| """ | ||
| Generates/retrieves the recommendation and returns the cluster configuration | ||
|
|
||
| Args: None | ||
|
|
||
| Returns: cluster config | ||
| """ | ||
| response = projects.create_project_recommendation(project_id) | ||
| recommendation_id = response.result | ||
|
|
||
| if recommendation_id is None: | ||
| return None | ||
|
|
||
| response = projects.wait_for_recommendation(project_id, recommendation_id) | ||
|
|
||
| return response.result['recommendation']['configuration'] | ||
|
|
||
|
|
||
| def apply_webhook_notification(workspace_config: dict, task: dict) -> dict: | ||
| webhook_id = workspace_config["webhook_id"] | ||
| if workspace_config.get("collection_type") == "hosted": | ||
| task = append_webhook(task, webhook_id, "on_start") | ||
| else: | ||
| task = append_webhook(task, webhook_id, "on_success") | ||
| return task | ||
|
|
||
|
|
||
| def append_webhook(task: dict, webhook_id: str, event: str) -> dict: | ||
| webhook_request = { | ||
| "id": webhook_id | ||
| } | ||
| task["webhook_notifications"] = task.get("webhook_notifications") or {} | ||
| task["webhook_notifications"][event] = task["webhook_notifications"].get(event) or [] | ||
| task["webhook_notifications"][event].append(webhook_request) | ||
|
|
||
| return task |
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,39 @@ | ||
| import logging | ||
|
|
||
| from sync.databricks.integrations._run_submit_runner import ( | ||
| apply_sync_gradient_cluster_recommendation, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def airflow_gradient_pre_execute_hook(context: dict): | ||
| try: | ||
| logger.info("Running airflow gradient pre-execute hook!") | ||
| logger.debug(f"Airflow operator context - context:{context}") | ||
|
|
||
| task_id = context["task"].task_id | ||
| gradient_app_id = context["params"]["gradient_app_id"] | ||
| auto_apply = context["params"]["gradient_auto_apply"] | ||
| cluster_log_url = context["params"]["cluster_log_url"] | ||
| workspace_id = context["params"]["databricks_workspace_id"] | ||
| run_submit_task = context[ | ||
| "task" | ||
| ].json.copy() # copy the run submit json from the task context | ||
|
|
||
| updated_task_configuration = apply_sync_gradient_cluster_recommendation( | ||
| run_submit_task=run_submit_task, | ||
| gradient_app_id=build_app_id(task_id, gradient_app_id), | ||
| auto_apply=auto_apply, | ||
| cluster_log_url=cluster_log_url, | ||
| workspace_id=workspace_id, | ||
| ) | ||
|
|
||
| context["task"].json = updated_task_configuration | ||
| except Exception as e: | ||
| logger.exception(e) | ||
| logger.error("Unable to apply gradient configuration to Databricks run submit tasks") | ||
|
|
||
|
|
||
| def build_app_id(task_id: str, app_id: str): | ||
| return f"{task_id}-{app_id}" | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.