Lee-W commented on code in PR #39771: URL: https://github.com/apache/airflow/pull/39771#discussion_r1619071184
########## airflow/providers/databricks/operators/databricks_workflow.py: ########## @@ -0,0 +1,302 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +from __future__ import annotations + +import json +import time +from dataclasses import dataclass +from functools import cached_property +from typing import TYPE_CHECKING, Any + +from mergedeep import merge + +from airflow.exceptions import AirflowException +from airflow.models import BaseOperator +from airflow.providers.databricks.hooks.databricks import DatabricksHook +from airflow.utils.task_group import TaskGroup + +if TYPE_CHECKING: + from airflow.models.taskmixin import DAGNode + from airflow.utils.context import Context + + +@dataclass +class WorkflowRunMetadata: + """ + Metadata for a Databricks workflow run. + + :param run_id: The ID of the Databricks workflow run. + :param job_id: The ID of the Databricks workflow job. + :param conn_id: The connection ID used to connect to Databricks. + """ + + conn_id: str + job_id: str + run_id: int + + +def _flatten_node( + node: TaskGroup | BaseOperator | DAGNode, tasks: list[BaseOperator] | None = None +) -> list[BaseOperator]: + """Flatten a node (either a TaskGroup or Operator) to a list of nodes.""" + if tasks is None: + tasks = [] + if isinstance(node, BaseOperator): + return [node] + + if isinstance(node, TaskGroup): + new_tasks = [] + for _, child in node.children.items(): + new_tasks += _flatten_node(child, tasks) + + return tasks + new_tasks + + return tasks + + +class _CreateDatabricksWorkflowOperator(BaseOperator): + """ + Creates a Databricks workflow from a DatabricksWorkflowTaskGroup specified in a DAG. + + :param task_id: The task_id of the operator + :param databricks_conn_id: The connection ID to use when connecting to Databricks. + :param existing_clusters: A list of existing clusters to use for the workflow. + :param extra_job_params: A dictionary of extra properties which will override the default Databricks + Workflow Job definitions. + :param job_clusters: A list of job clusters to use for the workflow. + :param max_concurrent_runs: The maximum number of concurrent runs for the workflow. + :param notebook_params: A dictionary of notebook parameters to pass to the workflow. These parameters + will be passed to all notebooks in the workflow. + :param tasks_to_convert: A list of tasks to convert to a Databricks workflow. This list can also be + populated after instantiation using the `add_task` method. + """ + + template_fields = ("notebook_params",) + caller = "_CreateDatabricksWorkflowOperator" + + def __init__( + self, + task_id, + databricks_conn_id, + existing_clusters: list[str] | None = None, + extra_job_params: dict[str, Any] | None = None, + job_clusters: list[dict[str, object]] | None = None, + max_concurrent_runs: int = 1, + notebook_params: dict | None = None, + tasks_to_convert: list[BaseOperator] | None = None, + **kwargs, + ): + self.databricks_conn_id = databricks_conn_id + self.existing_clusters = existing_clusters or [] + self.extra_job_params = extra_job_params or {} + self.job_clusters = job_clusters or [] + self.max_concurrent_runs = max_concurrent_runs + self.notebook_params = notebook_params or {} + self.tasks_to_convert = tasks_to_convert or [] + self.relevant_upstreams = [task_id] + super().__init__(task_id=task_id, **kwargs) + + def _get_hook(self, caller: str) -> DatabricksHook: + return DatabricksHook( + self.databricks_conn_id, + caller=caller, + ) + + @cached_property + def _hook(self) -> DatabricksHook: + return self._get_hook(caller=self.caller) + + def add_task(self, task: BaseOperator): + """Add a task to the list of tasks to convert to a Databricks workflow.""" + self.tasks_to_convert.append(task) + + @property + def databricks_job_name(self): + return f"{self.dag_id}.{self.task_group.group_id}" + + def create_workflow_json(self, context: Context | None = None) -> dict[str, object]: Review Comment: Sounds reasonable. If that's the case, we probably could do it more in the future 👀 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
