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 supervisor config to global singleton #1246

Merged
merged 1 commit into from
Sep 17, 2016
Merged

Conversation

adamhjk
Copy link
Contributor

@adamhjk adamhjk commented Sep 16, 2016

This commit converts to using a global singleton for configuration,
rather than threading a reference to everything that needs configuration
data.

Since we need to populate our Config struct with data from clap, we
need to be able to modify it at runtime with an argument, rather than
simply use lazy_static!. This implementation stores the configuration
as a static pointer to a location on the heap via Box, and updates
the pointer via mem::transmute in a gcache() method. This method
is called early in main - right after we populate the configuration
struct, in fact.

Once gcache() has been called (one time only; subsequent calls will be
ignored), you can access the Config struct through the
config::gconfig() method.

This removes all the references to threading an &Config through the
code base, and replaces them with calls to gconfig().

In particular, it cleans up the need for lifetimes in the topology and
worker code, which is a nice side effect.

gif-keyboard-11431932797035670168

Signed-off-by: Adam Jacob [email protected]

@thesentinels
Copy link
Contributor

@adamhjk, thanks for your PR! By analyzing the annotation information on this pull request, we identified @fnichol, @metadave, @reset, @smith, @juliandunn and @adamhjk to be potential reviewers

Copy link
Collaborator

@fnichol fnichol left a comment

Choose a reason for hiding this comment

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

Looks like there are still a couple of CI failures (in test suites?), otherwise I'm liking this version of the code. Despite the raw pointer craziness, its calling API is really straightforward.

/// The package we are supervising
pub package: Arc<RwLock<Package>>,
/// Name of the package being supervised
pub package_name: String,
/// A pointer to our current Config
pub config: &'a Config,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Bingo. That's a rather nice simplification.

/// Store a configuration, for later use through `gconfig()`.
///
/// MUST BE CALLED BEFORE ANY CALLS TO `gconfig()`.
pub fn gcache(config: Config) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I presume that if we ever had to update the Config, we'd call this again with a new copy (I noticed that .gconfig() doesn't return a mut borrow)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right now, the Config struct is immutable after it is created. It's passed around as a reference, without mutability.

If we decide we want it to be mutable, we will need to refactor into having the static hold a RwLock<Config> instead. Given that right now, for the supervisor at least, these are only options populated from the CLI, we're fine to let it remain immutable.

@adamhjk
Copy link
Contributor Author

adamhjk commented Sep 16, 2016

Fixed the test suite - this should pass now. @fnichol, you have to approve my changes for merge. :)

@fnichol
Copy link
Collaborator

fnichol commented Sep 16, 2016

Haha, I hold all the keys!

This commit converts to using a global singleton for configuration,
rather than threading a reference to everything that needs configuration
data.

Since we need to populate our `Config` struct with data from `clap`, we
need to be able to modify it at runtime with an argument, rather than
simply use `lazy_static!`. This implementation stores the configuration
as a static pointer to a location on the heap via `Box`, and updates
the pointer via `mem::transmute` in a `gcache()` method. This method
is called early in `main` - right after we populate the configuration
struct, in fact.

Once `gcache()` has been called (one time only; subsequent calls will be
ignored), you can access the `Config` struct through the
`config::gconfig()` method.

This removes all the references to threading an `&Config` through the
code base, and replaces them with calls to `gconfig()`.

In particular, it cleans up the need for lifetimes in the topology and
worker code, which is a nice side effect.

Signed-off-by: Adam Jacob <[email protected]>
@adamhjk adamhjk merged commit 7f108f5 into master Sep 17, 2016
@adamhjk adamhjk deleted the static-config branch September 17, 2016 00:06
@eeyun eeyun added C-chore and removed Chore labels Jun 6, 2017
@christophermaier christophermaier added Type: Chore Issues for general code and infrastructure maintenance and removed C-chore labels Jul 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Chore Issues for general code and infrastructure maintenance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants