Skip to content

[PECOBLR-1408] [PECOBLR-1407] [PECOBLR-1409][PECOBLR-1411] Address telemetry code audit comments- part1 - #1163

Merged
samikshya-db merged 22 commits into
databricks:mainfrom
samikshya-db:samikshya-chand_data/auditFeedback
Jan 20, 2026
Merged

samikshya-db merged 22 commits into
databricks:mainfrom
samikshya-db:samikshya-chand_data/auditFeedback

Conversation

@samikshya-db

@samikshya-db samikshya-db commented Dec 29, 2025 •

Copy link
Copy Markdown
Collaborator

Description

  • 4 things that is improved with respect to telemetry :
    • Common object mapper across telemetry use-case (This is already thread safe and is expensive to create, i.e., good tor re-use)
    • MakeflushIntervalMillis config same across both telemetry clients (un-auth and auth)
    • Clear connection param cache when connection is closed : this was a memory leak before
    • Rather than creating a scheduledExecutor for each telemetry client, we share it across a factory.

Testing

  • unit tests

Additional Notes to the Reviewer

  • When the LAST connection to a host is closed, all pending telemetry events for that host are flushed across all prior connections (since they all shared the same TelemetryClient). i.e., If you have 5 connections to host-A, closing connections 1-4 does nothing (just decrements refCount). Only when you close connection 5 (the last one) does the flush occur, sending all accumulated telemetry from all 5 connections.

NO_CHANGELOG=true

@samikshya-db samikshya-db changed the title [PECOBLR-1408] [PECOBLR-1407] Address telemetry code audit comments- part1 [PECOBLR-1408] [PECOBLR-1407] [PECOBLR-1409] Address telemetry code audit comments- part1 Dec 29, 2025
@samikshya-db samikshya-db changed the title [PECOBLR-1408] [PECOBLR-1407] [PECOBLR-1409] Address telemetry code audit comments- part1 [PECOBLR-1408] [PECOBLR-1407] [PECOBLR-1409][PECOBLR-1411] Address telemetry code audit comments- part1 Dec 30, 2025

@tejassp-db tejassp-db 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.

Connection ref count never decrements to zero. Should be fixed.

*/
public TelemetryCollector getOrCreateCollector(IDatabricksConnectionContext context) {
String key =
(context != null && context.getConnectionUuid() != 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.

Is it possible for the context or connection uuid to be null? If both are always non-null, then we should enforce that in the code.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I am skeptical of removing this because of ThreadLocal usage. Check the internal outage here : [Link]

this.executorService = executorService;
this.scheduledExecutorService =
Executors.newSingleThreadScheduledExecutor(createSchedulerThreadFactory());
this.scheduledExecutorService = scheduledExecutorService;

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.

If a flush is slow, does it block flush for other statements?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Based on my understanding :
I schedule periodic flushes in telemetry client : this is shared across all connections in the process. This submits flush task to executor service (Added a 10 thread case - link).

This should not be blocking other statements.

@samikshya-db

Copy link
Copy Markdown
Collaborator Author

@tejassp-db - the fixes for Connection ref count never decrements to zero is merged too. thanks for the review.

key,
(k, holder) -> {
if (holder.refCount.get() <= 1) {
holder.connectionUuids.remove(connectionUuid);

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.

Is this scenario probable

  1. ThreadA - It removes a connection and if branch goes through with empty condition being true.
  2. ThreadB - Adds a new connection ConnectionNew to the host
  3. ThreadA - Now operates under the incorrect assumption that there are no connections to the host. And will publish incomplete ConnectionNew details and truncate it as well.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[We have already discussed this offline, but adding the pointers for future reference]

The above scenario is not possible. create a connection-> every kind of export will be from TelemetryHelper -> which will in turn get the telemetry client from TelemetryClientFactory.

telemetryClientHolders is a ConcurrentHashMap in the telemetry client factory
ThreadA - It removes a connection and if branch goes through with empty condition being true. -> closes client in sync fashion
ThreadB add a new connection - telemetryClientHolders.compute will not start as threadA has the lock.

@tejassp-db has suggested that we document this behavior in comments. Will add comments to clarify.

@tejassp-db tejassp-db 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 changes look good. @samikshya-db Can you please add more details to the linked JIRA tickets.

TODO in the future -Telemetry threading issues would be solved if telemetry started using async http calls.

@samikshya-db
samikshya-db merged commit 8a2f333 into databricks:main Jan 20, 2026
12 of 13 checks passed
@samikshya-db
samikshya-db deleted the samikshya-chand_data/auditFeedback branch January 20, 2026 16:30
gopalldb added a commit that referenced this pull request Feb 21, 2026
…esultSet

PR #1163 refactored telemetry from singleton to per-connection, but
introduced per-row overhead in DatabricksResultSet.next() by traversing
the parentStatement → connection → context → ConcurrentHashMap chain
on every row (~6.5M times for a 1025MB query). This added ~2.3us/row,
compounding to ~15s of overhead.

Fix: resolve the TelemetryCollector once at construction time and store
it in a final field. The next() method calls the cached collector
directly, eliminating per-row object traversal, type casts, UUID
hashing, and ConcurrentHashMap lookups.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
gopalldb added a commit that referenced this pull request Feb 21, 2026
…esultSet (#1223)

## Summary

- Cache the `TelemetryCollector` reference at `DatabricksResultSet`
construction time instead of resolving it on every `next()` call
- Eliminates per-row overhead introduced by PR #1163 (telemetry refactor
from singleton to per-connection)
- Restores Arrow cloud-fetch throughput to baseline levels

## Problem

PR #1163 refactored telemetry collection from a singleton pattern to
per-connection, but inadvertently introduced per-row overhead in
`DatabricksResultSet.next()`. On every row iteration (~6.5M times for a
1025MB query), the code now traverses:

```
parentStatement.getStatement().getConnection()
  → cast to DatabricksConnection
  → getConnectionContext()
  → TelemetryCollectorManager.getInstance().getOrCreateCollector(context)
  → ConcurrentHashMap.computeIfAbsent()
```

This adds ~2.3 microseconds per row, compounding to ~15 seconds of
overhead over 6.5M rows.

## Fix

Resolve the `TelemetryCollector` once in each constructor via a
`resolveTelemetryCollector()` helper and store it in a `final` field.
The `next()` method calls the cached collector directly, bypassing the
entire per-row resolution chain. The connection-to-collector mapping is
stable for the lifetime of a result set, so caching at construction time
is safe.

**Before:**
```java
public boolean next() throws SQLException {
  checkIfClosed();
  boolean hasNext = this.executionResult.next();
  TelemetryHelper.recordResultSetIteration(
      parentStatement, statementId, resultSetMetaData.getChunkCount(), hasNext);
  return hasNext;
}
```

**After:**
```java
public boolean next() throws SQLException {
  checkIfClosed();
  boolean hasNext = this.executionResult.next();
  if (cachedTelemetryCollector != null) {
    cachedTelemetryCollector.recordResultSetIteration(
        statementId.toSQLExecStatementId(), resultSetMetaData.getChunkCount(), hasNext);
  }
  return hasNext;
}
```

## Benchmark Results

### Regression Bisection (1025MB query, ~6.5M rows, Arrow cloud-fetch,
Thrift mode, warm iteration)

| Build | Commit | Fetch (ms) | Rows/s | Per-row cost | Status |
|-------|--------|-----------|--------|-------------|--------|
| v3.1.1 (baseline) | `0e8b1ad` | 5,037 | 1.29M | 0.78 us | ✅ Baseline |
| Pre-#1163 | `9c9ffe2` | 4,720 | 1.38M | 0.73 us | ✅ Matches baseline |
| Post-#1163 | `8a2f333` | 19,727 | 329K | 3.04 us | ❌ 3.9x regression |
| v3.1.2 HEAD (unfixed) | `47235a1` | 20,382 | 318K | 3.14 us | ❌ 4.0x
regression |
| **v3.1.2 (this fix)** | `eaa3f64` | **4,447** | **1.46M** | **0.68
us** | ✅ **12% faster than baseline** |

### Post-Fix Segment Latency (500K rows per segment, warm iteration)

The fix restores the expected **bursty** pattern (fast in-memory
iteration alternating with chunk-download waits):

```
 500K rows:   73ms    (in-memory, fast)
   1M rows:  393ms    (chunk wait)
 1.5M rows:  147ms    (in-memory)
   2M rows:  317ms    (chunk wait)
 2.5M rows:  409ms    (chunk wait)
   3M rows:  282ms    (mixed)
 3.5M rows:  250ms    (mixed)
   4M rows:  314ms    (chunk wait)
 4.5M rows:  197ms    (in-memory)
   5M rows:  370ms    (chunk wait)
 5.5M rows:  361ms    (chunk wait)
   6M rows:  146ms    (in-memory)
```

vs the unfixed v3.1.2 which showed flat ~1,450ms per segment (dominated
by uniform per-row overhead).

### Comprehensive Comparison (1025MB, warm iteration)

| Driver | Mode | Arrow | Fetch (ms) | Rows/s |
|--------|------|-------|-----------|--------|
| Simba 2.7.6 | Thrift | N/A | 109,994 | 59.0K |
| OSS v3.1.1 | Thrift | Yes | 5,037 | 1.29M |
| OSS v3.1.1 | SEA | Yes | 4,202 | 1.54M |
| OSS v3.1.1 | Thrift | No | 163,051 | 39.8K |
| OSS v3.1.2 (unfixed) | Thrift | Yes | 20,382 | 318K |
| OSS v3.1.2 (unfixed) | SEA | Yes | 19,882 | 326K |
| OSS v3.1.2 (unfixed) | Thrift | No | 145,131 | 44.7K |
| **OSS v3.1.2 (this fix)** | **Thrift** | **Yes** | **4,447** |
**1.46M** |

NoArrow mode is unaffected by the regression (~145-163s for both
versions) because the per-row telemetry overhead (~2.3us) is negligible
compared to the ~25us/row cost of non-Arrow iteration.

## Test plan

- [x] `mvn test -Dtest=DatabricksResultSetTest` — all 48 tests pass (43
existing + 5 new)
- [x] New tests cover: next() with cached telemetry, null
parentStatement (no NPE), exception handling in resolution, null
connection context, collector resolved once at construction
- [x] Benchmark verified: fetch time drops from 20,382ms → 4,447ms (4.6x
improvement)
- [ ] CI pipeline

🤖 Generated with [Claude Code](https://claude.com/claude-code)

NO_CHANGELOG=true
OVERRIDE_FREEZE=true

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
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