Skip to main content

client/
flight.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::pin::Pin;
16
17use arrow_flight::FlightData;
18use common_grpc::flight::{FlightDecoder, FlightMessage};
19use futures_util::stream::Peekable;
20use futures_util::{Stream, StreamExt};
21use snafu::{OptionExt, ResultExt};
22
23use crate::Result;
24use crate::error::{ConvertFlightDataSnafu, Error, IllegalFlightMessagesSnafu};
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub(crate) enum FlightMessageKind {
28    Schema,
29    RecordBatch,
30    AffectedRows,
31    Metrics,
32}
33
34impl From<&FlightMessage> for FlightMessageKind {
35    fn from(message: &FlightMessage) -> Self {
36        match message {
37            FlightMessage::Schema(_) => Self::Schema,
38            FlightMessage::RecordBatch(_) => Self::RecordBatch,
39            FlightMessage::AffectedRows { .. } => Self::AffectedRows,
40            FlightMessage::Metrics(_) => Self::Metrics,
41        }
42    }
43}
44
45pub(crate) struct FlightMessageReader<S: Stream + Unpin> {
46    /// Remote Flight peer associated with this response stream.
47    remote_addr: String,
48    messages: Peekable<S>,
49}
50
51impl<S> FlightMessageReader<S>
52where
53    S: Stream<Item = Result<FlightMessage>> + Unpin,
54{
55    pub(crate) fn new(remote_addr: impl Into<String>, messages: S) -> Self {
56        Self {
57            remote_addr: remote_addr.into(),
58            messages: messages.peekable(),
59        }
60    }
61
62    pub(crate) fn remote_addr(&self) -> &str {
63        &self.remote_addr
64    }
65
66    pub(crate) async fn read_first(&mut self) -> Result<FlightMessage> {
67        self.read_next().await?.context(IllegalFlightMessagesSnafu {
68            reason: "Expect the response not to be empty",
69        })
70    }
71
72    pub(crate) async fn read_next(&mut self) -> Result<Option<FlightMessage>> {
73        self.messages.next().await.transpose()
74    }
75
76    pub(crate) async fn peek_next_message_kind(&mut self) -> Result<Option<FlightMessageKind>> {
77        match Pin::new(&mut self.messages).peek().await {
78            Some(Ok(message)) => Ok(Some(message.into())),
79            None => Ok(None),
80            Some(Err(_)) => match self.read_next().await {
81                // `peek` only borrows the error; consume it to preserve the source error.
82                Err(error) => Err(error),
83                Ok(_) => IllegalFlightMessagesSnafu {
84                    reason: "Flight stream changed after peek".to_string(),
85                }
86                .fail(),
87            },
88        }
89    }
90}
91
92pub(crate) fn decode_flight_data(
93    decoder: &mut FlightDecoder,
94    flight_data: std::result::Result<FlightData, tonic::Status>,
95) -> Option<Result<FlightMessage>> {
96    flight_data
97        .map_err(Error::from)
98        .and_then(|data| decoder.try_decode(&data).context(ConvertFlightDataSnafu))
99        .transpose()
100}
101
102#[cfg(test)]
103mod tests {
104    use std::sync::Arc;
105
106    use common_grpc::flight::FlightEncoder;
107    use datatypes::arrow::array::{DictionaryArray, StringArray, UInt32Array};
108    use datatypes::arrow::datatypes::{DataType, Field, Schema, UInt32Type};
109    use datatypes::arrow::record_batch::RecordBatch;
110
111    use super::*;
112
113    #[test]
114    fn test_decode_flight_data_skips_dictionary_batches() {
115        let schema = Arc::new(Schema::new(vec![Field::new_dictionary(
116            "host",
117            DataType::UInt32,
118            DataType::Utf8,
119            false,
120        )]));
121        let batch = RecordBatch::try_new(
122            schema.clone(),
123            vec![Arc::new(DictionaryArray::<UInt32Type>::new(
124                UInt32Array::from(vec![0, 1, 0]),
125                Arc::new(StringArray::from(vec!["host-a", "host-b"])),
126            ))],
127        )
128        .unwrap();
129
130        let mut encoder = FlightEncoder::default();
131        let mut flight_data = Vec::new();
132        flight_data.extend(encoder.encode(FlightMessage::Schema(schema.clone())));
133        let encoded_batch = encoder.encode(FlightMessage::RecordBatch(batch.clone()));
134        assert_eq!(2, encoded_batch.len());
135        flight_data.extend(encoded_batch);
136
137        let mut decoder = FlightDecoder::default();
138        let messages = flight_data
139            .into_iter()
140            .filter_map(|data| decode_flight_data(&mut decoder, Ok(data)))
141            .collect::<Result<Vec<_>>>()
142            .unwrap();
143
144        assert_eq!(2, messages.len());
145        assert!(matches!(&messages[0], FlightMessage::Schema(actual) if actual == &schema));
146        assert!(matches!(&messages[1], FlightMessage::RecordBatch(actual) if actual == &batch));
147    }
148}