Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add request id in context #1153

Merged
merged 5 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions proxy/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use std::time::Duration;

use common_types::request_id::RequestId;
use macros::define_result;
use snafu::{ensure, Backtrace, Snafu};

Expand All @@ -42,15 +43,16 @@ define_result!(Error);
/// Context for request, may contains
/// 1. Request context and options
/// 2. Info from http headers
#[derive(Debug)]
pub struct RequestContext {
jiacai2050 marked this conversation as resolved.
Show resolved Hide resolved
/// Catalog of the request
pub catalog: String,
/// Schema of request
pub schema: String,
/// Enable partition table_access flag
pub enable_partition_table_access: bool,
/// Request timeout
pub timeout: Option<Duration>,
/// Request id
pub request_id: RequestId,
}

impl RequestContext {
Expand All @@ -63,7 +65,6 @@ impl RequestContext {
pub struct Builder {
catalog: String,
schema: String,
enable_partition_table_access: bool,
timeout: Option<Duration>,
}

Expand All @@ -78,11 +79,6 @@ impl Builder {
self
}

pub fn enable_partition_table_access(mut self, enable_partition_table_access: bool) -> Self {
self.enable_partition_table_access = enable_partition_table_access;
self
}

pub fn timeout(mut self, timeout: Option<Duration>) -> Self {
self.timeout = timeout;
self
Expand All @@ -95,8 +91,8 @@ impl Builder {
Ok(RequestContext {
catalog: self.catalog,
schema: self.schema,
enable_partition_table_access: self.enable_partition_table_access,
timeout: self.timeout,
request_id: RequestId::next_id(),
})
}
}
3 changes: 1 addition & 2 deletions proxy/src/grpc/prom_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use ceresdbproto::{
use common_types::{
datum::DatumKind,
record_batch::RecordBatch,
request_id::RequestId,
schema::{RecordSchema, TSID_COLUMN},
};
use generic_error::BoxError;
Expand Down Expand Up @@ -76,7 +75,7 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
ctx: Context,
req: PrometheusQueryRequest,
) -> Result<PrometheusQueryResponse> {
let request_id = RequestId::next_id();
let request_id = ctx.request_id;
let begin_instant = Instant::now();
let deadline = ctx.timeout.map(|t| begin_instant + t);
let req_ctx = req.context.context(ErrNoCause {
Expand Down
36 changes: 16 additions & 20 deletions proxy/src/grpc/sql_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use log::{error, warn};
use query_engine::{executor::Executor as QueryExecutor, physical_planner::PhysicalPlanner};
use router::endpoint::Endpoint;
use snafu::ResultExt;
use tokio::sync::mpsc;
use tokio::{runtime::Handle, sync::mpsc};
use tokio_stream::wrappers::ReceiverStream;
use tonic::{transport::Channel, IntoRequest};

Expand All @@ -55,10 +55,11 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
GRPC_HANDLER_COUNTER_VEC.incoming_query.inc();

self.hotspot_recorder.inc_sql_query_reqs(&req).await;
match self.handle_sql_query_internal(ctx, req).await {
match self.handle_sql_query_internal(&ctx, &req).await {
Err(e) => {
error!("Failed to handle sql query, ctx:{ctx:?}, err:{e}");

GRPC_HANDLER_COUNTER_VEC.query_failed.inc();
error!("Failed to handle sql query, err:{e}");
SqlQueryResponse {
header: Some(error::build_err_header(e)),
..Default::default()
Expand All @@ -73,8 +74,8 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {

async fn handle_sql_query_internal(
&self,
ctx: Context,
req: SqlQueryRequest,
ctx: &Context,
req: &SqlQueryRequest,
) -> Result<SqlQueryResponse> {
if req.context.is_none() {
return ErrNoCause {
Expand All @@ -86,7 +87,7 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {

let req_context = req.context.as_ref().unwrap();
let schema = &req_context.database;
match self.handle_sql(ctx, schema, &req.sql).await? {
match self.handle_sql(ctx, schema, &req.sql, false).await? {
SqlResponse::Forwarded(resp) => Ok(resp),
SqlResponse::Local(output) => convert_output(&output, self.resp_compress_min_length),
}
Expand All @@ -99,7 +100,7 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
) -> BoxStream<'static, SqlQueryResponse> {
GRPC_HANDLER_COUNTER_VEC.stream_query.inc();
self.hotspot_recorder.inc_sql_query_reqs(&req).await;
match self.clone().handle_stream_query_internal(ctx, req).await {
match self.clone().handle_stream_query_internal(&ctx, &req).await {
Err(e) => stream::once(async {
error!("Failed to handle stream sql query, err:{e}");
GRPC_HANDLER_COUNTER_VEC.stream_query_failed.inc();
Expand All @@ -118,8 +119,8 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {

async fn handle_stream_query_internal(
self: Arc<Self>,
ctx: Context,
req: SqlQueryRequest,
ctx: &Context,
req: &SqlQueryRequest,
) -> Result<BoxStream<'static, SqlQueryResponse>> {
if req.context.is_none() {
return ErrNoCause {
Expand All @@ -130,12 +131,8 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
}

let req_context = req.context.as_ref().unwrap();
let schema = req_context.database.clone();
let req = match self
.clone()
.maybe_forward_stream_sql_query(ctx.clone(), &req)
.await
{
let schema = &req_context.database;
let req = match self.clone().maybe_forward_stream_sql_query(ctx, req).await {
Some(resp) => match resp {
ForwardResult::Forwarded(resp) => return resp,
ForwardResult::Local => req,
Expand All @@ -144,13 +141,12 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
};

let (tx, rx) = mpsc::channel(STREAM_QUERY_CHANNEL_LEN);
let runtime = ctx.runtime.clone();
let resp_compress_min_length = self.resp_compress_min_length;
let output = self
.as_ref()
.fetch_sql_query_output(ctx, &schema, &req.sql)
.fetch_sql_query_output(ctx, schema, &req.sql, false)
.await?;
runtime.spawn(async move {
Handle::current().spawn(async move {
ShiKaiWi marked this conversation as resolved.
Show resolved Hide resolved
match output {
Output::AffectedRows(rows) => {
let resp =
Expand Down Expand Up @@ -189,7 +185,7 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {

async fn maybe_forward_stream_sql_query(
self: Arc<Self>,
ctx: Context,
ctx: &Context,
req: &SqlQueryRequest,
) -> Option<ForwardResult<BoxStream<'static, SqlQueryResponse>, Error>> {
if req.tables.len() != 1 {
Expand All @@ -203,7 +199,7 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
schema: req_ctx.database.clone(),
table: req.tables[0].clone(),
req: req.clone().into_request(),
forwarded_from: ctx.forwarded_from,
forwarded_from: ctx.forwarded_from.clone(),
};
let do_query = |mut client: StorageServiceClient<Channel>,
request: tonic::Request<SqlQueryRequest>,
Expand Down
23 changes: 8 additions & 15 deletions proxy/src/http/prom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ use ceresdbproto::storage::{
};
use common_types::{
datum::DatumKind,
request_id::RequestId,
schema::{RecordSchema, TSID_COLUMN},
};
use generic_error::BoxError;
use http::StatusCode;
use interpreters::interpreter::Output;
use log::{debug, error};
use log::{error, info};
use prom_remote_api::types::{
Label, LabelMatcher, Query, QueryResult, RemoteStorage, Sample, TimeSeries, WriteRequest,
};
Expand Down Expand Up @@ -85,12 +84,7 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
}),
table_requests: write_table_requests,
};
let ctx = ProxyContext {
runtime: self.engine_runtimes.write_runtime.clone(),
timeout: ctx.timeout,
enable_partition_table_access: false,
forwarded_from: None,
};
let ctx = ProxyContext::new(ctx.timeout, None);

match self.handle_write_internal(ctx, table_request).await {
Ok(result) => {
Expand Down Expand Up @@ -127,15 +121,14 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
metric: String,
query: Query,
) -> Result<QueryResult> {
// Open partition table if needed.
self.maybe_open_partition_table_if_not_exist(&ctx.catalog, &ctx.schema, &metric)
.await?;

let request_id = RequestId::next_id();
let request_id = ctx.request_id;
let begin_instant = Instant::now();
let deadline = ctx.timeout.map(|t| begin_instant + t);
info!("Handle prom remote query begin, ctx:{ctx:?}, metric:{metric}, request:{query:?}");

debug!("Query handler try to process request, request_id:{request_id}, request:{query:?}");
// Open partition table if needed.
self.maybe_open_partition_table_if_not_exist(&ctx.catalog, &ctx.schema, &metric)
.await?;

let provider = CatalogMetaProvider {
manager: self.instance.catalog_manager.clone(),
Expand Down Expand Up @@ -171,7 +164,7 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
.await?;

let cost = begin_instant.saturating_elapsed().as_millis();
debug!("Query handler finished, request_id:{request_id}, cost:{cost}ms, query:{query:?}");
info!("Handle prom remote query sucess, ctx:{ctx:?}, cost:{cost}ms");
jiacai2050 marked this conversation as resolved.
Show resolved Hide resolved
jiacai2050 marked this conversation as resolved.
Show resolved Hide resolved

convert_query_result(metric, timestamp_col_name, field_col_name, output)
}
Expand Down
17 changes: 10 additions & 7 deletions proxy/src/http/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use common_types::{
use generic_error::BoxError;
use http::StatusCode;
use interpreters::interpreter::Output;
use log::error;
use query_engine::{
executor::{Executor as QueryExecutor, RecordBatchVec},
physical_planner::PhysicalPlanner,
Expand All @@ -49,14 +50,16 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
ctx: &RequestContext,
req: Request,
) -> Result<Output> {
let context = Context {
timeout: ctx.timeout,
runtime: self.engine_runtimes.read_runtime.clone(),
enable_partition_table_access: true,
forwarded_from: None,
};
let schema = &ctx.schema;
let ctx = Context::new(ctx.timeout, None);

match self.handle_sql(context, &ctx.schema, &req.query).await? {
match self
.handle_sql(&ctx, schema, &req.query, true)
.await
.map_err(|e| {
error!("Handle sql query failed, ctx:{ctx:?}, req:{req:?}, err:{e}");
e
})? {
SqlResponse::Forwarded(resp) => convert_sql_response_to_output(resp),
SqlResponse::Local(output) => Ok(output),
}
Expand Down
10 changes: 2 additions & 8 deletions proxy/src/influxdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use std::time::Instant;
use ceresdbproto::storage::{
RequestContext as GrpcRequestContext, WriteRequest as GrpcWriteRequest,
};
use common_types::request_id::RequestId;
use generic_error::BoxError;
use http::StatusCode;
use interpreters::interpreter::Output;
Expand Down Expand Up @@ -80,12 +79,7 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
}),
table_requests: write_table_requests,
};
let proxy_context = Context {
timeout: ctx.timeout,
runtime: self.engine_runtimes.write_runtime.clone(),
enable_partition_table_access: false,
forwarded_from: None,
};
let proxy_context = Context::new(ctx.timeout, None);

match self
.handle_write_internal(proxy_context, table_request)
Expand Down Expand Up @@ -126,7 +120,7 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
ctx: RequestContext,
req: InfluxqlRequest,
) -> Result<Output> {
let request_id = RequestId::next_id();
let request_id = ctx.request_id;
let begin_instant = Instant::now();
let deadline = ctx.timeout.map(|t| begin_instant + t);

Expand Down
20 changes: 14 additions & 6 deletions proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ use log::{error, info};
use query_engine::{executor::Executor as QueryExecutor, physical_planner::PhysicalPlanner};
use query_frontend::plan::Plan;
use router::{endpoint::Endpoint, Router};
use runtime::Runtime;
use snafu::{OptionExt, ResultExt};
use table_engine::{
engine::{EngineRuntimes, TableState},
Expand Down Expand Up @@ -554,10 +553,19 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Context {
pub timeout: Option<Duration>,
pub runtime: Arc<Runtime>,
pub enable_partition_table_access: bool,
pub forwarded_from: Option<String>,
request_id: RequestId,
timeout: Option<Duration>,
forwarded_from: Option<String>,
ShiKaiWi marked this conversation as resolved.
Show resolved Hide resolved
}

impl Context {
pub fn new(timeout: Option<Duration>, forwarded_from: Option<String>) -> Self {
Self {
request_id: RequestId::next_id(),
timeout,
forwarded_from,
}
}
}
7 changes: 1 addition & 6 deletions proxy/src/opentsdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ impl<Q: QueryExecutor + 'static, P: PhysicalPlanner> Proxy<Q, P> {
}),
table_requests: write_table_requests,
};
let proxy_context = Context {
timeout: ctx.timeout,
runtime: self.engine_runtimes.write_runtime.clone(),
enable_partition_table_access: false,
forwarded_from: None,
};
let proxy_context = Context::new(ctx.timeout, None);

match self
.handle_write_internal(proxy_context, table_request)
Expand Down
Loading
Loading