Storage holds minute and hourly statistics counters, gauges and top-N rollups per node, readable for one node or all nodes #111

Open
opened 2026-10-31 12:42:08 -04:00 by claude · 0 comments
Owner
  • Draft: PQ-13
  • Priority: high
  • Agent: storage-coder
  • Depends on: none
  • Decisions: Q-PQ-1, Q-PQ-12, R2-STATS-NODE

Why

The dashboard needs time series, range totals and top-N lists that survive query-log pruning, for DNS and DHCP, per node and combined. MetricEntity has no bucket size and no node, so a minute row and an hour row cannot be told apart, and two nodes on one database would write the same row. Its only index is non-unique on (Category, Name, TimestampUtc) (src/dotnet/src/Ninlil.Dns.Data.EntityFrameworkCore/Configuration/MetricConfiguration.cs:27-32). Nothing writes the table today, so its shape can change without a data conversion.

What changes

  • MetricEntity: add NodeId (Guid, the writing node's ClusterOptions.NodeId, see #112) and BucketSeconds (int: 60 or 3600). Replace the index with a unique one on (NodeId, Category, Name, BucketSeconds, TimestampUtc); TimestampUtc is the bucket start. Add an index on (Category, BucketSeconds, TimestampUtc) for all-node reads.
  • Series kinds, both in IMetricRepository:
    • Counters (queries, messages): AddAsync(IReadOnlyCollection<MetricIncrement>) adds to the row's Value, creating it when missing. Technical call: the add is atomic in the database (an UPDATE … SET Value = Value + @delta with an insert when no row matched, retried on a unique-key race; $inc with upsert on MongoDB), so an overlapping or retried flush never loses a count.
    • Gauges (active leases): SetAsync(IReadOnlyCollection<MetricSample>) replaces Value.
  • Reads, executed in the database, each with an optional Guid? nodeId (null = all nodes):
    • ListSeriesAsync(category, namePrefix, bucketSeconds, fromUtc, toUtc, nodeId, MetricAggregateEnum aggregate) returns rows (name, bucket start, value) for charts.
    • SumByNameAsync(category, namePrefix, bucketSeconds, fromUtc, toUtc, nodeId) returns one total per name, for tiles and doughnuts.
    • ListNodeIdsAsync(fromUtc, toUtc) returns the distinct node ids that wrote rows in the range, for the node filter.
    • Technical call: for all nodes, counters are summed across nodes (MetricAggregateEnum.Sum) and gauges take the maximum per bucket (Max). Nodes on one database read the same lease table, so summing their lease gauges would count every lease once per node.
  • Names are free-form, so new series need no schema change. Categories in use: dns, dhcp-v4, dhcp-v6 (the ones the entity summary already names).
  • New DnsQueryTopItemEntity (NodeId, BucketStartUtc hour, Kind enum Domain|BlockedDomain|Client, Key (max 255), Count), unique on (NodeId, BucketStartUtc, Kind, Key).
    • Repository: AddAsync(batch) with the same atomic add, and TopAsync(kind, fromUtc, toUtc, take, nodeId), which sums across buckets (and across nodes when nodeId is null) in the database.
  • DeleteOlderThanAsync(bucketSeconds, cutoffUtc) on metrics and DeleteOlderThanAsync(cutoffUtc) on top items, across every node.
  • One migration per provider (AddStatisticsRollups), Mongo repository, class map and indexes in MongoIndexCatalog, and a MongoSchemaSteps.Known entry if an older build would misread the documents.
  • Backup stays as it is: BackupEntityCatalog already excludes MetricEntity as telemetry (src/dotnet/src/Ninlil.Dns.Server.Backup/Catalog/BackupEntityCatalog.cs:27-29); exclude DnsQueryTopItemEntity the same way and add it to that summary.

Acceptance

  • Integration test fails first in src/dotnet/tests/Ninlil.Dns.Data.Integration.Tests: two hourly buckets of example.com (3 and 4) on one node return one top item with Count 7. Runs on every engine (SQLite without Docker, the rest RequiresDocker).
  • Test: node A adds 5 and node B adds 3 to the same counter name and bucket; nodeId = A reads 5, nodeId = null reads 8, and ListNodeIdsAsync returns both ids.
  • Test: gauges of 4 (node A) and 4 (node B) in one bucket read 4 for all nodes with Max.
  • Test: two concurrent AddAsync calls of 5 to the same counter row leave Value == 10 on every engine.
  • Test: SetAsync twice on one gauge row keeps the second value.
  • Test: a minute row and an hour row with the same name and start are both kept and read back by their bucketSeconds.
  • Schema change: 4 migrations plus Mongo. No API or option change.

Source

  • audit-feature-parity.md §2A #1.
  • Owner decision 2026-10-31 (Q-PQ-12): DHCP series are stored alongside the DNS ones.
  • Owner decision 2026-10-31, round 2 (R2-STATS-NODE): "node id stored per row".
- **Draft:** PQ-13 - **Priority:** high - **Agent:** storage-coder - **Depends on:** none - **Decisions:** Q-PQ-1, Q-PQ-12, R2-STATS-NODE ### Why The dashboard needs time series, range totals and top-N lists that survive query-log pruning, for DNS and DHCP, per node and combined. `MetricEntity` has no bucket size and no node, so a minute row and an hour row cannot be told apart, and two nodes on one database would write the same row. Its only index is non-unique on `(Category, Name, TimestampUtc)` (`src/dotnet/src/Ninlil.Dns.Data.EntityFrameworkCore/Configuration/MetricConfiguration.cs:27-32`). Nothing writes the table today, so its shape can change without a data conversion. ### What changes - `MetricEntity`: add `NodeId` (Guid, the writing node's `ClusterOptions.NodeId`, see #112) and `BucketSeconds` (int: 60 or 3600). Replace the index with a unique one on `(NodeId, Category, Name, BucketSeconds, TimestampUtc)`; `TimestampUtc` is the bucket start. Add an index on `(Category, BucketSeconds, TimestampUtc)` for all-node reads. - Series kinds, both in `IMetricRepository`: - Counters (queries, messages): `AddAsync(IReadOnlyCollection<MetricIncrement>)` adds to the row's `Value`, creating it when missing. **Technical call:** the add is atomic in the database (an `UPDATE … SET Value = Value + @delta` with an insert when no row matched, retried on a unique-key race; `$inc` with upsert on MongoDB), so an overlapping or retried flush never loses a count. - Gauges (active leases): `SetAsync(IReadOnlyCollection<MetricSample>)` replaces `Value`. - Reads, executed in the database, each with an optional `Guid? nodeId` (null = all nodes): - `ListSeriesAsync(category, namePrefix, bucketSeconds, fromUtc, toUtc, nodeId, MetricAggregateEnum aggregate)` returns rows (name, bucket start, value) for charts. - `SumByNameAsync(category, namePrefix, bucketSeconds, fromUtc, toUtc, nodeId)` returns one total per name, for tiles and doughnuts. - `ListNodeIdsAsync(fromUtc, toUtc)` returns the distinct node ids that wrote rows in the range, for the node filter. - **Technical call:** for all nodes, counters are summed across nodes (`MetricAggregateEnum.Sum`) and gauges take the maximum per bucket (`Max`). Nodes on one database read the same lease table, so summing their lease gauges would count every lease once per node. - Names are free-form, so new series need no schema change. Categories in use: `dns`, `dhcp-v4`, `dhcp-v6` (the ones the entity summary already names). - New `DnsQueryTopItemEntity` (`NodeId`, `BucketStartUtc` hour, `Kind` enum `Domain|BlockedDomain|Client`, `Key` (max 255), `Count`), unique on `(NodeId, BucketStartUtc, Kind, Key)`. - Repository: `AddAsync(batch)` with the same atomic add, and `TopAsync(kind, fromUtc, toUtc, take, nodeId)`, which sums across buckets (and across nodes when `nodeId` is null) in the database. - `DeleteOlderThanAsync(bucketSeconds, cutoffUtc)` on metrics and `DeleteOlderThanAsync(cutoffUtc)` on top items, across every node. - One migration per provider (`AddStatisticsRollups`), Mongo repository, class map and indexes in `MongoIndexCatalog`, and a `MongoSchemaSteps.Known` entry if an older build would misread the documents. - Backup stays as it is: `BackupEntityCatalog` already excludes `MetricEntity` as telemetry (`src/dotnet/src/Ninlil.Dns.Server.Backup/Catalog/BackupEntityCatalog.cs:27-29`); exclude `DnsQueryTopItemEntity` the same way and add it to that summary. ### Acceptance - [ ] Integration test fails first in `src/dotnet/tests/Ninlil.Dns.Data.Integration.Tests`: two hourly buckets of `example.com` (3 and 4) on one node return one top item with Count 7. Runs on every engine (SQLite without Docker, the rest `RequiresDocker`). - [ ] Test: node A adds 5 and node B adds 3 to the same counter name and bucket; `nodeId = A` reads 5, `nodeId = null` reads 8, and `ListNodeIdsAsync` returns both ids. - [ ] Test: gauges of 4 (node A) and 4 (node B) in one bucket read 4 for all nodes with `Max`. - [ ] Test: two concurrent `AddAsync` calls of 5 to the same counter row leave `Value == 10` on every engine. - [ ] Test: `SetAsync` twice on one gauge row keeps the second value. - [ ] Test: a minute row and an hour row with the same name and start are both kept and read back by their `bucketSeconds`. - [ ] Schema change: 4 migrations plus Mongo. No API or option change. ### Source - `audit-feature-parity.md` §2A #1. - Owner decision 2026-10-31 (Q-PQ-12): DHCP series are stored alongside the DNS ones. - Owner decision 2026-10-31, round 2 (R2-STATS-NODE): "node id stored per row".
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
lantea-one/ninlil#111
No description provided.