Skip to content

fix(bigquery-jdbc): harden Clearcut opt-out lifecycle and preserve DataSource URL opt-out - #15394

Open
keshavdandeva wants to merge 5 commits into
mainfrom
fix/jdbc-clearcut-lifecycle-optout
Open

keshavdandeva wants to merge 5 commits into
mainfrom
fix/jdbc-clearcut-lifecycle-optout

Conversation

@keshavdandeva

Copy link
Copy Markdown
Contributor

Summary

Hardens Clearcut diagnostic telemetry lifecycle and opt-out teardown in java-bigquery-jdbc so opting out drops buffered metrics without blocking on an outbound upload, and fixes DataSource clobbering URL-level EnableDiagnosticTelemetry=0 settings.

Key changes

  • Non-flushing opt-out teardown: Added TelemetryBatcher.close(boolean flush) and a discardPending check under flushLock before transport.send() and failed-flush requeueing so close(false) discards queued metrics immediately without initiating a flush.
  • Lock-free lifecycle & single shutdown hook: Updated TelemetryManager to run DriverEnvironmentDetector.build() and teardown (closeQuietly) outside synchronized (TelemetryManager.class), log global disable once at Level.FINE, honor BIGQUERY_JDBC_TELEMETRY_ENABLED on getInstance(), and register the JVM shutdown hook at most once via AtomicBoolean.
  • TelemetryConfiguration.toBuilder(): Added toBuilder() so preliminary opt-out evaluation can be extended with DriverEnvironment without re-reading configuration sources.
  • DataSource opt-out preservation: Guarded EnableDiagnosticTelemetry serialization in DataSource.createProperties() with if (this.enableDiagnosticTelemetry != null) so an unset DataSource does not emit EnableDiagnosticTelemetry=true and override EnableDiagnosticTelemetry=0 in the connection URL.
  • Tests: Added unit tests for TelemetryBatcher.close(false), TelemetryConfiguration.toBuilder(), non-flushing opt-out teardown, system-property opt-out on getInstance(), and DataSource URL opt-out preservation.

Decisions made

  • Drop queued metrics on opt-out instead of flushing: Flushing on opt-out previously blocked the connecting thread for up to 2 seconds under the class monitor and transmitted queued events after opt-out was requested. close(false) discards queued batches and shuts down the batcher executor immediately.
  • Keep DataSource fix scoped to enableDiagnosticTelemetry: DataSource.enableDiagnosticTelemetry was already a nullable Boolean, but createProperties() unconditionally called getEnableDiagnosticTelemetry() (which defaults to true). Guarding on the raw field fixes URL opt-out precedence without expanding scope to unrelated DataSource properties.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the BigQuery JDBC telemetry system to support discarding pending metrics on close or opt-out without flushing. It introduces a discardPending flag in TelemetryBatcher, adds a toBuilder() method to TelemetryConfiguration, and refactors TelemetryManager for safer lifecycle and shutdown hook management. The review comments correctly point out that reassigning metricsMap to a new ConcurrentHashMap in TelemetryBatcher can cause thread-safety or compilation issues, and suggest using metricsMap.clear() instead.

connectionProperties.setProperty(
BigQueryJdbcUrlUtility.ENABLE_DIAGNOSTIC_TELEMETRY_PROPERTY_NAME,
String.valueOf(getEnableDiagnosticTelemetry()));
if (this.enableDiagnosticTelemetry != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful when merging with @Neenu1995 changes around datasource

}

if (!result.isSuccess()) {
if (discardPending) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unreachable? Line 255 should return

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also reachable under concurrency: if close(false) fires while flush() is blocked inside transport.send(...) (including when executorService.shutdownNow() interrupts the in-flight send, causing !result.isSuccess()), discardPending flips to true after line 255. Re-checking here skips remergeFailedMetrics(snapMetrics) so discarded metrics aren't re-inserted into metricsMap after close(false) cleared it. Added an inline comment and a test for this path.

accumulator.addToPayload(payloadBuilder);
}

if (discardPending) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is also unreachable? Line 226

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reachable under concurrency: close(false) does not acquire flushLock, so another thread can set discardPending = true after the background flush() thread passed line 226 while it is assembling the payload. Re-checking here aborts before transport.send(...) sends an outbound request. Added an inline comment clarifying this.

private volatile boolean backoffActive;

private final AtomicBoolean isClosed = new AtomicBoolean(false);
private volatile boolean discardPending = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest just passing it as argument since it is only 2 methods

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discardPending can't be a method argument because close(false) doesn't call flush() — flush() is already running asynchronously on the background jdbc-telemetry-batcher thread holding flushLock, while close(false) runs on a JDBC connecting thread without acquiring flushLock (so connect never blocks on an in-flight upload).

volatile boolean discardPending is the cross-thread cancellation signal from close(false) to an in-flight flush(). Added comments explaining this and a concurrency unit test

* Stops background flushing. When {@code flush} is {@code false}, buffered metrics are discarded
* immediately without attempting an outbound upload for queued events.
*/
void close(boolean flush) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When do we call it with false?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's called via TelemetryManager.closeQuietly(manager, false) in three places:

  1. TelemetryManager.disableGlobally(...) — opt-out in this PR, custom universe/endpoint suppression in fix(bigquery-jdbc): suppress Clearcut diagnostic telemetry for custom universes and endpoint overrides #15395, and conflicting transport profiles in fix(bigquery-jdbc): propagate proxy and SSL TrustStore settings to Clearcut transport #15396 (must discard buffered metrics without uploading to play.googleapis.com or blocking connect for up to 2 s).
  2. Closing a losing race candidate in TelemetryManager.getInstance(...)
  3. Test teardown (TelemetryTestUtils.reset()).

Updated the close(boolean flush) Javadoc to call this out explicitly.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants