Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
0294058
Add Hashtable and LongHashingUtils to datadog.trace.util
dougqh May 18, 2026
f751ab4
Add AggregateTable + AggregateEntry backed by Hashtable
dougqh May 15, 2026
f1b030a
Swap LRUCache for AggregateTable in Aggregator + route disable() clear
dougqh May 15, 2026
3738c85
Eliminate MetricKey: inline its fields onto AggregateEntry
dougqh May 15, 2026
46a9055
Add unit tests for Hashtable and LongHashingUtils
dougqh May 18, 2026
031dc89
Apply spotless formatting to Hashtable and LongHashingUtils
dougqh May 18, 2026
f9e63b9
Add JMH benchmarks for Hashtable.D1 and D2
dougqh May 18, 2026
a534e4f
Add benchmark results to HashtableBenchmark header
dougqh May 18, 2026
ba66a36
Address review feedback on Hashtable
dougqh May 19, 2026
3108941
Fix dropped argument in HashingUtils 5-arg Object hash
dougqh May 19, 2026
1415f12
Address review feedback on Hashtable
dougqh May 19, 2026
b7cee2f
Drop reflection in iterator tests via package-private D1.buckets
dougqh May 19, 2026
553ffb8
Fold AggregateMetric into AggregateEntry
dougqh May 19, 2026
07ed635
Avoid capturing lambda in Aggregator.report
dougqh May 19, 2026
df58ad7
Add context-passing forEach to Hashtable.D1 and D2
dougqh May 19, 2026
9c6e95c
Move forEach loop body to Support helper
dougqh May 19, 2026
7ab47f7
Merge branch 'dougqh/util-hashtable' into dougqh/optimize-metric-key
dougqh May 19, 2026
590ab4a
Delegate AggregateTable.forEach to Support.forEach
dougqh May 19, 2026
447ea33
Move bucket-head cast to Support.bucket helper
dougqh May 19, 2026
46f147a
Merge branch 'dougqh/util-hashtable' into dougqh/optimize-metric-key
dougqh May 19, 2026
dd5e13f
Use Support.bucket and type chain walks as AggregateEntry
dougqh May 19, 2026
df7f98f
Drop d1_/d2_ prefix from per-table benchmark methods
dougqh May 19, 2026
e6ecc16
Add Hashtable.Support helpers: MAX_RATIO, insertHeadEntry, MutatingTa…
dougqh May 19, 2026
a409d5d
Merge branch 'dougqh/util-hashtable' into dougqh/optimize-metric-key
dougqh May 19, 2026
96b40b8
Simplify AggregateTable via new Hashtable.Support helpers
dougqh May 19, 2026
55ca204
Swap MAX_RATIO numerator/denominator pair for a single float + scaled…
dougqh May 19, 2026
7b9eccd
Merge branch 'dougqh/util-hashtable' into dougqh/optimize-metric-key
dougqh May 19, 2026
192de0c
Address second-round review on AggregateTable / Aggregator
dougqh May 19, 2026
4bac439
Tighten Hashtable docs + rename MAX_CAPACITY to MAX_BUCKETS
dougqh May 19, 2026
de289a0
Dedupe chain-head splice in D1/D2 via keyHash insertHeadEntry overload
dougqh May 19, 2026
2dd65ed
Tighten Entry.next encapsulation; doc hasNext; add D1/D2 getOrCreate
dougqh May 19, 2026
cc8d98a
Merge branch 'dougqh/util-hashtable' into dougqh/optimize-metric-key
dougqh May 19, 2026
6a9063b
Use keyHash insertHeadEntry overload in AggregateTable.findOrInsert
dougqh May 19, 2026
dbb1702
Replace // nullable comments with @Nullable annotations on AggregateE…
dougqh May 19, 2026
545e74c
Drop redundant load-factor comment from AggregateTable ctor
dougqh May 19, 2026
9983a59
Import java.util.Objects in AggregateEntry instead of fully qualifying
dougqh May 19, 2026
d2e4477
Document evictOneStale cost and disable() best-effort offer
dougqh May 19, 2026
66ec7f6
Hashtable: add missing braces and detach removed/replaced entries
dougqh May 20, 2026
be823ab
Merge branch 'dougqh/util-hashtable' into dougqh/optimize-metric-key
dougqh May 20, 2026
10956b2
Add Hashtable and LongHashingUtils to datadog.trace.util
dougqh May 18, 2026
035dc09
Add unit tests for Hashtable and LongHashingUtils
dougqh May 18, 2026
7728b60
Apply spotless formatting to Hashtable and LongHashingUtils
dougqh May 18, 2026
8cd2d86
Add JMH benchmarks for Hashtable.D1 and D2
dougqh May 18, 2026
c689ef9
Add benchmark results to HashtableBenchmark header
dougqh May 18, 2026
75790eb
Address review feedback on Hashtable
dougqh May 19, 2026
6056ff7
Fix dropped argument in HashingUtils 5-arg Object hash
dougqh May 19, 2026
da55021
Address review feedback on Hashtable
dougqh May 19, 2026
8b8b088
Drop reflection in iterator tests via package-private D1.buckets
dougqh May 19, 2026
0fde7cd
Add context-passing forEach to Hashtable.D1 and D2
dougqh May 19, 2026
6d6c2e0
Move forEach loop body to Support helper
dougqh May 19, 2026
268de2b
Move bucket-head cast to Support.bucket helper
dougqh May 19, 2026
93813b9
Drop d1_/d2_ prefix from per-table benchmark methods
dougqh May 19, 2026
11a58bf
Add Hashtable.Support helpers: MAX_RATIO, insertHeadEntry, MutatingTa…
dougqh May 19, 2026
8f1828d
Swap MAX_RATIO numerator/denominator pair for a single float + scaled…
dougqh May 19, 2026
c0d3e26
Tighten Hashtable docs + rename MAX_CAPACITY to MAX_BUCKETS
dougqh May 19, 2026
a0978ba
Dedupe chain-head splice in D1/D2 via keyHash insertHeadEntry overload
dougqh May 19, 2026
e604a8f
Tighten Entry.next encapsulation; doc hasNext; add D1/D2 getOrCreate
dougqh May 19, 2026
e2642cd
Hashtable: add missing braces and detach removed/replaced entries
dougqh May 20, 2026
585ca56
Rename LongHashingUtils.hashCodeX(Object) to hash(Object) for API con…
dougqh May 20, 2026
aab5b3b
Merge remote-tracking branch 'origin/master' into dougqh/optimize-met…
dougqh May 20, 2026
88f169f
Merge branch 'dougqh/util-hashtable' into dougqh/optimize-metric-key
dougqh May 20, 2026
c2ff012
Merge branch 'dougqh/conflating-metrics-background-work' into dougqh/…
dougqh May 20, 2026
1d1ba4d
Merge branch 'dougqh/conflating-metrics-background-work' into dougqh/…
dougqh May 21, 2026
3d4f380
Merge branch 'dougqh/conflating-metrics-background-work' into dougqh/…
dougqh May 21, 2026
9391c48
Use writer.finishBucket() count in bootstrap test for cascade compati…
dougqh May 21, 2026
ba0d390
Merge branch 'dougqh/conflating-metrics-background-work' into dougqh/…
dougqh May 21, 2026
f71384e
Merge branch 'dougqh/conflating-metrics-background-work' into dougqh/…
dougqh May 21, 2026
471f8f7
Merge branch 'dougqh/conflating-metrics-background-work' into dougqh/…
dougqh May 21, 2026
7a074e0
Merge branch 'dougqh/conflating-metrics-background-work' into dougqh/…
dougqh May 21, 2026
3fa7ac7
Merge branch 'dougqh/conflating-metrics-background-work' into dougqh/…
dougqh May 21, 2026
df3b31d
Adapt reconcileSwapsSchemaWhenTagSetChanges to AggregateEntry shape
dougqh May 21, 2026
8ec956d
Merge branch 'dougqh/conflating-metrics-background-work' into dougqh/…
dougqh May 21, 2026
a606692
Fix MetricsIntegrationTest entry recording call site
dougqh May 21, 2026
913e7d7
Make ConflatingMetricAggregatorTest counter checks actually verify
dougqh May 21, 2026
2dcea9a
Drop dead recordDurations(int, AtomicLongArray) batch API
dougqh May 21, 2026
50b06e5
Warn about colon split in AggregateEntry.of test factory
dougqh May 21, 2026
b0f21bf
Add coverage for disable() -> ClearSignal threading path
dougqh May 21, 2026
200028d
Merge branch 'dougqh/conflating-metrics-background-work' into dougqh/…
dougqh May 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package datadog.trace.common.metrics;

import datadog.trace.util.Hashtable;
import datadog.trace.util.Hashtable.MutatingTableIterator;
import datadog.trace.util.Hashtable.Support;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
* Consumer-side {@link AggregateEntry} store, keyed on the raw fields of a {@link SpanSnapshot}.
*
* <p>Replaces the prior {@code LRUCache<MetricKey, AggregateMetric>}. The win is on the
* steady-state hit path: a snapshot lookup is a 64-bit hash compute + bucket walk + field-wise
* {@code matches}, with no per-snapshot {@link AggregateEntry} allocation and no UTF8 cache
* lookups. The UTF8-encoded forms (formerly held on {@code MetricKey}) and the mutable counters
* (formerly held on {@code AggregateMetric}) both live on the {@link AggregateEntry} now, built
* once per unique key at insert time.
*
* <p><b>Not thread-safe.</b> The aggregator thread is the sole writer; {@link #clear()} must be
* routed through the inbox rather than called from arbitrary threads.
*/
final class AggregateTable {

private final Hashtable.Entry[] buckets;
private final int maxAggregates;
private int size;

AggregateTable(int maxAggregates) {
this.buckets = Support.create(maxAggregates, Support.MAX_RATIO);
this.maxAggregates = maxAggregates;
}

int size() {
return size;
}

boolean isEmpty() {
return size == 0;
}

/**
* Returns the {@link AggregateEntry} to update for {@code snapshot}, lazily creating one on miss.
* Returns {@code null} when the table is at capacity and no stale entry can be evicted -- the
* caller should drop the data point in that case.
*/
AggregateEntry findOrInsert(SpanSnapshot snapshot) {
long keyHash = AggregateEntry.hashOf(snapshot);
for (AggregateEntry candidate = Support.bucket(buckets, keyHash);
candidate != null;
candidate = candidate.next()) {
if (candidate.matches(keyHash, snapshot)) {
return candidate;
}
}
if (size >= maxAggregates && !evictOneStale()) {
return null;
}
AggregateEntry entry = AggregateEntry.forSnapshot(snapshot);
Support.insertHeadEntry(buckets, keyHash, entry);
size++;
return entry;
}

/**
* Unlinks the first entry whose {@code getHitCount() == 0}.
*
* <p>O(N) per call -- scans buckets in array order from the start every time. That's a regression
* from the prior {@code LRUCache}'s O(1) LRU eviction, but the semantic change is deliberate: at
* cap with all entries live, we drop the new key (and report it via {@code
* onStatsAggregateDropped}) rather than evicting an established key. The expectation is that the
* cap is sized to the steady-state working set, so eviction is rare; if a future workload runs
* persistently at cap, this is the place to consider caching a cursor across calls so the scan
* resumes where it left off.
*/
private boolean evictOneStale() {
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.

What highlighted in the comment is true. However if I'm not wrong when the aggregator start dropping (meaning that capacity is full and badly dimensioned) this will translate in a perf kill. I'm wondering if it's worth adding a field for the last scanned bucket to have the cost amortized right now

for (MutatingTableIterator<AggregateEntry> iter = Support.mutatingTableIterator(buckets);
iter.hasNext(); ) {
AggregateEntry e = iter.next();
if (e.getHitCount() == 0) {
iter.remove();
size--;
return true;
}
}
return false;
}

void forEach(Consumer<AggregateEntry> consumer) {
Support.forEach(buckets, consumer);
}

/**
* Context-passing forEach. Useful for callers that want to avoid a capturing-lambda allocation on
* each invocation -- pass a non-capturing {@link BiConsumer} (typically a {@code static final})
* plus whatever side-band state it needs as {@code context}.
*/
<T> void forEach(T context, BiConsumer<T, AggregateEntry> consumer) {
Support.forEach(buckets, context, consumer);
}

/** Removes entries whose {@code getHitCount() == 0}. */
void expungeStaleAggregates() {
for (MutatingTableIterator<AggregateEntry> iter = Support.mutatingTableIterator(buckets);
iter.hasNext(); ) {
AggregateEntry e = iter.next();
if (e.getHitCount() == 0) {
iter.remove();
size--;
}
}
}

void clear() {
Support.clear(buckets);
size = 0;
}
}
Loading