diff --git a/src/cmd/src/cli/database.rs b/src/cmd/src/cli/database.rs index eb5647699ef0..554d8083bc5b 100644 --- a/src/cmd/src/cli/database.rs +++ b/src/cmd/src/cli/database.rs @@ -16,7 +16,7 @@ use base64::engine::general_purpose; use base64::Engine; use common_catalog::consts::{DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME}; use serde_json::Value; -use servers::http::greptime_result_v1::GreptimedbV1Response; +use servers::http::result::greptime_result_v1::GreptimedbV1Response; use servers::http::GreptimeQueryOutput; use snafu::ResultExt; diff --git a/src/servers/src/error.rs b/src/servers/src/error.rs index 565b26dccf51..36c94d6c6283 100644 --- a/src/servers/src/error.rs +++ b/src/servers/src/error.rs @@ -16,6 +16,7 @@ use std::any::Any; use std::net::SocketAddr; use std::string::FromUtf8Error; +use axum::http::StatusCode as HttpStatusCode; use axum::response::{IntoResponse, Response}; use axum::{http, Json}; use base64::DecodeError; @@ -30,8 +31,6 @@ use query::parser::PromQuery; use serde_json::json; use snafu::{Location, Snafu}; -use crate::http::error_result::status_code_to_http_status; - #[derive(Snafu)] #[snafu(visibility(pub))] #[stack_trace_debug] @@ -711,3 +710,49 @@ impl IntoResponse for Error { (status, body).into_response() } } + +pub fn status_code_to_http_status(status_code: &StatusCode) -> HttpStatusCode { + match status_code { + StatusCode::Success | StatusCode::Cancelled => HttpStatusCode::OK, + + StatusCode::Unsupported + | StatusCode::InvalidArguments + | StatusCode::InvalidSyntax + | StatusCode::RequestOutdated + | StatusCode::RegionAlreadyExists + | StatusCode::TableColumnExists + | StatusCode::TableAlreadyExists + | StatusCode::RegionNotFound + | StatusCode::DatabaseNotFound + | StatusCode::TableNotFound + | StatusCode::TableColumnNotFound + | StatusCode::PlanQuery + | StatusCode::DatabaseAlreadyExists + | StatusCode::FlowNotFound + | StatusCode::FlowAlreadyExists => HttpStatusCode::BAD_REQUEST, + + StatusCode::AuthHeaderNotFound + | StatusCode::InvalidAuthHeader + | StatusCode::UserNotFound + | StatusCode::UnsupportedPasswordType + | StatusCode::UserPasswordMismatch + | StatusCode::RegionReadonly => HttpStatusCode::UNAUTHORIZED, + + StatusCode::PermissionDenied | StatusCode::AccessDenied => HttpStatusCode::FORBIDDEN, + + StatusCode::RateLimited => HttpStatusCode::TOO_MANY_REQUESTS, + + StatusCode::RegionNotReady + | StatusCode::TableUnavailable + | StatusCode::RegionBusy + | StatusCode::StorageUnavailable + | StatusCode::External => HttpStatusCode::SERVICE_UNAVAILABLE, + + StatusCode::Internal + | StatusCode::Unexpected + | StatusCode::IllegalState + | StatusCode::Unknown + | StatusCode::RuntimeResourcesExhausted + | StatusCode::EngineExecuteQuery => HttpStatusCode::INTERNAL_SERVER_ERROR, + } +} diff --git a/src/servers/src/http.rs b/src/servers/src/http.rs index d02eb86930c9..44a713c734bf 100644 --- a/src/servers/src/http.rs +++ b/src/servers/src/http.rs @@ -51,20 +51,20 @@ use tower_http::decompression::RequestDecompressionLayer; use tower_http::trace::TraceLayer; use self::authorize::AuthState; -use self::table_result::TableResponse; +use self::result::table_result::TableResponse; use crate::configurator::ConfiguratorRef; use crate::error::{AddressBindSnafu, AlreadyStartedSnafu, Error, HyperSnafu, Result, ToJsonSnafu}; -use crate::http::arrow_result::ArrowResponse; -use crate::http::csv_result::CsvResponse; -use crate::http::error_result::ErrorResponse; -use crate::http::greptime_result_v1::GreptimedbV1Response; use crate::http::influxdb::{influxdb_health, influxdb_ping, influxdb_write_v1, influxdb_write_v2}; -use crate::http::influxdb_result_v1::InfluxdbV1Response; -use crate::http::json_result::JsonResponse; use crate::http::prometheus::{ build_info_query, format_query, instant_query, label_values_query, labels_query, parse_query, range_query, series_query, }; +use crate::http::result::arrow_result::ArrowResponse; +use crate::http::result::csv_result::CsvResponse; +use crate::http::result::error_result::ErrorResponse; +use crate::http::result::greptime_result_v1::GreptimedbV1Response; +use crate::http::result::influxdb_result_v1::InfluxdbV1Response; +use crate::http::result::json_result::JsonResponse; use crate::interceptor::LogIngestInterceptorRef; use crate::metrics::http_metrics_layer; use crate::metrics_handler::MetricsHandler; @@ -77,6 +77,8 @@ use crate::query_handler::{ use crate::server::Server; pub mod authorize; +#[cfg(feature = "dashboard")] +mod dashboard; pub mod dyn_log; pub mod event; pub mod handler; @@ -88,20 +90,9 @@ pub mod otlp; pub mod pprof; pub mod prom_store; pub mod prometheus; -mod prometheus_resp; +pub mod result; pub mod script; -pub mod arrow_result; -pub mod csv_result; -#[cfg(feature = "dashboard")] -mod dashboard; -pub mod error_result; -pub mod greptime_manage_resp; -pub mod greptime_result_v1; -pub mod influxdb_result_v1; -pub mod json_result; -pub mod table_result; - #[cfg(any(test, feature = "testing"))] pub mod test_helpers; diff --git a/src/servers/src/http/authorize.rs b/src/servers/src/http/authorize.rs index 7578898c544a..7aee9fa9b431 100644 --- a/src/servers/src/http/authorize.rs +++ b/src/servers/src/http/authorize.rs @@ -36,7 +36,7 @@ use crate::error::{ self, InvalidAuthHeaderInvisibleASCIISnafu, InvalidAuthHeaderSnafu, InvalidParameterSnafu, NotFoundInfluxAuthSnafu, Result, UnsupportedAuthSchemeSnafu, UrlDecodeSnafu, }; -use crate::http::error_result::ErrorResponse; +use crate::http::result::error_result::ErrorResponse; use crate::http::HTTP_API_PREFIX; use crate::influxdb::{is_influxdb_request, is_influxdb_v2_request}; diff --git a/src/servers/src/http/event.rs b/src/servers/src/http/event.rs index 2c0563a92fc2..16d55a7a1a7c 100644 --- a/src/servers/src/http/event.rs +++ b/src/servers/src/http/event.rs @@ -47,8 +47,8 @@ use crate::error::{ DecodeOtlpRequestSnafu, Error, InvalidParameterSnafu, ParseJson5Snafu, ParseJsonSnafu, PipelineSnafu, Result, UnsupportedContentTypeSnafu, }; -use crate::http::greptime_manage_resp::GreptimedbManageResponse; -use crate::http::greptime_result_v1::GreptimedbV1Response; +use crate::http::result::greptime_manage_resp::GreptimedbManageResponse; +use crate::http::result::greptime_result_v1::GreptimedbV1Response; use crate::http::HttpResponse; use crate::interceptor::{LogIngestInterceptor, LogIngestInterceptorRef}; use crate::metrics::{ diff --git a/src/servers/src/http/handler.rs b/src/servers/src/http/handler.rs index 4925c79639ce..c27594014829 100644 --- a/src/servers/src/http/handler.rs +++ b/src/servers/src/http/handler.rs @@ -34,13 +34,13 @@ use serde_json::Value; use session::context::{Channel, QueryContext, QueryContextRef}; use super::header::collect_plan_metrics; -use crate::http::arrow_result::ArrowResponse; -use crate::http::csv_result::CsvResponse; -use crate::http::error_result::ErrorResponse; -use crate::http::greptime_result_v1::GreptimedbV1Response; -use crate::http::influxdb_result_v1::InfluxdbV1Response; -use crate::http::json_result::JsonResponse; -use crate::http::table_result::TableResponse; +use crate::http::result::arrow_result::ArrowResponse; +use crate::http::result::csv_result::CsvResponse; +use crate::http::result::error_result::ErrorResponse; +use crate::http::result::greptime_result_v1::GreptimedbV1Response; +use crate::http::result::influxdb_result_v1::InfluxdbV1Response; +use crate::http::result::json_result::JsonResponse; +use crate::http::result::table_result::TableResponse; use crate::http::{ ApiState, Epoch, GreptimeOptionsConfigState, GreptimeQueryOutput, HttpRecordsOutput, HttpResponse, ResponseFormat, diff --git a/src/servers/src/http/prometheus.rs b/src/servers/src/http/prometheus.rs index 6f749f259508..684845e69abc 100644 --- a/src/servers/src/http/prometheus.rs +++ b/src/servers/src/http/prometheus.rs @@ -45,7 +45,7 @@ use serde_json::Value; use session::context::QueryContext; use snafu::{Location, OptionExt, ResultExt}; -pub use super::prometheus_resp::PrometheusJsonResponse; +pub use super::result::prometheus_resp::PrometheusJsonResponse; use crate::error::{ CatalogSnafu, CollectRecordbatchSnafu, Error, InvalidQuerySnafu, Result, TableNotFoundSnafu, UnexpectedResultSnafu, diff --git a/src/servers/src/http/result.rs b/src/servers/src/http/result.rs new file mode 100644 index 000000000000..8fd4df85768a --- /dev/null +++ b/src/servers/src/http/result.rs @@ -0,0 +1,23 @@ +// Copyright 2023 Greptime Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub(crate) mod arrow_result; +pub(crate) mod csv_result; +pub mod error_result; +pub(crate) mod greptime_manage_resp; +pub mod greptime_result_v1; +pub mod influxdb_result_v1; +pub(crate) mod json_result; +pub(crate) mod prometheus_resp; +pub(crate) mod table_result; diff --git a/src/servers/src/http/arrow_result.rs b/src/servers/src/http/result/arrow_result.rs similarity index 99% rename from src/servers/src/http/arrow_result.rs rename to src/servers/src/http/result/arrow_result.rs index 6a739fee0464..e6e9abe6d398 100644 --- a/src/servers/src/http/arrow_result.rs +++ b/src/servers/src/http/result/arrow_result.rs @@ -29,8 +29,8 @@ use serde::{Deserialize, Serialize}; use snafu::ResultExt; use crate::error::{self, Error}; -use crate::http::error_result::ErrorResponse; use crate::http::header::{GREPTIME_DB_HEADER_EXECUTION_TIME, GREPTIME_DB_HEADER_FORMAT}; +use crate::http::result::error_result::ErrorResponse; use crate::http::{HttpResponse, ResponseFormat}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] diff --git a/src/servers/src/http/csv_result.rs b/src/servers/src/http/result/csv_result.rs similarity index 95% rename from src/servers/src/http/csv_result.rs rename to src/servers/src/http/result/csv_result.rs index d6b512653bde..4f78eb8da7b5 100644 --- a/src/servers/src/http/csv_result.rs +++ b/src/servers/src/http/result/csv_result.rs @@ -23,10 +23,10 @@ use mime_guess::mime; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use super::process_with_limit; -use crate::http::error_result::ErrorResponse; use crate::http::header::{GREPTIME_DB_HEADER_EXECUTION_TIME, GREPTIME_DB_HEADER_FORMAT}; -use crate::http::{handler, GreptimeQueryOutput, HttpResponse, ResponseFormat}; +// use super::process_with_limit; +use crate::http::result::error_result::ErrorResponse; +use crate::http::{handler, process_with_limit, GreptimeQueryOutput, HttpResponse, ResponseFormat}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] pub struct CsvResponse { diff --git a/src/servers/src/http/error_result.rs b/src/servers/src/http/result/error_result.rs similarity index 60% rename from src/servers/src/http/error_result.rs rename to src/servers/src/http/result/error_result.rs index 40766d2cbf89..bbc488e56250 100644 --- a/src/servers/src/http/error_result.rs +++ b/src/servers/src/http/result/error_result.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use axum::http::{HeaderValue, StatusCode as HttpStatusCode}; +use axum::http::HeaderValue; use axum::response::{IntoResponse, Response}; use axum::Json; use common_error::ext::ErrorExt; @@ -21,6 +21,7 @@ use common_telemetry::{debug, error}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; +use crate::error::status_code_to_http_status; use crate::http::header::constants::GREPTIME_DB_HEADER_ERROR_CODE; use crate::http::header::GREPTIME_DB_HEADER_EXECUTION_TIME; @@ -87,49 +88,3 @@ impl IntoResponse for ErrorResponse { (status_code, resp).into_response() } } - -pub fn status_code_to_http_status(status_code: &StatusCode) -> HttpStatusCode { - match status_code { - StatusCode::Success | StatusCode::Cancelled => HttpStatusCode::OK, - - StatusCode::Unsupported - | StatusCode::InvalidArguments - | StatusCode::InvalidSyntax - | StatusCode::RequestOutdated - | StatusCode::RegionAlreadyExists - | StatusCode::TableColumnExists - | StatusCode::TableAlreadyExists - | StatusCode::RegionNotFound - | StatusCode::DatabaseNotFound - | StatusCode::TableNotFound - | StatusCode::TableColumnNotFound - | StatusCode::PlanQuery - | StatusCode::DatabaseAlreadyExists - | StatusCode::FlowNotFound - | StatusCode::FlowAlreadyExists => HttpStatusCode::BAD_REQUEST, - - StatusCode::AuthHeaderNotFound - | StatusCode::InvalidAuthHeader - | StatusCode::UserNotFound - | StatusCode::UnsupportedPasswordType - | StatusCode::UserPasswordMismatch - | StatusCode::RegionReadonly => HttpStatusCode::UNAUTHORIZED, - - StatusCode::PermissionDenied | StatusCode::AccessDenied => HttpStatusCode::FORBIDDEN, - - StatusCode::RateLimited => HttpStatusCode::TOO_MANY_REQUESTS, - - StatusCode::RegionNotReady - | StatusCode::TableUnavailable - | StatusCode::RegionBusy - | StatusCode::StorageUnavailable - | StatusCode::External => HttpStatusCode::SERVICE_UNAVAILABLE, - - StatusCode::Internal - | StatusCode::Unexpected - | StatusCode::IllegalState - | StatusCode::Unknown - | StatusCode::RuntimeResourcesExhausted - | StatusCode::EngineExecuteQuery => HttpStatusCode::INTERNAL_SERVER_ERROR, - } -} diff --git a/src/servers/src/http/greptime_manage_resp.rs b/src/servers/src/http/result/greptime_manage_resp.rs similarity index 100% rename from src/servers/src/http/greptime_manage_resp.rs rename to src/servers/src/http/result/greptime_manage_resp.rs diff --git a/src/servers/src/http/greptime_result_v1.rs b/src/servers/src/http/result/greptime_result_v1.rs similarity index 92% rename from src/servers/src/http/greptime_result_v1.rs rename to src/servers/src/http/result/greptime_result_v1.rs index de8284c40a00..ba90b6b329b7 100644 --- a/src/servers/src/http/greptime_result_v1.rs +++ b/src/servers/src/http/result/greptime_result_v1.rs @@ -22,10 +22,10 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use serde_json::Value; -use super::header::GREPTIME_DB_HEADER_METRICS; -use super::process_with_limit; -use crate::http::header::{GREPTIME_DB_HEADER_EXECUTION_TIME, GREPTIME_DB_HEADER_FORMAT}; -use crate::http::{handler, GreptimeQueryOutput, HttpResponse, ResponseFormat}; +use crate::http::header::{ + GREPTIME_DB_HEADER_EXECUTION_TIME, GREPTIME_DB_HEADER_FORMAT, GREPTIME_DB_HEADER_METRICS, +}; +use crate::http::{handler, process_with_limit, GreptimeQueryOutput, HttpResponse, ResponseFormat}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] pub struct GreptimedbV1Response { diff --git a/src/servers/src/http/influxdb_result_v1.rs b/src/servers/src/http/result/influxdb_result_v1.rs similarity index 99% rename from src/servers/src/http/influxdb_result_v1.rs rename to src/servers/src/http/result/influxdb_result_v1.rs index 3fa4594728d2..ae65b8fa0acb 100644 --- a/src/servers/src/http/influxdb_result_v1.rs +++ b/src/servers/src/http/result/influxdb_result_v1.rs @@ -23,8 +23,8 @@ use serde_json::Value; use snafu::ResultExt; use crate::error::{Error, ToJsonSnafu}; -use crate::http::error_result::ErrorResponse; use crate::http::header::{GREPTIME_DB_HEADER_EXECUTION_TIME, GREPTIME_DB_HEADER_FORMAT}; +use crate::http::result::error_result::ErrorResponse; use crate::http::{Epoch, HttpResponse, ResponseFormat}; #[derive(Debug, Default, Serialize, Deserialize, JsonSchema)] diff --git a/src/servers/src/http/json_result.rs b/src/servers/src/http/result/json_result.rs similarity index 96% rename from src/servers/src/http/json_result.rs rename to src/servers/src/http/result/json_result.rs index bf4e4d77704c..71546c570d8a 100644 --- a/src/servers/src/http/json_result.rs +++ b/src/servers/src/http/result/json_result.rs @@ -21,10 +21,9 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use serde_json::{json, Map, Value}; -use super::process_with_limit; -use crate::http::error_result::ErrorResponse; use crate::http::header::{GREPTIME_DB_HEADER_EXECUTION_TIME, GREPTIME_DB_HEADER_FORMAT}; -use crate::http::{handler, GreptimeQueryOutput, HttpResponse, ResponseFormat}; +use crate::http::result::error_result::ErrorResponse; +use crate::http::{handler, process_with_limit, GreptimeQueryOutput, HttpResponse, ResponseFormat}; /// The json format here is different from the default json output of `GreptimedbV1` result. /// `JsonResponse` is intended to make it easier for user to consume data. diff --git a/src/servers/src/http/prometheus_resp.rs b/src/servers/src/http/result/prometheus_resp.rs similarity index 98% rename from src/servers/src/http/prometheus_resp.rs rename to src/servers/src/http/result/prometheus_resp.rs index 3ec4552696ea..84497c1533df 100644 --- a/src/servers/src/http/prometheus_resp.rs +++ b/src/servers/src/http/result/prometheus_resp.rs @@ -32,12 +32,13 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use snafu::{OptionExt, ResultExt}; -use super::header::{collect_plan_metrics, GREPTIME_DB_HEADER_METRICS}; -use super::prometheus::{ +use crate::error::{ + status_code_to_http_status, CollectRecordbatchSnafu, Result, UnexpectedResultSnafu, +}; +use crate::http::header::{collect_plan_metrics, GREPTIME_DB_HEADER_METRICS}; +use crate::http::prometheus::{ PromData, PromQueryResult, PromSeriesMatrix, PromSeriesVector, PrometheusResponse, }; -use crate::error::{CollectRecordbatchSnafu, Result, UnexpectedResultSnafu}; -use crate::http::error_result::status_code_to_http_status; #[derive(Debug, Default, Serialize, Deserialize, JsonSchema, PartialEq)] pub struct PrometheusJsonResponse { diff --git a/src/servers/src/http/table_result.rs b/src/servers/src/http/result/table_result.rs similarity index 97% rename from src/servers/src/http/table_result.rs rename to src/servers/src/http/result/table_result.rs index dacef51beace..9a69b08c0a5e 100644 --- a/src/servers/src/http/table_result.rs +++ b/src/servers/src/http/result/table_result.rs @@ -24,10 +24,9 @@ use mime_guess::mime; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use super::process_with_limit; -use crate::http::error_result::ErrorResponse; use crate::http::header::{GREPTIME_DB_HEADER_EXECUTION_TIME, GREPTIME_DB_HEADER_FORMAT}; -use crate::http::{handler, GreptimeQueryOutput, HttpResponse, ResponseFormat}; +use crate::http::result::error_result::ErrorResponse; +use crate::http::{handler, process_with_limit, GreptimeQueryOutput, HttpResponse, ResponseFormat}; #[derive(Serialize, Deserialize, Debug, JsonSchema)] pub struct TableResponse { diff --git a/src/servers/src/http/script.rs b/src/servers/src/http/script.rs index cb8ca7fc2b93..278e54457a85 100644 --- a/src/servers/src/http/script.rs +++ b/src/servers/src/http/script.rs @@ -26,7 +26,7 @@ use session::context::QueryContext; use snafu::ResultExt; use crate::error::{HyperSnafu, InvalidUtf8ValueSnafu}; -use crate::http::error_result::ErrorResponse; +use crate::http::result::error_result::ErrorResponse; use crate::http::{ApiState, GreptimedbV1Response, HttpResponse}; macro_rules! json_err { diff --git a/tests-integration/tests/http.rs b/tests-integration/tests/http.rs index f5a1080d768a..b3446c72bf3d 100644 --- a/tests-integration/tests/http.rs +++ b/tests-integration/tests/http.rs @@ -27,12 +27,12 @@ use opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest; use opentelemetry_proto::tonic::metrics::v1::ResourceMetrics; use prost::Message; use serde_json::{json, Value}; -use servers::http::error_result::ErrorResponse; -use servers::http::greptime_result_v1::GreptimedbV1Response; use servers::http::handler::HealthResponse; use servers::http::header::{GREPTIME_DB_HEADER_NAME, GREPTIME_TIMEZONE_HEADER_NAME}; -use servers::http::influxdb_result_v1::{InfluxdbOutput, InfluxdbV1Response}; use servers::http::prometheus::{PrometheusJsonResponse, PrometheusResponse}; +use servers::http::result::error_result::ErrorResponse; +use servers::http::result::greptime_result_v1::GreptimedbV1Response; +use servers::http::result::influxdb_result_v1::{InfluxdbOutput, InfluxdbV1Response}; use servers::http::test_helpers::{TestClient, TestResponse}; use servers::http::GreptimeQueryOutput; use servers::prom_store;