Skip to content

Conversation

@llucax
Copy link
Contributor

@llucax llucax commented Dec 17, 2024

This pull request adds a new ConfigManager class that takes care of the client-side of the ConfigManagingActor, providing the common tasks users of the config system need to do.

The class owns the config channel and provides a convenient method to get new receivers that validate the configuration using a dataclass as a marshmallow schema, and allow filtering to receive only configuration for a specific key. The receiver also reports errors when the configuration can't be properly loaded, and avoid spurious updates when the configuration didn't change compared to the previous one.

The manager also takes care of instantiating both the ConfigManagingActor and the LoggingConfigUpdatingActor and provides a utility function to wait for the first configuration to be received.

@github-actions github-actions bot added 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.) part:actor Affects an actor ot the actors utilities (decorator, etc.) part:config Affects the configuration management labels Dec 17, 2024
@llucax
Copy link
Contributor Author

llucax commented Dec 17, 2024

This is a draft because I need to complete/improve documentation and add tests, but the code should be pretty solid, and the existing tests pass (the CI is red because I left a couple of TODOs that make both pylint and pytest fail).

@llucax llucax self-assigned this Dec 17, 2024
@llucax llucax added the type:enhancement New feature or enhancement visitble to users label Dec 17, 2024
@llucax
Copy link
Contributor Author

llucax commented Dec 19, 2024

If there are no major complains about this approach, I will move forward to clean it up so we can merge it soon. I will start doing that, so if anyone wants to review this, please do it sooner than later @frequenz-floss/python-sdk-team .

@llucax llucax added this to the v1.0.0-rc1500 milestone Dec 19, 2024
@llucax
Copy link
Contributor Author

llucax commented Dec 20, 2024

Maybe too late for this, but these are the main changes compared to the old PR (I had them written down and forgot to put them in the PR when I created it):

  • Make unknown=EXCLUDE the default in ConfigManager
  • Make config manager a background service
  • Remove auto start (requires the constructor to be run in an async context, not useful)
  • Remove skip_none
  • Make passing of marshmallow arguments explicit
  • Make key mandatory
  • Make wrong value for key an error we send and send all errors
  • Catch unexpected exceptions
  • Add a new wait_for_first utility function

This is to be able to use type guards in with `Receiver.filter` to
narrow the received type and the new `WithPrevious` class.

Signed-off-by: Leandro Lucarella <[email protected]>
This class instantiates and starts the `ConfigManagingActor` and creates
the channel needed to communicate with it. It offers a convenience
method to get receivers to receive configuration updates.

Signed-off-by: Leandro Lucarella <[email protected]>
When this option is enabled, configurations will be sent to the receiver
only if they are different from the previously received configuration.

Signed-off-by: Leandro Lucarella <[email protected]>
This forces a better configuration structure, by always having separate
sections for separate entities, and avoids spurious updates and noise
in the received configuration for other parties.

Signed-off-by: Leandro Lucarella <[email protected]>
Configuration can be nested, and actors could have sub-actors that need
their own configuration, so we need to support filtering the
configuration by a sequence of keys.

For example, if the configuration is `{"key": {"subkey": "value"}}`, and
the key is `["key", "subkey"]`, then the receiver will get only
`"value"`.

Signed-off-by: Leandro Lucarella <[email protected]>
This commit adds support for validating configurations with a schema.
This is useful to ensure the configuration is correct and to receive it
as an instance of a dataclass.

The `new_receiver` method now requires a `config_class` argument that is
used to validate the configuration. If the configuration doesn't pass
the validation, it will be ignored and an error will be logged.

The schema is expected to be a dataclass, which is used to create a
marshmallow schema to validate the configuration. To customize the
schema, you can use `marshmallow_dataclass` to specify extra metadata.

Validation errors are logged and the `ValidationError` instance is sent
to the receiver.

Signed-off-by: Leandro Lucarella <[email protected]>
If `unkown` is not specified in the `marshmallow_load_kwargs`, it will
default to `marshmallow.EXCLUDE` instead of `marshmallow.RAISE`, as this
is what most users will need when loading to a dataclass.

But when `marshmallow.EXCLUDE` is used, a warning will be logged if
there are fields in the configuration that are being excluded, so the
user can still be aware of it and catch typos in the configuration file.

Signed-off-by: Leandro Lucarella <[email protected]>
This schema is provided to use as a default, and might be extended in
the future to support more commonly used fields that are not provided
by marshmallow by default.

To use the quantity schema we need to bump the `frequenz-quantities`
dependency and add the optional `marshmallow` dependency.

Signed-off-by: Leandro Lucarella <[email protected]>
The `LoggingConfigUpdatingActor` can now also receive `None` as
configuration (for example if the configuration is removed), in which
case it will go back to the default configuration.

Signed-off-by: Leandro Lucarella <[email protected]>
We were using `str(self)` as prefix, which prints the class name, but
the logger name already has enough information to figure out this log
comes from the config managing actor, so we now use only the unique ID
(name) of the actor in the logs.

Signed-off-by: Leandro Lucarella <[email protected]>
This function has a default timeout, so waiting for the first
configuration can be automatically aborted when it takes too long.

The method will also reports some progress, logging errors if there were
issues receiving or loading the configuration, or if the configuration
is missing, setting a logging baseline for all actors.

Signed-off-by: Leandro Lucarella <[email protected]>
We now wait for the first configuration using the new `wait_for_first()`
utility function.

We also make the logging more explicity about when the old configuration
is being kept and why.

Signed-off-by: Leandro Lucarella <[email protected]>
Both the `ConfigManager` and the `ConfigManagingActor` can now take a
single configuration file, both as a `str` or a `Path`.

This also fixes a bug where `str` was still accepted, but taken as a
`Sequence[str]`. This is a known issue with Python typing:
python/mypy#11001

And while we are it, we now also explicitly reject empty sequences,
raising a `ValueError` when one is passed.

Signed-off-by: Leandro Lucarella <[email protected]>
Signed-off-by: Leandro Lucarella <[email protected]>
@llucax llucax marked this pull request as ready for review December 20, 2024 12:10
@llucax llucax requested a review from a team as a code owner December 20, 2024 12:10
@llucax llucax requested review from shsms and removed request for a team December 20, 2024 12:10
@llucax llucax enabled auto-merge December 20, 2024 12:10
@llucax
Copy link
Contributor Author

llucax commented Dec 20, 2024

OK, this is ready for review. I completed the release notes, added tests, added comprehensive documentation, including recommended practices when writing actors and apps (please review!) and a user guide, and fixed a bug in the actor (and manager) when passing a string as a config file (now both accept also a single file).

I prepared the release notes to release after this is merged.

Add module-level documentation to the `frequenz.sdk.config` module and
include it in the generated documentation's user guide.

Signed-off-by: Leandro Lucarella <[email protected]>
Signed-off-by: Leandro Lucarella <[email protected]>
Copy link
Contributor

@ela-kotulska-frequenz ela-kotulska-frequenz left a comment

Choose a reason for hiding this comment

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

LGTM :)

@llucax llucax added this pull request to the merge queue Jan 3, 2025
Merged via the queue into frequenz-floss:v1.x.x with commit d8b8260 Jan 3, 2025
18 checks passed
@llucax llucax deleted the config-manager-3 branch January 3, 2025 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:actor Affects an actor ot the actors utilities (decorator, etc.) part:config Affects the configuration management 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

Development

Successfully merging this pull request may close these issues.

2 participants