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

feat: override config with cli opts #7613

Merged
merged 16 commits into from
Feb 1, 2023
Merged
57 changes: 51 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/cmd_all/src/playground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use std::sync::LazyLock;

use anyhow::Result;
use clap::StructOpt;
use risingwave_common::config::load_config;
use tempfile::TempPath;
use tokio::signal;

Expand Down Expand Up @@ -154,9 +153,6 @@ pub async fn playground() -> Result<()> {
opts.insert(0, "meta-node".into());
tracing::info!("starting meta-node thread with cli args: {:?}", opts);
let opts = risingwave_meta::MetaNodeOpts::parse_from(opts);

let _config = load_config(&opts.config_path);

tracing::info!("opts: {:#?}", opts);
let _meta_handle = tokio::spawn(async move {
risingwave_meta::start(opts).await;
Expand Down
1 change: 1 addition & 0 deletions src/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ byteorder = "1"
bytes = "1"
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
chrono-tz = { version = "0.7", features = ["case-insensitive"] }
clap = { version = "3", features = ["derive"] }
comfy-table = "6"
crc32fast = "1"
derivative = "2"
Expand Down
21 changes: 21 additions & 0 deletions src/common/proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "risingwave_common_proc_macro"
version = { workspace = true }
edition = { workspace = true }
homepage = { workspace = true }
keywords = { workspace = true }
license = { workspace = true }
repository = { workspace = true }

[lib]
proc-macro = true

[dependencies]
proc-macro-error = "1.0"
quote = "1"
proc-macro2 = { version = "1", default-features = false }
syn = "1"
bae = "0.1.7"

[target.'cfg(not(madsim))'.dependencies]
workspace-hack = { path = "../../workspace-hack" }
78 changes: 78 additions & 0 deletions src/common/proc_macro/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2023 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Copyright 2023 Singularity Data
Copy link
Member

Choose a reason for hiding this comment

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

May remove duplicated headers?

Copy link
Member

Choose a reason for hiding this comment

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

Same for proc_macro/src/lib.rs 😂

//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use bae::FromAttributes;
use proc_macro2::TokenStream;
use proc_macro_error::ResultExt;
use quote::quote;
use syn::DeriveInput;

#[derive(FromAttributes)]
pub struct OverrideOpts {
pub path: Option<syn::Expr>,
pub optional_in_config: Option<()>,
}

#[cfg_attr(coverage, no_coverage)]
pub fn produce_override_config(input: DeriveInput) -> TokenStream {
let struct_ident = input.ident;
let mut override_stmts = Vec::new();

if let syn::Data::Struct(syn::DataStruct { fields, .. }) = input.data {
for field in fields {
let override_opts = OverrideOpts::from_attributes(&field.attrs)
.expect_or_abort("Failed to parse `override_opts` attribute");
let path = override_opts.path.expect("`path` must exist");
let field_ident = field.ident;

let override_stmt = if override_opts.optional_in_config.is_some() {
quote! {
if self.#field_ident.is_some() {
config.#path = self.#field_ident;
}
}
} else {
quote! {
if let Some(v) = self.#field_ident {
config.#path = v;
}
}
};

override_stmts.push(override_stmt);
}
}

quote! {
impl risingwave_common::config::OverrideConfig for #struct_ident {
fn r#override(self, config: &mut risingwave_common::config::RwConfig) {
#(#override_stmts)*
}
}
}
}
71 changes: 71 additions & 0 deletions src/common/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2023 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Copyright 2023 Singularity Data
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(coverage, feature(no_coverage))]

use proc_macro_error::proc_macro_error;
use syn::parse_macro_input;

mod config;

/// Sections in the configuration file can use `#[derive(OverrideConfig)]` to generate the
/// implementation of overwriting configs from the file.
///
/// In the struct definition, use #[override_opts(path = ...)] on a field to indicate the field in
/// `RwConfig` to override.
///
/// An example:
///
/// ```ignore
/// #[derive(OverrideConfig)]
/// struct Opts {
/// #[override_opts(path = meta.listen_addr)]
/// listen_addr: Option<String>,
/// }
/// ```
///
/// will generate
///
/// impl OverrideConfig for Opts {
/// fn r#override(self, config: &mut RwConfig) {
/// if let Some(v) = self.required_str {
/// config.meta.listen_addr = v;
/// }
/// }
/// }
/// ```
#[cfg_attr(coverage, no_coverage)]
#[proc_macro_derive(OverrideConfig, attributes(override_opts))]
#[proc_macro_error]
pub fn override_config(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input);

let gen = config::produce_override_config(input);

gen.into()
}
Loading