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

Update connection object to cached_property in DatabricksHook #20526

Merged
merged 1 commit into from
Dec 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions airflow/providers/databricks/hooks/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
operators talk to the ``api/2.0/jobs/runs/submit``
`endpoint <https://docs.databricks.com/api/latest/jobs.html#runs-submit>`_.
"""
import sys
import time
from time import sleep
from typing import Dict
Expand All @@ -34,6 +35,12 @@
from airflow import __version__
from airflow.exceptions import AirflowException
from airflow.hooks.base import BaseHook
from airflow.models import Connection

if sys.version_info >= (3, 8):
from functools import cached_property
else:
from cached_property import cached_property

RESTART_CLUSTER_ENDPOINT = ("POST", "api/2.0/clusters/restart")
START_CLUSTER_ENDPOINT = ("POST", "api/2.0/clusters/start")
Expand Down Expand Up @@ -143,11 +150,10 @@ def __init__(
self.retry_delay = retry_delay
self.aad_tokens: Dict[str, dict] = {}
self.aad_timeout_seconds = 10
self.databricks_conn = self.get_connection(self.databricks_conn_id)
if 'host' in self.databricks_conn.extra_dejson:
self.host = self._parse_host(self.databricks_conn.extra_dejson['host'])
else:
self.host = self._parse_host(self.databricks_conn.host)

@cached_property
def databricks_conn(self) -> Connection:
return self.get_connection(self.databricks_conn_id)

@staticmethod
def _parse_host(host: str) -> str:
Expand Down Expand Up @@ -305,6 +311,11 @@ def _do_api_call(self, endpoint_info, json):
"""
method, endpoint = endpoint_info

if 'host' in self.databricks_conn.extra_dejson:
Copy link
Contributor

Choose a reason for hiding this comment

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

self.host might want to be a cached prop also. or not be an attribute at all. it feels a little uncomfortable to mutate attributes like this, i.e. as side effects of a method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah you're right. Copy pasta. This doesn't need to be an instance attribute. self.host is only referenced in _do_api_call().

self.host = self._parse_host(self.databricks_conn.extra_dejson['host'])
else:
self.host = self._parse_host(self.databricks_conn.host)

url = f'https://{self.host}/{endpoint}'

aad_headers = self._get_aad_headers()
Expand Down