← Back to Pull requests

perf: hash a precomputed segment ID digest in result-level cache ETags #20950

Open
unknown opened this pull request with 480 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 21, 2026 05:02 UTC
Author

Description

When the result-level cache is on, the Broker computes an ETag for every query before it can serve a cached result. The ETag hashes every segment in the query's interval, so even a cache hit costs time in proportion to the number of segments. On a datasource with 160,983 segments, a cache hit over the whole datasource spent about 1 s in the Broker (query/time 1,013 ms). The cost grew linearly from 20 ms at 519 segments, about 6.3 µs per segment, and Broker CPU time matched wall time.

A sampling profile of the Broker serving those hits put about 69% of the query thread in computeResultLevelCachingEtag. Most of that was string formatting: Interval.toString() (21%) and SegmentId.toString() (18%), which format two dates each per segment, plus SHA-1 over the resulting strings (12%). The profile had about 110 samples on the query thread, so the shares are approximate.

Hash a precomputed digest of each segment ID

Each ServerSelector now computes a 128-bit digest of its segment ID once, when it's created: the first 128 bits of a SHA-1 over the ID's fields. Those fields are the datasource, the interval start and end in milliseconds, the version and the partition number, with both strings length-prefixed so different IDs never produce the same input. A selector's segment ID never changes, because BrokerServerView keys selectors by segment ID. ServerSelector.putSegmentIdDigest(Hasher) adds the digest to a hasher.

computeResultLevelCachingEtag now hashes each segment's digest and the query interval's start and end in milliseconds, in place of the two formatted strings. The ETag still covers the same information, so it still changes whenever the set of segments or their query intervals changes. It just no longer formats anything per segment.

Alternatives considered:

  • Hash the ID's fields in the ETag loop without storing a digest. This adds no memory, but every query would still read each SegmentId and its strings.

  • Cache SegmentId.toString(). That costs around 100 bytes of Broker heap per segment, against 16 for the digest.

  • Memoize the whole ETag until the timeline changes. This would remove the per-segment cost entirely, but it needs invalidation on every segment and replica change. It's a much larger change and could be a follow-up.
  • Release note

    The Broker spends less CPU computing result-level cache ETags, which it does for every query when the result-level cache is enabled, and the savings are largest for queries over many segments. ETag values change with this release, so existing result-level cache entries miss once after upgrading, and clients that send a stored ETag in If-None-Match get a full response once.

    <hr>

    ##### Key changed/added classes in this PR
    ServerSelector
    CachingClusteredClient (CacheKeyManager.computeResultLevelCachingEtag)

    <hr>

    This PR has:

  • been self-reviewed.

  • - [x] using the concurrency checklist (the digest fields are final and set in the constructor)
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.

  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.

  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.

  • - ServerSelectorTest: the digest matches for equal IDs and changes with each ID field.
    - CachingClusteredClientCacheKeyManagerTest: the mocked selector adds a real digest.
    - CachingClusteredClientTest.testIfNoneMatch: the pinned ETag value changes.
  • been tested in a test Druid cluster.
  • Diff: 480 lines (+360 / -120) across 6 files Reviewer: agali-09 Review duration: 13600m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.47) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.47 · 480 lines diff across 6 files
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.369
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #20950"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.3% (MEDIUM) · Higher = larger blast radius
    47.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.0% · Safe to merge
    27.0%
    Repository