1use std::collections::HashMap;
16use std::sync::Arc;
17
18use api::prom_store::remote::read_request::ResponseType;
19use api::prom_store::remote::{Query, QueryResult, ReadRequest, ReadResponse};
20use api::v1::RowInsertRequests;
21use async_trait::async_trait;
22use auth::{PermissionChecker, PermissionCheckerRef, PermissionReq};
23use client::OutputData;
24use common_catalog::format_full_table_name;
25use common_error::ext::BoxedError;
26use common_query::Output;
27use common_query::prelude::GREPTIME_PHYSICAL_TABLE;
28use common_recordbatch::RecordBatches;
29use common_telemetry::{debug, tracing};
30use operator::insert::InserterRef;
31use operator::statement::StatementExecutor;
32use prost::Message;
33use servers::error::{self, AuthSnafu, Result as ServerResult};
34use servers::http::header::{CONTENT_ENCODING_SNAPPY, CONTENT_TYPE_PROTOBUF, collect_plan_metrics};
35use servers::http::prom_store::PHYSICAL_TABLE_PARAM;
36use servers::interceptor::{PromStoreProtocolInterceptor, PromStoreProtocolInterceptorRef};
37use servers::prom_store::{self, Metrics};
38use servers::query_handler::{
39 PromStoreProtocolHandler, PromStoreProtocolHandlerRef, PromStoreResponse,
40};
41use session::context::QueryContextRef;
42use snafu::{OptionExt, ResultExt};
43use tracing::instrument;
44
45use crate::error::{
46 CatalogSnafu, ExecLogicalPlanSnafu, PromStoreRemoteQueryPlanSnafu, ReadTableSnafu, Result,
47 TableNotFoundSnafu,
48};
49use crate::instance::Instance;
50
51const SAMPLES_RESPONSE_TYPE: i32 = ResponseType::Samples as i32;
52
53#[inline]
54fn is_supported(response_type: i32) -> bool {
55 response_type == SAMPLES_RESPONSE_TYPE
57}
58
59fn negotiate_response_type(accepted_response_types: &[i32]) -> ServerResult<ResponseType> {
65 if accepted_response_types.is_empty() {
66 return Ok(ResponseType::Samples);
67 }
68
69 let response_type = accepted_response_types
70 .iter()
71 .find(|t| is_supported(**t))
72 .with_context(|| error::NotSupportedSnafu {
73 feat: format!(
74 "server does not support any of the requested response types: {accepted_response_types:?}",
75 ),
76 })?;
77
78 Ok(ResponseType::try_from(*response_type).unwrap())
80}
81
82#[instrument(skip_all, fields(table_name))]
83async fn to_query_result(table_name: &str, output: Output) -> ServerResult<QueryResult> {
84 let OutputData::Stream(stream) = output.data else {
85 unreachable!()
86 };
87 let recordbatches = RecordBatches::try_collect(stream)
88 .await
89 .context(error::CollectRecordbatchSnafu)?;
90 Ok(QueryResult {
91 timeseries: prom_store::recordbatches_to_timeseries(table_name, recordbatches)?,
92 })
93}
94
95impl Instance {
96 #[tracing::instrument(skip_all)]
97 async fn handle_remote_query(
98 &self,
99 ctx: &QueryContextRef,
100 catalog_name: &str,
101 schema_name: &str,
102 table_name: &str,
103 query: &Query,
104 ) -> Result<Output> {
105 let table = self
106 .catalog_manager
107 .table(catalog_name, schema_name, table_name, Some(ctx))
108 .await
109 .context(CatalogSnafu)?
110 .with_context(|| TableNotFoundSnafu {
111 table_name: format_full_table_name(catalog_name, schema_name, table_name),
112 })?;
113
114 let dataframe = self
115 .query_engine
116 .read_table(table)
117 .with_context(|_| ReadTableSnafu {
118 table_name: format_full_table_name(catalog_name, schema_name, table_name),
119 })?;
120
121 let logical_plan =
122 prom_store::query_to_plan(dataframe, query).context(PromStoreRemoteQueryPlanSnafu)?;
123
124 debug!(
125 "Prometheus remote read, table: {}, logical plan: {}",
126 table_name,
127 logical_plan.display_indent(),
128 );
129
130 self.query_engine
131 .execute(logical_plan, ctx.clone())
132 .await
133 .context(ExecLogicalPlanSnafu)
134 }
135
136 #[tracing::instrument(skip_all)]
137 async fn handle_remote_queries(
138 &self,
139 ctx: QueryContextRef,
140 queries: &[Query],
141 ) -> ServerResult<Vec<(String, Output)>> {
142 let mut results = Vec::with_capacity(queries.len());
143
144 let catalog_name = ctx.current_catalog();
145 let schema_name = ctx.current_schema();
146
147 for query in queries {
148 let table_name = prom_store::table_name(query)?;
149
150 let output = self
151 .handle_remote_query(&ctx, catalog_name, &schema_name, &table_name, query)
152 .await
153 .map_err(BoxedError::new)
154 .context(error::ExecuteQuerySnafu)?;
155
156 results.push((table_name, output));
157 }
158 Ok(results)
159 }
160}
161
162#[async_trait]
163impl PromStoreProtocolHandler for Instance {
164 async fn pre_write(
165 &self,
166 request: &RowInsertRequests,
167 ctx: QueryContextRef,
168 ) -> ServerResult<()> {
169 self.plugins
170 .get::<PermissionCheckerRef>()
171 .as_ref()
172 .check_permission(ctx.current_user(), PermissionReq::PromStoreWrite)
173 .context(AuthSnafu)?;
174 let interceptor_ref = self
175 .plugins
176 .get::<PromStoreProtocolInterceptorRef<servers::error::Error>>();
177 interceptor_ref.pre_write(request, ctx)?;
178 Ok(())
179 }
180
181 async fn write(
182 &self,
183 request: RowInsertRequests,
184 ctx: QueryContextRef,
185 with_metric_engine: bool,
186 ) -> ServerResult<Output> {
187 self.pre_write(&request, ctx.clone()).await?;
188
189 let output = if with_metric_engine {
190 let physical_table = ctx
191 .extension(PHYSICAL_TABLE_PARAM)
192 .unwrap_or(GREPTIME_PHYSICAL_TABLE)
193 .to_string();
194 self.handle_metric_row_inserts(request, ctx.clone(), physical_table.clone())
195 .await
196 .map_err(BoxedError::new)
197 .context(error::ExecuteGrpcQuerySnafu)?
198 } else {
199 self.handle_row_inserts(request, ctx.clone(), true, true)
200 .await
201 .map_err(BoxedError::new)
202 .context(error::ExecuteGrpcQuerySnafu)?
203 };
204
205 Ok(output)
206 }
207
208 #[instrument(skip_all, fields(table_name))]
209 async fn read(
210 &self,
211 request: ReadRequest,
212 ctx: QueryContextRef,
213 ) -> ServerResult<PromStoreResponse> {
214 self.plugins
215 .get::<PermissionCheckerRef>()
216 .as_ref()
217 .check_permission(ctx.current_user(), PermissionReq::PromStoreRead)
218 .context(AuthSnafu)?;
219 let interceptor_ref = self
220 .plugins
221 .get::<PromStoreProtocolInterceptorRef<servers::error::Error>>();
222 interceptor_ref.pre_read(&request, ctx.clone())?;
223
224 let response_type = negotiate_response_type(&request.accepted_response_types)?;
225
226 let results = self.handle_remote_queries(ctx, &request.queries).await?;
228
229 match response_type {
230 ResponseType::Samples => {
231 let mut query_results = Vec::with_capacity(results.len());
232 let mut map = HashMap::new();
233 for (table_name, output) in results {
234 let plan = output.meta.plan.clone();
235 query_results.push(to_query_result(&table_name, output).await?);
236 if let Some(ref plan) = plan {
237 collect_plan_metrics(plan, &mut [&mut map]);
238 }
239 }
240
241 let response = ReadResponse {
242 results: query_results,
243 };
244
245 let resp_metrics = map
246 .into_iter()
247 .map(|(k, v)| (k, v.into()))
248 .collect::<HashMap<_, _>>();
249
250 Ok(PromStoreResponse {
252 content_type: CONTENT_TYPE_PROTOBUF.clone(),
253 content_encoding: CONTENT_ENCODING_SNAPPY.clone(),
254 resp_metrics,
255 body: prom_store::snappy_compress(&response.encode_to_vec())?,
256 })
257 }
258 ResponseType::StreamedXorChunks => error::NotSupportedSnafu {
259 feat: "streamed remote read",
260 }
261 .fail(),
262 }
263 }
264
265 async fn ingest_metrics(&self, _metrics: Metrics) -> ServerResult<()> {
266 todo!();
267 }
268}
269
270pub struct ExportMetricHandler {
275 inserter: InserterRef,
276 statement_executor: Arc<StatementExecutor>,
277}
278
279impl ExportMetricHandler {
280 pub fn new_handler(
281 inserter: InserterRef,
282 statement_executor: Arc<StatementExecutor>,
283 ) -> PromStoreProtocolHandlerRef {
284 Arc::new(Self {
285 inserter,
286 statement_executor,
287 })
288 }
289}
290
291#[async_trait]
292impl PromStoreProtocolHandler for ExportMetricHandler {
293 async fn write(
294 &self,
295 request: RowInsertRequests,
296 ctx: QueryContextRef,
297 _: bool,
298 ) -> ServerResult<Output> {
299 self.inserter
300 .handle_metric_row_inserts(
301 request,
302 ctx,
303 &self.statement_executor,
304 GREPTIME_PHYSICAL_TABLE.to_string(),
305 )
306 .await
307 .map_err(BoxedError::new)
308 .context(error::ExecuteGrpcQuerySnafu)
309 }
310
311 async fn read(
312 &self,
313 _request: ReadRequest,
314 _ctx: QueryContextRef,
315 ) -> ServerResult<PromStoreResponse> {
316 unreachable!();
317 }
318
319 async fn ingest_metrics(&self, _metrics: Metrics) -> ServerResult<()> {
320 unreachable!();
321 }
322}