Source code for airflow.providers.databricks.operators.genie

# 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.
"""Operators for asking Databricks Genie spaces questions as standalone tasks."""

from __future__ import annotations

import hashlib
import time
from collections.abc import Sequence
from datetime import timedelta
from functools import cached_property
from typing import TYPE_CHECKING, Any
from urllib.parse import quote

from airflow.providers.common.compat.sdk import AirflowFailException, BaseOperator, ResumableJobMixin, conf
from airflow.providers.databricks.exceptions import DatabricksGenieError
from airflow.providers.databricks.hooks.genie import (
    _PENDING_STATUSES,
    GENIE_TERMINAL_STATUSES,
    DatabricksGenieHook,
    _truncate_result,
)
from airflow.providers.databricks.triggers.genie import DatabricksGenieMessageTrigger

if TYPE_CHECKING:
    from airflow.providers.common.compat.sdk import Context

[docs] GenieMessageRef = dict[str, str]
_NOT_FOUND = "NOT_FOUND" _STALE = "STALE" _UNCONFIRMED_HINT = ( "Airflow will not retry this task. Inspect the Genie space before clearing it: a default clear " "discards the task state and asks the question again, while a clear that keeps task state " "refuses to ask until you attach with conversation_id and message_id or set " "resubmit_unconfirmed=True." )
[docs] class DatabricksGenieOperator(ResumableJobMixin, BaseOperator): """ Ask a Databricks Genie space one question and return a bounded, structured answer. The operator submits the question once, persists the Genie conversation and message IDs to task state before waiting, then polls (or defers) until the message finishes. With durable execution (the default on Airflow 3.3 and later), a retry after a worker crash or timeout reconnects to the stored message instead of asking again. With ``durable=False``, or on older Airflow without the task state store, every try asks again. Genie has no idempotency key, so a submission whose outcome is unknown (a timeout, a connection error or a 5xx response) cannot be told apart from one that was never received. The operator fails without retrying in that case. A later try that still finds the unconfirmed submission in task state refuses to submit again; set ``conversation_id`` and ``message_id`` to attach to the message you found, or ``resubmit_unconfirmed=True`` to ask again. A clear that discards task state (the default from Airflow 3.4) asks again without that check. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:DatabricksGenieOperator` :param space_id: Genie space ID. (templated) :param question: Question to ask. Required unless ``message_id`` is set. (templated) :param databricks_conn_id: Databricks connection. Defaults to ``databricks_default``. (templated) :param conversation_id: Existing conversation to ask the question in. The operator never deletes it. Defaults to ``None``, which starts a new conversation. An empty string means ``None``. (templated) :param message_id: Existing message to wait for instead of asking a question. Requires ``conversation_id``. An empty string means ``None``. A fixed value applies to every Dag run, so prefer a run-scoped template such as ``"{{ params.genie_message_id }}"``. (templated) :param polling_period_seconds: Seconds between status checks. Defaults to ``5``. :param timeout: Maximum seconds to wait for the answer in one try. Timing out does not cancel the message. With durable execution the next try reconnects to it; with ``durable=False``, or on Airflow before 3.3, the next try asks again. Defaults to ``1800``. :param resubmit_unconfirmed: Ask the question again even though an earlier submission's outcome is unknown and may have created a message. Defaults to ``False``. :param databricks_retry_limit: Attempts for each Genie read. Submissions are never retried. Defaults to ``3``. :param databricks_retry_delay: Minimum seconds between read attempts. Defaults to ``1``. :param deferrable: Release the worker while waiting. Defaults to ``[operators] default_deferrable``, with a fallback of ``False``. :param durable: When ``True`` (the default), the Genie message IDs are persisted to task state before waiting so a retry reconnects instead of asking again. Requires Airflow 3.3+; on earlier versions every try asks the question again. """
[docs] template_fields: Sequence[str] = ( "space_id", "question", "databricks_conn_id", "conversation_id", "message_id", )
[docs] ui_color = "#1CB1C2"
[docs] external_id_key = "databricks_genie_message"
def __init__( self, *, space_id: str, question: str | None = None, databricks_conn_id: str = "databricks_default", conversation_id: str | None = None, message_id: str | None = None, polling_period_seconds: float = 5, timeout: float = 1800, resubmit_unconfirmed: bool = False, databricks_retry_limit: int = 3, databricks_retry_delay: float = 1, deferrable: bool = conf.getboolean("operators", "default_deferrable", fallback=False), durable: bool | None = None, **kwargs: Any, ) -> None: # Named here (not left to **kwargs) so default_args reaches it on every supported Airflow version. if durable is not None: kwargs["durable"] = durable super().__init__(**kwargs) if polling_period_seconds <= 0: raise ValueError("polling_period_seconds must be positive") if timeout <= 0: raise ValueError("timeout must be positive")
[docs] self.space_id = space_id
[docs] self.question = question
[docs] self.databricks_conn_id = databricks_conn_id
[docs] self.conversation_id = conversation_id
[docs] self.message_id = message_id
[docs] self.polling_period_seconds = polling_period_seconds
[docs] self.timeout = timeout
[docs] self.resubmit_unconfirmed = resubmit_unconfirmed
[docs] self.databricks_retry_limit = databricks_retry_limit
[docs] self.databricks_retry_delay = databricks_retry_delay
[docs] self.deferrable = deferrable
self._result: dict[str, Any] | None = None @cached_property
[docs] def hook(self) -> DatabricksGenieHook: return DatabricksGenieHook( self.databricks_conn_id, retry_limit=self.databricks_retry_limit, retry_delay=self.databricks_retry_delay, caller=type(self).__name__, poll_interval=self.polling_period_seconds, )
@property def _submission_key(self) -> str: return f"{self.external_id_key}_submission" def _compute_fingerprint(self) -> str: """Identify the question so a retry never reconnects to an answer for a different one.""" material = "\x1f".join((self.space_id, self.conversation_id or "", self.question or "")) return hashlib.sha256(material.encode("utf-8")).hexdigest() def _get_task_state_store(self, context: Context) -> Any: return context.get("task_state_store") if self.durable else None def _describe_next_try(self, context: Context) -> str: """Say what a retry does, since only durable execution or an explicit message_id reconnects.""" if self.message_id is not None or self._get_task_state_store(context) is not None: return "A retry reconnects to the message." return "A retry asks the question again, because durable execution is off or unavailable." def _validate(self) -> None: self.conversation_id = self.conversation_id or None self.message_id = self.message_id or None self.hook._validate_space_id(self.space_id) if self.message_id is not None: if self.conversation_id is None: raise ValueError("message_id requires conversation_id") elif not isinstance(self.question, str) or not self.question.strip(): raise ValueError("question must be a non-empty string unless message_id is set")
[docs] def execute(self, context: Context) -> dict[str, Any]: self._validate() if self.message_id is None: return self.execute_resumable(context) ref = { "space_id": self.space_id, "conversation_id": self.conversation_id, "message_id": self.message_id, "fingerprint": self._compute_fingerprint(), } self.log.info( "Attaching to Genie message %s in conversation %s", self.message_id, self.conversation_id ) if (store := self._get_task_state_store(context)) is not None: store.set(self.external_id_key, ref) store.delete(self._submission_key) return self.poll_until_complete(ref, context)
[docs] def submit_job(self, context: Context) -> Any: space = quote(self.space_id, safe="") if self.conversation_id is None: endpoint = f"spaces/{space}/start-conversation" else: endpoint = f"spaces/{space}/conversations/{quote(self.conversation_id, safe='')}/messages" # Resolve the connection, the HTTPS check and credentials first: if any of them fails, nothing # was sent, so no marker may be written that would block the next try. prepared = self.hook._prepare_request(endpoint) store = self._get_task_state_store(context) if store is not None: marker = store.get(self._submission_key) if ( marker is not None and not self.resubmit_unconfirmed and (not isinstance(marker, dict) or marker.get("fingerprint") == self._compute_fingerprint()) ): raise AirflowFailException( "An earlier try submitted this Genie question but Databricks never confirmed the " "outcome, so a message may already exist. Inspect the Genie space, then set " "conversation_id and message_id to attach to it, or set resubmit_unconfirmed=True " "to ask again." ) store.set(self._submission_key, {"fingerprint": self._compute_fingerprint()}) try: created = self.hook._request( "POST", endpoint, body={"content": self.question, "enable_visualization": False}, retry_read=False, prepared=prepared, ) except DatabricksGenieError as err: if err.http_status_code is not None and err.http_status_code < 500: # Databricks rejected the request, so nothing was created and a retry may submit again. if store is not None: store.delete(self._submission_key) raise raise AirflowFailException(f"{err} {_UNCONFIRMED_HINT}") from err conversation_id = created.get("conversation_id") message_id = created.get("message_id") or created.get("id") if not isinstance(conversation_id, str) or not isinstance(message_id, str): raise AirflowFailException( f"Genie accepted the question but returned no conversation or message ID. {_UNCONFIRMED_HINT}" ) ref = { "space_id": self.space_id, "conversation_id": conversation_id, "message_id": message_id, "fingerprint": self._compute_fingerprint(), } self.log.info("Asked Genie message %s in conversation %s", message_id, conversation_id) if store is not None: # Persist before dropping the marker so no crash can lose both. store.set(self.external_id_key, ref) store.delete(self._submission_key) return ref
def _build_message_path(self, ref: GenieMessageRef) -> str: return ( f"spaces/{quote(ref['space_id'], safe='')}/conversations/" f"{quote(ref['conversation_id'], safe='')}/messages/{quote(ref['message_id'], safe='')}" ) def _get_message(self, ref: GenieMessageRef, deadline: float | None = None) -> dict[str, Any]: return self.hook._request("GET", self._build_message_path(ref), retry_read=True, deadline=deadline)
[docs] def get_job_status(self, external_id: Any, context: Context) -> str: if not isinstance(external_id, dict) or external_id.get("fingerprint") != self._compute_fingerprint(): self.log.warning("Stored Genie message was for a different question; asking again") return _STALE try: message = self._get_message(external_id) except DatabricksGenieError as err: if err.http_status_code == 404: return _NOT_FOUND raise status = message.get("status") if status in _PENDING_STATUSES or status in GENIE_TERMINAL_STATUSES: return str(status) raise DatabricksGenieError(f"Genie returned an unknown message status: {status!r}.")
[docs] def is_job_active(self, status: str) -> bool: return status in _PENDING_STATUSES
[docs] def is_job_succeeded(self, status: str) -> bool: return status == "COMPLETED"
def _check_message(self, message: dict[str, Any], ref: GenieMessageRef) -> bool: """Return whether the message is completed, raise if it ended without an answer.""" status = message.get("status") if status == "COMPLETED": return True if status in _PENDING_STATUSES: return False if status == "FAILED": error = message.get("error") or {} detail = error.get("error") if isinstance(error, dict) else str(error) raise DatabricksGenieError( f"Genie could not answer message {ref['message_id']}: {detail or 'unknown error'}" ) if status in {"CANCELLED", "QUERY_RESULT_EXPIRED"}: raise DatabricksGenieError(f"Genie message {ref['message_id']} ended with status {status}.") raise DatabricksGenieError(f"Genie returned an unknown message status: {status!r}.")
[docs] def poll_until_complete(self, external_id: Any, context: Context) -> Any: """ Wait for the message and return its bounded result. ``execute_resumable`` returns this value directly when it reconnects to an active message, so polling returns the result instead of ``None``. """ ref: GenieMessageRef = external_id deadline = time.monotonic() + self.timeout message = self._get_message(ref, deadline) if not self._check_message(message, ref) and self.deferrable: self.log.info("Deferring until Genie message %s completes", ref["message_id"]) self.defer( trigger=DatabricksGenieMessageTrigger( space_id=ref["space_id"], conversation_id=ref["conversation_id"], message_id=ref["message_id"], databricks_conn_id=self.databricks_conn_id, polling_period_seconds=self.polling_period_seconds, retry_limit=self.databricks_retry_limit, retry_delay=self.databricks_retry_delay, ), method_name="execute_complete", timeout=timedelta(seconds=self.timeout), kwargs={"ref": ref}, ) while not self._check_message(message, ref): remaining = deadline - time.monotonic() if remaining <= 0: raise DatabricksGenieError( f"Timed out after {self.timeout} seconds waiting for Genie message " f"{ref['message_id']}. {self._describe_next_try(context)}" ) time.sleep(min(self.polling_period_seconds, remaining)) message = self._get_message(ref, deadline) return self._summarize(message, ref, deadline)
def _summarize(self, message: dict[str, Any], ref: GenieMessageRef, deadline: float) -> dict[str, Any]: result = self.hook._summarize_result( ref["space_id"], quote(ref["space_id"], safe=""), ref["conversation_id"], ref["message_id"], message, deadline, ) self._result = _truncate_result(result) return self._result
[docs] def get_job_result(self, external_id: Any, context: Context) -> dict[str, Any]: ref: GenieMessageRef = external_id if self._result is not None and self._result.get("message_id") == ref["message_id"]: return self._result deadline = time.monotonic() + self.timeout message = self._get_message(ref, deadline) if not self._check_message(message, ref): raise DatabricksGenieError(f"Genie message {ref['message_id']} has not completed yet.") return self._summarize(message, ref, deadline)
[docs] def execute_complete( self, context: Context, event: dict[str, Any] | None = None, *, ref: GenieMessageRef ) -> dict[str, Any]: if ( not event or event.get("conversation_id") != ref["conversation_id"] or event.get("message_id") != ref["message_id"] or event.get("status") not in ("success", "error") ): raise DatabricksGenieError(f"Genie message {ref['message_id']} trigger returned an invalid event") if event["status"] == "error": status_code = event.get("status_code") raise DatabricksGenieError( f"Polling Genie message {ref['message_id']} failed ({event.get('error_type')}); " f"inspect the triggerer logs. {self._describe_next_try(context)}", http_status_code=status_code if isinstance(status_code, int) else None, ) return self.get_job_result(ref, context)

Was this entry helpful?