feat: introduce read preference (#5783)

* feat: introduce read preference

* feat: introduce `RegionQueryHandlerFactory`

* feat: extract ReadPreference from http header

* test: add more tests

* chore: apply suggestions from CR

* chore: apply suggestions from CR
This commit is contained in:
Weny Xu
2025-04-01 17:17:01 +08:00
committed by GitHub
parent f9221e9e66
commit 4ef9afd8d8
26 changed files with 311 additions and 17 deletions

View File

@@ -46,6 +46,7 @@ common-pprof = { workspace = true, optional = true }
common-query.workspace = true
common-recordbatch.workspace = true
common-runtime.workspace = true
common-session.workspace = true
common-telemetry.workspace = true
common-time.workspace = true
common-version = { workspace = true, features = ["codec"] }

View File

@@ -103,6 +103,7 @@ mod timeout;
pub(crate) use timeout::DynamicTimeoutLayer;
mod hints;
mod read_preference;
#[cfg(any(test, feature = "testing"))]
pub mod test_helpers;
@@ -804,7 +805,10 @@ impl HttpServer {
AuthState::new(self.user_provider.clone()),
authorize::check_http_auth,
))
.layer(middleware::from_fn(hints::extract_hints)),
.layer(middleware::from_fn(hints::extract_hints))
.layer(middleware::from_fn(
read_preference::extract_read_preference,
)),
)
// Handlers for debug, we don't expect a timeout.
.nest(

View File

@@ -43,6 +43,7 @@ pub mod constants {
pub const GREPTIME_DB_HEADER_EXECUTION_TIME: &str = "x-greptime-execution-time";
pub const GREPTIME_DB_HEADER_METRICS: &str = "x-greptime-metrics";
pub const GREPTIME_DB_HEADER_NAME: &str = "x-greptime-db-name";
pub const GREPTIME_DB_HEADER_READ_PREFERENCE: &str = "x-greptime-read-preference";
pub const GREPTIME_TIMEZONE_HEADER_NAME: &str = "x-greptime-timezone";
pub const GREPTIME_DB_HEADER_ERROR_CODE: &str = common_error::GREPTIME_DB_HEADER_ERROR_CODE;
@@ -76,6 +77,10 @@ pub static GREPTIME_DB_HEADER_NAME: HeaderName =
pub static GREPTIME_TIMEZONE_HEADER_NAME: HeaderName =
HeaderName::from_static(constants::GREPTIME_TIMEZONE_HEADER_NAME);
/// Header key of query specific read preference. Example format of the header value is `leader`.
pub static GREPTIME_DB_HEADER_READ_PREFERENCE: HeaderName =
HeaderName::from_static(constants::GREPTIME_DB_HEADER_READ_PREFERENCE);
pub static CONTENT_TYPE_PROTOBUF_STR: &str = "application/x-protobuf";
pub static CONTENT_TYPE_PROTOBUF: HeaderValue = HeaderValue::from_static(CONTENT_TYPE_PROTOBUF_STR);
pub static CONTENT_ENCODING_SNAPPY: HeaderValue = HeaderValue::from_static("snappy");

View File

@@ -0,0 +1,40 @@
// 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.
use std::str::FromStr;
use axum::body::Body;
use axum::http::Request;
use axum::middleware::Next;
use axum::response::Response;
use session::context::QueryContext;
use session::ReadPreference;
use crate::http::header::GREPTIME_DB_HEADER_READ_PREFERENCE;
/// Extract read preference from the request headers.
pub async fn extract_read_preference(mut request: Request<Body>, next: Next) -> Response {
let read_preference = request
.headers()
.get(&GREPTIME_DB_HEADER_READ_PREFERENCE)
.and_then(|header| header.to_str().ok())
.and_then(|s| ReadPreference::from_str(s).ok())
.unwrap_or_default();
if let Some(query_ctx) = request.extensions_mut().get_mut::<QueryContext>() {
common_telemetry::debug!("Setting read preference to {}", read_preference);
query_ctx.set_read_preference(read_preference);
}
next.run(request).await
}