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

Don't sleep on PowerDistributor startup #971

Merged
merged 5 commits into from
Jun 20, 2024

Conversation

shsms
Copy link
Contributor

@shsms shsms commented Jun 19, 2024

The PowerDistributor used to sleep for 2 seconds on startup, before
processing requests, to wait for data. This feature is no longer
being used, because it won't receive requests from the PowerManager
unless there's data. This PR removes it.

shsms added 5 commits June 19, 2024 12:49
This is done by patching the wait time for component data in the
battery pool to a lower value.

Signed-off-by: Sahas Subramanian <[email protected]>
A function's existence was being checked rather than its (boolean)
result.

This wasn't an issue in practice, because ever since we've had the
PowerManager, the PowerDistributor has not received requests when
there's no data.

Signed-off-by: Sahas Subramanian <[email protected]>
This is because the initial wait time is going to be removed from the
power distributor.

Signed-off-by: Sahas Subramanian <[email protected]>
The power distributor is no longer controlled directly by the users,
but instead gets requests only from the power manager.  So if will not
receive requests until there's data.

But if it receives requests when there's no data, it will return an
`Error` response, saying there's no data.

Signed-off-by: Sahas Subramanian <[email protected]>
And instead sleep for the same amount of time, before sending requests
to the power distributor.

This sleep is needed only in the direct tests of the PowerDistributor.

In the `*_pool_control_methods` tests, PowerDistributor receives
requests from the PowerManager, which would happen only when there are
data, so additional sleep is not required in those tests.

Signed-off-by: Sahas Subramanian <[email protected]>
@shsms shsms requested a review from a team as a code owner June 19, 2024 11:10
@github-actions github-actions bot added part:tests Affects the unit, integration and performance (benchmarks) tests part:actor Affects an actor ot the actors utilities (decorator, etc.) part:microgrid Affects the interactions with the microgrid labels Jun 19, 2024
@shsms shsms added the cmd:skip-release-notes It is not necessary to update release notes for this PR label Jun 19, 2024
@shsms
Copy link
Contributor Author

shsms commented Jun 19, 2024

Only internal changes, so no release notes necessary.

@llucax
Copy link
Contributor

llucax commented Jun 19, 2024

The CI is failing, not sure why suddenly a lot of cross referencing can't be found, but I guess it might be another ripple effect of mkdocstrings/griffe#294. Will do a quick check to see if downgrading the dependency fixes it.

Comment on lines +92 to +96
mocker.patch.object(
timeseries.battery_pool._methods, # pylint: disable=protected-access
"WAIT_FOR_COMPONENT_DATA_SEC",
0.1,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we still have some other waiting for data as a simple sleep? Shouldn't that wait be removed too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is for the metric streaming in the battery pool. It won't stream any metrics until it has data for all the batteries, etc. So we don't send a new value for capacity or soc as we get data from more and more batteries.

This is independent of the power distributor.

):
await asyncio.sleep(0.1) # wait for actor to collect data
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this PR just moving the issue elsewhere instead? Or is this synchronization only needed for the tests but when actually used one shouldn't care when there is data and when there isn't?

In any case, having tests depend on random sleep makes me sad (as I see the flakiness coming to bite us eventually). 😢 😞

If we can really really get rid of the need for synchronization, then I completely agree we shouldn't provide any way to synchronize, but otherwise I think having a way to explicitly synchronize that is it not a flaky sleep it is a better approach. In terms of performance, even if it is a hot path, it is just one extra boolean evaluation and branch, which modern CPUs probably won´t even care about because they will do branch prediction so the performance might not even be affected at all after the initialization.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I mentioned in the commit message, these are needed only in tests where we control the PowerDistributor directly.

And they are needed only for those tests that need component data, so that PowerDistributor can return Success. Else it will return Error("No data"), but that's not the case we're testing.

It is only a testing problem, not with behaviour in production.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, I'm still not happy about potential flaky tests, but this is still an improvement over what we had before, so approving 👍

@llucax
Copy link
Contributor

llucax commented Jun 20, 2024

The CI is failing, not sure why suddenly a lot of cross referencing can't be found, but I guess it might be another ripple effect of mkdocstrings/griffe#294. Will do a quick check to see if downgrading the dependency fixes it.

Fixed.

@llucax llucax added this to the v1.0.0-rc700 milestone Jun 20, 2024
@shsms shsms added this pull request to the merge queue Jun 20, 2024
Merged via the queue into frequenz-floss:v1.x.x with commit bfc8de2 Jun 20, 2024
18 of 19 checks passed
@shsms shsms deleted the power-dist-no-sleep branch June 20, 2024 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmd:skip-release-notes It is not necessary to update release notes for this PR part:actor Affects an actor ot the actors utilities (decorator, etc.) part:microgrid Affects the interactions with the microgrid part:tests Affects the unit, integration and performance (benchmarks) tests
Projects
Development

Successfully merging this pull request may close these issues.

2 participants