-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add http query interface for background tasks (#13027)
* save * add license header
- Loading branch information
Showing
5 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2021 Datafuse Labs | ||
// | ||
// 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 chrono::DateTime; | ||
use chrono::Utc; | ||
use common_config::GlobalConfig; | ||
use common_exception::Result; | ||
use common_meta_api::BackgroundApi; | ||
use common_meta_app::background::BackgroundTaskInfo; | ||
use common_meta_app::background::BackgroundTaskState; | ||
use common_meta_app::background::BackgroundTaskType; | ||
use common_meta_app::background::ListBackgroundTasksReq; | ||
use common_users::UserApiProvider; | ||
use log::debug; | ||
use poem::web::Json; | ||
use poem::web::Query; | ||
use poem::IntoResponse; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct BackgroundTaskQuery { | ||
timestamp: DateTime<Utc>, | ||
table_id: u64, | ||
task_state: BackgroundTaskState, | ||
task_type: BackgroundTaskType, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct ListBackgroundTasksResponse { | ||
task_infos: Vec<(String, BackgroundTaskInfo)>, | ||
params: BackgroundTaskQuery, | ||
} | ||
|
||
#[async_backtrace::framed] | ||
async fn load_background_tasks( | ||
tenant: &str, | ||
params: Query<BackgroundTaskQuery>, | ||
) -> Result<ListBackgroundTasksResponse> { | ||
let meta_api = UserApiProvider::instance().get_meta_store_client(); | ||
let tasks = meta_api | ||
.list_background_tasks(ListBackgroundTasksReq { | ||
tenant: tenant.to_string(), | ||
}) | ||
.await?; | ||
let mut task_infos = Vec::with_capacity(tasks.len()); | ||
for (_, name, task) in tasks { | ||
if task.task_state != params.task_state { | ||
continue; | ||
} | ||
if task.task_type != params.task_type { | ||
continue; | ||
} | ||
if task.task_type == BackgroundTaskType::COMPACTION { | ||
if task.compaction_task_stats.is_none() { | ||
continue; | ||
} | ||
if task.compaction_task_stats.as_ref().unwrap().table_id != params.table_id { | ||
continue; | ||
} | ||
} | ||
if task.last_updated.is_some() && task.last_updated.unwrap() < params.timestamp { | ||
continue; | ||
} | ||
task_infos.push((name, task)); | ||
} | ||
Ok(ListBackgroundTasksResponse { | ||
task_infos, | ||
params: params.0, | ||
}) | ||
} | ||
|
||
#[poem::handler] | ||
#[async_backtrace::framed] | ||
pub async fn list_background_tasks( | ||
params: Query<BackgroundTaskQuery>, | ||
) -> poem::Result<impl IntoResponse> { | ||
let tenant = &GlobalConfig::instance().query.tenant_id; | ||
debug!( | ||
"list_background_tasks: tenant: {}, params: {:?}", | ||
tenant, params | ||
); | ||
if tenant.is_empty() { | ||
return Ok(Json(ListBackgroundTasksResponse { | ||
task_infos: vec![], | ||
params: params.0, | ||
})); | ||
} | ||
|
||
let resp = load_background_tasks(tenant, params) | ||
.await | ||
.map_err(poem::error::InternalServerError)?; | ||
Ok(Json(resp)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
00261dc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
databend – ./
databend-git-main-databend.vercel.app
databend.rs
databend.vercel.app
databend.org
databend-databend.vercel.app