Andrey Yarovoy created HDDS-17881:
-------------------------------------
Summary: Avoid per-call HashMap and String.split in
TracingUtil.TextExtractor
Key: HDDS-17881
URL: https://issues.apache.org/jira/browse/HDDS-17881
Project: Apache Ozone
Issue Type: Improvement
Components: tracing
Reporter: Andrey Yarovoy
h3. Problem
{\{TracingUtil.importAndCreateSpan}} runs on every traced server-side request.
It is called from \{{OzoneProtocolMessageDispatcher}} on OM, SCM and datanodes,
and from the per-transaction \{{OzoneManagerDoubleBuffer}} spans. Each call
creates a new \{{TextExtractor}} that allocates a \{{HashMap}} and parses the
carrier with \{{split(";")}} plus one \{{split("=")}} per entry. Only after
that does \{{W3CTraceContextPropagator}} read two keys from it,
\{{traceparent}} and \{{tracestate}}.
We profiled a \{{freon ockg}} run with tracing on (sampler ratio 1.0, OTLP
exporter) using async-profiler on an HA cluster (3 OM, 3 SCM, 8 datanodes, JDK
17). \{{importAndCreateSpan}} was the largest server-side tracing frame:
||Service||importAndCreateSpan share of CPU||importAndCreateSpan share of
allocation||
|OM leader|0.7%|1.8%|
|OM followers|0.9–1.0%|4.0%|
|Datanodes|0.7–1.2%|2.7%|
|SCM|1.3%|1.5%|
On the OM leader, \{{TextExtractor.parse}} accounts for about 40% of that
frame's CPU, and its \{{byte[]}}, \{{String}}, \{{Object[]}} and \{{HashMap}}
allocations are the largest part of the allocation. The rest is the W3C decode
and \{{SdkSpanBuilder.startSpan}}, which this change does not touch.
The cost is small in absolute terms. Total tracing overhead per service was
about 1–3% of CPU, and none of the lock or park time came from tracing.
However, the parse is pure waste: the carrier always comes from
\{{TracingUtil.exportCurrentSpan}} and holds one or two entries.
h3. Proposed change
Make \{{TextExtractor}} stateless and share one instance:
* \{{get(carrier, key)}} scans the carrier with \{{indexOf}} and allocates only
the returned value string. There is no map and no split arrays.
* \{{keys(carrier)}} builds a \{{LinkedHashSet}} only when it is called.
\{{W3CTraceContextPropagator}} never calls it.
* \{{importAndCreateSpan}} passes a shared \{{TextExtractor.INSTANCE}} instead
of \{{new TextExtractor()}} on each call.
* Parsing results are unchanged, matching the current split-based parser:
** an entry counts only if it has exactly one \{{=}} separator (trailing \{{=}}
characters are dropped first, like \{{String.split}} does);
** keys and values are trimmed;
** when a key repeats, the last one wins.
The public no-arg constructor stays, so existing callers and tests compile
unchanged.
h3. Verification
* An equivalence check against the current parser on 200,013 carriers (edge
cases plus random strings over the characters a, b, k, =, ; and space) found 0
differences. The covered cases include malformed entries, empty values,
trailing \{{=}} characters, \{{a=b=c}}, surrounding whitespace and duplicate
keys.
* A micro-benchmark on JDK 21 used a real \{{traceparent}} carrier with one
lookup each for \{{traceparent}} and \{{tracestate}}, as the propagator does:
||Variant||Time per carrier||Allocation per carrier||
|current|51 ns|584 B|
|proposed|27 ns|96 B|
* The patch will add unit tests in \{{TestTracingUtil}} for trailing \{{=}}
characters, duplicate keys, and one shared instance used across carriers.
h3. Expected impact
About 80% less allocation in carrier parsing. That should lower tracing's share
of server allocation by about 1–2 percentage points and its CPU share by about
0.2–0.3 points at sampler ratio 1.0. No behavior change.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]