Skip to content

Commit c85eec1

Browse files
authored
chore: the fist http resp wait longer to tolerant old bendsql clients. (#15022)
related to databendlabs/bendsql#363
1 parent 31cdf38 commit c85eec1

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl QueryResponse {
144144
(JsonBlock::empty(), None)
145145
} else {
146146
match state.state {
147-
ExecuteStateKind::Running => match r.data {
147+
ExecuteStateKind::Running | ExecuteStateKind::Starting => match r.data {
148148
None => (JsonBlock::empty(), Some(make_state_uri(&id))),
149149
Some(d) => {
150150
let uri = match d.next_page_no {
@@ -179,9 +179,14 @@ impl QueryResponse {
179179
};
180180
let rows = data.data.len();
181181

182+
let state_kind = match state.state {
183+
ExecuteStateKind::Starting => ExecuteStateKind::Running,
184+
_ => state.state,
185+
};
186+
182187
Json(QueryResponse {
183188
data: data.into(),
184-
state: state.state,
189+
state: state_kind,
185190
schema: state.schema.clone(),
186191
session_id: Some(session_id),
187192
node_id: r.node_id,
@@ -368,22 +373,30 @@ pub(crate) async fn query_handler(
368373
match query {
369374
Ok(query) => {
370375
query.update_expire_time(true).await;
371-
let resp = query
372-
.get_response_page(0)
373-
.await
374-
.map_err(|err| err.display_with_sql(&sql))
375-
.map_err(|err| poem::Error::from_string(err.message(), StatusCode::NOT_FOUND))?;
376-
if matches!(resp.state.state,ExecuteStateKind::Failed) {
376+
// tmp workaround to tolerant old clients
377+
let max_wait_time = std::cmp::max(10, req.pagination.wait_time_secs);
378+
let start = std::time::Instant::now();
379+
let resp = loop {
380+
let resp = query
381+
.get_response_page(0)
382+
.await
383+
.map_err(|err| err.display_with_sql(&sql))
384+
.map_err(|err| poem::Error::from_string(err.message(), StatusCode::NOT_FOUND))?;
385+
if matches!(resp.state.state, ExecuteStateKind::Starting) && start.elapsed().as_secs() < max_wait_time as u64 {
386+
continue;
387+
}
388+
break resp
389+
};
390+
if matches!(resp.state.state, ExecuteStateKind::Failed) {
377391
ctx.set_fail();
378392
}
379393
let (rows, next_page) = match &resp.data {
380394
None => (0, None),
381395
Some(p) => (p.page.data.num_rows(), p.next_page_no),
382396
};
383-
info!(
384-
"http query initial response to http query_id={}, state={:?}, rows={}, next_page={:?}, sql='{}'",
385-
&query.id, &resp.state, rows, next_page, mask_connection_info(&sql)
386-
);
397+
info!( "http query initial response to http query_id={}, state={:?}, rows={}, next_page={:?}, sql='{}'",
398+
&query.id, &resp.state, rows, next_page, mask_connection_info(&sql)
399+
);
387400
query.update_expire_time(false).await;
388401
Ok(QueryResponse::from_internal(query.id.to_string(), resp, false).into_response())
389402
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use crate::sessions::TableContext;
5454

5555
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
5656
pub enum ExecuteStateKind {
57+
Starting,
5758
Running,
5859
Failed,
5960
Succeeded,
@@ -93,7 +94,8 @@ pub enum ExecuteState {
9394
impl ExecuteState {
9495
pub(crate) fn extract(&self) -> (ExecuteStateKind, Option<ErrorCode>) {
9596
match self {
96-
Starting(_) | Running(_) => (ExecuteStateKind::Running, None),
97+
Starting(_) => (ExecuteStateKind::Starting, None),
98+
Running(_) => (ExecuteStateKind::Running, None),
9799
Stopped(v) => match &v.reason {
98100
Ok(_) => (ExecuteStateKind::Succeeded, None),
99101
Err(e) => (ExecuteStateKind::Failed, Some(e.clone())),

0 commit comments

Comments
 (0)