Skip to content
Merged
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
5 changes: 5 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ changes go in their respective binding's NEXT_CHANGELOG.
certificate and private key are supplied atomically and must both be
non-empty.

- C ABI: `kernel_session_config_set_request_timeout()` configures the total
timeout for one HTTP request in milliseconds; `0` keeps the kernel's
120-second default. This enables Go and other C-ABI consumers to expose
request-timeout configuration.

### Changed

### Updated
Expand Down
7 changes: 7 additions & 0 deletions include/databricks_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ 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 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.
* `request_timeout_ms == 0` keeps the kernel default (120s). */
KernelStatusCode kernel_session_config_set_request_timeout(KernelSessionConfig* config,
uint64_t request_timeout_ms);

/* Initialize kernel logging, process-wide and ONCE (first non-OFF call wins;
* later calls are no-ops). `level` is OFF/ERROR/WARN/INFO/DEBUG/TRACE
* (NULL → RUST_LOG, default warn); `file_path` NULL → stderr. Not tied to
Expand Down
7 changes: 6 additions & 1 deletion pyo3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ license = "Apache-2.0"
description = "PyO3 Python bindings for the Databricks SQL Kernel"
repository = "https://github.com/databricks/databricks-sql-kernel"

[features]
# Unit-test binaries must link libpython. Maturin enables this feature only
# when building the extension module, where Python provides the symbols.
extension-module = ["pyo3/extension-module"]

# Built as a Python extension module by maturin. The lib basename here
# only affects the intermediate `.so` cargo produces; maturin renames
# the wheel artifact to `databricks_sql_kernel.<abi>.so` per
Expand Down Expand Up @@ -72,7 +77,7 @@ databricks-sql-kernel = { path = "..", default-features = false, features = ["tl
# step with `requires-python = ">=3.9"` in pyproject.toml; bumping
# both together avoids shipping a wheel whose ABI tag advertises an
# interpreter the package no longer claims to support.
pyo3 = { version = "0.29", features = ["extension-module", "abi3-py39"] }
pyo3 = { version = "0.29", features = ["abi3-py39"] }

# Multi-thread runtime — one per Session, owned on the Rust side.
tokio = { version = "1", features = ["rt-multi-thread"] }
Expand Down
3 changes: 3 additions & 0 deletions pyo3/NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ changes go in the root `NEXT_CHANGELOG.md`.

### Added

- `Session(..., request_timeout_secs=...)` configures the total timeout for one
HTTP request; unset or `0` keeps the kernel's 120-second default.

### Changed

### Updated
Expand Down
1 change: 1 addition & 0 deletions pyo3/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ dev = [

[tool.maturin]
module-name = "databricks_sql_kernel"
features = ["extension-module"]
143 changes: 113 additions & 30 deletions pyo3/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,71 @@ fn build_telemetry_config(
Ok(telemetry)
}

#[allow(clippy::too_many_arguments)]
fn build_http_config(
request_timeout_secs: Option<f64>,
retry_min_wait_secs: Option<u64>,
retry_max_wait_secs: Option<u64>,
retry_max_attempts: Option<u32>,
retry_overall_timeout_secs: Option<u64>,
http_headers: Option<Vec<(String, String)>>,
) -> PyResult<Option<databricks_sql_kernel::HttpConfig>> {
// Zero means "keep the kernel default", matching the C ABI setter. Use a
// float because databricks-sql-python's existing `_socket_timeout` accepts
// fractional seconds.
let request_timeout = match request_timeout_secs {
None => None,
Some(s) => {
if s == 0.0 {
None
} else {
let timeout = std::time::Duration::try_from_secs_f64(s).map_err(|_| {
pyo3::exceptions::PyValueError::new_err(
"request_timeout_secs must be finite and >= 0",
)
})?;
if timeout.is_zero() {
return Err(pyo3::exceptions::PyValueError::new_err(
"positive request_timeout_secs must round to at least 1 nanosecond",
));
}
Some(timeout)
}
}
};
let has_http_headers = http_headers.as_ref().is_some_and(|h| !h.is_empty());
if request_timeout.is_none()
&& retry_min_wait_secs.is_none()
&& retry_max_wait_secs.is_none()
&& retry_max_attempts.is_none()
&& retry_overall_timeout_secs.is_none()
&& !has_http_headers
{
return Ok(None);
}

let mut http = databricks_sql_kernel::HttpConfig::default();
if let Some(timeout) = request_timeout {
http.request_timeout = timeout;
}
if let Some(s) = retry_min_wait_secs {
http.retry_min_wait = std::time::Duration::from_secs(s);
}
if let Some(s) = retry_max_wait_secs {
http.retry_max_wait = std::time::Duration::from_secs(s);
}
if let Some(attempts) = retry_max_attempts {
http.retry_max_retries = attempts.saturating_sub(1);
}
if let Some(s) = retry_overall_timeout_secs {
http.overall_timeout = std::time::Duration::from_secs(s);
}
if let Some(headers) = http_headers {
http.custom_headers = headers;
}
Ok(Some(http))
}

#[allow(clippy::too_many_arguments)]
fn build_driver_system_configuration(
driver_name: Option<String>,
Expand Down Expand Up @@ -183,6 +248,12 @@ impl Session {
/// CloudFetch requests; token-endpoint (auth) calls keep a fixed
/// fast-fail policy.
///
/// `request_timeout_secs` sets 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. Unset or `0` keeps the kernel default
/// (120 seconds).
///
/// `http_headers` is a list of `(name, value)` pairs sent on every
/// request — used to forward the connector's caller headers (which
/// also carry the composed `User-Agent`). The kernel applies them
Expand Down Expand Up @@ -223,6 +294,7 @@ impl Session {
tls_skip_hostname_verify=false,
tls_client_cert=None,
tls_client_key=None,
request_timeout_secs=None,
retry_min_wait_secs=None,
retry_max_wait_secs=None,
retry_max_attempts=None,
Expand Down Expand Up @@ -279,6 +351,7 @@ impl Session {
tls_skip_hostname_verify: bool,
tls_client_cert: Option<Vec<u8>>,
tls_client_key: Option<Vec<u8>>,
request_timeout_secs: Option<f64>,
retry_min_wait_secs: Option<u64>,
retry_max_wait_secs: Option<u64>,
retry_max_attempts: Option<u32>,
Expand Down Expand Up @@ -371,9 +444,10 @@ impl Session {
None
};

// Build an HttpConfig only when the caller tuned retry/backoff
// or supplied custom headers; otherwise leave the builder on its
// defaults (1s/60s/5-retries, no extra headers).
// Build an HttpConfig only when the caller tuned the request timeout,
// retry/backoff, or supplied custom headers; otherwise leave the
// builder on its defaults (120s request timeout,
// 1s/60s/5-retries, no extra headers).
//
// `retry_max_attempts` is the **total** number of attempts
// (matching the connector's `_retry_stop_after_attempts_count`
Expand All @@ -387,33 +461,14 @@ impl Session {
// connector forwards its `http_headers`, which also carries the
// composed `User-Agent`). The kernel applies them before its own
// `Authorization` / `x-databricks-org-id`, which always win.
let has_http_headers = http_headers.as_ref().is_some_and(|h| !h.is_empty());
let http_config = if retry_min_wait_secs.is_some()
|| retry_max_wait_secs.is_some()
|| retry_max_attempts.is_some()
|| retry_overall_timeout_secs.is_some()
|| has_http_headers
{
let mut http = databricks_sql_kernel::HttpConfig::default();
if let Some(s) = retry_min_wait_secs {
http.retry_min_wait = std::time::Duration::from_secs(s);
}
if let Some(s) = retry_max_wait_secs {
http.retry_max_wait = std::time::Duration::from_secs(s);
}
if let Some(attempts) = retry_max_attempts {
http.retry_max_retries = attempts.saturating_sub(1);
}
if let Some(s) = retry_overall_timeout_secs {
http.overall_timeout = std::time::Duration::from_secs(s);
}
if let Some(headers) = http_headers {
http.custom_headers = headers;
}
Some(http)
} else {
None
};
let http_config = build_http_config(
request_timeout_secs,
retry_min_wait_secs,
retry_max_wait_secs,
retry_max_attempts,
retry_overall_timeout_secs,
http_headers,
)?;

let driver_system_configuration = build_driver_system_configuration(
driver_name,
Expand Down Expand Up @@ -635,6 +690,34 @@ mod send_sync_tests {
assert!(build_telemetry_config(false, None, None, None, None, Some(0)).is_err());
}

#[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)
.unwrap()
.is_none());
assert!(build_http_config(Some(0.0), None, None, None, None, None)
.unwrap()
.is_none());

let http = build_http_config(Some(0.5), 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));
assert_eq!(http.retry_min_wait, default.retry_min_wait);
assert_eq!(http.overall_timeout, default.overall_timeout);

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).is_err());
let http = build_http_config(Some(one_ns), 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).is_err());
assert!(build_http_config(Some(f64::NAN), None, None, None, None, None).is_err());
}

#[test]
fn driver_system_kwargs_map_to_kernel_config() {
assert!(build_driver_system_configuration(
Expand Down
Loading
Loading