11use crate :: config_loader;
22use crate :: error:: { CliError , Result } ;
33use crate :: executor;
4+ use crate :: tools:: { be, mysql} ;
45use crate :: ui;
6+ use std:: collections:: BTreeSet ;
57use std:: process:: Command ;
68
79const BE_DEFAULT_IP : & str = "127.0.0.1" ;
810
911/// Send an HTTP GET request to a BE API endpoint
1012pub fn request_be_webserver_port ( endpoint : & str , filter_pattern : Option < & str > ) -> Result < String > {
11- let be_http_ports = get_be_http_ports ( ) ? ;
13+ let mut be_targets : BTreeSet < ( String , u16 ) > = BTreeSet :: new ( ) ;
1214
13- for & port in & be_http_ports {
14- let url = format ! ( "http://{BE_DEFAULT_IP}:{port}{endpoint}" ) ;
15+ let ports = get_be_http_ports ( ) ?;
16+
17+ let selected_host = be:: list:: get_selected_be_host ( ) ;
18+
19+ let cluster_hosts = get_be_ip ( ) . unwrap_or_default ( ) ;
20+
21+ let mut all_hosts = BTreeSet :: new ( ) ;
22+ if let Some ( host) = & selected_host {
23+ all_hosts. insert ( host. clone ( ) ) ;
24+ }
25+ for host in cluster_hosts {
26+ all_hosts. insert ( host) ;
27+ }
28+
29+ if all_hosts. is_empty ( ) {
30+ all_hosts. insert ( BE_DEFAULT_IP . to_string ( ) ) ;
31+ }
32+
33+ for host in all_hosts {
34+ be_targets. extend ( ports. iter ( ) . map ( |p| ( host. clone ( ) , * p) ) ) ;
35+ }
36+
37+ for ( host, port) in & be_targets {
38+ let url = format ! ( "http://{host}:{port}{endpoint}" ) ;
1539 let mut curl_cmd = Command :: new ( "curl" ) ;
1640 curl_cmd. args ( [ "-sS" , & url] ) ;
1741
@@ -31,27 +55,52 @@ pub fn request_be_webserver_port(endpoint: &str, filter_pattern: Option<&str>) -
3155 }
3256 }
3357
34- let ports_str = be_http_ports
58+ let ports_str = be_targets
3559 . iter ( )
36- . map ( |p| p . to_string ( ) )
60+ . map ( |( h , p ) | format ! ( "{h}:{p}" ) )
3761 . collect :: < Vec < _ > > ( )
3862 . join ( ", " ) ;
3963
64+ ui:: print_warning (
65+ "Could not connect to any BE http endpoint. You can select a host via 'be-list'." ,
66+ ) ;
4067 Err ( CliError :: ToolExecutionFailed ( format ! (
4168 "Could not connect to any BE http port ({ports_str}). Check if BE is running."
4269 ) ) )
4370}
4471
4572/// Get BE HTTP ports from configuration or use defaults
4673pub fn get_be_http_ports ( ) -> Result < Vec < u16 > > {
47- match config_loader:: load_config ( ) {
48- Ok ( doris_config) => Ok ( doris_config. get_be_http_ports ( ) ) ,
49- Err ( _) => {
50- // Fallback to default ports if configuration cannot be loaded
51- ui:: print_warning (
52- "Could not load configuration, using default BE HTTP ports (8040, 8041)" ,
53- ) ;
54- Ok ( vec ! [ 8040 , 8041 ] )
74+ if let Ok ( doris_config) = config_loader:: load_config ( ) {
75+ let config_ports = doris_config. get_be_http_ports ( ) ;
76+ if !config_ports. is_empty ( ) && config_ports != vec ! [ 8040 , 8041 ] {
77+ return Ok ( config_ports) ;
5578 }
5679 }
80+
81+ if let Ok ( info) = mysql:: ClusterInfo :: load_from_file ( ) {
82+ let be_ports: Vec < u16 > = info
83+ . backends
84+ . iter ( )
85+ . filter ( |b| b. alive )
86+ . map ( |b| b. http_port )
87+ . collect ( ) ;
88+
89+ if !be_ports. is_empty ( ) {
90+ return Ok ( be_ports) ;
91+ }
92+ }
93+
94+ Ok ( vec ! [ 8040 , 8041 ] )
95+ }
96+
97+ pub fn get_be_ip ( ) -> Result < Vec < String > > {
98+ if let Ok ( info) = mysql:: ClusterInfo :: load_from_file ( ) {
99+ let hosts = info. list_be_hosts ( ) ;
100+ if !hosts. is_empty ( ) {
101+ return Ok ( hosts) ;
102+ }
103+ }
104+
105+ Ok ( vec ! [ BE_DEFAULT_IP . to_string( ) ] )
57106}
0 commit comments