Commit Graph

746 Commits (db6f0827aa9b9f9c31dd49f8c2ff552a8c9f7093)

Author SHA1 Message Date
J. Nick Koston 7bceedfc95
Bump sqlalchemy to 2.0.7 (#90256) 2023-03-25 07:05:35 -10:00
J. Nick Koston 02ef7d445d
Allow passing an optional name to async_track_time_interval (#90244)
* Allow passing an optional name to async_track_time_interval

This is the same idea as passing a name to asyncio.create_task which
makes it easier to track down bugs

* more

* short

* still cannot find it

* add a few more

* test
2023-03-25 10:11:14 -04:00
J. Nick Koston e17cefd61c
Clear recorder startup tasks from memory after processing (#90240)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
2023-03-24 10:24:02 -10:00
J. Nick Koston 4c45c3c63b
Add a faster query for get_last_state_changes when the number of states is 1 (#90211)
* Add a faster query for get_last_state_changes when the number of states is 1

related issue #90113

* Add a faster query for get_last_state_changes when the number of states is 1

related issue #90113

* coverage

* Apply suggestions from code review
2023-03-24 09:39:55 -04:00
J. Nick Koston a44d6f30c9
Fix refactoring error in states/events context id migration (#90193)
fixes #90074
2023-03-23 20:56:04 -04:00
J. Nick Koston d49fbc17df
Fix recorder attribute excludes not being effective until after startup (#90198)
* Fix attribute excludes not being effective until after startup

fixes #90016

* reduce
2023-03-23 20:52:37 -04:00
J. Nick Koston 8a591fa16e
Add auto repairs for events schema (#90136)
* Add auto repairs for events schema

* Add auto repairs for events schema

* Add auto repairs for events schema

* Add auto repairs for events schema

* Add auto repairs for events schema

* fix bug - wrong table
2023-03-22 21:17:36 -04:00
J. Nick Koston 31c988c4f0
Fix index not being dropped on postgresql databases with a schema prefix (#90144)
* Fix index not being dropped on postgresql databases with a schema prefix

Added logging in case index drops fail so we can tell
why in the future

* coverage
2023-03-22 21:03:26 -04:00
J. Nick Koston 4ebce9746d
Add schema auto repairs for states tables (#90083) 2023-03-22 10:05:23 -10:00
J. Nick Koston 96225bb287
Rename recorder run_history to table_managers.recorder_runs_manager (#90070) 2023-03-21 18:38:33 -10:00
J. Nick Koston ddcaa9d372
Break out statistics repairs into a `auto_repairs` modules (#90068)
* Break out statistics schema repairs into a repairs module

A future PR will add repairs for events, states, etc

* reorg

* reorg

* reorg

* reorg

* fixes

* fix patch targets

* name space rename
2023-03-21 21:08:06 -04:00
J. Nick Koston 086bcfb2fc
Make recorder migration column types for each dialect constants (#90072)
Make column types for each dialect constants
2023-03-21 21:06:10 -04:00
J. Nick Koston 616e6e6ae8
Fix missing length on context id and incorrect precision with MariaDB (dev only fix) (#90058)
Fix missing length on context id column with MariaDB

spotted by @dcoder42

The migration still worked as intented but the blob should have been
a bit smaller.

This only affects dev so there is no need for a backport
2023-03-21 13:14:27 -04:00
J. Nick Koston 6bb80adbb9
Rollback the session after performing stats schema validation (#89904) 2023-03-20 17:15:11 +01:00
J. Nick Koston d33a303a83
Fix statistics schema µs precision auto repair being ineffective (#89902)
If a user manually migrated their database to MySQL or PostgresSQL
and incorrectly created the timestamp columns as float we would
fail to correct them to double because when we migrated to use
timestamps for the columns I missed that we needed to change the
columns and types for µs precision
2023-03-20 00:06:37 -04:00
J. Nick Koston e798c30b8b
Fix statistics schema auto repair when there is bad data (#89903)
- If the user had previously duplicated data we could end up
  picking the next metadata_id and there could be stale rows
  in the database that have that metadata_id. This can only happen
  from bad manual migrations (which is what this is function
  is validating in the first place). To solve this we now insert
  data with a future date and look at the latest inserted row
  instead of the first.

Example
```
['stored_statistics',
  defaultdict(<class 'list'>,
              {'recorder.db_test_schema': [{'end': 948589200.0,
                                            'last_reset': None,
                                            'max': None,
                                            'mean': 2021.0,
                                            'min': None,
                                            'start': 948585600.0,
                                            'state': None,
                                            'sum': 394.5068},
                                          {'end': 1601946000.000001,
                                            'last_reset': 1601942400.000001,
                                            'max': 1.000000000000001,
                                            'mean': 1.000000000000001,
                                            'min': 1.000000000000001,
                                            'start': 1601942400.000001,
                                            'state': 1.000000000000001,
                                            'sum': 1.000000000000001}]})]
```
2023-03-20 00:06:23 -04:00
J. Nick Koston c94b054d75
Retain history when renaming an entity_id (#89963)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
2023-03-19 17:33:21 -10:00
J. Nick Koston affb48d271
Avoid joining states_meta for statistics queries (#89941) 2023-03-19 16:44:35 -10:00
J. Nick Koston bf63e6cbd4
Set unique on StatesMeta and EventTypes database tables (#89971)
Set unique on StatesMeta and EventTypes

These should have been marked unique originally to prevent
collision bugs from going unnoticed. These have not been
to beta yet so this is not a breaking change
2023-03-19 22:30:01 -04:00
J. Nick Koston 817ba97227
Remove unneeded lambda_stmt in place add in statistics (#89943)
We can generate this entire query in a single lambda_stmt
so there is no need to add two which increases the size
of the cache key
2023-03-19 22:05:39 -04:00
J. Nick Koston aebe4c66a6
Fix cpu thrashing during purge after all legacy events were removed (#89923)
* Fix cpu thrashing during purge after all legacy events were removed

We now remove the the index of of event ids on the states table when its
all NULLs to save space. The purge path needs to avoid checking for legacy
rows to purge if the index has been removed since it will result in a full
table scan each purge cycle that will always find no legacy rows to purge

* one more place

* drop the key constraint as well

* fixes

* more sqlite
2023-03-19 22:04:24 -04:00
J. Nick Koston 7f3e4cb3af
Guard against selecting all invalid entity_ids in history (#89929)
If all the entity_ids that were provided do not exist we would
end up passing an empty list of ids to the SQL query which
would do an unbounded select
2023-03-19 22:03:12 -04:00
J. Nick Koston 5ffb233004
Avoid database executor job to fetch statistic metadata on cache hit (#89960)
* Avoid database executor job to fetch statistic metadata on cache hit

Since we will almost always have a cache hit fetching
statistic meta data we can avoid an executor job

* Avoid database executor job to fetch statistic metadata on cache hit

Since we will almost always have a cache hit fetching
statistic meta data we can avoid an executor job

* Avoid database executor job to fetch statistic metadata on cache hit

Since we will almost always have a cache hit fetching
statistic meta data we can avoid an executor job

* remove exception catch since the threading.excepthook will actually catch this in production

* fix a few missed ones

* threadsafe

* Update homeassistant/components/recorder/table_managers/statistics_meta.py

* coverage and optimistic caching
2023-03-19 22:01:16 -04:00
J. Nick Koston b1f64de6ce
Remove the old ix_states_event_id index if its no longer being used (#89901)
* Remove the old ix_states_event_id index if its no longer being used

* cover it

* fixes

* fixup

* Update homeassistant/components/recorder/tasks.py
2023-03-17 20:27:33 -04:00
J. Nick Koston 377dff5ee4
Ensure all recorder session executes use retries or the execute helper (#89888) 2023-03-17 10:45:58 -10:00
J. Nick Koston aa72b48725
Mark recorder system_health session read_only (#89842) 2023-03-17 10:22:21 +01:00
J. Nick Koston dbb2706c76
Reduce number of tasks created by compiling missing statistics (#89835) 2023-03-16 21:07:14 -10:00
J. Nick Koston f6f3565796
Reduce latency to find stats metadata (#89824) 2023-03-16 19:00:02 -10:00
J. Nick Koston ed27dae173
Small cleanups to recorder history (#89774)
* Small cleanups to recorder history

* Small cleanups to recorder history

* fixes

* flake8 cannot figure it out
2023-03-15 23:44:33 -04:00
J. Nick Koston 99d6b1fa57
Migrate States to use a table manager (#89769) 2023-03-15 16:19:43 -10:00
J. Nick Koston e379aa23bd
Migrate StateAttributes to use a table manager (#89760)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
2023-03-15 15:26:29 -10:00
J. Nick Koston aec2d63302
Add keep_days to recorder.purge_entities (#89726) 2023-03-15 11:13:47 -10:00
J. Nick Koston a244749712
Make StatesMetaManager thread-safe when an entity_id is fully deleted from the database and than re-added (#89732)
* refactor to make StatesMetaManager threadsafe

* refactor to make StatesMetaManager threadsafe

* refactor to make StatesMetaManager threadsafe

* refactor to make StatesMetaManager threadsafe

* reduce

* comments
2023-03-15 08:54:02 -04:00
J. Nick Koston 59de7f3057
Migrate EventData management to a table manager (#89716) 2023-03-14 21:40:59 -10:00
J. Nick Koston b906d67c1e
Fix filtered purge not removing newer events (#89721) 2023-03-14 16:33:19 -10:00
J. Nick Koston 0630b7b962
Reduce size of load query to prime event_types and states_meta at startup (#89677) 2023-03-14 21:31:31 +01:00
J. Nick Koston a6d6807dd0
Add typing to statistics results (#89118) 2023-03-14 09:06:56 -10:00
J. Nick Koston dbc0890ce8
Add index to event_type and entity_id (#89676) 2023-03-14 10:09:21 +01:00
Paulus Schoutsen 8a4233ac8e
Bump SQLAlchemy to 2.0.6 (#89650) 2023-03-13 08:51:01 -10:00
J. Nick Koston d1ee303e85
Drop duplicated indices from recorder database schema (#89613)
Drop duplicated indices from schema

https://docs.percona.com/percona-toolkit/pt-duplicate-key-checker.html

```
% pt-duplicate-key-checker --databases fresh

ALTER TABLE `fresh`.`events` DROP INDEX `ix_events_event_type_id`;

ALTER TABLE `fresh`.`states` DROP INDEX `ix_states_metadata_id`;

ALTER TABLE `fresh`.`statistics` DROP INDEX `ix_statistics_metadata_id`;

ALTER TABLE `fresh`.`statistics_short_term` DROP INDEX `ix_statistics_short_term_metadata_id`;

```
2023-03-13 09:24:57 +01:00
J. Nick Koston b9ac6b4a7c
Improve reliability of context id migration (#89609)
* Split context id migration into states and events tasks

Since events can finish much earlier than states we
would keep looking at the table because states as not
done. Make them seperate tasks

* add retry dec

* fix migration happening twice

* another case
2023-03-12 21:41:48 -04:00
J. Nick Koston 85ca94e9d4
Mark database sessions that do not write data as read_only (#89600)
* Mark sessions that do not write data as read_only

* Mark sessions that do not write data as read_only
2023-03-12 21:33:28 -04:00
J. Nick Koston 459ea048ba
Fix old indices never being removed with PostgreSQL (#89599) 2023-03-12 14:07:05 -10:00
J. Nick Koston c41f91be89
Deduplicate entity_id in the states table (#89557) 2023-03-12 10:01:58 -10:00
J. Nick Koston 50c31a5355
Move legacy database queries and models to prepare for schema v38 (#89532) 2023-03-11 11:26:30 -10:00
J. Nick Koston 16b420d660
Fix get_significant_states_with_session query looking at legacy columns (#89558) 2023-03-11 10:37:00 -10:00
J. Nick Koston 8bd43760b6
Deduplicate event_types in the events table (#89465)
* Deduplicate event_types in the events table

* Deduplicate event_types in the events table

* more fixes

* adjust

* adjust

* fix product

* fix tests

* adjust

* migrate

* migrate

* migrate

* more test fixes

* more test fixes

* fix

* migration test

* adjust

* speed up

* fix index

* fix more tests

* handle db failure

* preload

* tweak

* adjust

* fix stale docs strings, remove dead code

* refactor

* fix slow tests

* coverage

* self join to resolve query performance

* fix typo

* no need for quiet

* no need to drop index already dropped

* remove index that will never be used

* drop index sooner as we no longer use it

* Revert "remove index that will never be used"

This reverts commit 461aad2c52.

* typo
2023-03-11 14:54:55 -05:00
J. Nick Koston 52cea16f74
Remove unused code in RecorderRuns.entity_ids (#89526) 2023-03-11 12:46:12 +01:00
J. Nick Koston 9e1ba8534a
Fix data migration never finishing when database has invalid datetimes (#89474)
* Fix data migration never finishing when database has invalid datetimes

If there were impossible datetime values in the database (likely
from a manual sqlite to MySQL conversion) the conversion would
never complete

* Update homeassistant/components/recorder/migration.py
2023-03-09 21:03:41 -05:00
J. Nick Koston 170a13302c
Reduce overhead to store context ids in the database (#88942) 2023-03-08 14:51:45 -10:00