Skip to content

HDDS-17881. Avoid per-call HashMap and String.split in TracingUtil.TextExtractor - #12117

Draft
yandrey321 wants to merge 2 commits into
apache:masterfrom
yandrey321:HDDS-17881
Draft

yandrey321 wants to merge 2 commits into
apache:masterfrom
yandrey321:HDDS-17881

Conversation

@yandrey321

@yandrey321 yandrey321 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Problem

TracingUtil.importAndCreateSpan runs for every traced request on OM, SCM and datanodes, and for each OzoneManagerDoubleBuffer child span. Each call creates a new TextExtractor, which allocates a HashMap and parses the carrier with split(";") plus a split("=") per entry. The W3C propagator then reads only traceparent and tracestate from it.

In a freon ockg profile with tracing on (sampler ratio 1.0), importAndCreateSpan was the largest server-side tracing frame: 0.7–1.3% of CPU and 1.5–4% of allocations. Carrier parsing accounted for about 40% of that frame's CPU on the OM leader.

Implementation idea

  • TextExtractor no longer holds state. get(carrier, key) locates the key in the carrier with indexOf and allocates only the returned value.

  • keys(carrier) builds a LinkedHashSet only when called. W3CTraceContextPropagator never calls it.

  • importAndCreateSpan uses a shared TextExtractor.INSTANCE instead of new TextExtractor() on every call.

  • Same parsing rules as the old code. An entry counts only if it has exactly one = after trailing = characters are dropped, as String.split drops trailing empty strings. Keys and values are trimmed, and the last duplicate wins. Simplifying these rules was rejected because it would change results for malformed carriers.

  • A hand-written scan, not a regex or a cached map. The carrier holds one or two entries, so a linear scan per lookup is cheaper than building any structure.

Co-authored-by: Claude Opus 5.5 noreply@anthropic.com

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-17881

How was this patch tested?

Unit tests, CI: https://github.com/yandrey321/ozone/actions/runs/44845485569

  • Micro-benchmark, not committed: JDK 21, a real traceparent carrier, one lookup each for traceparent and tracestate as the propagator does:
Variant Time per carrier Allocation per carrier
before 51 ns 584 B
after 27 ns 96 B

Allocation per carrier drops about 6x: the remaining 96 B is the returned traceparent string. Time halves because the per-entry substring and array copies are gone. At sampler ratio 1.0 this should cut tracing's share of server allocation by about 1–2 percentage points and its CPU share by about 0.2–0.3 points. The W3C decode and startSpan costs are unchanged.

…xtExtractor

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@yandrey321
yandrey321 marked this pull request as draft November 24, 2026 06:36
@yandrey321

Copy link
Copy Markdown
Contributor Author

@adoroszlai

Copy link
Copy Markdown
Contributor

marked this pull request as draft yesterday

Thanks @yandrey321 for the patch. Now that we have fork-ci, PR no longer needs to be marked as draft, unless it's really in progress.

@adoroszlai

Copy link
Copy Markdown
Contributor

@sravani-revuri @sumitagrawl please take a look

@sodonnel sodonnel 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.

I pointed Claude at this and added that I was concerned the new code is quite complex. It also ran some benchmarks of different techniques and confirmed the approach in the PR is about 2x faster than the current implementation, but it also offered a simpler alternative that is 2x faster again:

┌──────────────────────────────────────────────────┬──────────────────┬──────────────────────────┐
│                     Variant                      │ traceparent only │ traceparent + tracestate │
├──────────────────────────────────────────────────┼──────────────────┼──────────────────────────┤
│ master (split + HashMap)                         │ 95 ns, 648 B     │ 145 ns, 992 B            │
├──────────────────────────────────────────────────┼──────────────────┼──────────────────────────┤
│ PR                                               │ 43 ns, 96 B      │ 62 ns, 96 B *            │
├──────────────────────────────────────────────────┼──────────────────┼──────────────────────────┤
│ minimal scan (below)                             │ 22 ns, 96 B      │ 37 ns, 160 B             │
├──────────────────────────────────────────────────┼──────────────────┼──────────────────────────┤

The main thing it pointed out is that the complexity of the PR is trying to maintain the older parser rules (split semantic) and it locks in a bug that is on master. Things like spaces around equals etc should not happen as 'the propagator' should filter them out at source. In Claude's words:

  1. tracestate is always dropped, and the PR locks that in. A valid W3C tracestate is vendor=value[,vendor=value], so it always contains =. exportCurrentSpan() writes tracestate=congo=abc;, then split("=") gives three parts and the entry is discarded. This is a bug on master. The PR keeps it on purpose ("same parsing rules"), and the new test asserts it: assertNull(ex.get("traceparent=00-c-d-00;tracestate=s=1;", "tracestate")). It only matters when an upstream app sets tracestate, but keeping it on purpose is the wrong call.
  2. Copying split semantics for malformed input isn't needed. The carrier is always machine-written by exportCurrentSpan() as key=value;, with no whitespace, no duplicates and no trailing =. A hand-crafted malformed carrier from a client gets no protection from those rules either. The propagator validates the traceparent format itself and discards anything invalid. testTextExtractorSplitSemantics pins down cases such as "a=b=" → "b" and "=x" with key "", which future maintainers would then have to keep.
  3. scan() does two jobs. scan(carrier, key, keysOut) returns a value or collects keys depending on which argument is null. keys() is never called by the W3C propagator and isn't on the hot path. It could keep the old split-based code, or use the same simple loop.

The suggested simpler code to replace the get method which gives the fastest benchmark above, but also removes the extra "bad case" handling outlined above which shouldn't be needed;

@Override
public String get(String carrier, String key) {
  if (carrier == null || key == null) {
    return null;
  }
  int len = carrier.length();
  int start = 0;
  while (start < len) {
    int end = carrier.indexOf(';', start);
    if (end < 0) {
      end = len;
    }
    int eq = start + key.length();
    if (eq < end && carrier.charAt(eq) == '=' && carrier.startsWith(key, start)) {
      return carrier.substring(eq + 1, end);
    }
    start = end + 1;
  }
  return null;
}

@yandrey321

Copy link
Copy Markdown
Contributor Author

@sodonnel thank you for a review! I like the simplified approach and reworked PR based on suggested code.

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.

3 participants