Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions arrow-flight/examples/flight_sql_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ use prost::Message;
use std::collections::HashSet;
use std::pin::Pin;
use std::str::FromStr;
use std::sync::{Arc, LazyLock};
use std::sync::{Arc, LazyLock, Mutex};
use tonic::metadata::MetadataValue;
use tonic::transport::Server;
use tonic::transport::{Certificate, Identity, ServerTlsConfig};
use tonic::{Request, Response, Status, Streaming};
use uuid::Uuid;

use arrow_array::builder::StringBuilder;
use arrow_array::{ArrayRef, RecordBatch};
Expand All @@ -39,8 +40,9 @@ use arrow_flight::sql::{
};
use arrow_flight::utils::batches_to_flight_data;
use arrow_flight::{
Action, FlightData, FlightDescriptor, FlightEndpoint, FlightInfo, HandshakeRequest,
HandshakeResponse, IpcMessage, SchemaAsIpc, Ticket, flight_service_server::FlightService,
Action, CloseSessionRequest, CloseSessionResult, FlightData, FlightDescriptor, FlightEndpoint,
FlightInfo, HandshakeRequest, HandshakeResponse, IpcMessage, SchemaAsIpc, Ticket,
close_session_result::Status as CloseSessionStatus, flight_service_server::FlightService,
flight_service_server::FlightServiceServer,
};
use arrow_ipc::writer::IpcWriteOptions;
Expand All @@ -52,7 +54,6 @@ macro_rules! status {
};
}

const FAKE_TOKEN: &str = "uuid_token";
const FAKE_HANDLE: &str = "uuid_handle";
const FAKE_UPDATE_RESULT: i64 = 1;

Expand Down Expand Up @@ -94,11 +95,13 @@ static INSTANCE_XBDC_DATA: LazyLock<XdbcTypeInfoData> = LazyLock::new(|| {

static TABLES: LazyLock<Vec<&'static str>> = LazyLock::new(|| vec!["flight_sql.example.table"]);

#[derive(Clone)]
pub struct FlightSqlServiceImpl {}
#[derive(Clone, Default)]
pub struct FlightSqlServiceImpl {
sessions: Arc<Mutex<HashSet<String>>>,
}

impl FlightSqlServiceImpl {
fn check_token<T>(&self, req: &Request<T>) -> Result<(), Status> {
fn get_token<T>(&self, req: &Request<T>) -> Result<String, Status> {
let metadata = req.metadata();
let auth = metadata.get("authorization").ok_or_else(|| {
Status::internal(format!("No authorization header! metadata = {metadata:?}"))
Expand All @@ -111,8 +114,12 @@ impl FlightSqlServiceImpl {
if !authorization.starts_with(bearer) {
Err(Status::internal("Invalid auth header!"))?;
}
let token = authorization[bearer.len()..].to_string();
if token == FAKE_TOKEN {
Ok(authorization[bearer.len()..].to_string())
}

fn check_token<T>(&self, req: &Request<T>) -> Result<(), Status> {
let token = self.get_token(req)?;
if self.sessions.lock().unwrap().contains(&token) {
Ok(())
} else {
Err(Status::unauthenticated("invalid token "))
Expand Down Expand Up @@ -167,14 +174,17 @@ impl FlightSqlService for FlightSqlServiceImpl {
Err(Status::unauthenticated("Invalid credentials!"))?
}

let session_id = Uuid::new_v4().to_string();
self.sessions.lock().unwrap().insert(session_id.clone());
println!("Created session: {session_id}");
let result = HandshakeResponse {
protocol_version: 0,
payload: FAKE_TOKEN.into(),
payload: session_id.clone().into(),
};
let result = Ok(result);
let output = futures::stream::iter(vec![result]);

let token = format!("Bearer {FAKE_TOKEN}");
let token = format!("Bearer {session_id}");
let mut response: Response<Pin<Box<dyn Stream<Item = _> + Send>>> =
Response::new(Box::pin(output));
response.metadata_mut().append(
Expand Down Expand Up @@ -661,6 +671,22 @@ impl FlightSqlService for FlightSqlServiceImpl {
Ok(res)
}

async fn do_action_close_session(
&self,
_query: CloseSessionRequest,
request: Request<Action>,
) -> Result<CloseSessionResult, Status> {
let token = self.get_token(&request)?;
if !self.sessions.lock().unwrap().remove(&token) {
println!("CloseSession: NotFound");
return Err(Status::not_found("Session not found"));
}
println!("CloseSession: Closed");
Ok(CloseSessionResult {
status: CloseSessionStatus::Closed as i32,
})
}

async fn do_action_close_prepared_statement(
&self,
_query: ActionClosePreparedStatementRequest,
Expand Down Expand Up @@ -735,7 +761,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let key = std::fs::read_to_string("arrow-flight/examples/data/server.key")?;
let client_ca = std::fs::read_to_string("arrow-flight/examples/data/ca_root.pem")?;

let svc = FlightServiceServer::new(FlightSqlServiceImpl {});
let svc = FlightServiceServer::new(FlightSqlServiceImpl::default());
let tls_config = ServerTlsConfig::new()
.identity(Identity::from_pem(&cert, &key))
.client_ca_root(Certificate::from_pem(&client_ca));
Expand All @@ -746,7 +772,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.serve(addr)
.await?;
} else {
let svc = FlightServiceServer::new(FlightSqlServiceImpl {});
let svc = FlightServiceServer::new(FlightSqlServiceImpl::default());

Server::builder().add_service(svc).serve(addr).await?;
}
Expand Down Expand Up @@ -832,7 +858,7 @@ mod tests {
let uds = UnixListener::bind(path.clone()).unwrap();
let stream = UnixListenerStream::new(uds);

let service = FlightSqlServiceImpl {};
let service = FlightSqlServiceImpl::default();
let serve_future = Server::builder()
.add_service(FlightServiceServer::new(service))
.serve_with_incoming(stream);
Expand Down Expand Up @@ -863,7 +889,7 @@ mod tests {
let (incoming, addr) = bind_tcp().await;
let uri = format!("http://{}:{}", addr.ip(), addr.port());

let service = FlightSqlServiceImpl {};
let service = FlightSqlServiceImpl::default();
let serve_future = Server::builder()
.add_service(FlightServiceServer::new(service))
.serve_with_incoming(incoming);
Expand Down Expand Up @@ -899,7 +925,7 @@ mod tests {
let (incoming, addr) = bind_tcp().await;
let uri = format!("https://{}:{}", addr.ip(), addr.port());

let svc = FlightServiceServer::new(FlightSqlServiceImpl {});
let svc = FlightServiceServer::new(FlightSqlServiceImpl::default());

let serve_future = Server::builder()
.tls_config(tls_config)
Expand Down
70 changes: 70 additions & 0 deletions arrow-flight/src/arrow.flight.protocol.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions arrow-flight/src/bin/flight_sql_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ struct Args {
/// Different available commands.
#[derive(Debug, Subcommand)]
enum Command {
/// Close the current server session.
CloseSession,
/// Get catalogs.
Catalogs,
/// Get db schemas for a catalog.
Expand Down Expand Up @@ -243,6 +245,11 @@ async fn main() -> Result<()> {
.context("setup client")?;

let flight_info = match args.cmd {
Command::CloseSession => {
let result = client.close().await.context("close session")?;
println!("{result:?}");
return Ok(());
}
Command::Catalogs => client.get_catalogs().await.context("get catalogs")?,
Command::DbSchemas {
catalog,
Expand Down
8 changes: 8 additions & 0 deletions arrow-flight/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ pub mod flight_descriptor {
pub use r#gen::flight_descriptor::DescriptorType;
}

/// Defines the status returned when closing a session.
pub mod close_session_result {
use super::r#gen;
pub use r#gen::close_session_result::Status;
}

/// Low Level [tonic] [`FlightServiceClient`](gen::flight_service_client::FlightServiceClient).
pub mod flight_service_client {
use super::r#gen;
Expand Down Expand Up @@ -100,6 +106,8 @@ pub use r#gen::BasicAuth;
pub use r#gen::CancelFlightInfoRequest;
pub use r#gen::CancelFlightInfoResult;
pub use r#gen::CancelStatus;
pub use r#gen::CloseSessionRequest;
pub use r#gen::CloseSessionResult;
pub use r#gen::Criteria;
pub use r#gen::Empty;
pub use r#gen::FlightData;
Expand Down
27 changes: 16 additions & 11 deletions arrow-flight/src/sql/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::encode::FlightDataEncoderBuilder;
use crate::error::FlightError;
use crate::error::Result;
use crate::flight_service_client::FlightServiceClient;
use crate::r#gen::CloseSessionRequest;
use crate::sql::r#gen::action_end_transaction_request::EndTransaction;
use crate::sql::server::{
BEGIN_TRANSACTION, CLOSE_PREPARED_STATEMENT, CREATE_PREPARED_STATEMENT, END_TRANSACTION,
Expand All @@ -39,8 +40,8 @@ use crate::sql::{
use crate::streams::FallibleRequestStream;
use crate::trailers::extract_lazy_trailers;
use crate::{
Action, FlightData, FlightDescriptor, FlightInfo, HandshakeRequest, HandshakeResponse,
IpcMessage, PutResult, Ticket,
Action, CloseSessionResult, FlightData, FlightDescriptor, FlightInfo, HandshakeRequest,
HandshakeResponse, IpcMessage, PutResult, Ticket,
};
use arrow_array::RecordBatch;
use arrow_schema::{ArrowError, Schema};
Expand Down Expand Up @@ -442,15 +443,19 @@ where
Ok(())
}

/// Explicitly shut down and clean up the client.
#[expect(
clippy::unused_async,
clippy::unused_async_trait_impl,
reason = "public API: dropping `async` would break callers that `.await` it"
)]
pub async fn close(&mut self) -> Result<()> {
// TODO: consume self instead of &mut self to explicitly prevent reuse?
Ok(())
/// Close the current server session using the configured request headers.
///
/// Returns the server's close status for the caller to handle.
pub async fn close(&mut self) -> Result<CloseSessionResult> {
let action = Action::new("CloseSession", CloseSessionRequest {}.encode_to_vec());
let mut results = self.do_action(action).await?;
let result = results.message().await?.ok_or_else(|| {
FlightError::protocol("Server closed the stream without sending a result")
})?;
let result = CloseSessionResult::decode(result.body)?;
while results.message().await?.is_some() {}

Ok(result)
}

fn set_request_headers<M>(&self, mut req: tonic::Request<M>) -> Result<tonic::Request<M>> {
Expand Down
Loading
Loading