1515// specific language governing permissions and limitations
1616// under the License.
1717
18- pub mod services ;
18+ pub mod service ;
1919
2020use crate :: args:: DftArgs ;
2121use crate :: config:: AppConfig ;
@@ -25,9 +25,7 @@ use datafusion_app::config::merge_configs;
2525use datafusion_app:: extensions:: DftSessionStateBuilder ;
2626use datafusion_app:: local:: ExecutionContext ;
2727use log:: info;
28- use metrics:: { describe_counter, describe_histogram} ;
29- use metrics_exporter_prometheus:: { Matcher , PrometheusBuilder } ;
30- use services:: flightsql:: FlightSqlServiceImpl ;
28+ use service:: FlightSqlServiceImpl ;
3129use std:: net:: SocketAddr ;
3230use std:: time:: Duration ;
3331use tokio:: net:: TcpListener ;
@@ -37,25 +35,12 @@ use tonic::transport::Server;
3735#[ cfg( feature = "flightsql" ) ]
3836use tower_http:: validate_request:: ValidateRequestHeaderLayer ;
3937
40- const DEFAULT_TIMEOUT_SECONDS : u64 = 60 ;
41-
42- fn initialize_metrics ( ) {
43- describe_counter ! ( "requests" , "Incoming requests by FlightSQL endpoint" ) ;
38+ use super :: try_start_metrics_server;
4439
45- describe_histogram ! (
46- "get_flight_info_latency_ms" ,
47- metrics:: Unit :: Milliseconds ,
48- "Get flight info latency ms"
49- ) ;
50-
51- describe_histogram ! (
52- "do_get_fallback_latency_ms" ,
53- metrics:: Unit :: Milliseconds ,
54- "Do get fallback latency ms"
55- )
56- }
40+ const DEFAULT_TIMEOUT_SECONDS : u64 = 60 ;
41+ const DEFAULT_SERVER_ADDRESS : & str = "127.0.0.1:50051" ;
5742
58- fn create_server_handle (
43+ pub fn create_server_handle (
5944 config : & AppConfig ,
6045 flightsql : FlightSqlServiceImpl ,
6146 listener : TcpListener ,
@@ -139,37 +124,20 @@ impl FlightSqlApp {
139124 addr : & str ,
140125 metrics_addr : & str ,
141126 ) -> Result < Self > {
142- let flightsql = services :: flightsql :: FlightSqlServiceImpl :: new ( app_execution ) ;
143- // let OS choose a free port
127+ info ! ( "Listening to FlightSQL on {addr}" ) ;
128+ let flightsql = service :: FlightSqlServiceImpl :: new ( app_execution ) ;
144129 let listener = TcpListener :: bind ( addr) . await . unwrap ( ) ;
145- let addr = listener. local_addr ( ) . unwrap ( ) ;
146130
147131 // prepare the shutdown channel
148132 let ( tx, rx) = tokio:: sync:: oneshot:: channel ( ) ;
149133 let handle = create_server_handle ( config, flightsql, listener, rx) ?;
150134
151- {
152- let builder = PrometheusBuilder :: new ( ) ;
153- let addr: SocketAddr = metrics_addr. parse ( ) ?;
154- info ! ( "Listening to metrics on {addr}" ) ;
155- builder
156- . with_http_listener ( addr)
157- . set_buckets_for_metric (
158- Matcher :: Suffix ( "latency_ms" . to_string ( ) ) ,
159- & [
160- 1.0 , 3.0 , 5.0 , 10.0 , 25.0 , 50.0 , 75.0 , 100.0 , 250.0 , 500.0 , 1000.0 , 2500.0 ,
161- 5000.0 , 10000.0 , 20000.0 ,
162- ] ,
163- ) ?
164- . install ( )
165- . expect ( "failed to install metrics recorder/exporter" ) ;
166-
167- initialize_metrics ( ) ;
168- }
135+ let metrics_addr: SocketAddr = metrics_addr. parse ( ) ?;
136+ try_start_metrics_server ( metrics_addr) ?;
169137
170138 let app = Self {
171139 shutdown : Some ( tx) ,
172- addr,
140+ addr : metrics_addr ,
173141 handle : Some ( handle) ,
174142 } ;
175143 Ok ( app)
@@ -188,7 +156,7 @@ impl FlightSqlApp {
188156 }
189157 }
190158
191- pub async fn run_app ( self ) {
159+ pub async fn run ( self ) {
192160 if let Some ( handle) = self . handle {
193161 handle
194162 . await
@@ -208,10 +176,8 @@ pub async fn try_run(cli: DftArgs, config: AppConfig) -> Result<()> {
208176 let session_state_builder = DftSessionStateBuilder :: try_new ( Some ( merged_exec_config. clone ( ) ) ) ?
209177 . with_extensions ( )
210178 . await ?;
211- // FlightSQL Server mode: start a FlightSQL server
212- const DEFAULT_SERVER_ADDRESS : & str = "127.0.0.1:50051" ;
213- info ! ( "Starting FlightSQL server on {}" , DEFAULT_SERVER_ADDRESS ) ;
214179 let session_state = session_state_builder. build ( ) ?;
180+ // FlightSQL Server mode: start a FlightSQL server
215181 let execution_ctx = ExecutionContext :: try_new ( & merged_exec_config, session_state) ?;
216182 if cli. run_ddl {
217183 execution_ctx. execute_ddl ( ) . await ;
@@ -220,11 +186,10 @@ pub async fn try_run(cli: DftArgs, config: AppConfig) -> Result<()> {
220186 let app = FlightSqlApp :: try_new (
221187 app_execution,
222188 & config,
223- & cli. flightsql_host
224- . unwrap_or ( DEFAULT_SERVER_ADDRESS . to_string ( ) ) ,
189+ & cli. host . unwrap_or ( DEFAULT_SERVER_ADDRESS . to_string ( ) ) ,
225190 & config. flightsql_server . server_metrics_port ,
226191 )
227192 . await ?;
228- app. run_app ( ) . await ;
193+ app. run ( ) . await ;
229194 Ok ( ( ) )
230195}
0 commit comments