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 history API not returning data when recorder runs are lost #123449

Closed
wants to merge 4 commits into from

Conversation

tanelvakker
Copy link

@tanelvakker tanelvakker commented Aug 9, 2024

Proposed change

Removes check for recorder run when querying history using API. Fixes #117793

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

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:

@tanelvakker tanelvakker requested a review from a team as a code owner August 9, 2024 11:14
Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tanelvakker

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant home-assistant bot marked this pull request as draft August 9, 2024 11:14
@home-assistant
Copy link

home-assistant bot commented Aug 9, 2024

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant
Copy link

home-assistant bot commented Aug 9, 2024

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

Code owner commands

Code owners of history 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 history 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.

@@ -107,8 +107,7 @@ async def get(
no_attributes = "no_attributes" in request.query

if (
(end_time and not has_recorder_run_after(hass, end_time))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping has_recorder_run_after would get fixed instead of dropping the check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root cause of this issue is probably not in this code. There is something happening in recorder that loses previous runs in the database. What could be causing this is beyond my comprehension.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the recorder run missing from the table? Maybe the purge code deletes it too early, and that’s what we need to fix instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, but really no idea what causes this to happen. It happens less than once per month and makes it hard to debug for me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the logic to remove the recorder runs in case you see anything obviously wrong

def _purge_old_recorder_runs(

Copy link
Member

@bdraco bdraco Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the purge is finished we need to store:

The timestamp of the possible oldest row in the database (ie purge before) in the table
Update the memory cache of the same

I think we just need purge finished for the figuring out when to remove the old ones from the table and we don't care about when it started

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so we add a new row only when a purge completes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor

@emontnemery emontnemery Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #130378, which corrects the logic for purging recorder runs, has been merged and will be included in HA Core 2024.12.
I've started working on purge_runs table as suggested, but I'm not sure it'll be ready in time for HA Core 2024.12.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some more discussion, we don't think a purge runs table is needed. Here's a PR which replaces checking recorder run start time with the timestamp of the oldest state in the database: #131702

@joostlek joostlek marked this pull request as draft September 8, 2024 11:09
@joostlek
Copy link
Member

joostlek commented Sep 8, 2024

Putting to draft as the discussion above leads me to believe that the approach needs to be re-evaluated

@emontnemery
Copy link
Contributor

@tanelvakker @RudolfRendier I think we can close this PR now because #131702 has been merged which fixes the underlying issue.

@tanelvakker
Copy link
Author

As this was fixed on recorder side, closing this PR

@github-actions github-actions bot locked and limited conversation to collaborators Nov 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants