@@ -38,7 +38,7 @@ use crate::types::sea::{
3838 StatementExecutionResponse , StatementState ,
3939} ;
4040use async_trait:: async_trait;
41- use reqwest:: header:: { HeaderMap , HeaderValue } ;
41+ use reqwest:: header:: HeaderValue ;
4242use reqwest:: Method ;
4343use std:: collections:: HashMap ;
4444use std:: sync:: atomic:: { AtomicBool , AtomicI64 , Ordering } ;
@@ -76,27 +76,7 @@ enum StatementOutcome {
7676#[ derive( Debug ) ]
7777struct SessionRoutingHeaders {
7878 direct_routing_enabled : bool ,
79- dbsql_session_id : Option < HeaderValue > ,
80- }
81-
82- impl SessionRoutingHeaders {
83- fn from_response ( headers : & HeaderMap ) -> Self {
84- // The response marks the session for real-time routing with REALTIME_SESSION_HEADER;
85- // follow-up requests select that route with REALTIME_DIRECT_ROUTING_HEADER.
86- let direct_routing_enabled = headers
87- . get ( REALTIME_SESSION_HEADER )
88- . and_then ( |value| value. to_str ( ) . ok ( ) )
89- . is_some_and ( |value| value. eq_ignore_ascii_case ( "true" ) ) ;
90- let dbsql_session_id = headers
91- . get ( DBSQL_SESSION_ID_HEADER )
92- . filter ( |value| value. to_str ( ) . is_ok_and ( |value| !value. trim ( ) . is_empty ( ) ) )
93- . cloned ( ) ;
94-
95- Self {
96- direct_routing_enabled,
97- dbsql_session_id,
98- }
99- }
79+ dbsql_session_id : HeaderValue ,
10080}
10181
10282/// Mapped terminal outcome from the synchronous execution phase.
@@ -241,9 +221,7 @@ impl SeaRestTransport {
241221 if headers. direct_routing_enabled {
242222 builder = builder. header ( REALTIME_DIRECT_ROUTING_HEADER , "true" ) ;
243223 }
244- if let Some ( dbsql_session_id) = & headers. dbsql_session_id {
245- builder = builder. header ( DBSQL_SESSION_ID_HEADER , dbsql_session_id. clone ( ) ) ;
246- }
224+ builder = builder. header ( DBSQL_SESSION_ID_HEADER , headers. dbsql_session_id . clone ( ) ) ;
247225 }
248226 builder
249227 }
@@ -1124,7 +1102,13 @@ impl Transport for SeaRestTransport {
11241102 . http_client
11251103 . execute ( request, RequestType :: CreateSession )
11261104 . await ?;
1127- let session_routing_headers = SessionRoutingHeaders :: from_response ( response. headers ( ) ) ;
1105+ // The response marks the session for real-time routing with REALTIME_SESSION_HEADER;
1106+ // follow-up requests select that route with REALTIME_DIRECT_ROUTING_HEADER.
1107+ let direct_routing_enabled = response
1108+ . headers ( )
1109+ . get ( REALTIME_SESSION_HEADER )
1110+ . and_then ( |value| value. to_str ( ) . ok ( ) )
1111+ . is_some_and ( |value| value. eq_ignore_ascii_case ( "true" ) ) ;
11281112 let body = response
11291113 . text ( )
11301114 . await
@@ -1135,9 +1119,18 @@ impl Transport for SeaRestTransport {
11351119 "Failed to parse session response: {e} - body: {body}"
11361120 ) )
11371121 } ) ?;
1122+ let dbsql_session_id = HeaderValue :: try_from ( session_response. session_id . as_str ( ) )
1123+ . map_err ( |e| {
1124+ Error :: data_loss ( ) . message ( format ! (
1125+ "CreateSession returned a session_id that cannot be used as an HTTP header: {e}"
1126+ ) )
1127+ } ) ?;
11381128
11391129 self . session_routing_headers
1140- . set ( session_routing_headers)
1130+ . set ( SessionRoutingHeaders {
1131+ direct_routing_enabled,
1132+ dbsql_session_id,
1133+ } )
11411134 . map_err ( |_| {
11421135 Error :: invalid_state ( ) . message ( "SEA transport already created a session" )
11431136 } ) ?;
@@ -2580,7 +2573,6 @@ mod tests {
25802573 . respond_with (
25812574 ResponseTemplate :: new ( 200 )
25822575 . insert_header ( REALTIME_SESSION_HEADER , "true" )
2583- . insert_header ( DBSQL_SESSION_ID_HEADER , "routing-session-1" )
25842576 . set_body_json ( json ! ( {
25852577 "session_id" : "session-1" ,
25862578 "session_version" : { "version_id" : 7 } ,
@@ -2593,7 +2585,7 @@ mod tests {
25932585 Mock :: given ( method ( "POST" ) )
25942586 . and ( path ( "/api/2.0/sql/statements" ) )
25952587 . and ( header ( REALTIME_DIRECT_ROUTING_HEADER , "true" ) )
2596- . and ( header ( DBSQL_SESSION_ID_HEADER , "routing- session-1" ) )
2588+ . and ( header ( DBSQL_SESSION_ID_HEADER , "session-1" ) )
25972589 . respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( json ! ( {
25982590 "statement_id" : "statement-1" ,
25992591 "status" : { "state" : "PENDING" } ,
@@ -2604,7 +2596,7 @@ mod tests {
26042596 Mock :: given ( method ( "GET" ) )
26052597 . and ( path ( "/api/2.0/sql/statements/statement-1" ) )
26062598 . and ( header ( REALTIME_DIRECT_ROUTING_HEADER , "true" ) )
2607- . and ( header ( DBSQL_SESSION_ID_HEADER , "routing- session-1" ) )
2599+ . and ( header ( DBSQL_SESSION_ID_HEADER , "session-1" ) )
26082600 . respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( json ! ( {
26092601 "statement_id" : "statement-1" ,
26102602 "status" : { "state" : "RUNNING" } ,
@@ -2615,7 +2607,7 @@ mod tests {
26152607 Mock :: given ( method ( "GET" ) )
26162608 . and ( path ( "/api/2.0/sql/statements/statement-1/result/chunks/0" ) )
26172609 . and ( header ( REALTIME_DIRECT_ROUTING_HEADER , "true" ) )
2618- . and ( header ( DBSQL_SESSION_ID_HEADER , "routing- session-1" ) )
2610+ . and ( header ( DBSQL_SESSION_ID_HEADER , "session-1" ) )
26192611 . respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( json ! ( {
26202612 "external_links" : [ ] ,
26212613 "next_chunk_index" : null,
@@ -2629,7 +2621,7 @@ mod tests {
26292621 ) )
26302622 . and ( query_param ( "row_offset" , "3" ) )
26312623 . and ( header ( REALTIME_DIRECT_ROUTING_HEADER , "true" ) )
2632- . and ( header ( DBSQL_SESSION_ID_HEADER , "routing- session-1" ) )
2624+ . and ( header ( DBSQL_SESSION_ID_HEADER , "session-1" ) )
26332625 . respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( json ! ( {
26342626 "chunk_index" : 1 ,
26352627 "row_offset" : 3 ,
@@ -2643,15 +2635,15 @@ mod tests {
26432635 Mock :: given ( method ( "POST" ) )
26442636 . and ( path ( "/api/2.0/sql/statements/statement-1/cancel" ) )
26452637 . and ( header ( REALTIME_DIRECT_ROUTING_HEADER , "true" ) )
2646- . and ( header ( DBSQL_SESSION_ID_HEADER , "routing- session-1" ) )
2638+ . and ( header ( DBSQL_SESSION_ID_HEADER , "session-1" ) )
26472639 . respond_with ( ResponseTemplate :: new ( 200 ) )
26482640 . expect ( 1 )
26492641 . mount ( & server)
26502642 . await ;
26512643 Mock :: given ( method ( "DELETE" ) )
26522644 . and ( path ( "/api/2.0/sql/statements/statement-1" ) )
26532645 . and ( header ( REALTIME_DIRECT_ROUTING_HEADER , "true" ) )
2654- . and ( header ( DBSQL_SESSION_ID_HEADER , "routing- session-1" ) )
2646+ . and ( header ( DBSQL_SESSION_ID_HEADER , "session-1" ) )
26552647 . respond_with ( ResponseTemplate :: new ( 200 ) )
26562648 . expect ( 1 )
26572649 . mount ( & server)
@@ -2660,7 +2652,7 @@ mod tests {
26602652 . and ( path ( "/api/2.0/sql/sessions/session-1" ) )
26612653 . and ( query_param ( "warehouse_id" , "warehouse-123" ) )
26622654 . and ( header ( REALTIME_DIRECT_ROUTING_HEADER , "true" ) )
2663- . and ( header ( DBSQL_SESSION_ID_HEADER , "routing- session-1" ) )
2655+ . and ( header ( DBSQL_SESSION_ID_HEADER , "session-1" ) )
26642656 . respond_with ( ResponseTemplate :: new ( 200 ) )
26652657 . expect ( 1 )
26662658 . mount ( & server)
@@ -2710,28 +2702,20 @@ mod tests {
27102702 }
27112703
27122704 #[ tokio:: test]
2713- async fn create_session_response_headers_are_applied_independently ( ) {
2705+ async fn create_session_response_header_only_controls_direct_routing ( ) {
27142706 use wiremock:: matchers:: { header, method, path} ;
27152707 use wiremock:: { Mock , MockServer , ResponseTemplate } ;
27162708
2717- let cases = [
2718- ( Some ( "TRUE" ) , None , true , false ) ,
2719- ( None , Some ( "routing-session-1" ) , false , true ) ,
2720- ( Some ( "false" ) , Some ( "routing-session-1" ) , false , true ) ,
2721- ( None , None , false , false ) ,
2722- ] ;
2709+ let cases = [ ( Some ( "TRUE" ) , true ) , ( Some ( "false" ) , false ) , ( None , false ) ] ;
27232710
2724- for ( routing_enabled, routing_session_id , expect_routing, expect_session_id ) in cases {
2711+ for ( routing_enabled, expect_routing) in cases {
27252712 let server = MockServer :: start ( ) . await ;
27262713 let mut create_response = ResponseTemplate :: new ( 200 ) . set_body_json ( json ! ( {
27272714 "session_id" : "session-1" ,
27282715 } ) ) ;
27292716 if let Some ( value) = routing_enabled {
27302717 create_response = create_response. insert_header ( REALTIME_SESSION_HEADER , value) ;
27312718 }
2732- if let Some ( value) = routing_session_id {
2733- create_response = create_response. insert_header ( DBSQL_SESSION_ID_HEADER , value) ;
2734- }
27352719
27362720 Mock :: given ( method ( "POST" ) )
27372721 . and ( path ( "/api/2.0/sql/sessions" ) )
@@ -2773,8 +2757,11 @@ mod tests {
27732757 expect_routing
27742758 ) ;
27752759 assert_eq ! (
2776- execute. headers. get( DBSQL_SESSION_ID_HEADER ) . is_some( ) ,
2777- expect_session_id
2760+ execute
2761+ . headers
2762+ . get( DBSQL_SESSION_ID_HEADER )
2763+ . and_then( |value| value. to_str( ) . ok( ) ) ,
2764+ Some ( "session-1" )
27782765 ) ;
27792766 }
27802767 }
@@ -2791,7 +2778,6 @@ mod tests {
27912778 . respond_with (
27922779 ResponseTemplate :: new ( 200 )
27932780 . insert_header ( REALTIME_SESSION_HEADER , "true" )
2794- . insert_header ( DBSQL_SESSION_ID_HEADER , "routing-session-1" )
27952781 . set_body_json ( json ! ( { "session_id" : "session-1" } ) ) ,
27962782 )
27972783 . expect ( 1 )
0 commit comments