Skip to content

Commit e3dc218

Browse files
authored
chore: log more detail for temp table. (#18153)
1 parent ddc5c24 commit e3dc218

File tree

4 files changed

+52
-26
lines changed

4 files changed

+52
-26
lines changed

src/query/service/src/servers/http/middleware/session.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ impl<E> HTTPSessionEndpoint<E> {
365365
req: &Request,
366366
query_id: String,
367367
login_history: &mut LoginHistory,
368+
is_sticky_node: bool,
368369
) -> Result<HttpQueryContext> {
369370
let client_host = get_client_ip(req);
370371
let node_id = GlobalConfig::instance().query.node_id.clone();
@@ -506,6 +507,7 @@ impl<E> HTTPSessionEndpoint<E> {
506507
client_host,
507508
client_session_id,
508509
user_name,
510+
is_sticky_node,
509511
})
510512
}
511513
}
@@ -583,6 +585,8 @@ impl<E: Endpoint> Endpoint for HTTPSessionEndpoint<E> {
583585
async fn call(&self, mut req: Request) -> PoemResult<Self::Output> {
584586
let headers = req.headers().clone();
585587

588+
let mut is_sticky_node = false;
589+
586590
if self.endpoint_kind.may_need_sticky() {
587591
if let Some(sticky_node_id) = headers.get(HEADER_STICKY) {
588592
let sticky_node_id = sticky_node_id
@@ -593,6 +597,7 @@ impl<E: Endpoint> Endpoint for HTTPSessionEndpoint<E> {
593597
)))
594598
})?
595599
.to_string();
600+
is_sticky_node = true;
596601
let local_id = GlobalConfig::instance().query.node_id.clone();
597602
if local_id != sticky_node_id {
598603
return if let Some(node) = ClusterDiscovery::instance()
@@ -664,7 +669,7 @@ impl<E: Endpoint> Endpoint for HTTPSessionEndpoint<E> {
664669

665670
log::warn!("Ignore header ({HEADER_WAREHOUSE}: {warehouse:?})");
666671
}
667-
}
672+
};
668673

669674
let method = req.method().clone();
670675
let uri = req.uri().clone();
@@ -680,7 +685,10 @@ impl<E: Endpoint> Endpoint for HTTPSessionEndpoint<E> {
680685
login_history.connection_uri = uri.to_string();
681686

682687
ThreadTracker::tracking_future(async move {
683-
match self.auth(&req, query_id, &mut login_history).await {
688+
match self
689+
.auth(&req, query_id, &mut login_history, is_sticky_node)
690+
.await
691+
{
684692
Ok(ctx) => {
685693
login_history.event_type = LoginEventType::LoginSuccess;
686694
login_history.write_to_log();

src/query/service/src/servers/http/v1/query/http_query.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use databend_common_metrics::http::metrics_incr_http_response_errors_count;
3939
use databend_common_settings::ScopeLevel;
4040
use databend_storages_common_session::TxnState;
4141
use http::StatusCode;
42+
use log::error;
4243
use log::info;
4344
use log::warn;
4445
use parking_lot::Mutex;
@@ -434,11 +435,19 @@ fn try_set_txn(
434435
impl HttpQuery {
435436
#[async_backtrace::framed]
436437
#[fastrace::trace]
437-
pub async fn try_create(ctx: &HttpQueryContext, req: HttpQueryRequest) -> Result<HttpQuery> {
438+
pub async fn try_create(
439+
http_ctx: &HttpQueryContext,
440+
req: HttpQueryRequest,
441+
) -> Result<HttpQuery> {
438442
let http_query_manager = HttpQueryManager::instance();
439-
let session = ctx.upgrade_session(SessionType::HTTPQuery).map_err(|err| {
440-
ErrorCode::Internal(format!("[HTTP-QUERY] Failed to upgrade session: {err}"))
441-
})?;
443+
let client_session_id = http_ctx.client_session_id.as_deref().unwrap_or("None");
444+
let query_id = http_ctx.query_id.clone();
445+
let session = http_ctx
446+
.upgrade_session(SessionType::HTTPQuery)
447+
.map_err(|err| {
448+
ErrorCode::Internal(format!("[HTTP-QUERY] Failed to upgrade session: {err}"))
449+
})?;
450+
let session_id = session.get_id();
442451

443452
// Read the session variables in the request, and set them to the current session.
444453
// the session variables includes:
@@ -492,24 +501,36 @@ impl HttpQuery {
492501
}
493502
}
494503

495-
try_set_txn(&ctx.query_id, &session, session_conf, &http_query_manager)?;
504+
try_set_txn(
505+
&http_ctx.query_id,
506+
&session,
507+
session_conf,
508+
&http_query_manager,
509+
)?;
496510

497511
if session_conf.need_sticky
498512
&& matches!(session_conf.txn_state, None | Some(TxnState::AutoCommit))
499513
{
500-
http_query_manager.check_sticky_for_temp_table(&session_conf.last_server_info)?;
514+
http_query_manager
515+
.check_sticky_for_temp_table(&session_conf.last_server_info)
516+
.map_err(|e| {
517+
let msg = format!(
518+
"[TEMP TABLE] invalid session, session_id={} query_id={}, error={e}, is_sticky_node={}",
519+
client_session_id, query_id, http_ctx.is_sticky_node,
520+
);
521+
error!("{}, session_state={:?}", msg, session_conf);
522+
ErrorCode::InvalidSessionState(msg)
523+
})?;
501524
}
502525
};
503526

504527
let settings = session.get_settings();
505528
let result_timeout_secs = settings.get_http_handler_result_timeout_secs()?;
506-
let deduplicate_label = &ctx.deduplicate_label;
507-
let user_agent = &ctx.user_agent;
508-
let query_id = ctx.query_id.clone();
529+
let deduplicate_label = &http_ctx.deduplicate_label;
530+
let user_agent = &http_ctx.user_agent;
509531

510-
session.set_client_host(ctx.client_host.clone());
532+
session.set_client_host(http_ctx.client_host.clone());
511533

512-
let http_ctx = ctx;
513534
let ctx = session.create_query_context().await?;
514535

515536
// Deduplicate label is used on the DML queries which may be retried by the client.
@@ -526,10 +547,9 @@ impl HttpQuery {
526547
// TODO: validate the query_id to be uuid format
527548
ctx.update_init_query_id(query_id.clone());
528549

529-
let session_id = session.get_id().clone();
530550
let node_id = ctx.get_cluster().local_id.clone();
531551
let sql = &req.sql;
532-
info!(query_id = query_id, session_id = session_id, node_id = node_id, sql = sql; "[HTTP-QUERY] Creating new query");
552+
info!(query_id = query_id, session_id = client_session_id, node_id = node_id, sql = sql; "[HTTP-QUERY] Creating new query");
533553

534554
// Stage attachment is used to carry the data payload to the INSERT/REPLACE statements.
535555
// When stage attachment is specified, the query may looks like `INSERT INTO mytbl VALUES;`,

src/query/service/src/servers/http/v1/query/http_query_context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub struct HttpQueryContext {
4444
pub client_host: Option<String>,
4545
pub client_session_id: Option<String>,
4646
pub user_name: String,
47+
pub is_sticky_node: bool,
4748
}
4849

4950
impl HttpQueryContext {

src/query/service/src/servers/http/v1/query/http_query_manager.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,25 +246,22 @@ impl HttpQueryManager {
246246
pub(crate) fn check_sticky_for_temp_table(
247247
&self,
248248
last_server_info: &Option<ServerInfo>,
249-
) -> Result<()> {
249+
) -> std::result::Result<(), String> {
250250
if let Some(ServerInfo { id, start_time }) = last_server_info {
251251
if self.server_info.id != *id {
252-
return Err(ErrorCode::InvalidSessionState(format!(
253-
"[HTTP-QUERY] Session contains temporary tables but request was routed to the wrong server: current server is {}, expected server is {}",
252+
return Err(format!(
253+
"contains temporary tables but request was routed to the wrong server: current server is {}, expected server is {}",
254254
self.server_info.id, id
255-
)));
255+
));
256256
}
257257
if self.server_info.start_time != *start_time {
258-
return Err(ErrorCode::InvalidSessionState(format!(
259-
"[HTTP-QUERY] Temporary tables were lost because server restarted at {}",
258+
return Err(format!(
259+
"temporary tables were lost because server restarted at {}",
260260
start_time
261-
)));
261+
));
262262
}
263263
} else {
264-
return Err(ErrorCode::InvalidSessionState(
265-
"[HTTP-QUERY] Session contains temporary tables but missing server_info field"
266-
.to_string(),
267-
));
264+
return Err("contains temporary tables but missing last_server_info field".to_string());
268265
}
269266
Ok(())
270267
}

0 commit comments

Comments
 (0)