|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::collections::HashMap; |
| 16 | + |
| 17 | +use databend_common_config::GlobalConfig; |
| 18 | +use databend_common_exception::Result; |
| 19 | +use http::StatusCode; |
| 20 | +use poem::web::Json; |
| 21 | +use poem::web::Path; |
| 22 | +use poem::IntoResponse; |
| 23 | + |
| 24 | +use crate::clusters::ClusterDiscovery; |
| 25 | +use crate::clusters::ClusterHelper; |
| 26 | +use crate::clusters::FlightParams; |
| 27 | +use crate::servers::flight::v1::actions::GET_RUNNING_QUERY_DUMP; |
| 28 | + |
| 29 | +#[poem::handler] |
| 30 | +#[async_backtrace::framed] |
| 31 | +pub async fn running_query_dump(Path(query_id): Path<String>) -> poem::Result<impl IntoResponse> { |
| 32 | + #[derive(serde::Serialize)] |
| 33 | + struct QueryRunningGraphDump { |
| 34 | + query_id: String, |
| 35 | + graph_dump: HashMap<String, String>, |
| 36 | + } |
| 37 | + |
| 38 | + let graph_dump = match get_running_query_dump(&query_id).await { |
| 39 | + Ok(graph_dump) => graph_dump, |
| 40 | + Err(cause) => { |
| 41 | + return Err(poem::Error::from_string( |
| 42 | + format!("Failed to fetch executor dump. cause: {cause}"), |
| 43 | + StatusCode::INTERNAL_SERVER_ERROR, |
| 44 | + )) |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + Ok(Json(QueryRunningGraphDump { |
| 49 | + graph_dump, |
| 50 | + query_id: query_id.clone(), |
| 51 | + })) |
| 52 | +} |
| 53 | + |
| 54 | +async fn get_running_query_dump(query_id: &str) -> Result<HashMap<String, String>> { |
| 55 | + let config = GlobalConfig::instance(); |
| 56 | + let cluster = ClusterDiscovery::instance().discover(&config).await?; |
| 57 | + |
| 58 | + let mut message = HashMap::with_capacity(cluster.nodes.len()); |
| 59 | + |
| 60 | + for node_info in &cluster.nodes { |
| 61 | + message.insert(node_info.id.clone(), query_id.to_owned()); |
| 62 | + } |
| 63 | + |
| 64 | + let flight_params = FlightParams { |
| 65 | + timeout: 60, |
| 66 | + retry_times: 3, |
| 67 | + retry_interval: 3, |
| 68 | + }; |
| 69 | + cluster |
| 70 | + .do_action::<_, String>(GET_RUNNING_QUERY_DUMP, message, flight_params) |
| 71 | + .await |
| 72 | +} |
0 commit comments