-
Notifications
You must be signed in to change notification settings - Fork 13
feat(csharp): add IStatementOperationObserver and fail-open implementations (PECO-3022) #461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jadewang-db
wants to merge
5
commits into
main
Choose a base branch
from
stack/pr-phase2-observer-interface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0baa2c3
Define IStatementOperationObserver interface and NullObserver singlet…
4fa0e1d
Implement TelemetryObserver translating callbacks to StatementTelemet…
dad18e9
Add SafeObserver decorator that swallows exceptions from any inner ob…
7761e85
Address PR review feedback on observer implementations
92d7681
Address PR review feedback: drop SafeObserver, scope try/catch to OnF…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * Copyright (c) 2025 ADBC Drivers Contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System; | ||
| using AdbcDrivers.Databricks.Reader.CloudFetch; | ||
| using ExecutionResultFormat = AdbcDrivers.Databricks.Telemetry.Proto.ExecutionResult.Types.Format; | ||
| using OperationType = AdbcDrivers.Databricks.Telemetry.Proto.Operation.Types.Type; | ||
| using StatementType = AdbcDrivers.Databricks.Telemetry.Proto.Statement.Types.Type; | ||
|
|
||
| namespace AdbcDrivers.Databricks.Telemetry | ||
| { | ||
| /// <summary> | ||
| /// Observer of a single statement's operational lifecycle. Sits between the statement | ||
| /// classes (Thrift and SEA) and the telemetry implementation so the statement code | ||
| /// does not depend on telemetry types directly. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// <b>Fail-open contract:</b> Implementations <b>MUST NOT throw</b> from any method on | ||
| /// this interface. All exceptions raised inside an implementation must be swallowed | ||
| /// internally — callsites in statement code intentionally contain no try/catch around | ||
| /// observer calls. Telemetry must never affect the caller's control flow. | ||
| /// </para> | ||
| /// <para> | ||
| /// <b>Thread-safety:</b> Methods on this interface may be invoked from any thread. | ||
| /// Implementations <b>MUST be thread-safe</b>. In practice the calls happen from the | ||
| /// statement's executing task and the reader's disposal thread, which may differ. | ||
| /// </para> | ||
| /// <para> | ||
| /// <b>Terminal call:</b> <see cref="OnFinalized"/> is the terminal call. After it has | ||
| /// been invoked once, the observer's record is considered complete and any further | ||
| /// calls — including additional <see cref="OnFinalized"/> calls — <b>MUST be no-ops | ||
| /// (idempotent)</b>. This protects against the common case where both an error path | ||
| /// and a dispose path attempt to finalize the same statement. | ||
| /// </para> | ||
| /// <para> | ||
| /// <b>Non-telemetry uses:</b> The interface is shaped around the statement's lifecycle | ||
| /// rather than the telemetry data model so future observers (tracing, audit) can be | ||
| /// added without changing statement code. | ||
| /// </para> | ||
| /// </remarks> | ||
| internal interface IStatementOperationObserver | ||
| { | ||
| /// <summary> | ||
| /// Called once just before the statement is submitted to the server. | ||
| /// </summary> | ||
| /// <param name="stmtType">The statement type (QUERY, UPDATE, METADATA, ...).</param> | ||
| /// <param name="opType">The operation type (EXECUTE_STATEMENT, EXECUTE_STATEMENT_ASYNC, ...).</param> | ||
| /// <param name="isCompressed">Whether results are expected to be compressed (LZ4).</param> | ||
| void OnExecuteStarted(StatementType stmtType, OperationType opType, bool isCompressed); | ||
|
|
||
| /// <summary> | ||
| /// Called once after the server has accepted the statement and a statement id is known. | ||
| /// </summary> | ||
| /// <param name="statementId">The server-assigned statement id.</param> | ||
| /// <param name="resultFormat">The result format inferred for the execution (INLINE_ARROW, EXTERNAL_LINKS, ...).</param> | ||
| void OnExecuteSucceeded(string statementId, ExecutionResultFormat resultFormat); | ||
|
|
||
| /// <summary> | ||
| /// Called once after the polling loop reaches a terminal state, with the accumulated | ||
| /// poll count and total elapsed poll latency. | ||
| /// </summary> | ||
| /// <param name="count">Total number of status-poll calls issued.</param> | ||
| /// <param name="latencyMs">Sum of wall-clock time spent in poll calls, in milliseconds.</param> | ||
| void OnPollCompleted(int count, long latencyMs); | ||
|
|
||
| /// <summary> | ||
| /// Called when the first batch of results is available to the reader. | ||
| /// Implementations should treat repeated calls as a no-op (only the first wins). | ||
| /// </summary> | ||
| /// <param name="latencyMs">Elapsed time from execute start to first batch ready, in milliseconds.</param> | ||
| void OnFirstBatchReady(long latencyMs); | ||
|
|
||
| /// <summary> | ||
| /// Called when the reader has been fully consumed (or disposed). | ||
| /// </summary> | ||
| /// <param name="latencyMs">Elapsed time from execute start to results fully consumed, in milliseconds.</param> | ||
| void OnConsumed(long latencyMs); | ||
|
|
||
| /// <summary> | ||
| /// Called once when chunk metrics for the CloudFetch download are available. | ||
| /// </summary> | ||
| /// <param name="metrics">Aggregated CloudFetch chunk metrics. Implementations must | ||
| /// tolerate empty / default metrics if the gap-fix plumbing has not landed yet.</param> | ||
| void OnChunksDownloaded(ChunkMetrics metrics); | ||
|
|
||
| /// <summary> | ||
| /// Called once if the statement execution fails. Implementations should record the | ||
| /// error and continue to accept further calls; an explicit <see cref="OnFinalized"/> | ||
| /// call is still required to terminate the observer. | ||
| /// </summary> | ||
| /// <param name="ex">The exception that occurred.</param> | ||
| void OnError(Exception ex); | ||
|
|
||
| /// <summary> | ||
| /// Terminal call. Implementations build and dispatch any pending record and mark | ||
| /// the observer as finalized. <b>Must be idempotent</b>: repeated calls are no-ops. | ||
| /// </summary> | ||
| void OnFinalized(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Copyright (c) 2025 ADBC Drivers Contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System; | ||
| using AdbcDrivers.Databricks.Reader.CloudFetch; | ||
| using ExecutionResultFormat = AdbcDrivers.Databricks.Telemetry.Proto.ExecutionResult.Types.Format; | ||
| using OperationType = AdbcDrivers.Databricks.Telemetry.Proto.Operation.Types.Type; | ||
| using StatementType = AdbcDrivers.Databricks.Telemetry.Proto.Statement.Types.Type; | ||
|
|
||
| namespace AdbcDrivers.Databricks.Telemetry | ||
| { | ||
| /// <summary> | ||
| /// Singleton no-op implementation of <see cref="IStatementOperationObserver"/>. | ||
| /// Used as the default observer so callsites in statement classes never need to | ||
| /// null-check before calling observer methods. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// All methods are intentionally empty. They satisfy the fail-open, thread-safe, | ||
| /// and idempotent contract trivially. | ||
| /// </remarks> | ||
| internal sealed class NullObserver : IStatementOperationObserver | ||
| { | ||
| /// <summary> | ||
| /// The singleton instance. Use this directly rather than constructing new instances. | ||
| /// </summary> | ||
| public static readonly NullObserver Instance = new NullObserver(); | ||
|
|
||
| private NullObserver() | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void OnExecuteStarted(StatementType stmtType, OperationType opType, bool isCompressed) | ||
| { | ||
| // No-op. | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void OnExecuteSucceeded(string statementId, ExecutionResultFormat resultFormat) | ||
| { | ||
| // No-op. | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void OnPollCompleted(int count, long latencyMs) | ||
| { | ||
| // No-op. | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void OnFirstBatchReady(long latencyMs) | ||
| { | ||
| // No-op. | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void OnConsumed(long latencyMs) | ||
| { | ||
| // No-op. | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void OnChunksDownloaded(ChunkMetrics metrics) | ||
| { | ||
| // No-op. | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void OnError(Exception ex) | ||
| { | ||
| // No-op. | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void OnFinalized() | ||
| { | ||
| // No-op. Idempotent by construction. | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Low] Contract vs. implementation: post-
OnFinalizednon-OnFinalizedcalls.This contract says: "any further calls — including additional
OnFinalizedcalls — MUST be no-ops (idempotent)." That language covers all methods, not justOnFinalized.TelemetryObserveronly gates furtherOnFinalizedvia_emitted. After finalize, a strayOnPollCompleted/OnError/OnChunksDownloadedwill still mutate_ctx. The mutations are inert today because the log has already been built and no one reads the context post-finalize — but a strict reading of the contract would have all observer methods check_emittedfirst and return early.Two equally reasonable resolutions:
OnFinalizedis the only terminal call that must be idempotent; other methods after finalize are silently dropped but their effect on observer state is implementation-defined."if (Volatile.Read(ref _emitted) != 0) return;short-circuit at the top of everySafe(...)body inTelemetryObserver.The contract-shipped-as-written is what later reviewers will hold us to, so picking one of these and aligning is worth doing now while there are no external consumers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Done — tightened the implementation (the safer interpretation per the review comment). Added a private
IsFinalized()helper (Volatile.Read(ref _emitted) != 0) and a gate at the top of every non-finalize lifecycle method body inTelemetryObserver. AfterOnFinalized()fires, all other lifecycle methods become true no-ops with no further mutation of_ctx.