Skip to content

Prevent sqlalchemy from refetching the old_state_id as it will never change#40982

Merged
balloob merged 1 commit intohome-assistant:devfrom
bdraco:avoid_old_state_id_refetch
Oct 1, 2020
Merged

Prevent sqlalchemy from refetching the old_state_id as it will never change#40982
balloob merged 1 commit intohome-assistant:devfrom
bdraco:avoid_old_state_id_refetch

Conversation

@bdraco
Copy link
Copy Markdown
Member

@bdraco bdraco commented Oct 1, 2020

Proposed change

Prevent sqlalchemy from refetching the old_state_id as it will never change

Disable expire_on_commit for the event writer. Since we never expect the
old_state_id to change in the database, it was never worth the expense of
refetching the id after the commit.

Screen_Shot_2020-10-01_at_10_38_17_AM

I had thought I solved this in #40467 but
since I didn't have the number of state changes needed for it to come up in the profile,
I missed that sqlalchemy would refresh the old state id since it would expire the object
on commit (default behavior) even though it never changes.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example entry for configuration.yaml:

# Example configuration.yaml

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

…change

Disable expire_on_commit for the event writer. Since we never expect the
old_state_id to change in the database, it was never worth the expense of
refetching the id after the commit.
@balloob balloob merged commit 6b509fd into home-assistant:dev Oct 1, 2020
balloob pushed a commit that referenced this pull request Oct 1, 2020
…change (#40982)

Disable expire_on_commit for the event writer. Since we never expect the
old_state_id to change in the database, it was never worth the expense of
refetching the id after the commit.
bdraco added a commit to bdraco/home-assistant that referenced this pull request Oct 7, 2020
While home-assistant#40982 solved the performance overhead of expiring every time
we commit the event session, it caused a regression which was
fixed in home-assistant#41349.

Ideally we could avoid the overhead of expiring objects on commit
since we are never going to use them again by using expunge after
the commit to remove the objects we no longer need. Unfortunately
this causes sqlalchemy to spend quite a bit of time sorted out
state when adding new objects to the event session after an
expunge_all so it wasn't a viable option.

As not expring the objects causes unexpected side effects, we can
limit the impact of having to expring them by only doing the expire
every 120 commits (by default every 2 minutes) instead of every commit
(by default every second). This works because the expire operation
itself is expensive reguardless of the number of objects it is expiring.

To verify this approach does not leak memory, a scale test integration
was created (https://github.com/bdraco/scaletest) and run for hours
along with an objgraph dump setup every 30 seconds to verify States
and Events were being properly disposed of
(https://mg.pov.lt/objgraph/objgraph.html#locating-and-filtering-objects)

In testing this took a machine running the scaletest integration
from 100% cpu to 4% cpu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Higher CPU continues in 0.118

3 participants