Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logic for purge of recorder runs #130378

Merged
merged 3 commits into from
Nov 26, 2024

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Nov 11, 2024

Proposed change

Fix logic for purge of recorder runs to purge recorder runs which end before the purge date instead of purging runs which start before the purge data

Rationale:
Until fairly recently we did not use the existence of recorder runs as a shortcut to avoid searching for history events, so when we purged the recorder runs was not very important.

However, we do now because of #104724 and I think @RudolfRendier is right, we need to change the purging of recorder runs.

This was noticed in #123449

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)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

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
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format 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.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (recorder) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of recorder can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign recorder Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@emontnemery emontnemery added this to the 2024.11.2 milestone Nov 11, 2024
@RudolfRendier
Copy link

Can you safely compare to .end even if it's not always present in the database record? Will it be set if a recorder_run is closed incorrectly?

Answering my own question:
Looks like 'end_incomplete_runs' does exactly that, it sets closed_incorrect to true and updates 'end' with the start_time (=now):

def end_incomplete_runs(session: Session, start_time: datetime) -> None:

Thanks for the fix @emontnemery

@emontnemery emontnemery marked this pull request as draft November 11, 2024 19:00
@emontnemery
Copy link
Contributor Author

@RudolfRendier That's a good point, I wonder if the order here matters:

def delete_recorder_runs_rows(
    purge_before: datetime, current_run_id: int
) -> StatementLambdaElement:
    """Delete recorder_runs rows."""
    return lambda_stmt(
        lambda: delete(RecorderRuns)
        .filter(RecorderRuns.end < purge_before)
        .filter(RecorderRuns.run_id != current_run_id)
        .execution_options(synchronize_session=False)
    )

I'm setting this to draft until I've had the time to take a closer look.

@frenck frenck removed this from the 2024.11.2 milestone Nov 15, 2024
@frenck
Copy link
Member

frenck commented Nov 15, 2024

As we are releasing the milestone today, I've removed this draft PR for now.

@emontnemery emontnemery marked this pull request as ready for review November 25, 2024 15:47
@emontnemery
Copy link
Contributor Author

Implementation updated to explicitly not consider unclosed recorder runs in purge.
Resulting SQLite query:

INFO:sqlalchemy.engine.Engine:DELETE FROM recorder_runs WHERE recorder_runs."end" IS NOT NULL AND recorder_runs."end" < ? AND recorder_runs.run_id != ?
INFO:sqlalchemy.engine.Engine:[generated in 0.00016s] ('2024-11-25 15:48:52.311384', 1)

@frenck frenck added this to the 2024.12.0b0 milestone Nov 25, 2024
@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label Nov 25, 2024
@emontnemery emontnemery merged commit 44f90dc into dev Nov 26, 2024
58 of 60 checks passed
@emontnemery emontnemery deleted the recorder_fix_recorder_run_purge_logic branch November 26, 2024 06:47
@frenck frenck removed this from the 2024.12.0b0 milestone Nov 26, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Nov 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bugfix by-code-owner cla-signed core has-tests integration: recorder Quality Scale: internal small-pr PRs with less than 30 lines. smash Indicator this PR is close to finish for merging or closing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants