Repository navigation
fix(telemetry): finalize async statement telemetry on close - #306
Conversation
Signed-off-by: Jay Xiao <jay.xiao@databricks.com>
There was a problem hiding this comment.
Verdict: 1 Medium
Looks good — the async close backstop faithfully mirrors the sync ExecutedStatement::close pattern, finalize is idempotent (shared finalized flag prevents double-emit with the live ResultStream), and the napi/pyo3 bindings correctly force the inherent method via UFCS. One medium note: the finalizing close(&mut self) is shadowed by the trait's close(&self) under normal method syntax and unreachable via dyn ExecutedStatementHandle, so trait-in-scope Rust callers and the C ABI still bypass the telemetry finalize.
| /// async statement is the binding-visible lifecycle boundary, though, so finalize | ||
| /// here as a deterministic backstop for partial-fetch callers that keep the stream | ||
| /// alive past session close. | ||
| pub async fn close(&mut self) -> Result<()> { |
There was a problem hiding this comment.
🟡 Medium — The new finalizing close(&mut self) is only reached via UFCS or when the ExecutedStatementHandle trait is out of scope — otherwise the non-finalizing trait close(&self) silently wins, reintroducing the RCA-29 telemetry gap for those callers.
Rust method resolution tries the &self receiver candidate before &mut self. When ExecutedStatementHandle is in scope, exec.close() finds the trait's close(&self) at the &Self step and selects it (server-close only, no finalize) — the inherent &mut self method is never considered. The napi and pyo3 bindings dodge this correctly with UFCS (ExecutedAsyncStatement::close(&mut …)), and the new session.rs test works only because that test module keeps the trait out of scope and binds mut. But:
- A direct Rust consumer that does
use databricks_sql_kernel::ExecutedStatementHandle;(needed forcancel()) and then callsexec.close()gets the non-finalizing close — the exact partial-fetch telemetry loss this PR sets out to fix. - The C ABI closes through
Box<dyn ExecutedStatementHandle>(persrc/statement/handle.rs:32), which can only dispatch toclose(&self), so async statements closed via the trait object never hit the new backstop.
In those paths telemetry still eventually flushes when the ResultStream drops, so this is a determinism regression rather than guaranteed loss, and it mirrors the pre-existing sync pattern. Worth a doc note on the inherent method ("trait-object / trait-in-scope callers get the non-finalizing close; use UFCS") so the guarantee isn't silently lost by a future caller.
There was a problem hiding this comment.
It's okay for now, all 3 drivers pass the telemetry emitted on statement closure test. How they work may not be the same but it's good for now for PuPr.
There was a problem hiding this comment.
Agreed the tests passing across all three drivers is a reasonable bar for PuPr, and this was always flagged as a determinism regression rather than guaranteed loss — not a blocker. The reason I'm keeping it open is that "how they work may not be the same" is exactly the trap: the trait-in-scope and Box<dyn ExecutedStatementHandle> paths still resolve to the non-finalizing close(&self), and nothing in the current code records that. A one-line doc note on the inherent close(&mut self) ("trait-object / trait-in-scope callers get the non-finalizing close; use UFCS to finalize") would preserve the guarantee for the next caller at effectively zero cost. Happy to leave it as a post-PuPr follow-up rather than a merge gate.
Summary
Fix async partial-fetch telemetry finalization at the kernel owning-handle close boundary, mirroring the sync
ExecutedStatement::close(&mut self)pattern from #299.When
await_result()/fetch_result()returns aResultStream, success telemetry ownership moves to that stream so full result consumption can include result metrics. Node async partial-fetch flows can keep that result stream alive pastop.close()andsession.close(), so the terminalEXECUTE_STATEMENT_ASYNCrow may miss the final telemetry flush.This PR adds a concrete
ExecutedAsyncStatement::close(&mut self)backstop that finalizes statement telemetry before closing the server statement. The immutableExecutedStatementHandle::close(&self)remains server-close only.RCA
Addresses RCA 29: Node async partial result fetching emitted
CREATE_SESSIONandDELETE_SESSION, but missed theEXECUTE_STATEMENT_ASYNCrow because the returned async result stream was still live when the parent async statement/session closed.Changes
ExecutedAsyncStatement::close(&mut self)that finalizes statement telemetry and then closes the server statement.AsyncStatement.close()through the concrete async close.ExecutedAsyncStatement.close()through the concrete async close for consistency.ResultStreamremains live across async statement close and session close.Notes
This guarantees the terminal async statement row at the owning async statement close boundary. If the partial
ResultStreamis still live, close cannot pull final reader chunk metrics from that stream; stream exhaustion/drop still records reader metrics when it wins the finalization race.Testing