1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15+ use std:: net:: SocketAddr ;
1516use std:: ops:: RangeInclusive ;
17+ use std:: str:: FromStr ;
1618use std:: sync:: atomic:: AtomicBool ;
1719use std:: sync:: atomic:: Ordering ;
1820use std:: sync:: Arc ;
@@ -35,7 +37,7 @@ use common_exception::Result;
3537use common_grpc:: ConnectionFactory ;
3638use common_management:: ClusterApi ;
3739use common_management:: ClusterMgr ;
38- use common_meta_api :: KVApi ;
40+ use common_meta_store :: MetaStore ;
3941use common_meta_store:: MetaStoreProvider ;
4042use common_meta_types:: NodeInfo ;
4143use common_metrics:: label_counter_with_val_and_labels;
@@ -139,24 +141,24 @@ static CLUSTER_DISCOVERY: OnceCell<Singleton<Arc<ClusterDiscovery>>> = OnceCell:
139141impl ClusterDiscovery {
140142 const METRIC_LABEL_FUNCTION : & ' static str = "function" ;
141143
142- pub async fn create_meta_client ( cfg : & Config ) -> Result < Arc < dyn KVApi > > {
144+ pub async fn create_meta_client ( cfg : & Config ) -> Result < MetaStore > {
143145 let meta_api_provider = MetaStoreProvider :: new ( cfg. meta . to_meta_grpc_client_conf ( ) ) ;
144146 match meta_api_provider. try_get_meta_store ( ) . await {
145- Ok ( client ) => Ok ( client . arc ( ) ) ,
147+ Ok ( meta_store ) => Ok ( meta_store ) ,
146148 Err ( cause) => Err ( cause. add_message_back ( "(while create cluster api)." ) ) ,
147149 }
148150 }
149151
150152 pub async fn init ( cfg : Config , v : Singleton < Arc < ClusterDiscovery > > ) -> Result < ( ) > {
151- let meta_client = ClusterDiscovery :: create_meta_client ( & cfg) . await ?;
152- v. init ( Self :: try_create ( & cfg, meta_client ) . await ?) ?;
153+ let metastore = ClusterDiscovery :: create_meta_client ( & cfg) . await ?;
154+ v. init ( Self :: try_create ( & cfg, metastore ) . await ?) ?;
153155
154156 CLUSTER_DISCOVERY . set ( v) . ok ( ) ;
155157 Ok ( ( ) )
156158 }
157159
158- pub async fn try_create ( cfg : & Config , api : Arc < dyn KVApi > ) -> Result < Arc < ClusterDiscovery > > {
159- let ( lift_time, provider) = Self :: create_provider ( cfg, api ) ?;
160+ pub async fn try_create ( cfg : & Config , metastore : MetaStore ) -> Result < Arc < ClusterDiscovery > > {
161+ let ( lift_time, provider) = Self :: create_provider ( cfg, metastore ) ?;
160162
161163 Ok ( Arc :: new ( ClusterDiscovery {
162164 local_id : GlobalUniqName :: unique ( ) ,
@@ -182,13 +184,13 @@ impl ClusterDiscovery {
182184
183185 fn create_provider (
184186 cfg : & Config ,
185- api : Arc < dyn KVApi > ,
187+ metastore : MetaStore ,
186188 ) -> Result < ( Duration , Arc < dyn ClusterApi > ) > {
187189 // TODO: generate if tenant or cluster id is empty
188190 let tenant_id = & cfg. query . tenant_id ;
189191 let cluster_id = & cfg. query . cluster_id ;
190192 let lift_time = Duration :: from_secs ( 60 ) ;
191- let cluster_manager = ClusterMgr :: create ( api , tenant_id, cluster_id, lift_time) ?;
193+ let cluster_manager = ClusterMgr :: create ( metastore , tenant_id, cluster_id, lift_time) ?;
192194
193195 Ok ( ( lift_time, Arc :: new ( cluster_manager) ) )
194196 }
@@ -350,8 +352,27 @@ impl ClusterDiscovery {
350352
351353 pub async fn register_to_metastore ( self : & Arc < Self > , cfg : & Config ) -> Result < ( ) > {
352354 let cpus = cfg. query . num_cpus ;
353- // TODO: 127.0.0.1 || ::0
354- let address = cfg. query . flight_api_address . clone ( ) ;
355+ let mut address = cfg. query . flight_api_address . clone ( ) ;
356+
357+ if let Ok ( socket_addr) = SocketAddr :: from_str ( & address) {
358+ let ip_addr = socket_addr. ip ( ) ;
359+ if ip_addr. is_loopback ( ) || ip_addr. is_unspecified ( ) {
360+ if let Some ( local_addr) = self . api_provider . get_local_addr ( ) . await ? {
361+ let local_socket_addr = SocketAddr :: from_str ( & local_addr) ?;
362+ let new_addr = format ! ( "{}:{}" , local_socket_addr. ip( ) , socket_addr. port( ) ) ;
363+ tracing:: warn!(
364+ "Used loopback or unspecified address as cluster flight address. \
365+ we rewrite it(\" {}\" -> \" {}\" ) for other nodes can connect it.\
366+ If your has proxy between nodes, you can specify the node's IP address in the configuration file.",
367+ address,
368+ new_addr
369+ ) ;
370+
371+ address = new_addr;
372+ }
373+ }
374+ }
375+
355376 let node_info = NodeInfo :: create ( self . local_id . clone ( ) , cpus, address) ;
356377
357378 self . drop_invalid_nodes ( & node_info) . await ?;
0 commit comments