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

fix(models.core:Database): Allocate a SQLA engine once per process per URL. #27899

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
33 changes: 29 additions & 4 deletions superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import json
import logging
import textwrap
import threading
from ast import literal_eval
from contextlib import closing, contextmanager, nullcontext, suppress
from copy import deepcopy
Expand Down Expand Up @@ -113,6 +114,32 @@ class ConfigurationMethod(StrEnum):
DYNAMIC_FORM = "dynamic_form"


class EngineManager:
"""
Allocate an engine once per process as per API documentation
https://docs.sqlalchemy.org/en/20/core/connections.html

Quote:
The typical usage of create_engine() is once per particular database URL,
held globally for the lifetime of a single application process
"""

_lock = threading.Lock()
_sqla_engines: dict[str, tuple[Engine, dict]] = {}

@classmethod
def create_engine(cls, sqlalchemy_url: str, **params) -> Engine:
with cls._lock:
tpl = cls._sqla_engines.get(sqlalchemy_url)
if tpl is not None:
engine, cparams = tpl
if cparams == params:
Copy link
Member

Choose a reason for hiding this comment

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

Can we have params be part of the key as well, in some deterministic serialized form? A few databases that support user impersonation will add the user info to params, and in that case the engine manager would not be very useful.

Copy link
Author

Choose a reason for hiding this comment

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

PTAL

return engine
engine = create_engine(sqlalchemy_url, **params)
cls._sqla_engines[sqlalchemy_url] = (engine, params)
return engine


class Database(
Model, AuditMixinNullable, ImportExportMixin
): # pylint: disable=too-many-public-methods
Expand Down Expand Up @@ -376,9 +403,7 @@ def get_effective_user(self, object_url: URL) -> str | None:
return (
username
if (username := get_username())
else object_url.username
if self.impersonate_user
else None
else object_url.username if self.impersonate_user else None
)

@contextmanager
Expand Down Expand Up @@ -513,7 +538,7 @@ def _get_sqla_engine(
source,
)
try:
return create_engine(sqlalchemy_url, **params)
return EngineManager.create_engine(sqlalchemy_url, **params)
except Exception as ex:
raise self.db_engine_spec.get_dbapi_mapped_exception(ex)

Expand Down