From 4a27bc148ed3a6c612a9d024a114915a02a5cb93 Mon Sep 17 00:00:00 2001 From: cat23123 <58714163+cat23123@users.noreply.github.com> Date: Tue, 22 Sep 2026 00:53:19 +0000 Subject: [PATCH] feat(feature-flags): hand off client identity from bindings Signed-off-by: cat23123 <58714163+cat23123@users.noreply.github.com> --- NEXT_CHANGELOG.md | 2 + include/databricks_kernel.h | 19 ++++ napi/NEXT_CHANGELOG.md | 3 + napi/index.d.ts | 5 +- napi/src/database.rs | 31 +++++- pyo3/NEXT_CHANGELOG.md | 3 + pyo3/src/session.rs | 37 +++++-- src/c_abi/session.rs | 192 ++++++++++++++++++++++++++++++++++++ tests/c/session_smoke.c | 6 ++ 9 files changed, 286 insertions(+), 12 deletions(-) diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index 6fbc09fb..0bc431c8 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -14,6 +14,8 @@ changes go in their respective binding's NEXT_CHANGELOG. - Added Thrift-native metadata routing with compatible public-schema casts and one-based `getColumns` ordinals. - Add connector-service feature-flag resolution with authenticated requests, JSON-serialized per-driver values, and bounded process-wide caching. +- Add a C ABI client-identity setter so native drivers can use kernel-owned + connector-service feature flags without duplicate calls. ### Changed diff --git a/include/databricks_kernel.h b/include/databricks_kernel.h index 684639c5..9ab0c6b1 100644 --- a/include/databricks_kernel.h +++ b/include/databricks_kernel.h @@ -227,6 +227,16 @@ typedef struct kernel_statement_canceller_t kernel_statement_canceller_t; * stream is permanently aborted (every later next_batch returns Cancelled). */ typedef struct kernel_result_stream_canceller_t kernel_result_stream_canceller_t; +/* Outer driver family used by connector-service feature-flag evaluation. + * This is an integer rather than a C enum so every value crosses the FFI + * boundary safely; the setter returns InvalidArgument for unknown values. */ +typedef uint32_t KernelClientType; +enum { + KernelClientType_Go = 1, + KernelClientType_NodeJs = 2, + KernelClientType_Python = 5, +}; + /* ─── Session config ────────────────────────────────────────────────── * * Build a config, set exactly one connection form and exactly one auth @@ -237,6 +247,15 @@ typedef struct kernel_result_stream_canceller_t kernel_result_stream_canceller_t KernelStatusCode kernel_session_config_new(KernelSessionConfig** out); void kernel_session_config_free(KernelSessionConfig* config); +/* Optional outer-driver identity for connector-service feature flags. Omitting + * this setter preserves legacy behavior and performs no feature-flag request. + * `version` must be non-NULL UTF-8 containing at least one non-whitespace + * character. The kernel owns fetching and caching; callers must not issue a + * duplicate request on the kernel path. */ +KernelStatusCode kernel_session_config_set_client_identity(KernelSessionConfig* config, + KernelClientType client_type, + const char* version); + /* Connection identity — call exactly one; the last one wins. */ KernelStatusCode kernel_session_config_set_warehouse(KernelSessionConfig* config, const char* host, diff --git a/napi/NEXT_CHANGELOG.md b/napi/NEXT_CHANGELOG.md index 02f6246b..865501e4 100644 --- a/napi/NEXT_CHANGELOG.md +++ b/napi/NEXT_CHANGELOG.md @@ -16,6 +16,9 @@ layout, or npm publishing. Pure-kernel changes go in the root ### Changed +- Use a non-empty `driverVersion` as the Node.js client identity for + connector-service feature flags. + ### Updated ### Fixed diff --git a/napi/index.d.ts b/napi/index.d.ts index 1c66b437..bceeae3b 100644 --- a/napi/index.d.ts +++ b/napi/index.d.ts @@ -324,8 +324,9 @@ export interface ConnectionOptions { */ driverName?: string /** - * Driver version reported in telemetry system configuration. Omitted ⇒ - * kernel default. + * Driver version reported in telemetry system configuration and used for + * connector-service feature-flag evaluation. Omitted ⇒ kernel default and + * no feature-flag request. */ driverVersion?: string /** diff --git a/napi/src/database.rs b/napi/src/database.rs index c6fab22b..ae7982dc 100644 --- a/napi/src/database.rs +++ b/napi/src/database.rs @@ -34,8 +34,9 @@ use napi::bindgen_prelude::Buffer; type Buffer = Vec; use databricks_sql_kernel::{ - AuthConfig, DefaultOpts, DriverSystemConfiguration, Error as KernelError, HttpConfig, - ProxyConfig, ResultConfig, Session, TelemetryConfig, TlsConfig, TokenCacheConfig, + AuthConfig, ClientIdentity, ClientType, DefaultOpts, DriverSystemConfiguration, + Error as KernelError, HttpConfig, ProxyConfig, ResultConfig, Session, TelemetryConfig, + TlsConfig, TokenCacheConfig, }; /// NodeJS-driver-appropriate default for `pool_max_idle_per_host`. @@ -234,8 +235,9 @@ pub struct ConnectionOptions { /// Driver name reported in telemetry system configuration. Omitted ⇒ /// kernel default. pub driver_name: Option, - /// Driver version reported in telemetry system configuration. Omitted ⇒ - /// kernel default. + /// Driver version reported in telemetry system configuration and used for + /// connector-service feature-flag evaluation. Omitted ⇒ kernel default and + /// no feature-flag request. pub driver_version: Option, /// Runtime name reported in telemetry system configuration. Omitted ⇒ /// kernel default. @@ -763,6 +765,12 @@ fn build_driver_system_configuration( }) } +fn build_client_identity(driver_version: Option<&str>) -> Option { + driver_version + .filter(|version| !version.trim().is_empty()) + .map(|version| ClientIdentity::new(ClientType::NodeJs, version)) +} + /// Open a Databricks SQL session and return an opaque `Connection` /// wrapping the kernel `Session`. Authentication is selected by /// `options.auth_mode` (PAT / OAuth M2M / OAuth U2M) — see @@ -798,6 +806,7 @@ pub async fn open_session(mut options: ConnectionOptions) -> napi::Result napi::Result) -> Option { + driver_version + .filter(|version| !version.trim().is_empty()) + .map(|version| ClientIdentity::new(ClientType::Python, version)) +} + #[pymethods] impl Session { /// Open a new session against the given workspace. @@ -325,6 +331,9 @@ impl Session { /// presets, etc. The kernel does not validate keys; the server /// rejects unknown ones with a `KernelError(code='SqlError')` /// when it picks them up. + /// + /// A non-empty `driver_version` also identifies the Python driver for + /// connector-service feature flags. Omitting it performs no flag request. #[new] #[pyo3(signature = ( host, @@ -545,6 +554,7 @@ impl Session { http_headers, )?; + let client_identity = build_client_identity(driver_version.as_deref()); let driver_system_configuration = build_driver_system_configuration( driver_name, driver_version, @@ -616,6 +626,9 @@ impl Session { if let Some(system_configuration) = driver_system_configuration { builder = builder.driver_system_configuration(system_configuration); } + if let Some(identity) = client_identity { + builder = builder.client_identity(identity); + } if catalog.is_some() || schema.is_some() { let mut defaults = databricks_sql_kernel::DefaultOpts::default(); defaults.catalog = catalog; @@ -771,6 +784,17 @@ mod send_sync_tests { ); } + #[test] + fn driver_version_builds_python_client_identity() { + assert!(build_client_identity(None).is_none()); + assert!(build_client_identity(Some("")).is_none()); + assert!(build_client_identity(Some(" \t")).is_none()); + + let identity = build_client_identity(Some("1.2.3")).unwrap(); + assert_eq!(identity.client_type(), ClientType::Python); + assert_eq!(identity.version(), "1.2.3"); + } + #[test] fn telemetry_kwargs_reject_zero_required_fields() { assert!(build_telemetry_config(TelemetryKwargs { @@ -819,9 +843,11 @@ mod send_sync_tests { assert!(build_http_config(None, None, None, None, None, None, None) .unwrap() .is_none()); - assert!(build_http_config(Some(0.0), None, None, None, None, None, None) - .unwrap() - .is_none()); + assert!( + build_http_config(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) .unwrap() @@ -832,8 +858,7 @@ 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) - .is_err() + build_http_config(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) .unwrap() diff --git a/src/c_abi/session.rs b/src/c_abi/session.rs index 86317dbf..e2530a0a 100644 --- a/src/c_abi/session.rs +++ b/src/c_abi/session.rs @@ -30,6 +30,7 @@ use crate::config::{ AuthConfig, ConnectionConfig, HttpConfig, ProxyConfig, ResultConfig, TelemetryConfig, TlsConfig, TokenCacheConfig, }; +use crate::feature_flags::{ClientIdentity, ClientType}; use crate::kernel_error::Error; use crate::logging::{LoggingOutcome, LoggingSink}; use crate::session::Session; @@ -44,6 +45,34 @@ pub struct kernel_session_t { _private: [u8; 0], } +/// Driver family sent to the connector-service feature-flag endpoint. +/// +/// This is a transparent integer wrapper rather than a Rust enum because C can +/// pass any 32-bit value. Every bit pattern is valid at the ABI boundary; the +/// setter validates the known values before constructing a [`ClientType`]. +#[repr(transparent)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct KernelClientType(u32); + +impl KernelClientType { + pub const GO: Self = Self(1); + pub const NODE_JS: Self = Self(2); + pub const PYTHON: Self = Self(5); +} + +impl TryFrom for ClientType { + type Error = u32; + + fn try_from(value: KernelClientType) -> Result { + match value.0 { + 1 => Ok(Self::Go), + 2 => Ok(Self::NodeJs), + 5 => Ok(Self::Python), + unknown => Err(unknown), + } + } +} + /// How the caller chose to identify the warehouse. enum ConnectionSpec { /// `host` + bare `warehouse_id`. @@ -182,6 +211,9 @@ pub struct KernelSessionConfig { /// Telemetry collection/export knobs. Defaults to disabled; populated by /// `kernel_session_config_set_telemetry_config`. telemetry: TelemetryConfig, + /// Outer driver identity used for connector-service feature flags. `None` + /// preserves the legacy path and performs no feature-flag request. + client_identity: Option, /// Driver/runtime/system identity supplied by the binding layer. `None` /// lets the kernel use only its own defaults; `Some` lets the kernel fill /// any omitted fields it knows how to derive. @@ -278,6 +310,7 @@ pub unsafe extern "C" fn kernel_session_config_new( // Kernel result defaults (CloudFetch enabled, Arrow-native types). result: ResultConfig::default(), telemetry: TelemetryConfig::default(), + client_identity: None, driver_system_configuration: None, }); std::ptr::write(out, Box::into_raw(config)); @@ -298,6 +331,39 @@ pub unsafe extern "C" fn kernel_session_config_free(config: *mut KernelSessionCo } } +/// Set the outer driver's family and version for connector-service feature +/// flags. The kernel reuses its authenticated HTTP client and process-wide +/// cache; the native driver must not make a second request on the kernel path. +/// +/// This setter is optional. Omitting it preserves legacy behavior and makes no +/// feature-flag request. `version` must be UTF-8 containing at least one +/// non-whitespace character. +/// +/// # Safety +/// `config` must be a live config handle; `version` must point to a +/// NUL-terminated C string. +#[no_mangle] +pub unsafe extern "C" fn kernel_session_config_set_client_identity( + config: *mut KernelSessionConfig, + client_type: KernelClientType, + version: *const c_char, +) -> KernelStatusCode { + set_on_config(config, |c| { + let client_type = ClientType::try_from(client_type).map_err(|unknown| { + Error::invalid_argument().message(format!( + "unknown client type {unknown}; expected Go (1), NodeJs (2), or Python (5)" + )) + })?; + let version = required_str(version, "driver version")?; + if version.trim().is_empty() { + return Err(Error::invalid_argument() + .message("driver version must contain a non-whitespace character")); + } + c.client_identity = Some(ClientIdentity::new(client_type, version)); + Ok(()) + }) +} + /// Set the workspace host and bare warehouse id. Mutually exclusive with /// [`kernel_session_config_set_http_path`] — the last one called wins. /// @@ -1564,6 +1630,7 @@ fn build_session(config: KernelSessionConfig) -> Result Result