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

Convert to betterproto #37

Merged
merged 10 commits into from
May 23, 2024
Merged

Convert to betterproto #37

merged 10 commits into from
May 23, 2024

Conversation

llucax
Copy link
Contributor

@llucax llucax commented Apr 11, 2024

This PR migrated the Microgrid API to use betterproto instead of grpc.

A couple of bugs were found on the way and fixed, one of them directly related to the lack of proper type hints in grpc.

The changes are fairly small.

Note

This PR is just a draft, tests need to be adapted still and I don't plan to merge this to v0.x.x directly, as it would be an unnecessary breaking change for the branch used by the SDK. This is mainly to show what changes are needed to migrate a project to betterproto.

This PR is using an unreleased repository that hosts the files generated by betterproto from the protobuf files. It will be published soon ™️

@github-actions github-actions bot added part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.) part:client Affects the client code labels Apr 11, 2024
@llucax llucax self-assigned this Apr 11, 2024
Copy link

@cwasicki cwasicki left a comment

Choose a reason for hiding this comment

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

Wouldn't the first two commits make sense even when we not migrate to betterproto?

pyproject.toml Outdated Show resolved Hide resolved
@llucax
Copy link
Contributor Author

llucax commented Apr 16, 2024

Wouldn't the first two commits make sense even when we not migrate to betterproto?

Yes, also fixing the missing await, I just left it like that for "presentation purposes", but I think it makes sense to make a separate PR fixing the other issues in this PR that are not related to betterproto 👍

@llucax llucax force-pushed the betterproto branch 2 times, most recently from 36bf487 to c3ec230 Compare May 8, 2024 16:18
@llucax llucax added this to the v0.4.0 milestone May 15, 2024
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Some notable changes:

* The imports can be greatly simplified as now we can import whole
  modules and avoid clashes without having to create one alias per
  symbol.
* We don't need to cast awaitable functions anymore as betterproto has
  the correct type hints
* Because of the above, a bug was uncovered and fixed, where an `await`
  was missing for the API call to `AddInclusionBounds`. I'm not sure if
  this call was not working at all or just errors wouldn't be detected.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Since betterproto use type hints, it expects the `ConnectionFilter`
`starts` and `end` to be `list`s.

We might also just take a `list` in the `connections()` methods instead
to avoid the copy, maybe ensuring that the filter elements are unique
(which was probably the reason to make them a `set`) is not that
important.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
These tests need just minor changes to the imports.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
This only adds unnecessary extra indentation.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
In future commits we'll remove the testing grpc server in favour of
just mocking the stub, so we don't need to really listen to a port and
include in the unit tests all the gRPC machinery, when we only want to
test the client does what it is supposed to do when getting some data
from the stub.

This also removes another level of extra indentation.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Fixing this tests is quite complicated because we also stop using a real
gRPC server (`mock_api`) for the tests, to avoid introducing too much
noise and flakiness to *unit tests* (see
frequenz-floss/frequenz-sdk-python#662 for
example).

For now we just tried to replace `mock_api` with `unittest.mock` mocks,
doing the minimal work to get the tests working and move forward with
the migration to betterproto, but in the future we'll provide a higher
level API client mock (see
frequenz-floss#39).

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
The timeouts are now tested as part of gRPC failure tests in the client
tests, since timeouts are handled by `grpclib` there isn't much we can
specifically test about timeouts that is not just testing `grpclib`
itself.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
We don't use this module anymore and we don't plan to use it for now as
it is very flaky. In the future we might use something similar to do
integration tests.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax llucax marked this pull request as ready for review May 23, 2024 07:56
@llucax llucax requested review from a team as code owners May 23, 2024 07:56
@llucax
Copy link
Contributor Author

llucax commented May 23, 2024

This will not pass the tests because it is still waiting for a release of microgrid-betterproto, but it should be ready for the final review, as no code changes should be needed.

@github-actions github-actions bot added the part:docs Affects the documentation label May 23, 2024
@llucax
Copy link
Contributor Author

llucax commented May 23, 2024

Now everything is green 💚

@llucax
Copy link
Contributor Author

llucax commented May 23, 2024

Enabling auto-merge.

@llucax llucax enabled auto-merge May 23, 2024 10:20
@llucax llucax added the type:enhancement New feature or enhancement visitble to users label May 23, 2024
requires = [
"setuptools == 68.1.0",
"setuptools_scm[toml] == 7.1.0",
"frequenz-repo-config[lib] == 0.9.1",

Choose a reason for hiding this comment

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

Why is repo-config removed here?

Copy link
Contributor Author

@llucax llucax May 23, 2024

Choose a reason for hiding this comment

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

Including repo-config as a build dependency in every project was actually a mistake, it was only needed for API projects and now that this is taken care by setuptools-betterproto (if we do the switch), it will not be used by any project. My idea is to split repo-config even further in the future, it was an experiment and now that we have a much clear picture about what we need from it, it is a good time to split it into smaller projects and use those smaller projects only where necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is unrelated to this PR, though, I just did it now because I realized it was a mistake to include repo-config in the build dependencies right after creating setuptools-betterproto.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@@ -390,7 +382,7 @@ def from_proto(cls, raw: PbComponentData) -> Self:
raw_power = raw.inverter.data.ac.power_active
inverter_data = cls(
component_id=raw.id,
timestamp=raw.ts.ToDatetime(tzinfo=timezone.utc),
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm we have functions for the conversion in the client library

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 comes from the SDK, predates all the recent work we've been doing with APIs.

@llucax llucax added this pull request to the merge queue May 23, 2024
Merged via the queue into frequenz-floss:v0.x.x with commit c46c680 May 23, 2024
14 checks passed
@llucax llucax deleted the betterproto branch May 23, 2024 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
part:client Affects the client code part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.) type:enhancement New feature or enhancement visitble to users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants