servers/
http.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::HashMap;
16use std::convert::Infallible;
17use std::fmt::Display;
18use std::net::SocketAddr;
19use std::sync::Mutex as StdMutex;
20use std::time::Duration;
21
22use async_trait::async_trait;
23use auth::UserProviderRef;
24use axum::extract::{DefaultBodyLimit, Request};
25use axum::http::StatusCode as HttpStatusCode;
26use axum::response::{IntoResponse, Response};
27use axum::routing::Route;
28use axum::serve::ListenerExt;
29use axum::{Router, middleware, routing};
30use common_base::Plugins;
31use common_base::readable_size::ReadableSize;
32use common_recordbatch::RecordBatch;
33use common_telemetry::{error, info};
34use common_time::Timestamp;
35use common_time::timestamp::TimeUnit;
36use datatypes::data_type::DataType;
37use datatypes::schema::SchemaRef;
38use event::{LogState, LogValidatorRef};
39use futures::FutureExt;
40use http::{HeaderValue, Method};
41use serde::{Deserialize, Serialize};
42use serde_json::Value;
43use snafu::{ResultExt, ensure};
44use tokio::sync::Mutex;
45use tokio::sync::oneshot::{self, Sender};
46use tonic::codegen::Service;
47use tower::{Layer, ServiceBuilder};
48use tower_http::compression::CompressionLayer;
49use tower_http::cors::{AllowOrigin, Any, CorsLayer};
50use tower_http::decompression::RequestDecompressionLayer;
51use tower_http::trace::TraceLayer;
52
53use self::authorize::AuthState;
54use self::result::table_result::TableResponse;
55use crate::configurator::HttpConfiguratorRef;
56use crate::elasticsearch;
57use crate::error::{
58    AddressBindSnafu, AlreadyStartedSnafu, Error, InternalIoSnafu, InvalidHeaderValueSnafu,
59    OtherSnafu, Result,
60};
61use crate::http::influxdb::{influxdb_health, influxdb_ping, influxdb_write_v1, influxdb_write_v2};
62use crate::http::otlp::OtlpState;
63use crate::http::prom_store::PromStoreState;
64use crate::http::prometheus::{
65    build_info_query, format_query, instant_query, label_values_query, labels_query, parse_query,
66    range_query, series_query,
67};
68use crate::http::result::arrow_result::ArrowResponse;
69use crate::http::result::csv_result::CsvResponse;
70use crate::http::result::error_result::ErrorResponse;
71use crate::http::result::greptime_result_v1::GreptimedbV1Response;
72use crate::http::result::influxdb_result_v1::InfluxdbV1Response;
73use crate::http::result::json_result::JsonResponse;
74use crate::http::result::null_result::NullResponse;
75use crate::interceptor::LogIngestInterceptorRef;
76use crate::metrics::http_metrics_layer;
77use crate::metrics_handler::MetricsHandler;
78use crate::prometheus_handler::PrometheusHandlerRef;
79use crate::query_handler::sql::ServerSqlQueryHandlerRef;
80use crate::query_handler::{
81    InfluxdbLineProtocolHandlerRef, JaegerQueryHandlerRef, LogQueryHandlerRef,
82    OpenTelemetryProtocolHandlerRef, OpentsdbProtocolHandlerRef, PipelineHandlerRef,
83    PromStoreProtocolHandlerRef,
84};
85use crate::request_memory_limiter::ServerMemoryLimiter;
86use crate::server::Server;
87
88pub mod authorize;
89#[cfg(feature = "dashboard")]
90mod dashboard;
91pub mod dyn_log;
92pub mod dyn_trace;
93pub mod event;
94pub mod extractor;
95pub mod handler;
96pub mod header;
97pub mod influxdb;
98pub mod jaeger;
99pub mod logs;
100pub mod loki;
101pub mod mem_prof;
102mod memory_limit;
103pub mod opentsdb;
104pub mod otlp;
105pub mod pprof;
106pub mod prom_store;
107pub mod prometheus;
108pub mod result;
109mod timeout;
110pub mod utils;
111
112use result::HttpOutputWriter;
113pub(crate) use timeout::DynamicTimeoutLayer;
114
115use crate::prom_remote_write::validation::PromValidationMode;
116
117mod hints;
118mod read_preference;
119#[cfg(any(test, feature = "testing"))]
120pub mod test_helpers;
121
122pub const HTTP_API_VERSION: &str = "v1";
123pub const HTTP_API_PREFIX: &str = "/v1/";
124/// Default http body limit (64M).
125const DEFAULT_BODY_LIMIT: ReadableSize = ReadableSize::mb(64);
126
127/// Authorization header
128pub const AUTHORIZATION_HEADER: &str = "x-greptime-auth";
129
130// TODO(fys): This is a temporary workaround, it will be improved later
131pub static PUBLIC_APIS: [&str; 3] = ["/v1/influxdb/ping", "/v1/influxdb/health", "/v1/health"];
132
133#[derive(Default)]
134pub struct HttpServer {
135    router: StdMutex<Router>,
136    shutdown_tx: Mutex<Option<Sender<()>>>,
137    user_provider: Option<UserProviderRef>,
138    memory_limiter: ServerMemoryLimiter,
139
140    // plugins
141    plugins: Plugins,
142
143    // server configs
144    options: HttpOptions,
145    bind_addr: Option<SocketAddr>,
146}
147
148#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
149#[serde(default)]
150pub struct HttpOptions {
151    pub addr: String,
152
153    #[serde(with = "humantime_serde")]
154    pub timeout: Duration,
155
156    #[serde(skip)]
157    pub disable_dashboard: bool,
158
159    pub body_limit: ReadableSize,
160
161    /// Validation mode while decoding Prometheus remote write requests.
162    pub prom_validation_mode: PromValidationMode,
163
164    pub cors_allowed_origins: Vec<String>,
165
166    pub enable_cors: bool,
167}
168
169impl Default for HttpOptions {
170    fn default() -> Self {
171        Self {
172            addr: "127.0.0.1:4000".to_string(),
173            timeout: Duration::from_secs(0),
174            disable_dashboard: false,
175            body_limit: DEFAULT_BODY_LIMIT,
176            cors_allowed_origins: Vec::new(),
177            enable_cors: true,
178            prom_validation_mode: PromValidationMode::Strict,
179        }
180    }
181}
182
183#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
184pub struct ColumnSchema {
185    name: String,
186    data_type: String,
187}
188
189impl ColumnSchema {
190    pub fn new(name: String, data_type: String) -> ColumnSchema {
191        ColumnSchema { name, data_type }
192    }
193}
194
195#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
196pub struct OutputSchema {
197    column_schemas: Vec<ColumnSchema>,
198}
199
200impl OutputSchema {
201    pub fn new(columns: Vec<ColumnSchema>) -> OutputSchema {
202        OutputSchema {
203            column_schemas: columns,
204        }
205    }
206}
207
208impl From<SchemaRef> for OutputSchema {
209    fn from(schema: SchemaRef) -> OutputSchema {
210        OutputSchema {
211            column_schemas: schema
212                .column_schemas()
213                .iter()
214                .map(|cs| ColumnSchema {
215                    name: cs.name.clone(),
216                    data_type: cs.data_type.name(),
217                })
218                .collect(),
219        }
220    }
221}
222
223#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
224pub struct HttpRecordsOutput {
225    schema: OutputSchema,
226    rows: Vec<Vec<Value>>,
227    // total_rows is equal to rows.len() in most cases,
228    // the Dashboard query result may be truncated, so we need to return the total_rows.
229    #[serde(default)]
230    total_rows: usize,
231
232    // plan level execution metrics
233    #[serde(skip_serializing_if = "HashMap::is_empty")]
234    #[serde(default)]
235    metrics: HashMap<String, Value>,
236}
237
238impl HttpRecordsOutput {
239    pub fn num_rows(&self) -> usize {
240        self.rows.len()
241    }
242
243    pub fn num_cols(&self) -> usize {
244        self.schema.column_schemas.len()
245    }
246
247    pub fn schema(&self) -> &OutputSchema {
248        &self.schema
249    }
250
251    pub fn rows(&self) -> &Vec<Vec<Value>> {
252        &self.rows
253    }
254}
255
256impl HttpRecordsOutput {
257    pub fn try_new(
258        schema: SchemaRef,
259        recordbatches: Vec<RecordBatch>,
260    ) -> std::result::Result<HttpRecordsOutput, Error> {
261        if recordbatches.is_empty() {
262            Ok(HttpRecordsOutput {
263                schema: OutputSchema::from(schema),
264                rows: vec![],
265                total_rows: 0,
266                metrics: Default::default(),
267            })
268        } else {
269            let num_rows = recordbatches.iter().map(|r| r.num_rows()).sum::<usize>();
270            let mut rows = Vec::with_capacity(num_rows);
271
272            for recordbatch in recordbatches {
273                let mut writer = HttpOutputWriter::new(schema.num_columns(), None);
274                writer.write(recordbatch, &mut rows)?;
275            }
276
277            Ok(HttpRecordsOutput {
278                schema: OutputSchema::from(schema),
279                total_rows: rows.len(),
280                rows,
281                metrics: Default::default(),
282            })
283        }
284    }
285}
286
287#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
288#[serde(rename_all = "lowercase")]
289pub enum GreptimeQueryOutput {
290    AffectedRows(usize),
291    Records(HttpRecordsOutput),
292}
293
294/// It allows the results of SQL queries to be presented in different formats.
295#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
296pub enum ResponseFormat {
297    Arrow,
298    // (with_names, with_types)
299    Csv(bool, bool),
300    Table,
301    #[default]
302    GreptimedbV1,
303    InfluxdbV1,
304    Json,
305    Null,
306}
307
308impl ResponseFormat {
309    pub fn parse(s: &str) -> Option<Self> {
310        match s {
311            "arrow" => Some(ResponseFormat::Arrow),
312            "csv" => Some(ResponseFormat::Csv(false, false)),
313            "csvwithnames" => Some(ResponseFormat::Csv(true, false)),
314            "csvwithnamesandtypes" => Some(ResponseFormat::Csv(true, true)),
315            "table" => Some(ResponseFormat::Table),
316            "greptimedb_v1" => Some(ResponseFormat::GreptimedbV1),
317            "influxdb_v1" => Some(ResponseFormat::InfluxdbV1),
318            "json" => Some(ResponseFormat::Json),
319            "null" => Some(ResponseFormat::Null),
320            _ => None,
321        }
322    }
323
324    pub fn as_str(&self) -> &'static str {
325        match self {
326            ResponseFormat::Arrow => "arrow",
327            ResponseFormat::Csv(_, _) => "csv",
328            ResponseFormat::Table => "table",
329            ResponseFormat::GreptimedbV1 => "greptimedb_v1",
330            ResponseFormat::InfluxdbV1 => "influxdb_v1",
331            ResponseFormat::Json => "json",
332            ResponseFormat::Null => "null",
333        }
334    }
335}
336
337#[derive(Debug, Clone, Copy, PartialEq, Eq)]
338pub enum Epoch {
339    Nanosecond,
340    Microsecond,
341    Millisecond,
342    Second,
343}
344
345impl Epoch {
346    pub fn parse(s: &str) -> Option<Epoch> {
347        // Both u and µ indicate microseconds.
348        // epoch = [ns,u,µ,ms,s],
349        // For details, see the Influxdb documents.
350        // https://docs.influxdata.com/influxdb/v1/tools/api/#query-string-parameters-1
351        match s {
352            "ns" => Some(Epoch::Nanosecond),
353            "u" | "µ" => Some(Epoch::Microsecond),
354            "ms" => Some(Epoch::Millisecond),
355            "s" => Some(Epoch::Second),
356            _ => None, // just returns None for other cases
357        }
358    }
359
360    pub fn convert_timestamp(&self, ts: Timestamp) -> Option<Timestamp> {
361        match self {
362            Epoch::Nanosecond => ts.convert_to(TimeUnit::Nanosecond),
363            Epoch::Microsecond => ts.convert_to(TimeUnit::Microsecond),
364            Epoch::Millisecond => ts.convert_to(TimeUnit::Millisecond),
365            Epoch::Second => ts.convert_to(TimeUnit::Second),
366        }
367    }
368}
369
370impl Display for Epoch {
371    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
372        match self {
373            Epoch::Nanosecond => write!(f, "Epoch::Nanosecond"),
374            Epoch::Microsecond => write!(f, "Epoch::Microsecond"),
375            Epoch::Millisecond => write!(f, "Epoch::Millisecond"),
376            Epoch::Second => write!(f, "Epoch::Second"),
377        }
378    }
379}
380
381#[derive(Serialize, Deserialize, Debug)]
382pub enum HttpResponse {
383    Arrow(ArrowResponse),
384    Csv(CsvResponse),
385    Table(TableResponse),
386    Error(ErrorResponse),
387    GreptimedbV1(GreptimedbV1Response),
388    InfluxdbV1(InfluxdbV1Response),
389    Json(JsonResponse),
390    Null(NullResponse),
391}
392
393impl HttpResponse {
394    pub fn with_execution_time(self, execution_time: u64) -> Self {
395        match self {
396            HttpResponse::Arrow(resp) => resp.with_execution_time(execution_time).into(),
397            HttpResponse::Csv(resp) => resp.with_execution_time(execution_time).into(),
398            HttpResponse::Table(resp) => resp.with_execution_time(execution_time).into(),
399            HttpResponse::GreptimedbV1(resp) => resp.with_execution_time(execution_time).into(),
400            HttpResponse::InfluxdbV1(resp) => resp.with_execution_time(execution_time).into(),
401            HttpResponse::Json(resp) => resp.with_execution_time(execution_time).into(),
402            HttpResponse::Null(resp) => resp.with_execution_time(execution_time).into(),
403            HttpResponse::Error(resp) => resp.with_execution_time(execution_time).into(),
404        }
405    }
406
407    pub fn with_limit(self, limit: usize) -> Self {
408        match self {
409            HttpResponse::Csv(resp) => resp.with_limit(limit).into(),
410            HttpResponse::Table(resp) => resp.with_limit(limit).into(),
411            HttpResponse::GreptimedbV1(resp) => resp.with_limit(limit).into(),
412            HttpResponse::Json(resp) => resp.with_limit(limit).into(),
413            _ => self,
414        }
415    }
416}
417
418pub fn process_with_limit(
419    mut outputs: Vec<GreptimeQueryOutput>,
420    limit: usize,
421) -> Vec<GreptimeQueryOutput> {
422    outputs
423        .drain(..)
424        .map(|data| match data {
425            GreptimeQueryOutput::Records(mut records) => {
426                if records.rows.len() > limit {
427                    records.rows.truncate(limit);
428                    records.total_rows = limit;
429                }
430                GreptimeQueryOutput::Records(records)
431            }
432            _ => data,
433        })
434        .collect()
435}
436
437impl IntoResponse for HttpResponse {
438    fn into_response(self) -> Response {
439        match self {
440            HttpResponse::Arrow(resp) => resp.into_response(),
441            HttpResponse::Csv(resp) => resp.into_response(),
442            HttpResponse::Table(resp) => resp.into_response(),
443            HttpResponse::GreptimedbV1(resp) => resp.into_response(),
444            HttpResponse::InfluxdbV1(resp) => resp.into_response(),
445            HttpResponse::Json(resp) => resp.into_response(),
446            HttpResponse::Null(resp) => resp.into_response(),
447            HttpResponse::Error(resp) => resp.into_response(),
448        }
449    }
450}
451
452impl From<ArrowResponse> for HttpResponse {
453    fn from(value: ArrowResponse) -> Self {
454        HttpResponse::Arrow(value)
455    }
456}
457
458impl From<CsvResponse> for HttpResponse {
459    fn from(value: CsvResponse) -> Self {
460        HttpResponse::Csv(value)
461    }
462}
463
464impl From<TableResponse> for HttpResponse {
465    fn from(value: TableResponse) -> Self {
466        HttpResponse::Table(value)
467    }
468}
469
470impl From<ErrorResponse> for HttpResponse {
471    fn from(value: ErrorResponse) -> Self {
472        HttpResponse::Error(value)
473    }
474}
475
476impl From<GreptimedbV1Response> for HttpResponse {
477    fn from(value: GreptimedbV1Response) -> Self {
478        HttpResponse::GreptimedbV1(value)
479    }
480}
481
482impl From<InfluxdbV1Response> for HttpResponse {
483    fn from(value: InfluxdbV1Response) -> Self {
484        HttpResponse::InfluxdbV1(value)
485    }
486}
487
488impl From<JsonResponse> for HttpResponse {
489    fn from(value: JsonResponse) -> Self {
490        HttpResponse::Json(value)
491    }
492}
493
494impl From<NullResponse> for HttpResponse {
495    fn from(value: NullResponse) -> Self {
496        HttpResponse::Null(value)
497    }
498}
499
500#[derive(Clone)]
501pub struct ApiState {
502    pub sql_handler: ServerSqlQueryHandlerRef,
503}
504
505#[derive(Clone)]
506pub struct GreptimeOptionsConfigState {
507    pub greptime_config_options: String,
508}
509
510pub struct HttpServerBuilder {
511    options: HttpOptions,
512    plugins: Plugins,
513    user_provider: Option<UserProviderRef>,
514    router: Router,
515    memory_limiter: ServerMemoryLimiter,
516}
517
518impl HttpServerBuilder {
519    pub fn new(options: HttpOptions) -> Self {
520        Self {
521            options,
522            plugins: Plugins::default(),
523            user_provider: None,
524            router: Router::new(),
525            memory_limiter: ServerMemoryLimiter::default(),
526        }
527    }
528
529    /// Set a global memory limiter for all server protocols.
530    pub fn with_memory_limiter(mut self, limiter: ServerMemoryLimiter) -> Self {
531        self.memory_limiter = limiter;
532        self
533    }
534
535    pub fn with_sql_handler(self, sql_handler: ServerSqlQueryHandlerRef) -> Self {
536        let sql_router = HttpServer::route_sql(ApiState { sql_handler });
537
538        Self {
539            router: self
540                .router
541                .nest(&format!("/{HTTP_API_VERSION}"), sql_router),
542            ..self
543        }
544    }
545
546    pub fn with_logs_handler(self, logs_handler: LogQueryHandlerRef) -> Self {
547        let logs_router = HttpServer::route_logs(logs_handler);
548
549        Self {
550            router: self
551                .router
552                .nest(&format!("/{HTTP_API_VERSION}"), logs_router),
553            ..self
554        }
555    }
556
557    pub fn with_opentsdb_handler(self, handler: OpentsdbProtocolHandlerRef) -> Self {
558        Self {
559            router: self.router.nest(
560                &format!("/{HTTP_API_VERSION}/opentsdb"),
561                HttpServer::route_opentsdb(handler),
562            ),
563            ..self
564        }
565    }
566
567    pub fn with_influxdb_handler(self, handler: InfluxdbLineProtocolHandlerRef) -> Self {
568        Self {
569            router: self.router.nest(
570                &format!("/{HTTP_API_VERSION}/influxdb"),
571                HttpServer::route_influxdb(handler),
572            ),
573            ..self
574        }
575    }
576
577    pub fn with_prom_handler(
578        self,
579        handler: PromStoreProtocolHandlerRef,
580        pipeline_handler: Option<PipelineHandlerRef>,
581        prom_store_with_metric_engine: bool,
582        prom_validation_mode: PromValidationMode,
583    ) -> Self {
584        let state = PromStoreState {
585            prom_store_handler: handler,
586            pipeline_handler,
587            prom_store_with_metric_engine,
588            prom_validation_mode,
589        };
590
591        Self {
592            router: self.router.nest(
593                &format!("/{HTTP_API_VERSION}/prometheus"),
594                HttpServer::route_prom(state),
595            ),
596            ..self
597        }
598    }
599
600    pub fn with_prometheus_handler(self, handler: PrometheusHandlerRef) -> Self {
601        Self {
602            router: self.router.nest(
603                &format!("/{HTTP_API_VERSION}/prometheus/api/v1"),
604                HttpServer::route_prometheus(handler),
605            ),
606            ..self
607        }
608    }
609
610    pub fn with_otlp_handler(
611        self,
612        handler: OpenTelemetryProtocolHandlerRef,
613        with_metric_engine: bool,
614    ) -> Self {
615        Self {
616            router: self.router.nest(
617                &format!("/{HTTP_API_VERSION}/otlp"),
618                HttpServer::route_otlp(handler, with_metric_engine),
619            ),
620            ..self
621        }
622    }
623
624    pub fn with_user_provider(self, user_provider: UserProviderRef) -> Self {
625        Self {
626            user_provider: Some(user_provider),
627            ..self
628        }
629    }
630
631    pub fn with_metrics_handler(self, handler: MetricsHandler) -> Self {
632        Self {
633            router: self.router.merge(HttpServer::route_metrics(handler)),
634            ..self
635        }
636    }
637
638    pub fn with_log_ingest_handler(
639        self,
640        handler: PipelineHandlerRef,
641        validator: Option<LogValidatorRef>,
642        ingest_interceptor: Option<LogIngestInterceptorRef<Error>>,
643    ) -> Self {
644        let log_state = LogState {
645            log_handler: handler,
646            log_validator: validator,
647            ingest_interceptor,
648        };
649
650        let router = self.router.nest(
651            &format!("/{HTTP_API_VERSION}"),
652            HttpServer::route_pipelines(log_state.clone()),
653        );
654        // deprecated since v0.11.0. Use `/logs` and `/pipelines` instead.
655        let router = router.nest(
656            &format!("/{HTTP_API_VERSION}/events"),
657            #[allow(deprecated)]
658            HttpServer::route_log_deprecated(log_state.clone()),
659        );
660
661        let router = router.nest(
662            &format!("/{HTTP_API_VERSION}/loki"),
663            HttpServer::route_loki(log_state.clone()),
664        );
665
666        let router = router.nest(
667            &format!("/{HTTP_API_VERSION}/elasticsearch"),
668            HttpServer::route_elasticsearch(log_state.clone()),
669        );
670
671        let router = router.nest(
672            &format!("/{HTTP_API_VERSION}/elasticsearch/"),
673            Router::new()
674                .route("/", routing::get(elasticsearch::handle_get_version))
675                .with_state(log_state),
676        );
677
678        Self { router, ..self }
679    }
680
681    pub fn with_plugins(self, plugins: Plugins) -> Self {
682        Self { plugins, ..self }
683    }
684
685    pub fn with_greptime_config_options(self, opts: String) -> Self {
686        let config_router = HttpServer::route_config(GreptimeOptionsConfigState {
687            greptime_config_options: opts,
688        });
689
690        Self {
691            router: self.router.merge(config_router),
692            ..self
693        }
694    }
695
696    pub fn with_jaeger_handler(self, handler: JaegerQueryHandlerRef) -> Self {
697        Self {
698            router: self.router.nest(
699                &format!("/{HTTP_API_VERSION}/jaeger"),
700                HttpServer::route_jaeger(handler),
701            ),
702            ..self
703        }
704    }
705
706    pub fn with_extra_router(self, router: Router) -> Self {
707        Self {
708            router: self.router.merge(router),
709            ..self
710        }
711    }
712
713    pub fn add_layer<L>(self, layer: L) -> Self
714    where
715        L: Layer<Route> + Clone + Send + Sync + 'static,
716        L::Service: Service<Request> + Clone + Send + Sync + 'static,
717        <L::Service as Service<Request>>::Response: IntoResponse + 'static,
718        <L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
719        <L::Service as Service<Request>>::Future: Send + 'static,
720    {
721        Self {
722            router: self.router.layer(layer),
723            ..self
724        }
725    }
726
727    pub fn build(self) -> HttpServer {
728        HttpServer {
729            options: self.options,
730            user_provider: self.user_provider,
731            shutdown_tx: Mutex::new(None),
732            plugins: self.plugins,
733            router: StdMutex::new(self.router),
734            bind_addr: None,
735            memory_limiter: self.memory_limiter,
736        }
737    }
738}
739
740impl HttpServer {
741    /// Gets the router and adds necessary root routes (health, status, dashboard).
742    pub fn make_app(&self) -> Router {
743        let mut router = {
744            let router = self.router.lock().unwrap();
745            router.clone()
746        };
747
748        router = router
749            .route(
750                "/health",
751                routing::get(handler::health).post(handler::health),
752            )
753            .route(
754                &format!("/{HTTP_API_VERSION}/health"),
755                routing::get(handler::health).post(handler::health),
756            )
757            .route(
758                "/ready",
759                routing::get(handler::health).post(handler::health),
760            );
761
762        router = router.route("/status", routing::get(handler::status));
763
764        #[cfg(feature = "dashboard")]
765        {
766            if !self.options.disable_dashboard {
767                info!("Enable dashboard service at '/dashboard'");
768                // redirect /dashboard to /dashboard/
769                router = router.route(
770                    "/dashboard",
771                    routing::get(|uri: axum::http::uri::Uri| async move {
772                        let path = uri.path();
773                        let query = uri.query().map(|q| format!("?{}", q)).unwrap_or_default();
774
775                        let new_uri = format!("{}/{}", path, query);
776                        axum::response::Redirect::permanent(&new_uri)
777                    }),
778                );
779
780                // "/dashboard" and "/dashboard/" are two different paths in Axum.
781                // We cannot nest "/dashboard/", because we already mapping "/dashboard/{*x}" while nesting "/dashboard".
782                // So we explicitly route "/dashboard/" here.
783                router = router
784                    .route(
785                        "/dashboard/",
786                        routing::get(dashboard::static_handler).post(dashboard::static_handler),
787                    )
788                    .route(
789                        "/dashboard/{*x}",
790                        routing::get(dashboard::static_handler).post(dashboard::static_handler),
791                    );
792            }
793        }
794
795        // Add a layer to collect HTTP metrics for axum.
796        router = router.route_layer(middleware::from_fn(http_metrics_layer));
797
798        router
799    }
800
801    /// Attaches middlewares and debug routes to the router.
802    /// Callers should call this method after [HttpServer::make_app()].
803    pub fn build(&self, router: Router) -> Result<Router> {
804        let timeout_layer = if self.options.timeout != Duration::default() {
805            Some(ServiceBuilder::new().layer(DynamicTimeoutLayer::new(self.options.timeout)))
806        } else {
807            info!("HTTP server timeout is disabled");
808            None
809        };
810        let body_limit_layer = if self.options.body_limit != ReadableSize(0) {
811            Some(
812                ServiceBuilder::new()
813                    .layer(DefaultBodyLimit::max(self.options.body_limit.0 as usize)),
814            )
815        } else {
816            info!("HTTP server body limit is disabled");
817            None
818        };
819        let cors_layer = if self.options.enable_cors {
820            Some(
821                CorsLayer::new()
822                    .allow_methods([
823                        Method::GET,
824                        Method::POST,
825                        Method::PUT,
826                        Method::DELETE,
827                        Method::HEAD,
828                    ])
829                    .allow_origin(if self.options.cors_allowed_origins.is_empty() {
830                        AllowOrigin::from(Any)
831                    } else {
832                        AllowOrigin::from(
833                            self.options
834                                .cors_allowed_origins
835                                .iter()
836                                .map(|s| {
837                                    HeaderValue::from_str(s.as_str())
838                                        .context(InvalidHeaderValueSnafu)
839                                })
840                                .collect::<Result<Vec<HeaderValue>>>()?,
841                        )
842                    })
843                    .allow_headers(Any),
844            )
845        } else {
846            info!("HTTP server cross-origin is disabled");
847            None
848        };
849
850        Ok(router
851            // middlewares
852            .layer(
853                ServiceBuilder::new()
854                    // disable on failure tracing. because printing out isn't very helpful,
855                    // and we have impl IntoResponse for Error. It will print out more detailed error messages
856                    .layer(TraceLayer::new_for_http().on_failure(()))
857                    .option_layer(cors_layer)
858                    .option_layer(timeout_layer)
859                    .option_layer(body_limit_layer)
860                    // memory limit layer - must be before body is consumed
861                    .layer(middleware::from_fn_with_state(
862                        self.memory_limiter.clone(),
863                        memory_limit::memory_limit_middleware,
864                    ))
865                    // auth layer
866                    .layer(middleware::from_fn_with_state(
867                        AuthState::new(self.user_provider.clone()),
868                        authorize::check_http_auth,
869                    ))
870                    .layer(middleware::from_fn(hints::extract_hints))
871                    .layer(middleware::from_fn(
872                        read_preference::extract_read_preference,
873                    )),
874            )
875            // Handlers for debug, we don't expect a timeout.
876            .nest(
877                "/debug",
878                Router::new()
879                    // handler for changing log level dynamically
880                    .route("/log_level", routing::post(dyn_log::dyn_log_handler))
881                    .route("/enable_trace", routing::post(dyn_trace::dyn_trace_handler))
882                    .nest(
883                        "/prof",
884                        Router::new()
885                            .route("/cpu", routing::post(pprof::pprof_handler))
886                            .route("/mem", routing::post(mem_prof::mem_prof_handler))
887                            .route("/mem/symbol", routing::post(mem_prof::symbolicate_handler))
888                            .route(
889                                "/mem/activate",
890                                routing::post(mem_prof::activate_heap_prof_handler),
891                            )
892                            .route(
893                                "/mem/deactivate",
894                                routing::post(mem_prof::deactivate_heap_prof_handler),
895                            )
896                            .route(
897                                "/mem/status",
898                                routing::get(mem_prof::heap_prof_status_handler),
899                            ) // jemalloc gdump flag status and toggle
900                            .route(
901                                "/mem/gdump",
902                                routing::get(mem_prof::gdump_status_handler)
903                                    .post(mem_prof::gdump_toggle_handler),
904                            ),
905                    ),
906            ))
907    }
908
909    fn route_metrics<S>(metrics_handler: MetricsHandler) -> Router<S> {
910        Router::new()
911            .route("/metrics", routing::get(handler::metrics))
912            .with_state(metrics_handler)
913    }
914
915    fn route_loki<S>(log_state: LogState) -> Router<S> {
916        Router::new()
917            .route("/api/v1/push", routing::post(loki::loki_ingest))
918            .layer(
919                ServiceBuilder::new()
920                    .layer(RequestDecompressionLayer::new().pass_through_unaccepted(true)),
921            )
922            .with_state(log_state)
923    }
924
925    fn route_elasticsearch<S>(log_state: LogState) -> Router<S> {
926        Router::new()
927            // Return fake responsefor HEAD '/' request.
928            .route(
929                "/",
930                routing::head((HttpStatusCode::OK, elasticsearch::elasticsearch_headers())),
931            )
932            // Return fake response for Elasticsearch version request.
933            .route("/", routing::get(elasticsearch::handle_get_version))
934            // Return fake response for Elasticsearch license request.
935            .route("/_license", routing::get(elasticsearch::handle_get_license))
936            .route("/_bulk", routing::post(elasticsearch::handle_bulk_api))
937            .route(
938                "/{index}/_bulk",
939                routing::post(elasticsearch::handle_bulk_api_with_index),
940            )
941            // Return fake response for Elasticsearch ilm request.
942            .route(
943                "/_ilm/policy/{*path}",
944                routing::any((
945                    HttpStatusCode::OK,
946                    elasticsearch::elasticsearch_headers(),
947                    axum::Json(serde_json::json!({})),
948                )),
949            )
950            // Return fake response for Elasticsearch index template request.
951            .route(
952                "/_index_template/{*path}",
953                routing::any((
954                    HttpStatusCode::OK,
955                    elasticsearch::elasticsearch_headers(),
956                    axum::Json(serde_json::json!({})),
957                )),
958            )
959            // Return fake response for Elasticsearch ingest pipeline request.
960            // See: https://www.elastic.co/guide/en/elasticsearch/reference/8.8/put-pipeline-api.html.
961            .route(
962                "/_ingest/{*path}",
963                routing::any((
964                    HttpStatusCode::OK,
965                    elasticsearch::elasticsearch_headers(),
966                    axum::Json(serde_json::json!({})),
967                )),
968            )
969            // Return fake response for Elasticsearch nodes discovery request.
970            // See: https://www.elastic.co/guide/en/elasticsearch/reference/8.8/cluster.html.
971            .route(
972                "/_nodes/{*path}",
973                routing::any((
974                    HttpStatusCode::OK,
975                    elasticsearch::elasticsearch_headers(),
976                    axum::Json(serde_json::json!({})),
977                )),
978            )
979            // Return fake response for Logstash APIs requests.
980            // See: https://www.elastic.co/guide/en/elasticsearch/reference/8.8/logstash-apis.html
981            .route(
982                "/logstash/{*path}",
983                routing::any((
984                    HttpStatusCode::OK,
985                    elasticsearch::elasticsearch_headers(),
986                    axum::Json(serde_json::json!({})),
987                )),
988            )
989            .route(
990                "/_logstash/{*path}",
991                routing::any((
992                    HttpStatusCode::OK,
993                    elasticsearch::elasticsearch_headers(),
994                    axum::Json(serde_json::json!({})),
995                )),
996            )
997            .layer(ServiceBuilder::new().layer(RequestDecompressionLayer::new()))
998            .with_state(log_state)
999    }
1000
1001    #[deprecated(since = "0.11.0", note = "Use `route_pipelines()` instead.")]
1002    fn route_log_deprecated<S>(log_state: LogState) -> Router<S> {
1003        Router::new()
1004            .route("/logs", routing::post(event::log_ingester))
1005            .route(
1006                "/pipelines/{pipeline_name}",
1007                routing::get(event::query_pipeline),
1008            )
1009            .route(
1010                "/pipelines/{pipeline_name}",
1011                routing::post(event::add_pipeline),
1012            )
1013            .route(
1014                "/pipelines/{pipeline_name}",
1015                routing::delete(event::delete_pipeline),
1016            )
1017            .route("/pipelines/dryrun", routing::post(event::pipeline_dryrun))
1018            .layer(
1019                ServiceBuilder::new()
1020                    .layer(RequestDecompressionLayer::new().pass_through_unaccepted(true)),
1021            )
1022            .with_state(log_state)
1023    }
1024
1025    fn route_pipelines<S>(log_state: LogState) -> Router<S> {
1026        Router::new()
1027            .route("/ingest", routing::post(event::log_ingester))
1028            .route(
1029                "/pipelines/{pipeline_name}",
1030                routing::get(event::query_pipeline),
1031            )
1032            .route(
1033                "/pipelines/{pipeline_name}/ddl",
1034                routing::get(event::query_pipeline_ddl),
1035            )
1036            .route(
1037                "/pipelines/{pipeline_name}",
1038                routing::post(event::add_pipeline),
1039            )
1040            .route(
1041                "/pipelines/{pipeline_name}",
1042                routing::delete(event::delete_pipeline),
1043            )
1044            .route("/pipelines/_dryrun", routing::post(event::pipeline_dryrun))
1045            .layer(
1046                ServiceBuilder::new()
1047                    .layer(RequestDecompressionLayer::new().pass_through_unaccepted(true)),
1048            )
1049            .with_state(log_state)
1050    }
1051
1052    fn route_sql<S>(api_state: ApiState) -> Router<S> {
1053        Router::new()
1054            .route("/sql", routing::get(handler::sql).post(handler::sql))
1055            .route(
1056                "/sql/parse",
1057                routing::get(handler::sql_parse).post(handler::sql_parse),
1058            )
1059            .route(
1060                "/sql/format",
1061                routing::get(handler::sql_format).post(handler::sql_format),
1062            )
1063            .route(
1064                "/promql",
1065                routing::get(handler::promql).post(handler::promql),
1066            )
1067            .with_state(api_state)
1068    }
1069
1070    fn route_logs<S>(log_handler: LogQueryHandlerRef) -> Router<S> {
1071        Router::new()
1072            .route("/logs", routing::get(logs::logs).post(logs::logs))
1073            .with_state(log_handler)
1074    }
1075
1076    /// Route Prometheus [HTTP API].
1077    ///
1078    /// [HTTP API]: https://prometheus.io/docs/prometheus/latest/querying/api/
1079    pub fn route_prometheus<S>(prometheus_handler: PrometheusHandlerRef) -> Router<S> {
1080        Router::new()
1081            .route(
1082                "/format_query",
1083                routing::post(format_query).get(format_query),
1084            )
1085            .route("/status/buildinfo", routing::get(build_info_query))
1086            .route("/query", routing::post(instant_query).get(instant_query))
1087            .route("/query_range", routing::post(range_query).get(range_query))
1088            .route("/labels", routing::post(labels_query).get(labels_query))
1089            .route("/series", routing::post(series_query).get(series_query))
1090            .route("/parse_query", routing::post(parse_query).get(parse_query))
1091            .route(
1092                "/label/{label_name}/values",
1093                routing::get(label_values_query),
1094            )
1095            .layer(ServiceBuilder::new().layer(CompressionLayer::new()))
1096            .with_state(prometheus_handler)
1097    }
1098
1099    /// Route Prometheus remote [read] and [write] API. In other places the related modules are
1100    /// called `prom_store`.
1101    ///
1102    /// [read]: https://prometheus.io/docs/prometheus/latest/querying/remote_read_api/
1103    /// [write]: https://prometheus.io/docs/concepts/remote_write_spec/
1104    fn route_prom<S>(state: PromStoreState) -> Router<S> {
1105        Router::new()
1106            .route("/read", routing::post(prom_store::remote_read))
1107            .route("/write", routing::post(prom_store::remote_write))
1108            .with_state(state)
1109    }
1110
1111    fn route_influxdb<S>(influxdb_handler: InfluxdbLineProtocolHandlerRef) -> Router<S> {
1112        Router::new()
1113            .route("/write", routing::post(influxdb_write_v1))
1114            .route("/api/v2/write", routing::post(influxdb_write_v2))
1115            .layer(
1116                ServiceBuilder::new()
1117                    .layer(RequestDecompressionLayer::new().pass_through_unaccepted(true)),
1118            )
1119            .route("/ping", routing::get(influxdb_ping))
1120            .route("/health", routing::get(influxdb_health))
1121            .with_state(influxdb_handler)
1122    }
1123
1124    fn route_opentsdb<S>(opentsdb_handler: OpentsdbProtocolHandlerRef) -> Router<S> {
1125        Router::new()
1126            .route("/api/put", routing::post(opentsdb::put))
1127            .with_state(opentsdb_handler)
1128    }
1129
1130    fn route_otlp<S>(
1131        otlp_handler: OpenTelemetryProtocolHandlerRef,
1132        with_metric_engine: bool,
1133    ) -> Router<S> {
1134        Router::new()
1135            .route("/v1/metrics", routing::post(otlp::metrics))
1136            .route("/v1/traces", routing::post(otlp::traces))
1137            .route("/v1/logs", routing::post(otlp::logs))
1138            .layer(
1139                ServiceBuilder::new()
1140                    .layer(RequestDecompressionLayer::new().pass_through_unaccepted(true)),
1141            )
1142            .with_state(OtlpState {
1143                with_metric_engine,
1144                handler: otlp_handler,
1145            })
1146    }
1147
1148    fn route_config<S>(state: GreptimeOptionsConfigState) -> Router<S> {
1149        Router::new()
1150            .route("/config", routing::get(handler::config))
1151            .with_state(state)
1152    }
1153
1154    fn route_jaeger<S>(handler: JaegerQueryHandlerRef) -> Router<S> {
1155        Router::new()
1156            .route("/api/services", routing::get(jaeger::handle_get_services))
1157            .route(
1158                "/api/services/{service_name}/operations",
1159                routing::get(jaeger::handle_get_operations_by_service),
1160            )
1161            .route(
1162                "/api/operations",
1163                routing::get(jaeger::handle_get_operations),
1164            )
1165            .route("/api/traces", routing::get(jaeger::handle_find_traces))
1166            .route(
1167                "/api/traces/{trace_id}",
1168                routing::get(jaeger::handle_get_trace),
1169            )
1170            .with_state(handler)
1171    }
1172}
1173
1174pub const HTTP_SERVER: &str = "HTTP_SERVER";
1175
1176#[async_trait]
1177impl Server for HttpServer {
1178    async fn shutdown(&self) -> Result<()> {
1179        let mut shutdown_tx = self.shutdown_tx.lock().await;
1180        if let Some(tx) = shutdown_tx.take()
1181            && tx.send(()).is_err()
1182        {
1183            info!("Receiver dropped, the HTTP server has already exited");
1184        }
1185        info!("Shutdown HTTP server");
1186
1187        Ok(())
1188    }
1189
1190    async fn start(&mut self, listening: SocketAddr) -> Result<()> {
1191        let (tx, rx) = oneshot::channel();
1192        let serve = {
1193            let mut shutdown_tx = self.shutdown_tx.lock().await;
1194            ensure!(
1195                shutdown_tx.is_none(),
1196                AlreadyStartedSnafu { server: "HTTP" }
1197            );
1198
1199            let mut app = self.make_app();
1200            if let Some(configurator) = self.plugins.get::<HttpConfiguratorRef<()>>() {
1201                app = configurator
1202                    .configure_http(app, ())
1203                    .await
1204                    .context(OtherSnafu)?;
1205            }
1206            let app = self.build(app)?;
1207            let listener = tokio::net::TcpListener::bind(listening)
1208                .await
1209                .context(AddressBindSnafu { addr: listening })?
1210                .tap_io(|tcp_stream| {
1211                    if let Err(e) = tcp_stream.set_nodelay(true) {
1212                        error!(e; "Failed to set TCP_NODELAY on incoming connection");
1213                    }
1214                });
1215            let serve = axum::serve(listener, app.into_make_service());
1216
1217            // FIXME(yingwen): Support keepalive.
1218            // See:
1219            // - https://github.com/tokio-rs/axum/discussions/2939
1220            // - https://stackoverflow.com/questions/73069718/how-do-i-keep-alive-tokiotcpstream-in-rust
1221            // let server = axum::Server::try_bind(&listening)
1222            //     .with_context(|_| AddressBindSnafu { addr: listening })?
1223            //     .tcp_nodelay(true)
1224            //     // Enable TCP keepalive to close the dangling established connections.
1225            //     // It's configured to let the keepalive probes first send after the connection sits
1226            //     // idle for 59 minutes, and then send every 10 seconds for 6 times.
1227            //     // So the connection will be closed after roughly 1 hour.
1228            //     .tcp_keepalive(Some(Duration::from_secs(59 * 60)))
1229            //     .tcp_keepalive_interval(Some(Duration::from_secs(10)))
1230            //     .tcp_keepalive_retries(Some(6))
1231            //     .serve(app.into_make_service());
1232
1233            *shutdown_tx = Some(tx);
1234
1235            serve
1236        };
1237        let listening = serve.local_addr().context(InternalIoSnafu)?;
1238        info!("HTTP server is bound to {}", listening);
1239
1240        common_runtime::spawn_global(async move {
1241            if let Err(e) = serve
1242                .with_graceful_shutdown(rx.map(drop))
1243                .await
1244                .context(InternalIoSnafu)
1245            {
1246                error!(e; "Failed to shutdown http server");
1247            }
1248        });
1249
1250        self.bind_addr = Some(listening);
1251        Ok(())
1252    }
1253
1254    fn name(&self) -> &str {
1255        HTTP_SERVER
1256    }
1257
1258    fn bind_addr(&self) -> Option<SocketAddr> {
1259        self.bind_addr
1260    }
1261
1262    fn as_any(&self) -> &dyn std::any::Any {
1263        self
1264    }
1265}
1266
1267#[cfg(test)]
1268mod test {
1269    use std::future::pending;
1270    use std::io::Cursor;
1271    use std::sync::Arc;
1272
1273    use arrow_ipc::reader::FileReader;
1274    use arrow_schema::DataType;
1275    use axum::handler::Handler;
1276    use axum::http::StatusCode;
1277    use axum::routing::get;
1278    use common_query::Output;
1279    use common_recordbatch::RecordBatches;
1280    use datafusion_expr::LogicalPlan;
1281    use datatypes::prelude::*;
1282    use datatypes::schema::{ColumnSchema, Schema};
1283    use datatypes::vectors::{StringVector, UInt32Vector};
1284    use header::constants::GREPTIME_DB_HEADER_TIMEOUT;
1285    use query::parser::PromQuery;
1286    use query::query_engine::DescribeResult;
1287    use session::context::QueryContextRef;
1288    use sql::statements::statement::Statement;
1289    use tokio::sync::mpsc;
1290    use tokio::time::Instant;
1291
1292    use super::*;
1293    use crate::http::test_helpers::TestClient;
1294    use crate::prom_remote_write::validation::validate_label_name;
1295    use crate::query_handler::sql::SqlQueryHandler;
1296
1297    struct DummyInstance {
1298        _tx: mpsc::Sender<(String, Vec<u8>)>,
1299    }
1300
1301    #[async_trait]
1302    impl SqlQueryHandler for DummyInstance {
1303        async fn do_query(&self, _: &str, _: QueryContextRef) -> Vec<Result<Output>> {
1304            unimplemented!()
1305        }
1306
1307        async fn do_promql_query(&self, _: &PromQuery, _: QueryContextRef) -> Vec<Result<Output>> {
1308            unimplemented!()
1309        }
1310
1311        async fn do_exec_plan(
1312            &self,
1313            _stmt: Option<Statement>,
1314            _plan: LogicalPlan,
1315            _query_ctx: QueryContextRef,
1316        ) -> Result<Output> {
1317            unimplemented!()
1318        }
1319
1320        async fn do_describe(
1321            &self,
1322            _stmt: sql::statements::statement::Statement,
1323            _query_ctx: QueryContextRef,
1324        ) -> Result<Option<DescribeResult>> {
1325            unimplemented!()
1326        }
1327
1328        async fn is_valid_schema(&self, _catalog: &str, _schema: &str) -> Result<bool> {
1329            Ok(true)
1330        }
1331    }
1332
1333    fn timeout() -> DynamicTimeoutLayer {
1334        DynamicTimeoutLayer::new(Duration::from_millis(10))
1335    }
1336
1337    async fn forever() {
1338        pending().await
1339    }
1340
1341    fn make_test_app(tx: mpsc::Sender<(String, Vec<u8>)>) -> Router {
1342        make_test_app_custom(tx, HttpOptions::default())
1343    }
1344
1345    fn make_test_app_custom(tx: mpsc::Sender<(String, Vec<u8>)>, options: HttpOptions) -> Router {
1346        let instance = Arc::new(DummyInstance { _tx: tx });
1347        let server = HttpServerBuilder::new(options)
1348            .with_sql_handler(instance.clone())
1349            .build();
1350        server.build(server.make_app()).unwrap().route(
1351            "/test/timeout",
1352            get(forever.layer(ServiceBuilder::new().layer(timeout()))),
1353        )
1354    }
1355
1356    #[tokio::test]
1357    pub async fn test_cors() {
1358        // cors is on by default
1359        let (tx, _rx) = mpsc::channel(100);
1360        let app = make_test_app(tx);
1361        let client = TestClient::new(app).await;
1362
1363        let res = client.get("/health").send().await;
1364
1365        assert_eq!(res.status(), StatusCode::OK);
1366        assert_eq!(
1367            res.headers()
1368                .get(http::header::ACCESS_CONTROL_ALLOW_ORIGIN)
1369                .expect("expect cors header origin"),
1370            "*"
1371        );
1372
1373        let res = client.get("/v1/health").send().await;
1374
1375        assert_eq!(res.status(), StatusCode::OK);
1376        assert_eq!(
1377            res.headers()
1378                .get(http::header::ACCESS_CONTROL_ALLOW_ORIGIN)
1379                .expect("expect cors header origin"),
1380            "*"
1381        );
1382
1383        let res = client
1384            .options("/health")
1385            .header("Access-Control-Request-Headers", "x-greptime-auth")
1386            .header("Access-Control-Request-Method", "DELETE")
1387            .header("Origin", "https://example.com")
1388            .send()
1389            .await;
1390        assert_eq!(res.status(), StatusCode::OK);
1391        assert_eq!(
1392            res.headers()
1393                .get(http::header::ACCESS_CONTROL_ALLOW_ORIGIN)
1394                .expect("expect cors header origin"),
1395            "*"
1396        );
1397        assert_eq!(
1398            res.headers()
1399                .get(http::header::ACCESS_CONTROL_ALLOW_HEADERS)
1400                .expect("expect cors header headers"),
1401            "*"
1402        );
1403        assert_eq!(
1404            res.headers()
1405                .get(http::header::ACCESS_CONTROL_ALLOW_METHODS)
1406                .expect("expect cors header methods"),
1407            "GET,POST,PUT,DELETE,HEAD"
1408        );
1409    }
1410
1411    #[tokio::test]
1412    pub async fn test_cors_custom_origins() {
1413        // cors is on by default
1414        let (tx, _rx) = mpsc::channel(100);
1415        let origin = "https://example.com";
1416
1417        let options = HttpOptions {
1418            cors_allowed_origins: vec![origin.to_string()],
1419            ..Default::default()
1420        };
1421
1422        let app = make_test_app_custom(tx, options);
1423        let client = TestClient::new(app).await;
1424
1425        let res = client.get("/health").header("Origin", origin).send().await;
1426
1427        assert_eq!(res.status(), StatusCode::OK);
1428        assert_eq!(
1429            res.headers()
1430                .get(http::header::ACCESS_CONTROL_ALLOW_ORIGIN)
1431                .expect("expect cors header origin"),
1432            origin
1433        );
1434
1435        let res = client
1436            .get("/health")
1437            .header("Origin", "https://notallowed.com")
1438            .send()
1439            .await;
1440
1441        assert_eq!(res.status(), StatusCode::OK);
1442        assert!(
1443            !res.headers()
1444                .contains_key(http::header::ACCESS_CONTROL_ALLOW_ORIGIN)
1445        );
1446    }
1447
1448    #[tokio::test]
1449    pub async fn test_cors_disabled() {
1450        // cors is on by default
1451        let (tx, _rx) = mpsc::channel(100);
1452
1453        let options = HttpOptions {
1454            enable_cors: false,
1455            ..Default::default()
1456        };
1457
1458        let app = make_test_app_custom(tx, options);
1459        let client = TestClient::new(app).await;
1460
1461        let res = client.get("/health").send().await;
1462
1463        assert_eq!(res.status(), StatusCode::OK);
1464        assert!(
1465            !res.headers()
1466                .contains_key(http::header::ACCESS_CONTROL_ALLOW_ORIGIN)
1467        );
1468    }
1469
1470    #[test]
1471    fn test_http_options_default() {
1472        let default = HttpOptions::default();
1473        assert_eq!("127.0.0.1:4000".to_string(), default.addr);
1474        assert_eq!(Duration::from_secs(0), default.timeout)
1475    }
1476
1477    #[tokio::test]
1478    async fn test_http_server_request_timeout() {
1479        common_telemetry::init_default_ut_logging();
1480
1481        let (tx, _rx) = mpsc::channel(100);
1482        let app = make_test_app(tx);
1483        let client = TestClient::new(app).await;
1484        let res = client.get("/test/timeout").send().await;
1485        assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
1486
1487        let now = Instant::now();
1488        let res = client
1489            .get("/test/timeout")
1490            .header(GREPTIME_DB_HEADER_TIMEOUT, "20ms")
1491            .send()
1492            .await;
1493        assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
1494        let elapsed = now.elapsed();
1495        assert!(elapsed > Duration::from_millis(15));
1496
1497        tokio::time::timeout(
1498            Duration::from_millis(15),
1499            client
1500                .get("/test/timeout")
1501                .header(GREPTIME_DB_HEADER_TIMEOUT, "0s")
1502                .send(),
1503        )
1504        .await
1505        .unwrap_err();
1506
1507        tokio::time::timeout(
1508            Duration::from_millis(15),
1509            client
1510                .get("/test/timeout")
1511                .header(
1512                    GREPTIME_DB_HEADER_TIMEOUT,
1513                    humantime::format_duration(Duration::default()).to_string(),
1514                )
1515                .send(),
1516        )
1517        .await
1518        .unwrap_err();
1519    }
1520
1521    #[tokio::test]
1522    async fn test_schema_for_empty_response() {
1523        let column_schemas = vec![
1524            ColumnSchema::new("numbers", ConcreteDataType::uint32_datatype(), false),
1525            ColumnSchema::new("strings", ConcreteDataType::string_datatype(), true),
1526        ];
1527        let schema = Arc::new(Schema::new(column_schemas));
1528
1529        let recordbatches = RecordBatches::try_new(schema.clone(), vec![]).unwrap();
1530        let outputs = vec![Ok(Output::new_with_record_batches(recordbatches))];
1531
1532        let json_resp = GreptimedbV1Response::from_output(outputs).await;
1533        if let HttpResponse::GreptimedbV1(json_resp) = json_resp {
1534            let json_output = &json_resp.output[0];
1535            if let GreptimeQueryOutput::Records(r) = json_output {
1536                assert_eq!(r.num_rows(), 0);
1537                assert_eq!(r.num_cols(), 2);
1538                assert_eq!(r.schema.column_schemas[0].name, "numbers");
1539                assert_eq!(r.schema.column_schemas[0].data_type, "UInt32");
1540            } else {
1541                panic!("invalid output type");
1542            }
1543        } else {
1544            panic!("invalid format")
1545        }
1546    }
1547
1548    #[tokio::test]
1549    async fn test_recordbatches_conversion() {
1550        let column_schemas = vec![
1551            ColumnSchema::new("numbers", ConcreteDataType::uint32_datatype(), false),
1552            ColumnSchema::new("strings", ConcreteDataType::string_datatype(), true),
1553        ];
1554        let schema = Arc::new(Schema::new(column_schemas));
1555        let columns: Vec<VectorRef> = vec![
1556            Arc::new(UInt32Vector::from_slice(vec![1, 2, 3, 4])),
1557            Arc::new(StringVector::from(vec![
1558                None,
1559                Some("hello"),
1560                Some("greptime"),
1561                None,
1562            ])),
1563        ];
1564        let recordbatch = RecordBatch::new(schema.clone(), columns).unwrap();
1565
1566        for format in [
1567            ResponseFormat::GreptimedbV1,
1568            ResponseFormat::InfluxdbV1,
1569            ResponseFormat::Csv(true, true),
1570            ResponseFormat::Table,
1571            ResponseFormat::Arrow,
1572            ResponseFormat::Json,
1573            ResponseFormat::Null,
1574        ] {
1575            let recordbatches =
1576                RecordBatches::try_new(schema.clone(), vec![recordbatch.clone()]).unwrap();
1577            let outputs = vec![Ok(Output::new_with_record_batches(recordbatches))];
1578            let json_resp = match format {
1579                ResponseFormat::Arrow => ArrowResponse::from_output(outputs, None).await,
1580                ResponseFormat::Csv(with_names, with_types) => {
1581                    CsvResponse::from_output(outputs, with_names, with_types).await
1582                }
1583                ResponseFormat::Table => TableResponse::from_output(outputs).await,
1584                ResponseFormat::GreptimedbV1 => GreptimedbV1Response::from_output(outputs).await,
1585                ResponseFormat::InfluxdbV1 => InfluxdbV1Response::from_output(outputs, None).await,
1586                ResponseFormat::Json => JsonResponse::from_output(outputs).await,
1587                ResponseFormat::Null => NullResponse::from_output(outputs).await,
1588            };
1589
1590            match json_resp {
1591                HttpResponse::GreptimedbV1(resp) => {
1592                    let json_output = &resp.output[0];
1593                    if let GreptimeQueryOutput::Records(r) = json_output {
1594                        assert_eq!(r.num_rows(), 4);
1595                        assert_eq!(r.num_cols(), 2);
1596                        assert_eq!(r.schema.column_schemas[0].name, "numbers");
1597                        assert_eq!(r.schema.column_schemas[0].data_type, "UInt32");
1598                        assert_eq!(r.rows[0][0], serde_json::Value::from(1));
1599                        assert_eq!(r.rows[0][1], serde_json::Value::Null);
1600                    } else {
1601                        panic!("invalid output type");
1602                    }
1603                }
1604                HttpResponse::InfluxdbV1(resp) => {
1605                    let json_output = &resp.results()[0];
1606                    assert_eq!(json_output.num_rows(), 4);
1607                    assert_eq!(json_output.num_cols(), 2);
1608                    assert_eq!(json_output.series[0].columns.clone()[0], "numbers");
1609                    assert_eq!(
1610                        json_output.series[0].values[0][0],
1611                        serde_json::Value::from(1)
1612                    );
1613                    assert_eq!(json_output.series[0].values[0][1], serde_json::Value::Null);
1614                }
1615                HttpResponse::Csv(resp) => {
1616                    let output = &resp.output()[0];
1617                    if let GreptimeQueryOutput::Records(r) = output {
1618                        assert_eq!(r.num_rows(), 4);
1619                        assert_eq!(r.num_cols(), 2);
1620                        assert_eq!(r.schema.column_schemas[0].name, "numbers");
1621                        assert_eq!(r.schema.column_schemas[0].data_type, "UInt32");
1622                        assert_eq!(r.rows[0][0], serde_json::Value::from(1));
1623                        assert_eq!(r.rows[0][1], serde_json::Value::Null);
1624                    } else {
1625                        panic!("invalid output type");
1626                    }
1627                }
1628
1629                HttpResponse::Table(resp) => {
1630                    let output = &resp.output()[0];
1631                    if let GreptimeQueryOutput::Records(r) = output {
1632                        assert_eq!(r.num_rows(), 4);
1633                        assert_eq!(r.num_cols(), 2);
1634                        assert_eq!(r.schema.column_schemas[0].name, "numbers");
1635                        assert_eq!(r.schema.column_schemas[0].data_type, "UInt32");
1636                        assert_eq!(r.rows[0][0], serde_json::Value::from(1));
1637                        assert_eq!(r.rows[0][1], serde_json::Value::Null);
1638                    } else {
1639                        panic!("invalid output type");
1640                    }
1641                }
1642
1643                HttpResponse::Arrow(resp) => {
1644                    let output = resp.data;
1645                    let mut reader =
1646                        FileReader::try_new(Cursor::new(output), None).expect("Arrow reader error");
1647                    let schema = reader.schema();
1648                    assert_eq!(schema.fields[0].name(), "numbers");
1649                    assert_eq!(schema.fields[0].data_type(), &DataType::UInt32);
1650                    assert_eq!(schema.fields[1].name(), "strings");
1651                    assert_eq!(schema.fields[1].data_type(), &DataType::Utf8);
1652
1653                    let rb = reader.next().unwrap().expect("read record batch failed");
1654                    assert_eq!(rb.num_columns(), 2);
1655                    assert_eq!(rb.num_rows(), 4);
1656                }
1657
1658                HttpResponse::Json(resp) => {
1659                    let output = &resp.output()[0];
1660                    if let GreptimeQueryOutput::Records(r) = output {
1661                        assert_eq!(r.num_rows(), 4);
1662                        assert_eq!(r.num_cols(), 2);
1663                        assert_eq!(r.schema.column_schemas[0].name, "numbers");
1664                        assert_eq!(r.schema.column_schemas[0].data_type, "UInt32");
1665                        assert_eq!(r.rows[0][0], serde_json::Value::from(1));
1666                        assert_eq!(r.rows[0][1], serde_json::Value::Null);
1667                    } else {
1668                        panic!("invalid output type");
1669                    }
1670                }
1671
1672                HttpResponse::Null(resp) => {
1673                    assert_eq!(resp.rows(), 4);
1674                }
1675
1676                HttpResponse::Error(err) => unreachable!("{err:?}"),
1677            }
1678        }
1679    }
1680
1681    #[test]
1682    fn test_response_format_misc() {
1683        assert_eq!(ResponseFormat::default(), ResponseFormat::GreptimedbV1);
1684        assert_eq!(ResponseFormat::parse("arrow"), Some(ResponseFormat::Arrow));
1685        assert_eq!(
1686            ResponseFormat::parse("csv"),
1687            Some(ResponseFormat::Csv(false, false))
1688        );
1689        assert_eq!(
1690            ResponseFormat::parse("csvwithnames"),
1691            Some(ResponseFormat::Csv(true, false))
1692        );
1693        assert_eq!(
1694            ResponseFormat::parse("csvwithnamesandtypes"),
1695            Some(ResponseFormat::Csv(true, true))
1696        );
1697        assert_eq!(ResponseFormat::parse("table"), Some(ResponseFormat::Table));
1698        assert_eq!(
1699            ResponseFormat::parse("greptimedb_v1"),
1700            Some(ResponseFormat::GreptimedbV1)
1701        );
1702        assert_eq!(
1703            ResponseFormat::parse("influxdb_v1"),
1704            Some(ResponseFormat::InfluxdbV1)
1705        );
1706        assert_eq!(ResponseFormat::parse("json"), Some(ResponseFormat::Json));
1707        assert_eq!(ResponseFormat::parse("null"), Some(ResponseFormat::Null));
1708
1709        // invalid formats
1710        assert_eq!(ResponseFormat::parse("invalid"), None);
1711        assert_eq!(ResponseFormat::parse(""), None);
1712        assert_eq!(ResponseFormat::parse("CSV"), None); // Case sensitive
1713
1714        // as str
1715        assert_eq!(ResponseFormat::Arrow.as_str(), "arrow");
1716        assert_eq!(ResponseFormat::Csv(false, false).as_str(), "csv");
1717        assert_eq!(ResponseFormat::Csv(true, true).as_str(), "csv");
1718        assert_eq!(ResponseFormat::Table.as_str(), "table");
1719        assert_eq!(ResponseFormat::GreptimedbV1.as_str(), "greptimedb_v1");
1720        assert_eq!(ResponseFormat::InfluxdbV1.as_str(), "influxdb_v1");
1721        assert_eq!(ResponseFormat::Json.as_str(), "json");
1722        assert_eq!(ResponseFormat::Null.as_str(), "null");
1723        assert_eq!(ResponseFormat::default().as_str(), "greptimedb_v1");
1724    }
1725
1726    #[test]
1727    fn test_decode_label_name_strict() {
1728        let strict = PromValidationMode::Strict;
1729
1730        // Valid Prometheus label names
1731        assert!(strict.decode_label_name(b"__name__").is_ok());
1732        assert!(strict.decode_label_name(b"job").is_ok());
1733        assert!(strict.decode_label_name(b"instance").is_ok());
1734        assert!(strict.decode_label_name(b"_private").is_ok());
1735        assert!(strict.decode_label_name(b"label_with_underscores").is_ok());
1736        assert!(strict.decode_label_name(b"abc123").is_ok());
1737        assert!(strict.decode_label_name(b"A").is_ok());
1738        assert!(strict.decode_label_name(b"_").is_ok());
1739
1740        // Invalid: starts with digit
1741        assert!(strict.decode_label_name(b"0abc").is_err());
1742        assert!(strict.decode_label_name(b"123").is_err());
1743
1744        // Invalid: contains special characters
1745        assert!(strict.decode_label_name(b"label-name").is_err());
1746        assert!(strict.decode_label_name(b"label.name").is_err());
1747        assert!(strict.decode_label_name(b"label name").is_err());
1748        assert!(strict.decode_label_name(b"label/name").is_err());
1749
1750        // Invalid: empty
1751        assert!(strict.decode_label_name(b"").is_err());
1752
1753        // Invalid: non-ASCII UTF-8
1754        assert!(strict.decode_label_name("ラベル".as_bytes()).is_err());
1755
1756        // Invalid UTF-8 bytes should fail
1757        assert!(strict.decode_label_name(&[0xff, 0xfe]).is_err());
1758    }
1759
1760    #[test]
1761    fn test_decode_label_name_lossy() {
1762        let lossy = PromValidationMode::Lossy;
1763
1764        // Label name validation is always enforced.
1765        assert!(lossy.decode_label_name(b"__name__").is_ok());
1766        assert!(lossy.decode_label_name(b"label-name").is_err());
1767        assert!(lossy.decode_label_name(b"0abc").is_err());
1768
1769        // Invalid UTF-8 bytes fail the label-name byte check.
1770        assert!(lossy.decode_label_name(&[0xff, 0xfe]).is_err());
1771    }
1772
1773    #[test]
1774    fn test_decode_label_name_unchecked() {
1775        let unchecked = PromValidationMode::Unchecked;
1776
1777        // Label name validation is always enforced.
1778        assert!(unchecked.decode_label_name(b"__name__").is_ok());
1779        assert!(unchecked.decode_label_name(b"label-name").is_err());
1780        assert!(unchecked.decode_label_name(b"0abc").is_err());
1781    }
1782
1783    #[test]
1784    fn test_is_valid_prom_label_name_bytes() {
1785        assert!(validate_label_name(b"__name__"));
1786        assert!(validate_label_name(b"job"));
1787        assert!(validate_label_name(b"_"));
1788        assert!(validate_label_name(b"A"));
1789        assert!(validate_label_name(b"abc123"));
1790        assert!(validate_label_name(b"_leading_underscore"));
1791
1792        assert!(!validate_label_name(b""));
1793        assert!(!validate_label_name(b"0starts_with_digit"));
1794        assert!(!validate_label_name(b"has-dash"));
1795        assert!(!validate_label_name(b"has.dot"));
1796        assert!(!validate_label_name(b"has space"));
1797        assert!(!validate_label_name(&[0xff, 0xfe]));
1798    }
1799}