client/
region.rs

1// Copyright 2023 Greptime Team
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
15use std::sync::Arc;
16
17use api::region::RegionResponse;
18use api::v1::region::RegionRequest;
19use api::v1::ResponseHeader;
20use arc_swap::ArcSwapOption;
21use arrow_flight::Ticket;
22use async_stream::stream;
23use async_trait::async_trait;
24use common_error::ext::BoxedError;
25use common_error::status_code::StatusCode;
26use common_grpc::flight::{FlightDecoder, FlightMessage};
27use common_meta::error::{self as meta_error, Result as MetaResult};
28use common_meta::node_manager::Datanode;
29use common_query::request::QueryRequest;
30use common_recordbatch::error::ExternalSnafu;
31use common_recordbatch::{RecordBatch, RecordBatchStreamWrapper, SendableRecordBatchStream};
32use common_telemetry::error;
33use common_telemetry::tracing_context::TracingContext;
34use prost::Message;
35use query::query_engine::DefaultSerializer;
36use snafu::{location, OptionExt, ResultExt};
37use substrait::{DFLogicalSubstraitConvertor, SubstraitPlan};
38use tokio_stream::StreamExt;
39
40use crate::error::{
41    self, ConvertFlightDataSnafu, FlightGetSnafu, IllegalDatabaseResponseSnafu,
42    IllegalFlightMessagesSnafu, MissingFieldSnafu, Result, ServerSnafu,
43};
44use crate::{metrics, Client, Error};
45
46#[derive(Debug)]
47pub struct RegionRequester {
48    client: Client,
49    send_compression: bool,
50    accept_compression: bool,
51}
52
53#[async_trait]
54impl Datanode for RegionRequester {
55    async fn handle(&self, request: RegionRequest) -> MetaResult<RegionResponse> {
56        self.handle_inner(request).await.map_err(|err| {
57            if err.should_retry() {
58                meta_error::Error::RetryLater {
59                    source: BoxedError::new(err),
60                    clean_poisons: false,
61                }
62            } else {
63                meta_error::Error::External {
64                    source: BoxedError::new(err),
65                    location: location!(),
66                }
67            }
68        })
69    }
70
71    async fn handle_query(&self, request: QueryRequest) -> MetaResult<SendableRecordBatchStream> {
72        let plan = DFLogicalSubstraitConvertor
73            .encode(&request.plan, DefaultSerializer)
74            .map_err(BoxedError::new)
75            .context(meta_error::ExternalSnafu)?
76            .to_vec();
77        let request = api::v1::region::QueryRequest {
78            header: request.header,
79            region_id: request.region_id.as_u64(),
80            plan,
81        };
82
83        let ticket = Ticket {
84            ticket: request.encode_to_vec().into(),
85        };
86        self.do_get_inner(ticket)
87            .await
88            .map_err(BoxedError::new)
89            .context(meta_error::ExternalSnafu)
90    }
91}
92
93impl RegionRequester {
94    pub fn new(client: Client, send_compression: bool, accept_compression: bool) -> Self {
95        Self {
96            client,
97            send_compression,
98            accept_compression,
99        }
100    }
101
102    pub async fn do_get_inner(&self, ticket: Ticket) -> Result<SendableRecordBatchStream> {
103        let mut flight_client = self
104            .client
105            .make_flight_client(self.send_compression, self.accept_compression)?;
106        let response = flight_client
107            .mut_inner()
108            .do_get(ticket)
109            .await
110            .or_else(|e| {
111                let tonic_code = e.code();
112                let e: error::Error = e.into();
113                error!(
114                    e; "Failed to do Flight get, addr: {}, code: {}",
115                    flight_client.addr(),
116                    tonic_code
117                );
118                let error = Err(BoxedError::new(e)).with_context(|_| FlightGetSnafu {
119                    addr: flight_client.addr().to_string(),
120                    tonic_code,
121                });
122                error
123            })?;
124
125        let flight_data_stream = response.into_inner();
126        let mut decoder = FlightDecoder::default();
127
128        let mut flight_message_stream = flight_data_stream.map(move |flight_data| {
129            flight_data
130                .map_err(Error::from)
131                .and_then(|data| decoder.try_decode(&data).context(ConvertFlightDataSnafu))?
132                .context(IllegalFlightMessagesSnafu {
133                    reason: "none message",
134                })
135        });
136
137        let Some(first_flight_message) = flight_message_stream.next().await else {
138            return IllegalFlightMessagesSnafu {
139                reason: "Expect the response not to be empty",
140            }
141            .fail();
142        };
143        let FlightMessage::Schema(schema) = first_flight_message? else {
144            return IllegalFlightMessagesSnafu {
145                reason: "Expect schema to be the first flight message",
146            }
147            .fail();
148        };
149
150        let metrics = Arc::new(ArcSwapOption::from(None));
151        let metrics_ref = metrics.clone();
152
153        let tracing_context = TracingContext::from_current_span();
154
155        let schema = Arc::new(
156            datatypes::schema::Schema::try_from(schema).context(error::ConvertSchemaSnafu)?,
157        );
158        let schema_cloned = schema.clone();
159        let stream = Box::pin(stream!({
160            let _span = tracing_context.attach(common_telemetry::tracing::info_span!(
161                "poll_flight_data_stream"
162            ));
163
164            let mut buffered_message: Option<FlightMessage> = None;
165            let mut stream_ended = false;
166
167            while !stream_ended {
168                // get the next message from the buffered message or read from the flight message stream
169                let flight_message_item = if let Some(msg) = buffered_message.take() {
170                    Some(Ok(msg))
171                } else {
172                    flight_message_stream.next().await
173                };
174
175                let flight_message = match flight_message_item {
176                    Some(Ok(message)) => message,
177                    Some(Err(e)) => {
178                        yield Err(BoxedError::new(e)).context(ExternalSnafu);
179                        break;
180                    }
181                    None => break,
182                };
183
184                match flight_message {
185                    FlightMessage::RecordBatch(record_batch) => {
186                        let result_to_yield = RecordBatch::try_from_df_record_batch(
187                            schema_cloned.clone(),
188                            record_batch,
189                        );
190
191                        // get the next message from the stream. normally it should be a metrics message.
192                        if let Some(next_flight_message_result) = flight_message_stream.next().await
193                        {
194                            match next_flight_message_result {
195                                Ok(FlightMessage::Metrics(s)) => {
196                                    let m = serde_json::from_str(&s).ok().map(Arc::new);
197                                    metrics_ref.swap(m);
198                                }
199                                Ok(FlightMessage::RecordBatch(rb)) => {
200                                    // for some reason it's not a metrics message, so we need to buffer this record batch
201                                    // and yield it in the next iteration.
202                                    buffered_message = Some(FlightMessage::RecordBatch(rb));
203                                }
204                                Ok(_) => {
205                                    yield IllegalFlightMessagesSnafu {
206                                        reason: "A RecordBatch message can only be succeeded by a Metrics message or another RecordBatch message"
207                                    }
208                                    .fail()
209                                    .map_err(BoxedError::new)
210                                    .context(ExternalSnafu);
211                                    break;
212                                }
213                                Err(e) => {
214                                    yield Err(BoxedError::new(e)).context(ExternalSnafu);
215                                    break;
216                                }
217                            }
218                        } else {
219                            // the stream has ended
220                            stream_ended = true;
221                        }
222
223                        yield result_to_yield;
224                    }
225                    FlightMessage::Metrics(s) => {
226                        // just a branch in case of some metrics message comes after other things.
227                        let m = serde_json::from_str(&s).ok().map(Arc::new);
228                        metrics_ref.swap(m);
229                        break;
230                    }
231                    _ => {
232                        yield IllegalFlightMessagesSnafu {
233                            reason: "A Schema message must be succeeded exclusively by a set of RecordBatch messages"
234                        }
235                        .fail()
236                        .map_err(BoxedError::new)
237                        .context(ExternalSnafu);
238                        break;
239                    }
240                }
241            }
242        }));
243        let record_batch_stream = RecordBatchStreamWrapper {
244            schema,
245            stream,
246            output_ordering: None,
247            metrics,
248        };
249        Ok(Box::pin(record_batch_stream))
250    }
251
252    async fn handle_inner(&self, request: RegionRequest) -> Result<RegionResponse> {
253        let request_type = request
254            .body
255            .as_ref()
256            .with_context(|| MissingFieldSnafu { field: "body" })?
257            .as_ref()
258            .to_string();
259        let _timer = metrics::METRIC_REGION_REQUEST_GRPC
260            .with_label_values(&[request_type.as_str()])
261            .start_timer();
262
263        let (addr, mut client) = self.client.raw_region_client()?;
264
265        let response = client
266            .handle(request)
267            .await
268            .map_err(|e| {
269                let code = e.code();
270                // Uses `Error::RegionServer` instead of `Error::Server`
271                error::Error::RegionServer {
272                    addr,
273                    code,
274                    source: BoxedError::new(error::Error::from(e)),
275                    location: location!(),
276                }
277            })?
278            .into_inner();
279
280        check_response_header(&response.header)?;
281
282        Ok(RegionResponse::from_region_response(response))
283    }
284
285    pub async fn handle(&self, request: RegionRequest) -> Result<RegionResponse> {
286        self.handle_inner(request).await
287    }
288}
289
290pub fn check_response_header(header: &Option<ResponseHeader>) -> Result<()> {
291    let status = header
292        .as_ref()
293        .and_then(|header| header.status.as_ref())
294        .context(IllegalDatabaseResponseSnafu {
295            err_msg: "either response header or status is missing",
296        })?;
297
298    if StatusCode::is_success(status.status_code) {
299        Ok(())
300    } else {
301        let code =
302            StatusCode::from_u32(status.status_code).context(IllegalDatabaseResponseSnafu {
303                err_msg: format!("unknown server status: {:?}", status),
304            })?;
305        ServerSnafu {
306            code,
307            msg: status.err_msg.clone(),
308        }
309        .fail()
310    }
311}
312
313#[cfg(test)]
314mod test {
315    use api::v1::Status as PbStatus;
316
317    use super::*;
318    use crate::Error::{IllegalDatabaseResponse, Server};
319
320    #[test]
321    fn test_check_response_header() {
322        let result = check_response_header(&None);
323        assert!(matches!(
324            result.unwrap_err(),
325            IllegalDatabaseResponse { .. }
326        ));
327
328        let result = check_response_header(&Some(ResponseHeader { status: None }));
329        assert!(matches!(
330            result.unwrap_err(),
331            IllegalDatabaseResponse { .. }
332        ));
333
334        let result = check_response_header(&Some(ResponseHeader {
335            status: Some(PbStatus {
336                status_code: StatusCode::Success as u32,
337                err_msg: String::default(),
338            }),
339        }));
340        assert!(result.is_ok());
341
342        let result = check_response_header(&Some(ResponseHeader {
343            status: Some(PbStatus {
344                status_code: u32::MAX,
345                err_msg: String::default(),
346            }),
347        }));
348        assert!(matches!(
349            result.unwrap_err(),
350            IllegalDatabaseResponse { .. }
351        ));
352
353        let result = check_response_header(&Some(ResponseHeader {
354            status: Some(PbStatus {
355                status_code: StatusCode::Internal as u32,
356                err_msg: "blabla".to_string(),
357            }),
358        }));
359        let Server { code, msg, .. } = result.unwrap_err() else {
360            unreachable!()
361        };
362        assert_eq!(code, StatusCode::Internal);
363        assert_eq!(msg, "blabla");
364    }
365}