Repository navigation
HDDS-17881. Avoid per-call HashMap and String.split in TracingUtil.TextExtractor - #12117
yandrey321 wants to merge 2 commits into
Conversation
…xtExtractor Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Thanks @yandrey321 for the patch. Now that we have |
|
@sravani-revuri @sumitagrawl please take a look |
sodonnel
left a comment
There was a problem hiding this comment.
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:
- 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.
- 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.
- 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;
}
|
@sodonnel thank you for a review! I like the simplified approach and reworked PR based on suggested code. |
What changes were proposed in this pull request?
Problem
TracingUtil.importAndCreateSpanruns for every traced request on OM, SCM and datanodes, and for eachOzoneManagerDoubleBufferchild span. Each call creates a newTextExtractor, which allocates aHashMapand parses the carrier withsplit(";")plus asplit("=")per entry. The W3C propagator then reads onlytraceparentandtracestatefrom it.In a
freon ockgprofile with tracing on (sampler ratio 1.0),importAndCreateSpanwas 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
TextExtractorno longer holds state.get(carrier, key)locates the key in the carrier withindexOfand allocates only the returned value.keys(carrier)builds aLinkedHashSetonly when called.W3CTraceContextPropagatornever calls it.importAndCreateSpanuses a sharedTextExtractor.INSTANCEinstead ofnew 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, asString.splitdrops 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
traceparentcarrier, one lookup each fortraceparentandtracestateas the propagator does:Allocation per carrier drops about 6x: the remaining 96 B is the returned
traceparentstring. 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 andstartSpancosts are unchanged.