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 _wait_for_cluster_to_start func to check for libraries statuses #822

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 20 additions & 3 deletions dbt/adapters/databricks/python_submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,20 @@ def get_cluster_status(self) -> Dict:
json_response = response.json()
return json_response

def get_cluster_libraries_status(self) -> Dict:
# https://docs.databricks.com/api/workspace/libraries/clusterstatus

response = self.session.get(
f"https://{self.host}/api/2.0/libraries/cluster-status",
headers=self.extra_headers,
json={"cluster_id": self.cluster_id},
)
if response.status_code != 200:
raise DbtRuntimeError(f"Error getting status of libraries of a cluster.\n {response.content!r}")

json_response = response.json()
return json_response

def start_cluster(self) -> None:
"""Send the start command and poll for the cluster status until it shows "Running"

Expand Down Expand Up @@ -406,6 +420,7 @@ def _wait_for_cluster_to_start(self) -> None:
logger.info("Waiting for cluster to be ready")

MAX_CLUSTER_START_TIME = 900
LIBRARY_VALID_STATUSES = {"INSTALLED", "RESTORED", "SKIPPED"}
start_time = time.time()

def get_elapsed() -> float:
Expand All @@ -414,9 +429,11 @@ def get_elapsed() -> float:
while get_elapsed() < MAX_CLUSTER_START_TIME:
status_response = self.get_cluster_status()
if str(status_response.get("state")).lower() == "running":
return
else:
time.sleep(5)
libraries_status_response = self.get_cluster_libraries_status()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm surprised this isn't done as part of 'STARTING'. Does every run need to wait for library installs, or just those that specify additional libraries?

Copy link
Author

Choose a reason for hiding this comment

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

Good question. I assume that basic default libraries are a part of 'STARTING' indeed and only the libraries that are added explicitly (the ones exposed via Libraries API) are not and need extra time so that python models do work correctly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If that's the case, let's take this branch only if additional libraries have been specified.

if all(library["status"] in LIBRARY_VALID_STATUSES for library in libraries_status_response["library_statuses"]):
return

time.sleep(5)

raise DbtRuntimeError(
f"Cluster {self.cluster_id} restart timed out after {MAX_CLUSTER_START_TIME} seconds"
Expand Down
Loading