1414
1515use std:: str;
1616use std:: sync:: Arc ;
17- use std:: sync:: Mutex ;
1817
19- use bytes:: Bytes ;
20- use common_auth:: RefreshableToken ;
2118use common_catalog:: table:: Table ;
22- use common_config:: GlobalConfig ;
2319use common_exception:: ErrorCode ;
2420use common_exception:: Result ;
2521use common_meta_api:: SchemaApi ;
26- use common_meta_api:: ShareApi ;
2722use common_meta_app:: schema:: CreateTableReq ;
2823use common_meta_app:: schema:: DatabaseInfo ;
2924use common_meta_app:: schema:: DropTableByIdReq ;
@@ -43,89 +38,23 @@ use common_meta_app::schema::UpsertTableCopiedFileReply;
4338use common_meta_app:: schema:: UpsertTableCopiedFileReq ;
4439use common_meta_app:: schema:: UpsertTableOptionReply ;
4540use common_meta_app:: schema:: UpsertTableOptionReq ;
46- use common_meta_app:: share:: GetShareEndpointReq ;
47- use common_meta_app:: share:: TableInfoMap ;
48- use common_users:: UserApiProvider ;
49- use http:: header:: AUTHORIZATION ;
50- use http:: header:: CONTENT_LENGTH ;
51- use http:: Method ;
52- use http:: Request ;
53- use opendal:: raw:: AsyncBody ;
54- use opendal:: raw:: HttpClient ;
55- use tracing:: error;
56- use tracing:: info;
41+ use common_sharing:: ShareEndpointManager ;
5742
5843use crate :: databases:: Database ;
5944use crate :: databases:: DatabaseContext ;
6045
61- const TENANT_HEADER : & str = "X-DATABEND-TENANT" ;
62-
63- #[ derive( Clone , Debug ) ]
64- struct EndpointConfig {
65- pub url : String ,
66-
67- pub token : RefreshableToken ,
68- }
69-
7046// Share Database implementation for `Database` trait.
7147#[ derive( Clone ) ]
7248pub struct ShareDatabase {
7349 ctx : DatabaseContext ,
7450
7551 db_info : DatabaseInfo ,
76-
77- client : HttpClient ,
78-
79- endpoint_config : Arc < Mutex < Option < EndpointConfig > > > ,
8052}
8153
8254impl ShareDatabase {
8355 pub const NAME : & ' static str = "SHARE" ;
8456 pub fn try_create ( ctx : DatabaseContext , db_info : DatabaseInfo ) -> Result < Box < dyn Database > > {
85- Ok ( Box :: new ( Self {
86- ctx,
87- db_info,
88- client : HttpClient :: new ( ) ?,
89- endpoint_config : Arc :: new ( Mutex :: new ( None ) ) ,
90- } ) )
91- }
92-
93- async fn get_share_endpoint ( & self ) -> Result < EndpointConfig > {
94- let endpoint_config = {
95- let endpoint_config = self . endpoint_config . lock ( ) . unwrap ( ) ;
96- endpoint_config. clone ( )
97- } ;
98-
99- match endpoint_config {
100- Some ( ref endpoint_config) => Ok ( endpoint_config. clone ( ) ) ,
101- None => {
102- let share_name = self . db_info . meta . from_share . clone ( ) . unwrap ( ) ;
103- let req = GetShareEndpointReq {
104- tenant : self . db_info . name_ident . tenant . clone ( ) ,
105- endpoint : None ,
106- to_tenant : Some ( share_name. tenant . clone ( ) ) ,
107- } ;
108- let meta_api = UserApiProvider :: instance ( ) . get_meta_store_client ( ) ;
109- let resp = meta_api. get_share_endpoint ( req) . await ?;
110- if let Some ( ( _, endpoint_meta) ) = resp. share_endpoint_meta_vec . into_iter ( ) . next ( ) {
111- let endpoint = format ! (
112- "{}tenant/{}/{}/meta" ,
113- endpoint_meta. url, share_name. tenant, share_name. share_name,
114- ) ;
115- let config = EndpointConfig {
116- url : endpoint,
117- token : RefreshableToken :: Direct ( self . db_info . name_ident . tenant . clone ( ) ) ,
118- } ;
119- let mut endpoint_config = self . endpoint_config . lock ( ) . unwrap ( ) ;
120- * endpoint_config = Some ( config. clone ( ) ) ;
121- return Ok ( config) ;
122- }
123-
124- Err ( ErrorCode :: EmptyShareEndpointConfig (
125- "EmptyShareEndpointConfig, cannot query share databases" . to_string ( ) ,
126- ) )
127- }
128- }
57+ Ok ( Box :: new ( Self { ctx, db_info } ) )
12958 }
13059
13160 fn load_tables ( & self , table_infos : Vec < Arc < TableInfo > > ) -> Result < Vec < Arc < dyn Table > > > {
@@ -136,53 +65,11 @@ impl ShareDatabase {
13665 } )
13766 }
13867
139- // Read table info map from operator
140- async fn get_table_info_map ( & self , req : Vec < String > ) -> Result < TableInfoMap > {
141- // only when endpoint_config is Some can try again
142- let mut try_again = {
143- let endpoint_config = self . endpoint_config . lock ( ) . unwrap ( ) ;
144- !endpoint_config. is_some ( )
145- } ;
146-
147- loop {
148- let endpoint_config = self . get_share_endpoint ( ) . await ?;
149- let bs = Bytes :: from ( serde_json:: to_vec ( & req) ?) ;
150- let auth = endpoint_config. token . to_header ( ) . await ?;
151- let requester = GlobalConfig :: instance ( ) . as_ref ( ) . query . tenant_id . clone ( ) ;
152- let req = Request :: builder ( )
153- . method ( Method :: POST )
154- . uri ( & endpoint_config. url )
155- . header ( AUTHORIZATION , auth)
156- . header ( CONTENT_LENGTH , bs. len ( ) )
157- . header ( TENANT_HEADER , requester)
158- . body ( AsyncBody :: Bytes ( bs) ) ?;
159- let resp = self . client . send_async ( req) . await ;
160- match resp {
161- Ok ( resp) => {
162- let bs = resp. into_body ( ) . bytes ( ) . await ?;
163- let table_info_map: TableInfoMap = serde_json:: from_slice ( & bs) ?;
164-
165- return Ok ( table_info_map) ;
166- }
167- Err ( err) => {
168- if try_again {
169- error ! ( "get_table_info_map error: {:?}" , err) ;
170- return Err ( err. into ( ) ) ;
171- } else {
172- // endpoint may be changed, so cleanup endpoint and try again
173- try_again = true ;
174- let mut endpoint_config = self . endpoint_config . lock ( ) . unwrap ( ) ;
175- * endpoint_config = None ;
176- info ! ( "get_table_info_map error: {:?}, try again" , err) ;
177- }
178- }
179- }
180- }
181- }
182-
18368 async fn get_table_info ( & self , table_name : & str ) -> Result < Arc < TableInfo > > {
184- let table_info_map = self
185- . get_table_info_map ( vec ! [ table_name. to_string( ) ] )
69+ let table_info_map = ShareEndpointManager :: instance ( )
70+ . get_table_info_map ( & self . ctx . tenant , & self . db_info , vec ! [
71+ table_name. to_string( ) ,
72+ ] )
18673 . await ?;
18774 match table_info_map. get ( table_name) {
18875 None => Err ( ErrorCode :: UnknownTable ( format ! (
@@ -194,7 +81,9 @@ impl ShareDatabase {
19481 }
19582
19683 async fn list_tables ( & self ) -> Result < Vec < Arc < TableInfo > > > {
197- let table_info_map = self . get_table_info_map ( vec ! [ ] ) . await ?;
84+ let table_info_map = ShareEndpointManager :: instance ( )
85+ . get_table_info_map ( & self . ctx . tenant , & self . db_info , vec ! [ ] )
86+ . await ?;
19887 let table_infos: Vec < Arc < TableInfo > > = table_info_map
19988 . values ( )
20089 . map ( |table_info| Arc :: new ( table_info. to_owned ( ) ) )
0 commit comments