Skip to content

Commit

Permalink
Reduce latency to find stats metadata (#89824)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Mar 17, 2023
1 parent 04a99fd commit f6f3565
Show file tree
Hide file tree
Showing 13 changed files with 589 additions and 255 deletions.
25 changes: 19 additions & 6 deletions homeassistant/components/recorder/core.py
Expand Up @@ -86,6 +86,7 @@
from .table_managers.state_attributes import StateAttributesManager
from .table_managers.states import StatesManager
from .table_managers.states_meta import StatesMetaManager
from .table_managers.statistics_meta import StatisticsMetaManager
from .tasks import (
AdjustLRUSizeTask,
AdjustStatisticsTask,
Expand Down Expand Up @@ -172,6 +173,7 @@ def __init__(
threading.Thread.__init__(self, name="Recorder")

self.hass = hass
self.thread_id: int | None = None
self.auto_purge = auto_purge
self.auto_repack = auto_repack
self.keep_days = keep_days
Expand Down Expand Up @@ -208,6 +210,7 @@ def __init__(
self.state_attributes_manager = StateAttributesManager(
self, exclude_attributes_by_domain
)
self.statistics_meta_manager = StatisticsMetaManager(self)
self.event_session: Session | None = None
self._get_session: Callable[[], Session] | None = None
self._completed_first_database_setup: bool | None = None
Expand Down Expand Up @@ -613,6 +616,7 @@ def _wait_startup_or_shutdown(self) -> object | None:

def run(self) -> None:
"""Start processing events to save."""
self.thread_id = threading.get_ident()
setup_result = self._setup_recorder()

if not setup_result:
Expand Down Expand Up @@ -668,7 +672,7 @@ def run(self) -> None:
"Database Migration Failed",
"recorder_database_migration",
)
self._activate_and_set_db_ready()
self.hass.add_job(self.async_set_db_ready)
self._shutdown()
return

Expand All @@ -687,7 +691,14 @@ def run(self) -> None:

def _activate_and_set_db_ready(self) -> None:
"""Activate the table managers or schedule migrations and mark the db as ready."""
with session_scope(session=self.get_session()) as session:
with session_scope(session=self.get_session(), read_only=True) as session:
# Prime the statistics meta manager as soon as possible
# since we want the frontend queries to avoid a thundering
# herd of queries to find the statistics meta data if
# there are a lot of statistics graphs on the frontend.
if self.schema_version >= 23:
self.statistics_meta_manager.load(session)

if (
self.schema_version < 36
or session.execute(has_events_context_ids_to_migrate()).scalar()
Expand Down Expand Up @@ -758,10 +769,11 @@ def _pre_process_startup_tasks(self, startup_tasks: list[RecorderTask]) -> None:
non_state_change_events.append(event_)

assert self.event_session is not None
self.event_data_manager.load(non_state_change_events, self.event_session)
self.event_type_manager.load(non_state_change_events, self.event_session)
self.states_meta_manager.load(state_change_events, self.event_session)
self.state_attributes_manager.load(state_change_events, self.event_session)
session = self.event_session
self.event_data_manager.load(non_state_change_events, session)
self.event_type_manager.load(non_state_change_events, session)
self.states_meta_manager.load(state_change_events, session)
self.state_attributes_manager.load(state_change_events, session)

def _guarded_process_one_task_or_recover(self, task: RecorderTask) -> None:
"""Process a task, guarding against exceptions to ensure the loop does not collapse."""
Expand Down Expand Up @@ -1077,6 +1089,7 @@ def _close_event_session(self) -> None:
self.event_data_manager.reset()
self.event_type_manager.reset()
self.states_meta_manager.reset()
self.statistics_meta_manager.reset()

if not self.event_session:
return
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/recorder/migration.py
Expand Up @@ -873,7 +873,7 @@ def _apply_update( # noqa: C901
# There may be duplicated statistics_meta entries, delete duplicates
# and try again
with session_scope(session=session_maker()) as session:
delete_statistics_meta_duplicates(session)
delete_statistics_meta_duplicates(instance, session)
_create_index(
session_maker, "statistics_meta", "ix_statistics_meta_statistic_id"
)
Expand Down

0 comments on commit f6f3565

Please sign in to comment.