Repository navigation
Shard hot metric writes by scheduler, and add a stub build - #44
Conversation
Many processes writing the same counter, histogram bucket or exemplar
serialized on one ETS row lock, which can saturate every scheduler under
load. Writes now go to one row per scheduler, keyed {Key, SchedulerId} and
created on first use, and reads combine the shards.
Sharding
- Counters and histogram bucket counts (new imetrics_hist_counts table) are
summed across shards on read. Exemplars are sharded the same way, and
get_exemplar/1 returns the one with the newest timestamp.
- A full scrape reads each table in one scan: get_all/0 for histograms, and
the new imetrics:get_exemplars/0 for exemplars in the /metrics exporter.
- Write-heavy tables use write_concurrency auto, so OTP 25 is now the
minimum version. Histogram definitions move to a read_concurrency table.
- Gauges are not sharded.
Behaviour changes
- add/add_m return the calling scheduler's shard value, not the total, and
histogram add returns {Bucket, ShardCount}.
- Points within a counter come out sorted by tags.
- An exemplar set later with an older explicit timestamp is no longer
reported over a newer one.
- Memory and full-read cost grow with series x schedulers.
Stub build
Public API modules are facades over *_impl or no-op *_stub modules,
selected by the IMETRICS_STUB define, and imetrics:impl/0 reports which.
Stubs return the same shapes, and tick/tock still measure durations. A
`stub` rebar3 profile and CI step run the tests against it.
Also adds imetrics_bench (run on demand through the `bench` common_test
group), shard tests with writers pinned to every scheduler, and README
sections on concurrency, trade-offs, benchmarks and the stub build.
Every counter write converted its name and tags to binaries before
touching ETS: atoms to charlists to binaries, plus three maps. That
was about 380 of the 560 ns an add_m took, and about 1.1 KB of
garbage per call.
Counter rows are now keyed {{Name, Tags}, SchedulerId}, with Name and
Tags stored as given, and converted on read.
- The fast path is ets:update_counter/3 on an existing row. Only the
first write of a series on a scheduler converts the key, to check it,
before creating the row, so an invalid key still returns
{error, {function_clause, check_inputs}} and stores nothing.
- get_counter_points/0 converts each distinct raw key once per read,
and merges raw keys that read as the same series (v, "v", <<"v">>).
- delete_counter/2 removes every raw form of a series.
- Tuple names and tuple add_m keys are still joined with the separator
when written, so changing the separator later doesn't rename them.
add_m now takes about 141 ns, 8 reductions and 10 heap words per call,
against 560 ns, 41 and about 138 before (OTP 25.3, one process, dev
machine benchmark).
Histograms and exemplars still convert their keys on write.
Adds shard tests for merged raw keys, invalid keys, delete, and tuple
add_m keys.
There was a problem hiding this comment.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.
| Benchmark suite | Current: 870357f | Previous: 14d7f61 | Ratio |
|---|---|---|---|
Metrics throughput (avg between workers) |
2268407.655340109 metrics/second |
822037.9132002811 metrics/second |
2.76 |
This comment was automatically generated by workflow using github-action-benchmark.
|
The sharding approach makes sense, but the stub modules make the review very difficult. If we want a compile-time flag, I think it would be simpler to build that as a feature driven by macros (like logger does) rather than having to swap out module implementations. To that end, can we back out the stub modules so that we can review the sharding in isolation? |
Totally. I originally did the stub first when I was running down performance issues, then just left it in. Sorry it definitely does make the review more difficult. Per our meeting discussion I'll let you run with that part. Just let me know what you need from me! |
|
Two removals:
|
|
Many processes writing the same counter, histogram bucket or exemplar serialized on one ETS row lock, which can saturate every scheduler under load. Writes now go to one row per scheduler, keyed {Key, SchedulerId} and created on first use, and reads combine the shards.
Sharding
Behaviour changes
Stub build
Public API modules are facades over *_impl or no-op *_stub modules, selected by the IMETRICS_STUB define, and imetrics:impl/0 reports which. Stubs return the same shapes, and tick/tock still measure durations. A
stubrebar3 profile and CI step run the tests against it.Also adds imetrics_bench (run on demand through the
benchcommon_test group), shard tests with writers pinned to every scheduler, and README sections on concurrency, trade-offs, benchmarks and the stub build.