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

Load swbusd configuration from config_db #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yue-fred-gao
Copy link
Contributor

why

We need to derive swbusd configuration from dpu table in config_db.

What this PR does

  1. Add slot number to swbusd command arguments. If this is set, it will read configuration from DPU table in config_db.
    Otherwise, it will read configuration from the specified yaml file and bind to the specified address. The second option
    is for development where we don't have full sonic environment.
  2. Add start_config_db to Redis in swss-common-testing crate. The existing start() starts redis without specifying the unix socket in sonic config. Some swss-common components like Logger, use the unix socket attribute in sonic-config to connect to redis. If we add the unix socket also for start(), we won't be able to start multiple redis instances, which happens when we run tests in swss-common. So start_config_db is introduced where it needs to have unix socket in sonic-config.

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

.status()
.unwrap();
self.proc.wait().unwrap();
// Command::new("kill")
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this some pending work item todo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ran some test but forgot to turn it back on.

struct ConfigDBDPUEntry {
dpu_type: DpuType,
state: Option<String>,
slot_id: u8,
Copy link
Collaborator

Choose a reason for hiding this comment

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

no need to restrict here, we can simply use u32 or i32

const CONFIG_DB: &str = "CONFIG_DB";
const SWBUSD_PORT: u16 = 51000;
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct SwbusdConfig {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this file is not just route config, maybe split the file will be better.

let file = File::open(yaml_file)?;
let reader = BufReader::new(file);

// Parse the YAML data
let routes_config: RoutesConfig = serde_yaml::from_reader(reader)?;
// let routes_config = RoutesConfig::from_routes_config_serde(&route_config_serde);
let routes_config: SwbusdConfig = serde_yaml::from_reader(reader)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

rename variable

Ok(routes_config)
}

pub fn load_from_configdb(slot_id: u8) -> Result<SwbusdConfig, Box<dyn std::error::Error>> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

will be great to add instrument

use swss_common::{DbConnector, Table};
use swss_serde::from_table;
use tracing::*;
const CONFIG_DB: &str = "CONFIG_DB";
Copy link
Collaborator

Choose a reason for hiding this comment

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

will be better to move all the db loading logic into swbusd instead of core. in this way, it will be much easier for testing, because there is no swss and redis dependency at all.

#[serde(rename_all = "lowercase")]
enum DpuType {
Local,
Cluster,
Copy link
Collaborator

Choose a reason for hiding this comment

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

will be better to add some comments here

}
impl RouteConfig {
#[instrument]
pub(crate) fn from_dpu_table(
Copy link
Collaborator

Choose a reason for hiding this comment

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

from_dpu_table_entry

impl RouteConfig {
#[instrument]
pub(crate) fn from_dpu_table(
dpu_table: &ConfigDBDPUEntry,
Copy link
Collaborator

Choose a reason for hiding this comment

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

dpu_entry or something similar might be better

dpu_table: &ConfigDBDPUEntry,
region: &str,
cluster: &str,
) -> Result<Vec<RouteConfig>, String> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

will be better to actually use a real error.

The practice in rust is:

  1. the lib crate will have its own error defined
  2. outside lib, in exe, we can use anyhow or something similar.

if we can move these db loading code out into swbusd, then we can simply use anyhow, otherwise, it will be better to use thiserror crate and having explicit error defined.

}

impl SwbusdConfig {
pub fn load_from_yaml(yaml_file: String) -> Result<SwbusdConfig, Box<dyn std::error::Error>> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Box dyn error is not a good practice. either we can use thiserror to define it, or move these code into the swbusd and use anyhow

Ok(routes_config)
}

pub fn load_from_configdb(slot_id: u8) -> Result<SwbusdConfig, Box<dyn std::error::Error>> {
let db = DbConnector::new_named(CONFIG_DB, false, 0)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

since there are multiple config db load here, it will be better to split this function into multiple ones. the code will be more readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants