Skip to content
Closed
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
1 change: 1 addition & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ changes go in their respective binding's NEXT_CHANGELOG.
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.
- Add a C ABI lookup for values in a session's feature-flag snapshot.

### Changed

Expand Down
10 changes: 10 additions & 0 deletions include/databricks_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,16 @@ KernelStatusCode kernel_session_close(kernel_session_t* session);
* "not known-dead", not a positive liveness guarantee. No ownership. */
KernelStatusCode kernel_session_is_open(const kernel_session_t* session, bool* out);

/* Read one value from the immutable feature-flag snapshot captured when the
* session opened. `name` is the connector-service flag name. On a hit,
* `*out_value` points to borrowed UTF-8 JSON text and `*out_value_len` is its
* byte length; the bytes are NOT NUL-terminated and remain valid until
* kernel_session_close. On a miss, returns Success with NULL/0. No I/O. */
KernelStatusCode kernel_session_get_feature_flag(const kernel_session_t* session,
const char* name,
const uint8_t** out_value,
size_t* out_value_len);

/* Actively validate the session against the server by round-tripping a
* lightweight `SELECT 1` test query (result discarded). This is the
* I/O-performing complement of kernel_session_is_open: it reaches the
Expand Down
2 changes: 2 additions & 0 deletions napi/NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ layout, or npm publishing. Pure-kernel changes go in the root

### Added

- Add `Connection.featureFlag()` to read the kernel-owned session snapshot.

### Changed

- Use a non-empty `driverVersion` as the Node.js client identity for
Expand Down
8 changes: 7 additions & 1 deletion napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,14 @@ export declare class Connection {
* Server-issued session id. Cached at construction; readable
* even after `close()` so JS-side log lines can correlate
* against kernel / server logs which key on the same id.
*/
*/
get sessionId(): string
/**
* Return one opaque JSON value from the immutable feature-flag snapshot
* captured when this connection opened. A missing flag returns `null`.
* This reads local state and performs no connector-service request.
*/
featureFlag(name: string): Promise<string | null>
/**
* Execute a SQL statement and return a Statement handle that
* streams batches via `fetchNextBatch()`.
Expand Down
87 changes: 86 additions & 1 deletion napi/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ impl Connection {
&self.session_id
}

/// Return one opaque JSON value from the immutable feature-flag snapshot
/// captured when this connection opened. A missing flag returns `null`.
/// This reads local state and performs no connector-service request.
#[napi]
Comment thread
cathleeny marked this conversation as resolved.
Resolved
pub async fn feature_flag(&self, name: String) -> napi::Result<Option<String>> {
let guard = self.inner.lock().await;
let session = guard.as_ref().ok_or_else(connection_closed_err)?;
Ok(session.feature_flags().get(&name).map(str::to_owned))
}

/// Execute a SQL statement and return a Statement handle that
/// streams batches via `fetchNextBatch()`.
///
Expand Down Expand Up @@ -651,7 +661,9 @@ impl Drop for Connection {
#[cfg(test)]
mod tests {
use super::*;
use databricks_sql_kernel::{AuthConfig, ConnectionConfig, ErrorCode};
use databricks_sql_kernel::{
AuthConfig, ClientIdentity, ClientType, ConnectionConfig, ErrorCode,
};
use serde_json::json;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
Expand Down Expand Up @@ -702,6 +714,79 @@ mod tests {
assert_eq!(conn.session_id(), "session-survives-close");
}

#[tokio::test]
async fn feature_flag_reads_snapshot_and_rejects_closed_connection() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path(
"/api/2.0/connector-service/feature-flags/NODEJS/napi-test",
))
.respond_with(ResponseTemplate::new(200).set_body_string(
r#"{"flags":[{"name":"example.flag","value":"[\"a\",\"b\"]"}],"ttl_seconds":300}"#,
))
.expect(1)
.mount(&server)
.await;
Mock::given(method("POST"))
.and(path("/api/2.0/sql/sessions"))
.respond_with(
ResponseTemplate::new(200).set_body_string(
r#"{"session_id":"sess-napi-flags","warehouse_id":"test_wh"}"#,
),
)
.expect(1)
.mount(&server)
.await;
Mock::given(method("DELETE"))
.and(path("/api/2.0/sql/sessions/sess-napi-flags"))
.respond_with(ResponseTemplate::new(200))
.expect(1)
.mount(&server)
.await;

let session = Session::builder()
.connection(
ConnectionConfig::from_http_path(server.uri(), "/sql/1.0/warehouses/test_wh")
.expect("valid HTTP path"),
)
.auth(AuthConfig::Pat {
token: "dapi_test_token".into(),
identity_federation_client_id: None,
})
.client_identity(ClientIdentity::new(ClientType::NodeJs, "napi-test"))
.open()
.await
.expect("session opens against mock server");
let connection = Connection {
session_id: session.session_id().to_owned(),
inner: Arc::new(Mutex::new(Some(session))),
};

assert_eq!(
connection
.feature_flag("example.flag".to_owned())
.await
.unwrap()
.as_deref(),
Some(r#"["a","b"]"#)
);
assert_eq!(
connection
.feature_flag("missing.flag".to_owned())
.await
.unwrap(),
None
);

connection.close().await.unwrap();
let error = connection
.feature_flag("example.flag".to_owned())
.await
.unwrap_err();
assert_invalid_statement_handle_envelope(&error);
server.verify().await;
}

#[tokio::test]
async fn build_statement_forwards_raw_params_and_rejects_mixed_binding() {
let server = MockServer::start().await;
Expand Down
129 changes: 129 additions & 0 deletions pyo3/Cargo.lock

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

3 changes: 3 additions & 0 deletions pyo3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@ chrono = "0.4"
pyo3-log = "0.13"
log = "0.4"
tracing = "0.1"

[dev-dependencies]
wiremock = "0.6"
2 changes: 2 additions & 0 deletions pyo3/NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ changes go in the root `NEXT_CHANGELOG.md`.

### Added

- Add `Session.feature_flag()` to read the kernel-owned session snapshot.

### Changed

- Use a non-empty `driver_version` as the Python client identity for
Expand Down
Loading
Loading