Skip to content

Feature/reorg recorder#6237

Merged
balloob merged 12 commits into
devfrom
feature/reorg-recorder
Feb 26, 2017
Merged

Feature/reorg recorder#6237
balloob merged 12 commits into
devfrom
feature/reorg-recorder

Conversation

@balloob
Copy link
Copy Markdown
Member

@balloob balloob commented Feb 26, 2017

Description:

This is the successor of #6196 and builds on the work of @kellerza in #6167.

  • Hopefully fixes the flaky restore_state test
  • Keep track of recorder instance in hass.data
  • Break up recorder into migration, util, purge
  • Waiting for connection is now always async

Thanks to @kellerza for all the amazing tests on the recorder component 👍

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.github.io#<home-assistant.github.io PR number goes here>

Example entry for configuration.yaml (if applicable):

history:

Checklist:

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

@mention-bot
Copy link
Copy Markdown

@balloob, thanks for your PR! By analyzing the history of the files in this pull request, we identified @kellerza, @rhooper and @justyns to be potential reviewers.

from .test_init import hass_recorder # noqa


def test_recorder_bad_commit(hass_recorder):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

redefinition of unused 'hass_recorder' from line 8

Comment thread tests/components/recorder/test_init.py Outdated
from homeassistant.const import MATCH_ALL
from homeassistant.components import recorder
from homeassistant.components.recorder.const import DATA_INSTANCE
from homeassistant.components.recorder.util import session_scope, execute
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'homeassistant.components.recorder.util.execute' imported but unused

Comment thread tests/components/recorder/test_init.py Outdated
from datetime import datetime, timedelta
import unittest
from unittest.mock import patch, call, MagicMock
from unittest.mock import patch, MagicMock
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'unittest.mock.patch' imported but unused
'unittest.mock.MagicMock' imported but unused

Comment thread tests/common.py
{recorder.DOMAIN: config})
assert recorder.DOMAIN in hass.config.components
run_coroutine_threadsafe(
recorder.wait_connection_ready(hass), hass.loop).result()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One nice thing about threading.Event.wait() was the timeout, do you know if asyncio.Event.wait() has an equivalent?

Raising an exception after some seconds of waiting really helps with fault finding. Before that tests would block without any indication of what you did wrong.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe the same mechanism used with aiohttp can be used?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You mean async_timeout

Copy link
Copy Markdown
Member Author

@balloob balloob Feb 26, 2017

Choose a reason for hiding this comment

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

There is a timeout but it's better to run tests with the timeout flag (like we do on travis), that way we don't have to add timeouts all over the place in our tests but instead have PyTest manage it for us:

py.test tests/components/recorder -x --timeout=2


if event.event_type == EVENT_TIME_CHANGED:
elif event is purge_task:
purge.purge_old_data(self, self.purge_days)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍 probably never got issues since this happens so infrequently, but much better as part of this worker loop

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We should make sure that any data mutation happens from 1 thread.

session.add(work)
session.commit()
return True
except sqlalchemy.exc.OperationalError as err:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Never could really get to the bottom if repeating this 3 times had any value. Guess you are of the opinion it wont make a difference?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see the commit function does this, but not used everywhere

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I find it kinda weird that we do things 3 times and I think that it also added to the confusion of how SQLAlchemy works (instantiating sessions everywhere for example).

try:
yield session
session.commit()
except SQLAlchemyError as err:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I prefer this as a general Exception, since it is important to log any error.

There was an issue aboit invalid codepage TypeError that this also fixed.

All errors will be passed on in any case, but if we log it at least users can fix these type of things

continue
with session_scope(session=self.get_session()) as session:
dbevent = models.Events.from_event(event)
session.add(dbevent)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this one not use the commit function that retries?

Comment thread tests/components/recorder/test_util.py Outdated
def test_recorder_bad_execute(hass_recorder):
"""Bad execute, retry 3 times."""
from sqlalchemy.exc import SQLAlchemyError
hass = hass_recorder()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

local variable 'hass' is assigned to but never used

@balloob balloob merged commit 61909e8 into dev Feb 26, 2017
@balloob balloob deleted the feature/reorg-recorder branch February 26, 2017 22:38
@balloob
Copy link
Copy Markdown
Member Author

balloob commented Feb 26, 2017

Merging this now so we can have the tests work again. We can iterate on it later if we want more changes.

If recorder issues stay persistent on 0.39 we might want to consider adding this PR as a point release.

@home-assistant home-assistant locked and limited conversation to collaborators Jun 2, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants