From d67f2dabd6d2e682423bc799cd877d4d90d5753f Mon Sep 17 00:00:00 2001 From: taohong Date: Wed, 22 Feb 2023 13:52:51 +0800 Subject: [PATCH 1/2] service: improve the resuability of create_daemon In order to improve the reusability of the function create_daemon the parameter processing logic is moved from the create_daemon and initialize_fscache_service functions to the upper layer function process_singleton_arguments Signed-off-by: taohong --- service/src/singleton.rs | 38 ++++++++++++++------------------------ src/bin/nydusd/main.rs | 26 +++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/service/src/singleton.rs b/service/src/singleton.rs index d98efd60314..69a4d44fb06 100644 --- a/service/src/singleton.rs +++ b/service/src/singleton.rs @@ -19,7 +19,7 @@ use crate::daemon::{ NydusDaemon, }; use crate::fs_service::FsService; -use crate::{Error, Result, ServiceArgs}; +use crate::{Error, Result}; #[allow(dead_code)] struct ServiceController { @@ -98,9 +98,10 @@ impl ServiceController { #[cfg(target_os = "linux")] impl ServiceController { - fn initialize_fscache_service( + fn initialize_fscache_service( &self, - args: &T, + tag: Option<&str>, + threads: Option<&str>, path: &str, ) -> std::io::Result<()> { // Validate --fscache option value is an existing directory. @@ -124,9 +125,8 @@ impl ServiceController { return Err(einval!("--fscache option contains invalid characters")); } }; - let tag = args.value_of("fscache-tag").map(|s| s.as_str()); - let threads = if let Some(threads_value) = args.value_of("fscache-threads") { + let threads = if let Some(threads_value) = threads { crate::validate_threads_configuration(threads_value).map_err(|err| einval!(err))? } else { 1usize @@ -237,26 +237,16 @@ impl DaemonStateMachineSubscriber for ServiceController { } /// Create and start a Nydus daemon to host fscache and fusedev services. -/// -/// The `args` argument is derived from commandline options and controls services enabled. -/// The fscache service will be enabled if `args` contains the `--fscache` argument. -pub fn create_daemon( - args: &T, +pub fn create_daemon( + id: Option, + supervisor: Option, + fscache: Option<&str>, + tag: Option<&str>, + threads: Option<&str>, + config: Option, bti: BuildTimeInfo, waker: Arc, ) -> std::io::Result> { - let id = args.value_of("id").map(|id| id.to_string()); - let supervisor = args.value_of("supervisor").map(|s| s.to_string()); - let config = match args.value_of("config") { - None => None, - Some(path) => { - let config = std::fs::read_to_string(path)?; - let config: serde_json::Value = serde_json::from_str(&config) - .map_err(|_e| einval!("invalid configuration file"))?; - Some(config) - } - }; - let (to_sm, from_client) = channel::(); let (to_client, from_sm) = channel::>(); let service_controller = ServiceController { @@ -277,8 +267,8 @@ pub fn create_daemon( service_controller.initialize_blob_cache(&config)?; #[cfg(target_os = "linux")] - if let Some(path) = args.value_of("fscache") { - service_controller.initialize_fscache_service(args, path)?; + if let Some(path) = fscache { + service_controller.initialize_fscache_service(tag, threads, path)?; } let daemon = Arc::new(service_controller); diff --git a/src/bin/nydusd/main.rs b/src/bin/nydusd/main.rs index 8032e719818..6a8eeab6988 100644 --- a/src/bin/nydusd/main.rs +++ b/src/bin/nydusd/main.rs @@ -553,8 +553,32 @@ fn process_singleton_arguments( _apisock: Option<&str>, bti: BuildTimeInfo, ) -> Result<()> { + let id = subargs.value_of("id").map(|id| id.to_string()); + let supervisor = subargs.value_of("supervisor").map(|s| s.to_string()); + let config = match subargs.value_of("config") { + None => None, + Some(path) => { + let config = std::fs::read_to_string(path)?; + let config: serde_json::Value = serde_json::from_str(&config) + .map_err(|_e| einval!("invalid configuration file"))?; + Some(config) + } + }; + let fscache = subargs.value_of("fscache").map(|s| s.as_str()); + let tag = subargs.value_of("fscache-tag").map(|s| s.as_str()); + let threads = subargs.value_of("fscache-threads").map(|s| s.as_str()); info!("Start Nydus daemon in singleton mode!"); - let daemon = create_daemon(subargs, bti, DAEMON_CONTROLLER.alloc_waker()).map_err(|e| { + let daemon = create_daemon( + id, + supervisor, + fscache, + tag, + threads, + config, + bti, + DAEMON_CONTROLLER.alloc_waker(), + ) + .map_err(|e| { error!("Failed to start singleton daemon: {}", e); e })?; From 48a49587e53d7c9c5685516b35cddefe68e46c73 Mon Sep 17 00:00:00 2001 From: taohong Date: Wed, 22 Feb 2023 14:25:14 +0800 Subject: [PATCH 2/2] fix: function create_daemon has too many arguments Signed-off-by: taohong --- service/src/singleton.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/service/src/singleton.rs b/service/src/singleton.rs index 69a4d44fb06..7feb90c34ed 100644 --- a/service/src/singleton.rs +++ b/service/src/singleton.rs @@ -237,6 +237,7 @@ impl DaemonStateMachineSubscriber for ServiceController { } /// Create and start a Nydus daemon to host fscache and fusedev services. +#[allow(clippy::too_many_arguments)] pub fn create_daemon( id: Option, supervisor: Option,