perf(store): SQLite loop-safety, connection lifecycle & query pushdown - #16
Open
buggedcom wants to merge 1 commit into
Open
perf(store): SQLite loop-safety, connection lifecycle & query pushdown#16buggedcom wants to merge 1 commit into
buggedcom wants to merge 1 commit into
Conversation
… scans Addresses the four findings from the store review: 1. Reads no longer block the event loop. The websocket handlers (ws_get_events, ws_get_event_bounds) and the DB-backed global sensors now run their blocking SQLite reads in the executor. Sensors gained a `_compute_blocks_io` flag: the eight event-DB sensors refresh via `async_add_executor_job`, while monitor sensors that read in-memory state keep refreshing inline (offloading those would race with loop-side mutations). One-time initial reads at construction are left inline. 2. _EventDb.query() pushes LIMIT/OFFSET into SQL when there is no entity filter, instead of materialising the whole table and paginating in Python. With an entity filter the JSON-array intersection still runs in Python (then paginates). 3 & 4. _EventDb holds a single long-lived, lock-guarded connection instead of opening — and leaking (relying on GC to close) — a fresh connection per operation. Added _EventDb.close()/DatapointsStore.close(), invoked from async_unload_entry so the connection is released on unload. Tests: added a sync-hass stub so offloaded sensor refreshes run inline under test; updated the websocket assertions to positional args. All 425 pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Addresses all four findings from the SQLite event-store review.
1 — Reads no longer block the event loop
Previously the DB reads ran synchronously on the loop. Now:
ws_get_eventsandws_get_event_boundsrun their reads viaasync_add_executor_job(these are the heavy ones —get_eventscan materialise a wide time range)._compute_blocks_ioflag on the base. The 8 event-DB global sensors refresh off-loop through the executor; monitor sensors that read in-memory state keep refreshing inline (offloading those would race with loop-side mutations). One-time initial reads at construction stay inline.2 —
query()stops full-scanning_EventDb.query()now pushesLIMIT/OFFSETinto SQL when there's no entity filter, instead of loading the whole table and slicing in Python. With an entity filter the JSON-array intersection still runs in Python (indexed time-range in SQL first), then paginates.3 & 4 — Connection lifecycle
_EventDbnow holds a single long-lived, lock-guarded connection (check_same_thread=False+ athreading.Lockserialising access) instead of opening a fresh connection per operation and relying on GC to close it. Addedclose()on_EventDb/DatapointsStore, called fromasync_unload_entryto release it on unload.Tests
async_add_executor_job).ruff check+ruff formatclean.Backend-only; frontend bundle changes are just the version banner. Bumps to 0.6.4.
🤖 Generated with Claude Code