diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index 380b525e..254cd71b 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -24,6 +24,9 @@ changes go in their respective binding's NEXT_CHANGELOG. ### Changed - Route function metadata through the Thrift-native metadata path when supported by the server. +- Authenticated SEA requests now use `Accept-Encoding: identity` by default to + avoid gzipping inline LZ4-compressed Arrow results. Response compression can + be re-enabled through `HttpConfig` or the C ABI. ### Updated diff --git a/include/databricks_kernel.h b/include/databricks_kernel.h index e0093a39..1c9817a5 100644 --- a/include/databricks_kernel.h +++ b/include/databricks_kernel.h @@ -349,6 +349,11 @@ KernelStatusCode kernel_session_config_set_session_conf(KernelSessionConfig* con KernelStatusCode kernel_session_config_set_custom_header(KernelSessionConfig* config, const char* name, const char* value); +/* Configure whether authenticated SEA responses may use HTTP compression. + * Disabled by default. */ +KernelStatusCode kernel_session_config_set_sea_response_compression_enabled( + KernelSessionConfig* config, bool enabled); + /* Configure the total timeout for one HTTP request, from connection start * through response-body completion. A separate 30s connection timeout also * applies, so connection establishment is bounded by the shorter limit. diff --git a/napi/NEXT_CHANGELOG.md b/napi/NEXT_CHANGELOG.md index 02f6246b..8c2fb66f 100644 --- a/napi/NEXT_CHANGELOG.md +++ b/napi/NEXT_CHANGELOG.md @@ -14,6 +14,9 @@ layout, or npm publishing. Pure-kernel changes go in the root ### Added +- Added `ConnectionOptions.seaResponseCompressionEnabled` to opt back into + HTTP compression for bandwidth-constrained SEA connections. + ### Changed ### Updated diff --git a/napi/index.d.ts b/napi/index.d.ts index aff1537c..57e595ff 100644 --- a/napi/index.d.ts +++ b/napi/index.d.ts @@ -505,6 +505,10 @@ export interface ConnectionOptions { * `User-Agent` entry here. */ customHeaders?: Array + /** + * Whether SEA API responses may use HTTP compression. Defaults to false. + */ + seaResponseCompressionEnabled?: boolean /** * Retry/backoff tuning — all optional. An unset field keeps the kernel's * built-in policy (1s/60s exponential backoff, 6 total attempts, 900s diff --git a/napi/src/database.rs b/napi/src/database.rs index c6fab22b..93e5b6dd 100644 --- a/napi/src/database.rs +++ b/napi/src/database.rs @@ -377,6 +377,9 @@ pub struct ConnectionOptions { /// `User-Agent` entry here. pub custom_headers: Option>, + /// Whether SEA API responses may use HTTP compression. Defaults to false. + pub sea_response_compression_enabled: Option, + /// Retry/backoff tuning — all optional. An unset field keeps the kernel's /// built-in policy (1s/60s exponential backoff, 6 total attempts, 900s /// budget). Mirrors the pyo3 binding's `retry_*` kwargs so the Node.js @@ -610,6 +613,8 @@ fn build_tls_config(options: &ConnectionOptions) -> TlsConfig { /// strings. fn build_http_config(options: &mut ConnectionOptions) -> HttpConfig { let mut http = HttpConfig::default(); + http.sea_response_compression_enabled = + options.sea_response_compression_enabled.unwrap_or(false); http.pool_max_idle_per_host = options .max_connections .map(|max| max as usize) @@ -922,6 +927,7 @@ mod tests { client_cert_pem: None, client_key_pem: None, custom_headers: None, + sea_response_compression_enabled: None, retry_min_wait_secs: None, retry_max_wait_secs: None, retry_max_attempts: None, @@ -1525,6 +1531,16 @@ mod tests { assert_eq!(http.custom_headers[1].1, "Connector/2.0"); } + #[test] + fn sea_response_compression_option_maps_to_http_config() { + let mut defaults = base(); + assert!(!build_http_config(&mut defaults).sea_response_compression_enabled); + + let mut enabled = base(); + enabled.sea_response_compression_enabled = Some(true); + assert!(build_http_config(&mut enabled).sea_response_compression_enabled); + } + #[test] fn http_retry_knobs_map_onto_http_config() { let mut o = base(); diff --git a/pyo3/NEXT_CHANGELOG.md b/pyo3/NEXT_CHANGELOG.md index e0881e54..819d7599 100644 --- a/pyo3/NEXT_CHANGELOG.md +++ b/pyo3/NEXT_CHANGELOG.md @@ -13,6 +13,9 @@ changes go in the root `NEXT_CHANGELOG.md`. ### Added +- Added `Session(..., sea_response_compression_enabled=...)` to opt back into + HTTP compression for bandwidth-constrained SEA connections. + ### Changed ### Updated diff --git a/pyo3/src/session.rs b/pyo3/src/session.rs index 01660521..2b12dc9d 100644 --- a/pyo3/src/session.rs +++ b/pyo3/src/session.rs @@ -142,6 +142,7 @@ fn build_telemetry_config( #[allow(clippy::too_many_arguments)] fn build_http_config( + sea_response_compression_enabled: bool, request_timeout_secs: Option, max_connections: Option, retry_min_wait_secs: Option, @@ -179,7 +180,8 @@ fn build_http_config( )); } let has_http_headers = http_headers.as_ref().is_some_and(|h| !h.is_empty()); - if request_timeout.is_none() + if !sea_response_compression_enabled + && request_timeout.is_none() && max_connections.is_none() && retry_min_wait_secs.is_none() && retry_max_wait_secs.is_none() @@ -191,6 +193,7 @@ fn build_http_config( } let mut http = databricks_sql_kernel::HttpConfig::default(); + http.sea_response_compression_enabled = sea_response_compression_enabled; if let Some(timeout) = request_timeout { http.request_timeout = timeout; } @@ -354,6 +357,7 @@ impl Session { tls_skip_hostname_verify=false, tls_client_cert=None, tls_client_key=None, + sea_response_compression_enabled=false, request_timeout_secs=None, max_connections=None, retry_min_wait_secs=None, @@ -419,6 +423,7 @@ impl Session { tls_skip_hostname_verify: bool, tls_client_cert: Option>, tls_client_key: Option>, + sea_response_compression_enabled: bool, request_timeout_secs: Option, max_connections: Option, retry_min_wait_secs: Option, @@ -536,6 +541,7 @@ impl Session { // composed `User-Agent`). The kernel applies them before its own // `Authorization` / `x-databricks-org-id`, which always win. let http_config = build_http_config( + sea_response_compression_enabled, request_timeout_secs, max_connections, retry_min_wait_secs, @@ -816,14 +822,14 @@ mod send_sync_tests { #[test] fn request_timeout_kwarg_maps_to_http_config() { let default = databricks_sql_kernel::HttpConfig::default(); - assert!(build_http_config(None, None, None, None, None, None, None) + assert!(build_http_config(false, None, None, None, None, None, None, None) .unwrap() .is_none()); - assert!(build_http_config(Some(0.0), None, None, None, None, None, None) + assert!(build_http_config(false, Some(0.0), None, None, None, None, None, None) .unwrap() .is_none()); - let http = build_http_config(Some(0.5), None, None, None, None, None, None) + let http = build_http_config(false, Some(0.5), None, None, None, None, None, None) .unwrap() .expect("positive request timeout should build an HTTP config"); assert_eq!(http.request_timeout, std::time::Duration::from_millis(500)); @@ -832,28 +838,39 @@ mod send_sync_tests { let one_ns = std::time::Duration::from_nanos(1).as_secs_f64(); assert!( - build_http_config(Some(one_ns / 1_000.0), None, None, None, None, None, None) + build_http_config(false, Some(one_ns / 1_000.0), None, None, None, None, None, None) .is_err() ); - let http = build_http_config(Some(one_ns), None, None, None, None, None, None) + let http = build_http_config(false, Some(one_ns), None, None, None, None, None, None) .unwrap() .expect("one-nanosecond request timeout should be accepted"); assert_eq!(http.request_timeout, std::time::Duration::from_nanos(1)); - assert!(build_http_config(Some(-1.0), None, None, None, None, None, None).is_err()); - assert!(build_http_config(Some(f64::NAN), None, None, None, None, None, None).is_err()); + assert!(build_http_config(false, Some(-1.0), None, None, None, None, None, None).is_err()); + assert!(build_http_config(false, Some(f64::NAN), None, None, None, None, None, None).is_err()); } #[test] fn max_connections_kwarg_maps_to_http_config() { let default = databricks_sql_kernel::HttpConfig::default(); - let http = build_http_config(None, Some(17), None, None, None, None, None) + let http = build_http_config(false, None, Some(17), None, None, None, None, None) .unwrap() .expect("max connections should build an HTTP config"); assert_eq!(http.pool_max_idle_per_host, 17); assert_eq!(http.request_timeout, default.request_timeout); - assert!(build_http_config(None, Some(0), None, None, None, None, None).is_err()); + assert!(build_http_config(false, None, Some(0), None, None, None, None, None).is_err()); + } + + #[test] + fn sea_response_compression_kwarg_maps_to_http_config() { + assert!(build_http_config(false, None, None, None, None, None, None, None) + .unwrap() + .is_none()); + let http = build_http_config(true, None, None, None, None, None, None, None) + .unwrap() + .expect("enabling SEA response compression should build an HTTP config"); + assert!(http.sea_response_compression_enabled); } #[test] diff --git a/src/c_abi/session.rs b/src/c_abi/session.rs index 86317dbf..8f1bfc6d 100644 --- a/src/c_abi/session.rs +++ b/src/c_abi/session.rs @@ -131,6 +131,8 @@ pub struct KernelSessionConfig { /// Extra HTTP headers sent on every request (accumulated by /// `..._set_custom_header`, one `(name, value)` pair per call). custom_headers: Vec<(String, String)>, + /// Whether authenticated SEA responses may use HTTP compression. + sea_response_compression_enabled: bool, /// Total request timeout (populated by `..._set_request_timeout`). `None` /// keeps [`HttpConfig::default`]'s 120-second timeout. request_timeout: Option, @@ -265,6 +267,7 @@ pub unsafe extern "C" fn kernel_session_config_new( proxy: None, tls: TlsSpec::default(), custom_headers: Vec::new(), + sea_response_compression_enabled: false, request_timeout: None, max_connections: None, retry: None, @@ -587,6 +590,22 @@ pub unsafe extern "C" fn kernel_session_config_set_custom_header( }) } +/// Configure whether authenticated SEA responses may use HTTP compression. +/// The default is `false`. +/// +/// # Safety +/// `config` must be a live config handle. +#[no_mangle] +pub unsafe extern "C" fn kernel_session_config_set_sea_response_compression_enabled( + config: *mut KernelSessionConfig, + enabled: bool, +) -> KernelStatusCode { + set_on_config(config, |c| { + c.sea_response_compression_enabled = enabled; + Ok(()) + }) +} + /// Configure the total timeout for one HTTP request, from connection start /// through response-body completion. A separate 30-second connection timeout /// also applies, so connection establishment is bounded by the shorter limit. @@ -1417,12 +1436,14 @@ fn parse_scopes(s: &str) -> Option> { /// kept unit-testable without a live session. fn merge_http_config( custom_headers: Vec<(String, String)>, + sea_response_compression_enabled: bool, request_timeout: Option, max_connections: Option, retry: Option, ) -> HttpConfig { let mut http = HttpConfig { custom_headers, + sea_response_compression_enabled, ..HttpConfig::default() }; if let Some(t) = request_timeout { @@ -1554,6 +1575,7 @@ fn build_session(config: KernelSessionConfig) -> Result Result Self { Self { + sea_response_compression_enabled: false, connect_timeout: Duration::from_secs(30), read_timeout: Duration::from_secs(60), max_connections_per_host: 100, @@ -877,6 +880,17 @@ impl DatabricksHttpClient { ); continue; } + if lname == "accept-encoding" + && with_auth + && !self.config.sea_response_compression_enabled + { + warn!( + header = %name, + "ignoring caller-supplied Accept-Encoding header because SEA response \ + compression is disabled" + ); + continue; + } req_builder = req_builder.header(name, value); } @@ -885,6 +899,9 @@ impl DatabricksHttpClient { } if with_auth { + if !self.config.sea_response_compression_enabled { + req_builder = req_builder.header("Accept-Encoding", "identity"); + } let auth_header = self.auth_header()?; req_builder = req_builder.header("Authorization", auth_header); // Extra auth headers (Azure SP management-token pair). Empty for @@ -1172,6 +1189,85 @@ mod tests { // custom header + caller UA present AND auth = real-token. } + #[tokio::test] + async fn authenticated_requests_use_identity_encoding_by_default() { + use wiremock::matchers::{header, method, path}; + use wiremock::{Mock, MockServer, ResponseTemplate}; + + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/probe")) + .and(header("accept-encoding", "identity")) + .respond_with(ResponseTemplate::new(200)) + .mount(&server) + .await; + + let config = HttpClientConfig { + custom_headers: vec![("Accept-Encoding".to_string(), "gzip".to_string())], + ..HttpClientConfig::default() + }; + let client = DatabricksHttpClient::with_default_retry(config).unwrap(); + client.set_auth_provider(Arc::new(PersonalAccessToken::new("token".to_string()))); + let request = client + .inner() + .get(format!("{}/probe", server.uri())) + .build() + .unwrap(); + let response = client + .execute(request, RequestType::GetStatementStatus) + .await + .unwrap(); + assert_eq!(response.status(), reqwest::StatusCode::OK); + + let requests = server.received_requests().await.unwrap(); + let accept_encodings: Vec<_> = requests[0] + .headers + .get_all("accept-encoding") + .iter() + .map(|value| value.to_str().unwrap()) + .collect(); + assert_eq!(accept_encodings, vec!["identity"]); + } + + #[tokio::test] + async fn authenticated_requests_can_enable_response_compression() { + use wiremock::matchers::{method, path}; + use wiremock::{Mock, MockServer, ResponseTemplate}; + + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/probe")) + .respond_with(ResponseTemplate::new(200)) + .mount(&server) + .await; + + let config = HttpClientConfig { + sea_response_compression_enabled: true, + custom_headers: vec![("Accept-Encoding".to_string(), "gzip".to_string())], + ..HttpClientConfig::default() + }; + let client = DatabricksHttpClient::with_default_retry(config).unwrap(); + client.set_auth_provider(Arc::new(PersonalAccessToken::new("token".to_string()))); + let request = client + .inner() + .get(format!("{}/probe", server.uri())) + .build() + .unwrap(); + client + .execute(request, RequestType::GetStatementStatus) + .await + .unwrap(); + + let requests = server.received_requests().await.unwrap(); + assert_eq!( + requests[0] + .headers + .get("accept-encoding") + .map(|value| value.to_str().unwrap()), + Some("gzip") + ); + } + #[test] fn test_caller_user_agent_replaces_base() { // A caller User-Agent fully replaces the base UA, and is removed diff --git a/src/config.rs b/src/config.rs index defaae55..28591559 100644 --- a/src/config.rs +++ b/src/config.rs @@ -748,6 +748,10 @@ impl std::fmt::Debug for AuthConfig { #[non_exhaustive] #[derive(Debug, Clone)] pub struct HttpConfig { + /// Whether authenticated SEA requests may negotiate HTTP response compression. + /// Defaults to `false` because inline Arrow results are already LZ4-compressed. + pub sea_response_compression_enabled: bool, + /// Total timeout for one HTTP request, from connection start through /// response-body completion. A separate 30-second connection timeout also /// applies, so connection establishment is bounded by the shorter limit. @@ -830,6 +834,7 @@ pub struct HttpConfig { impl Default for HttpConfig { fn default() -> Self { Self { + sea_response_compression_enabled: false, request_timeout: Duration::from_secs(120), overall_timeout: Duration::from_secs(900), pool_max_idle_per_host: 100, diff --git a/src/session.rs b/src/session.rs index b6a8ec4a..8775e984 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1046,6 +1046,7 @@ fn validate_mtls_endpoint_scheme(config: &SessionConfig) -> Result<()> { /// [`HttpTlsConfig`] via [`HttpTlsConfig::from_public`]. fn build_http_client_config(config: &SessionConfig, org_id: Option) -> HttpClientConfig { HttpClientConfig { + sea_response_compression_enabled: config.http.sea_response_compression_enabled, connect_timeout: Duration::from_secs(30), // The historical internal `read_timeout` field is passed to reqwest's // total request `.timeout()` and corresponds to public `request_timeout`. diff --git a/tests/c/session_smoke.c b/tests/c/session_smoke.c index f29bc018..bdb63e06 100644 --- a/tests/c/session_smoke.c +++ b/tests/c/session_smoke.c @@ -237,6 +237,9 @@ static void test_u2m_and_header_setters(void) { rc = kernel_session_config_set_custom_header(config, "X-Trace-Id", "abc123"); CHECK(rc == KernelStatusCode_Success, "set_custom_header succeeds"); + rc = kernel_session_config_set_sea_response_compression_enabled(config, true); + CHECK(rc == KernelStatusCode_Success, + "set_sea_response_compression_enabled succeeds"); rc = kernel_session_config_set_custom_header(config, NULL, "v"); CHECK(rc == KernelStatusCode_InvalidArgument, "set_custom_header(NULL name) → InvalidArgument"); rc = kernel_session_config_set_custom_header(config, "X-Foo", NULL);