mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Increase the sqlite cache size from ~2MiB to 8MiB (#50747)
This commit is contained in:
parent
3554316f3f
commit
72288710ca
@ -331,7 +331,7 @@ class Recorder(threading.Thread):
|
|||||||
self._pending_expunge = []
|
self._pending_expunge = []
|
||||||
self.event_session = None
|
self.event_session = None
|
||||||
self.get_session = None
|
self.get_session = None
|
||||||
self._completed_database_setup = None
|
self._completed_first_database_setup = None
|
||||||
self._event_listener = None
|
self._event_listener = None
|
||||||
self.async_migration_event = asyncio.Event()
|
self.async_migration_event = asyncio.Event()
|
||||||
self.migration_in_progress = False
|
self.migration_in_progress = False
|
||||||
@ -837,15 +837,16 @@ class Recorder(threading.Thread):
|
|||||||
def _setup_connection(self):
|
def _setup_connection(self):
|
||||||
"""Ensure database is ready to fly."""
|
"""Ensure database is ready to fly."""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
self._completed_database_setup = False
|
self._completed_first_database_setup = False
|
||||||
|
|
||||||
def setup_recorder_connection(dbapi_connection, connection_record):
|
def setup_recorder_connection(dbapi_connection, connection_record):
|
||||||
"""Dbapi specific connection settings."""
|
"""Dbapi specific connection settings."""
|
||||||
if self._completed_database_setup:
|
setup_connection_for_dialect(
|
||||||
return
|
self.engine.dialect.name,
|
||||||
self._completed_database_setup = setup_connection_for_dialect(
|
dbapi_connection,
|
||||||
self.engine.dialect.name, dbapi_connection
|
not self._completed_first_database_setup,
|
||||||
)
|
)
|
||||||
|
self._completed_first_database_setup = True
|
||||||
|
|
||||||
if self.db_url == SQLITE_URL_PREFIX or ":memory:" in self.db_url:
|
if self.db_url == SQLITE_URL_PREFIX or ":memory:" in self.db_url:
|
||||||
kwargs["connect_args"] = {"check_same_thread": False}
|
kwargs["connect_args"] = {"check_same_thread": False}
|
||||||
|
@ -254,12 +254,13 @@ def execute_on_connection(dbapi_connection, statement):
|
|||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
|
|
||||||
def setup_connection_for_dialect(dialect_name, dbapi_connection):
|
def setup_connection_for_dialect(dialect_name, dbapi_connection, first_connection):
|
||||||
"""Execute statements needed for dialect connection."""
|
"""Execute statements needed for dialect connection."""
|
||||||
# Returns False if the the connection needs to be setup
|
# Returns False if the the connection needs to be setup
|
||||||
# on the next connection, returns True if the connection
|
# on the next connection, returns True if the connection
|
||||||
# never needs to be setup again.
|
# never needs to be setup again.
|
||||||
if dialect_name == "sqlite":
|
if dialect_name == "sqlite":
|
||||||
|
if first_connection:
|
||||||
old_isolation = dbapi_connection.isolation_level
|
old_isolation = dbapi_connection.isolation_level
|
||||||
dbapi_connection.isolation_level = None
|
dbapi_connection.isolation_level = None
|
||||||
execute_on_connection(dbapi_connection, "PRAGMA journal_mode=WAL")
|
execute_on_connection(dbapi_connection, "PRAGMA journal_mode=WAL")
|
||||||
@ -267,13 +268,13 @@ def setup_connection_for_dialect(dialect_name, dbapi_connection):
|
|||||||
# WAL mode only needs to be setup once
|
# WAL mode only needs to be setup once
|
||||||
# instead of every time we open the sqlite connection
|
# instead of every time we open the sqlite connection
|
||||||
# as its persistent and isn't free to call every time.
|
# as its persistent and isn't free to call every time.
|
||||||
return True
|
|
||||||
|
# approximately 8MiB of memory
|
||||||
|
execute_on_connection(dbapi_connection, "PRAGMA cache_size = -8192")
|
||||||
|
|
||||||
if dialect_name == "mysql":
|
if dialect_name == "mysql":
|
||||||
execute_on_connection(dbapi_connection, "SET session wait_timeout=28800")
|
execute_on_connection(dbapi_connection, "SET session wait_timeout=28800")
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def end_incomplete_runs(session, start_time):
|
def end_incomplete_runs(session, start_time):
|
||||||
"""End any incomplete recorder runs."""
|
"""End any incomplete recorder runs."""
|
||||||
|
@ -152,7 +152,7 @@ def test_setup_connection_for_dialect_mysql():
|
|||||||
|
|
||||||
dbapi_connection = MagicMock(cursor=_make_cursor_mock)
|
dbapi_connection = MagicMock(cursor=_make_cursor_mock)
|
||||||
|
|
||||||
assert util.setup_connection_for_dialect("mysql", dbapi_connection) is False
|
util.setup_connection_for_dialect("mysql", dbapi_connection, True)
|
||||||
|
|
||||||
assert execute_mock.call_args[0][0] == "SET session wait_timeout=28800"
|
assert execute_mock.call_args[0][0] == "SET session wait_timeout=28800"
|
||||||
|
|
||||||
@ -167,9 +167,17 @@ def test_setup_connection_for_dialect_sqlite():
|
|||||||
|
|
||||||
dbapi_connection = MagicMock(cursor=_make_cursor_mock)
|
dbapi_connection = MagicMock(cursor=_make_cursor_mock)
|
||||||
|
|
||||||
assert util.setup_connection_for_dialect("sqlite", dbapi_connection) is True
|
util.setup_connection_for_dialect("sqlite", dbapi_connection, True)
|
||||||
|
|
||||||
assert execute_mock.call_args[0][0] == "PRAGMA journal_mode=WAL"
|
assert len(execute_mock.call_args_list) == 2
|
||||||
|
assert execute_mock.call_args_list[0][0][0] == "PRAGMA journal_mode=WAL"
|
||||||
|
assert execute_mock.call_args_list[1][0][0] == "PRAGMA cache_size = -8192"
|
||||||
|
|
||||||
|
execute_mock.reset_mock()
|
||||||
|
util.setup_connection_for_dialect("sqlite", dbapi_connection, False)
|
||||||
|
|
||||||
|
assert len(execute_mock.call_args_list) == 1
|
||||||
|
assert execute_mock.call_args_list[0][0][0] == "PRAGMA cache_size = -8192"
|
||||||
|
|
||||||
|
|
||||||
def test_basic_sanity_check(hass_recorder):
|
def test_basic_sanity_check(hass_recorder):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user