Repository navigation
fix(databricks): run U2M OAuth queries concurrently with a shared login #6188
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
gabepesco
wants to merge
3
commits into
SQLMesh:main
Choose a base branch
from
gabepesco:fix/databricks-u2m-concurrency
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.
+479
−13
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,137 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import importlib.util | ||
| import re | ||
| import typing as t | ||
| from functools import lru_cache | ||
| from threading import Lock | ||
|
|
||
| HeaderFactory = t.Callable[[], t.Dict[str, str]] | ||
|
|
||
|
|
||
| class SharedU2MCredentialsProvider: | ||
| """A Databricks SQL connector `credentials_provider` that shares one U2M OAuth login across connections. | ||
|
|
||
| Without this, every `databricks.sql.connect()` call builds its own OAuth provider. For U2M OAuth that means | ||
| each thread runs its own browser flow, and these race on the CSRF `state` parameter | ||
| (https://github.com/tobymao/sqlmesh/issues/5646). Even if the browser flow is only run once, each connection | ||
| would still hold its own copy of the refresh token. Databricks can issue single-use refresh tokens, so the | ||
| first connection to refresh would invalidate the token held by every other connection. | ||
|
|
||
| Instead, a single connector OAuth provider is created lazily (behind a lock, so the browser flow only runs once) | ||
| and every connection reads its headers from it. Reads are also serialized so only one thread can refresh the | ||
| token at a time. This lets each thread have its own connection while sharing a single login. | ||
|
|
||
| If reading the token fails (for example because the refresh token expired or was revoked) the login is | ||
| discarded, so the next connection or request logs in again rather than failing for the rest of the process. | ||
| Network failures while refreshing keep the login, so the next request retries the refresh without a browser flow. | ||
| """ | ||
|
|
||
| def __init__(self, server_hostname: str, auth_type: str): | ||
| self._server_hostname = server_hostname | ||
| self._auth_type = auth_type | ||
| self._auth_provider: t.Optional[t.Any] = None | ||
| self._lock = Lock() | ||
| # Incremented when a login attempt finishes, so threads that waited on a failed attempt can tell it happened | ||
| self._login_attempts_finished = 0 | ||
| self._login_error: t.Optional[Exception] = None | ||
|
|
||
| def auth_type(self) -> str: | ||
| return self._auth_type | ||
|
|
||
| def __call__(self, *args: t.Any, **kwargs: t.Any) -> HeaderFactory: | ||
| """Called by the connector once per connection. Logs in if needed and returns that connection's header factory.""" | ||
| login_attempts_seen = self._login_attempts_finished | ||
| with self._lock: | ||
| self._ensure_logged_in(login_attempts_seen) | ||
| return self._headers | ||
|
|
||
| def _headers(self) -> t.Dict[str, str]: | ||
| """Returns the auth headers for one request, refreshing the token if it has expired.""" | ||
| from databricks.sql.exc import RequestError | ||
|
|
||
| headers: t.Dict[str, str] = {} | ||
| login_attempts_seen = self._login_attempts_finished | ||
| with self._lock: | ||
| auth_provider = self._ensure_logged_in(login_attempts_seen) | ||
| try: | ||
| auth_provider.add_headers(headers) | ||
| except RequestError: | ||
| # A network failure while refreshing leaves the login intact, since the connector only replaces its | ||
| # tokens after a successful refresh, so the next request retries the refresh rather than logging in | ||
| raise | ||
| except Exception: | ||
| self._auth_provider = None | ||
| raise | ||
| return headers | ||
|
|
||
| def _ensure_logged_in(self, login_attempts_seen: int) -> t.Any: | ||
| """Returns the shared connector auth provider, logging in if needed. Must be called with the lock held.""" | ||
| if self._auth_provider is not None: | ||
| return self._auth_provider | ||
|
|
||
| # A login attempt failed while this thread was waiting on the lock. Re-raise its error rather than opening | ||
| # another browser window for every waiting thread; callers that arrive afterwards will try again. | ||
| if self._login_attempts_finished != login_attempts_seen and self._login_error is not None: | ||
| raise self._login_error | ||
|
|
||
| try: | ||
| self._auth_provider = self._create_auth_provider() | ||
| self._login_error = None | ||
| except Exception as e: | ||
| self._login_error = e | ||
| raise | ||
| finally: | ||
| self._login_attempts_finished += 1 | ||
| return self._auth_provider | ||
|
|
||
| def _create_auth_provider(self) -> t.Any: | ||
| # These are internal to the connector, but using them means the OAuth flow is identical to the one the | ||
| # connector runs when `auth_type` is passed to `connect()` (same client ID, redirect ports, and scopes) | ||
| from databricks.sql import __version__ | ||
| from databricks.sql.auth.auth import get_python_sql_connector_auth_provider | ||
| from databricks.sql.common.unified_http_client import UnifiedHttpClient | ||
| from databricks.sql.utils import build_client_context | ||
|
|
||
| # The provider outlives any single connection, so it gets its own HTTP client rather than borrowing one | ||
| # that would be closed along with its connection | ||
| http_client = UnifiedHttpClient( | ||
| build_client_context(self._server_hostname, __version__, user_agent_entry="sqlmesh") | ||
| ) | ||
| return get_python_sql_connector_auth_provider( | ||
| self._server_hostname, http_client=http_client, auth_type=self._auth_type | ||
| ) | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def supports_shared_u2m_login() -> bool: | ||
| """Whether the installed connector has the internals `SharedU2MCredentialsProvider` builds on (4.1.0+).""" | ||
| try: | ||
| return importlib.util.find_spec("databricks.sql.common.unified_http_client") is not None | ||
| except ModuleNotFoundError: | ||
| return False | ||
|
|
||
|
|
||
| _shared_u2m_credentials_providers: t.Dict[t.Tuple[str, str], SharedU2MCredentialsProvider] = {} | ||
| _shared_u2m_credentials_providers_lock = Lock() | ||
|
|
||
|
|
||
| def get_shared_u2m_credentials_provider( | ||
| server_hostname: str, auth_type: str | ||
| ) -> SharedU2MCredentialsProvider: | ||
| """Returns the process-wide U2M credentials provider for the given workspace and auth type. | ||
|
|
||
| Engine adapters and config copies are created several times per process, so the provider is kept at module | ||
| level to avoid prompting the user to log in more than once. U2M OAuth is an interactive, single-user login, so | ||
| every connection to a workspace in this process uses the identity of whoever completed the browser flow. | ||
| """ | ||
| # The same workspace can be written with or without a scheme or trailing slash | ||
| normalized_hostname = re.sub(r"^https?://", "", server_hostname.strip(), flags=re.I) | ||
| normalized_hostname = normalized_hostname.rstrip("/").lower() | ||
| key = (normalized_hostname, auth_type) | ||
| with _shared_u2m_credentials_providers_lock: | ||
| if key not in _shared_u2m_credentials_providers: | ||
| _shared_u2m_credentials_providers[key] = SharedU2MCredentialsProvider( | ||
| normalized_hostname, auth_type | ||
| ) | ||
| return _shared_u2m_credentials_providers[key] | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.