Skip to content

Commit 6f937ab

Browse files
authored
chore: reduce redundancy (#8)
1 parent 14da79a commit 6f937ab

File tree

10 files changed

+63
-346
lines changed

10 files changed

+63
-346
lines changed

src/config.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ const ENV_NO_PROGRESS: &str = "CLOUD_CLI_NO_PROGRESS";
2020

2121
impl Default for Config {
2222
fn default() -> Self {
23-
Self {
24-
jdk_path: PathBuf::from("/opt/jdk"),
25-
output_dir: PathBuf::from("/opt/selectdb/information"),
26-
timeout_seconds: 60,
27-
no_progress_animation: false,
28-
}
23+
config_loader::to_app_config(config_loader::DorisConfig::default())
2924
}
3025
}
3126

@@ -74,16 +69,6 @@ impl Config {
7469
self
7570
}
7671

77-
pub fn with_timeout(mut self, seconds: u64) -> Self {
78-
self.timeout_seconds = seconds;
79-
self
80-
}
81-
82-
pub fn with_progress_animation(mut self, enable: bool) -> Self {
83-
self.no_progress_animation = !enable;
84-
self
85-
}
86-
8772
pub fn validate(&self) -> Result<()> {
8873
self.validate_jdk_path()?;
8974
self.validate_output_dir()?;

src/config_loader/config_parser.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ impl<'a> ConfigParser for StringConfigParser<'a> {
102102
}
103103
}
104104

105-
/// Parse configuration for the specified environment
106-
pub fn parse_config(env: Environment) -> Result<DorisConfig> {
107-
let (install_dir, jdk_path) = process_detector::get_paths(env)?;
108-
parse_config_internal(env, &install_dir, &jdk_path)
109-
}
110-
111105
/// Parse configuration from specified path
112106
pub fn parse_config_from_path(env: Environment, install_dir: &Path) -> Result<DorisConfig> {
113107
let jdk_path = PathBuf::from("/opt/jdk");

src/config_loader/config_persister.rs

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,6 @@ impl ConfigConverter<ProcessInfo> for DorisConfig {
203203
}
204204
}
205205

206-
impl ConfigConverter<PersistentConfig> for DorisConfig {
207-
fn convert_to(&self) -> PersistentConfig {
208-
PersistentConfig {
209-
metadata: self.convert_to(),
210-
paths: self.convert_to(),
211-
ports: self.convert_to(),
212-
network: self.convert_to(),
213-
settings: self.convert_to(),
214-
process: self.convert_to(),
215-
}
216-
}
217-
}
218-
219206
impl ConfigConverter<DorisConfig> for PersistentConfig {
220207
fn convert_to(&self) -> DorisConfig {
221208
let environment = match self.metadata.environment.as_str() {
@@ -380,14 +367,6 @@ impl PersistResult {
380367
pub fn is_success(&self) -> bool {
381368
!matches!(self, PersistResult::AllFailed(_))
382369
}
383-
384-
pub fn success_path(&self) -> Option<&PathBuf> {
385-
match self {
386-
PersistResult::Success(path) => Some(path),
387-
PersistResult::PartialSuccess(path, _) => Some(path),
388-
PersistResult::AllFailed(_) => None,
389-
}
390-
}
391370
}
392371

393372
/// Persist configuration to file
@@ -751,53 +730,3 @@ fn to_organized_config(config: &DorisConfig) -> OrganizedConfig {
751730
process: config.convert_to(),
752731
}
753732
}
754-
755-
/// Save configuration in organized format
756-
pub fn save_organized_config(config: &DorisConfig) -> Result<PersistResult> {
757-
let config_paths = get_config_file_paths()?;
758-
let organized_config = to_organized_config(config);
759-
let toml_str = toml::to_string_pretty(&organized_config)?;
760-
761-
let mut errors = Vec::new();
762-
763-
for config_path in &config_paths {
764-
if let Err(e) = ensure_dir_exists(config_path) {
765-
errors.push((
766-
config_path.clone(),
767-
format!("Failed to create directory: {e}"),
768-
));
769-
continue;
770-
}
771-
772-
if !is_path_writable(config_path.parent().unwrap_or(config_path)) {
773-
errors.push((config_path.clone(), "No write permission".to_string()));
774-
continue;
775-
}
776-
777-
match fs::File::create(config_path) {
778-
Ok(mut file) => match file.write_all(toml_str.as_bytes()) {
779-
Ok(_) => {
780-
if errors.is_empty() {
781-
return Ok(PersistResult::Success(config_path.clone()));
782-
} else {
783-
return Ok(PersistResult::PartialSuccess(config_path.clone(), errors));
784-
}
785-
}
786-
Err(e) => {
787-
errors.push((config_path.clone(), format!("Write error: {e}")));
788-
}
789-
},
790-
Err(e) => {
791-
errors.push((config_path.clone(), format!("Create file error: {e}")));
792-
}
793-
}
794-
}
795-
796-
if !errors.is_empty() {
797-
Ok(PersistResult::AllFailed(errors))
798-
} else {
799-
Err(CliError::ConfigError(
800-
"No valid paths to persist config".to_string(),
801-
))
802-
}
803-
}

src/config_loader/mod.rs

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ impl std::fmt::Display for Environment {
2525
}
2626
}
2727

28+
/// Doris configuration model with all system settings
2829
#[derive(Debug, Clone)]
2930
pub struct DorisConfig {
3031
pub environment: Environment,
@@ -159,8 +160,16 @@ fn clean_process_info(config: &mut DorisConfig) {
159160
config.be_install_dir = None;
160161
}
161162

162-
fn update_mixed_deployment(config: &mut DorisConfig) -> Result<()> {
163+
/// Update mixed deployment detection and environment setting
164+
fn update_mixed_environment(config: &mut DorisConfig) -> Result<()> {
165+
// Detect if both FE and BE processes are running
163166
process_detector::detect_mixed_deployment(config)?;
167+
168+
// Update environment to Mixed if both FE and BE processes are detected
169+
if config.fe_process_pid.is_some() && config.be_process_pid.is_some() {
170+
config.environment = Environment::Mixed;
171+
}
172+
164173
Ok(())
165174
}
166175

@@ -170,13 +179,48 @@ fn persist_configuration(config: &DorisConfig) {
170179
}
171180
}
172181

173-
/// Update environment to Mixed if both FE and BE processes are detected
174-
fn update_environment_for_mixed_deployment(config: &mut DorisConfig) {
175-
if config.fe_process_pid.is_some() && config.be_process_pid.is_some() {
176-
config.environment = Environment::Mixed;
182+
/// Apply environment-specific port configurations
183+
fn apply_environment_specific_ports(
184+
config: &mut DorisConfig,
185+
parsed_config: &DorisConfig,
186+
env: Environment,
187+
) {
188+
match env {
189+
Environment::BE => {
190+
apply_be_ports(config, parsed_config);
191+
}
192+
Environment::FE => {
193+
apply_fe_ports(config, parsed_config);
194+
}
195+
Environment::Mixed => {
196+
// For Mixed environment, apply both BE and FE configurations
197+
apply_be_ports(config, parsed_config);
198+
apply_fe_ports(config, parsed_config);
199+
}
200+
Environment::Unknown => {
201+
// No specific ports to apply for unknown environment
202+
}
177203
}
178204
}
179205

206+
/// Apply BE-specific port configurations
207+
fn apply_be_ports(config: &mut DorisConfig, parsed_config: &DorisConfig) {
208+
config.be_port = parsed_config.be_port;
209+
config.brpc_port = parsed_config.brpc_port;
210+
config.webserver_port = parsed_config.webserver_port;
211+
config.heartbeat_service_port = parsed_config.heartbeat_service_port;
212+
}
213+
214+
/// Apply FE-specific port configurations
215+
fn apply_fe_ports(config: &mut DorisConfig, parsed_config: &DorisConfig) {
216+
config.http_port = parsed_config.http_port;
217+
config.rpc_port = parsed_config.rpc_port;
218+
config.query_port = parsed_config.query_port;
219+
config.edit_log_port = parsed_config.edit_log_port;
220+
config.cloud_http_port = parsed_config.cloud_http_port;
221+
config.meta_dir = parsed_config.meta_dir.clone();
222+
}
223+
180224
/// Load configuration, first from persisted file, then detect environment and generate if needed
181225
pub fn load_config() -> Result<DorisConfig> {
182226
let mut config = config_persister::load_persisted_config().unwrap_or_default();
@@ -185,14 +229,10 @@ pub fn load_config() -> Result<DorisConfig> {
185229
Ok(current_process) => {
186230
if needs_config_update(&config, &current_process) {
187231
config = update_config_from_process(config, current_process)?;
188-
189-
let _ = update_mixed_deployment(&mut config);
190-
update_environment_for_mixed_deployment(&mut config);
191-
232+
let _ = update_mixed_environment(&mut config);
192233
persist_configuration(&config);
193234
} else {
194-
let _ = update_mixed_deployment(&mut config);
195-
update_environment_for_mixed_deployment(&mut config);
235+
let _ = update_mixed_environment(&mut config);
196236
}
197237
}
198238
Err(_) => {
@@ -215,7 +255,7 @@ fn parse_env_specific_config(env: Environment) -> DorisConfig {
215255
let result = match env {
216256
Environment::BE => config_parser::parse_be_config(),
217257
Environment::FE => config_parser::parse_fe_config(),
218-
Environment::Mixed => config_parser::parse_be_config(), // For Mixed, we'll start with BE config and add FE later
258+
Environment::Mixed => config_parser::parse_be_config(),
219259
Environment::Unknown => return DorisConfig::default(),
220260
};
221261
result.unwrap_or_else(|_| DorisConfig::default())
@@ -235,8 +275,7 @@ fn fallback_load_config() -> Result<DorisConfig> {
235275

236276
// If we detect both FE and BE processes, update to Mixed environment
237277
if env != Environment::Unknown {
238-
let _ = update_mixed_deployment(&mut config);
239-
update_environment_for_mixed_deployment(&mut config);
278+
let _ = update_mixed_environment(&mut config);
240279
}
241280

242281
persist_configuration(&config);
@@ -253,11 +292,6 @@ pub fn to_app_config(doris_config: DorisConfig) -> crate::config::Config {
253292
}
254293
}
255294

256-
/// Get the current Doris configuration
257-
pub fn get_current_config() -> Result<DorisConfig> {
258-
load_config()
259-
}
260-
261295
/// Get current process PID from configuration (convenience function)
262296
pub fn get_current_pid() -> Option<u32> {
263297
load_config().ok()?.get_valid_pid()
@@ -299,38 +333,8 @@ fn update_config_from_process(
299333
if let Ok(parsed_config) =
300334
config_parser::parse_config_from_path(process.environment, &process.doris_home)
301335
{
302-
match process.environment {
303-
Environment::BE => {
304-
config.be_port = parsed_config.be_port;
305-
config.brpc_port = parsed_config.brpc_port;
306-
config.webserver_port = parsed_config.webserver_port;
307-
config.heartbeat_service_port = parsed_config.heartbeat_service_port;
308-
}
309-
Environment::FE => {
310-
config.http_port = parsed_config.http_port;
311-
config.rpc_port = parsed_config.rpc_port;
312-
config.query_port = parsed_config.query_port;
313-
config.edit_log_port = parsed_config.edit_log_port;
314-
config.cloud_http_port = parsed_config.cloud_http_port;
315-
config.meta_dir = parsed_config.meta_dir;
316-
}
317-
Environment::Mixed => {
318-
// For Mixed environment, we'll update both BE and FE ports
319-
config.be_port = parsed_config.be_port;
320-
config.brpc_port = parsed_config.brpc_port;
321-
config.webserver_port = parsed_config.webserver_port;
322-
config.heartbeat_service_port = parsed_config.heartbeat_service_port;
323-
config.http_port = parsed_config.http_port;
324-
config.rpc_port = parsed_config.rpc_port;
325-
config.query_port = parsed_config.query_port;
326-
config.edit_log_port = parsed_config.edit_log_port;
327-
config.cloud_http_port = parsed_config.cloud_http_port;
328-
config.meta_dir = parsed_config.meta_dir;
329-
}
330-
Environment::Unknown => {
331-
// This shouldn't happen as detect_current_process only returns FE/BE
332-
}
333-
}
336+
// Apply port configurations based on environment
337+
apply_environment_specific_ports(&mut config, &parsed_config, process.environment);
334338
}
335339

336340
Ok(config)

src/config_loader/process_detector.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,6 @@ fn get_paths_by_pid(pid: u32) -> (PathBuf, PathBuf) {
207207
(PathBuf::from("/opt/selectdb"), PathBuf::from("/opt/jdk"))
208208
}
209209

210-
/// Check if a process with given PID is still running
211-
pub fn is_process_alive(pid: u32) -> bool {
212-
Command::new("kill")
213-
.args(["-0", &pid.to_string()])
214-
.output()
215-
.map(|output| output.status.success())
216-
.unwrap_or(false)
217-
}
218-
219210
/// Verify that a config file exists
220211
pub fn verify_config_file(path: &Path) -> Result<()> {
221212
if !path.exists() {
@@ -286,31 +277,3 @@ pub fn detect_mixed_deployment(config: &mut crate::config_loader::DorisConfig) -
286277

287278
Ok(is_mixed)
288279
}
289-
290-
/// Get config path for the specified environment
291-
pub fn get_config_path(env: Environment) -> Result<PathBuf> {
292-
let (install_path, _) = get_paths(env)?;
293-
294-
let config_file = match env {
295-
Environment::BE => "be.conf",
296-
Environment::FE => "fe.conf",
297-
Environment::Mixed => {
298-
return Err(CliError::ConfigError(
299-
"Cannot get config path for Mixed environment".to_string(),
300-
));
301-
}
302-
_ => return Err(CliError::ConfigError("Invalid environment".to_string())),
303-
};
304-
305-
Ok(install_path.join("conf").join(config_file))
306-
}
307-
308-
/// Get BE config path
309-
pub fn get_be_config_path() -> Result<PathBuf> {
310-
get_config_path(Environment::BE)
311-
}
312-
313-
/// Get FE config path
314-
pub fn get_fe_config_path() -> Result<PathBuf> {
315-
get_config_path(Environment::FE)
316-
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ fn handle_tool_execution_error(config: &Config, error: &error::CliError) -> Resu
218218

219219
/// Persist updated configuration to disk
220220
fn persist_updated_config(config: &Config) -> Result<()> {
221-
let mut doris_config = config_loader::get_current_config()?;
221+
let mut doris_config = config_loader::load_config()?;
222222
doris_config = doris_config.with_app_config(config);
223223
match config_loader::config_persister::persist_config(&doris_config) {
224224
Ok(_) => Ok(()),

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@ fn main() -> Result<()> {
55
ui::print_header();
66
let doris_config = config_loader::load_config()?;
77
let config = config_loader::to_app_config(doris_config);
8+
9+
if let Err(e) = config.validate() {
10+
ui::print_error(&format!("Config warning: {e}"));
11+
}
12+
813
run_cli(config)
914
}

0 commit comments

Comments
 (0)