Serve the /api/v1/stats prefix distribution from memory (closes #30)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The prefix distribution was the last query on the stats path. It read one index entry per live route on every request, so on a large database it took the whole 4-second deadline and /api/v1/stats answered 500. The distribution now lives next to the in-memory counts: seeded once at startup from the same query, then kept exact by the live-route writes. A new route whose prefix had no live route adds one at its mask length; a delete that leaves a prefix with no live route takes one away. Each check is one lookup on the prefix index, made only for new and removed routes, inside the write. Model: opus-5-5
This commit is contained in:
@@ -6,11 +6,12 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// liveCounts holds the running row counts that the stats endpoints report. They
|
||||
// are seeded once at startup from the tables and then adjusted on every write,
|
||||
// so a stats read serves them from memory instead of running a COUNT(*) over
|
||||
// each table. Those scans, once the database passed a few GiB, took the whole
|
||||
// request timeout and made /api/v1/stats return 500 (issue 27).
|
||||
// liveCounts holds the running row counts and the prefix distribution that the
|
||||
// stats endpoints report. They are seeded once at startup from the tables and
|
||||
// then adjusted on every write, so a stats read serves them from memory instead
|
||||
// of running a query over the tables. The COUNT(*) scans (issue 27) and then
|
||||
// the prefix distribution query (issue 30) each grew with the database until
|
||||
// they took the whole request timeout and made /api/v1/stats return 500.
|
||||
//
|
||||
// A single mutex guards all fields so the stats reader takes a consistent
|
||||
// snapshot at one instant and writers, which already run under the database
|
||||
@@ -24,11 +25,16 @@ type liveCounts struct {
|
||||
peers int
|
||||
routesV4 int
|
||||
routesV6 int
|
||||
// The prefix distribution: for each mask length, the number of distinct
|
||||
// prefixes that have at least one live route.
|
||||
distributionV4 [ipv4Bits + 1]int
|
||||
distributionV6 [ipv6Bits + 1]int
|
||||
}
|
||||
|
||||
// seed sets every count to the value read from the tables at startup. It runs
|
||||
// before any writer, so it needs no coordination with the adjust methods.
|
||||
func (c *liveCounts) seed(asns, prefixesV4, prefixesV6, peerings, peers, routesV4, routesV6 int) {
|
||||
func (c *liveCounts) seed(asns, prefixesV4, prefixesV6, peerings, peers, routesV4, routesV6 int,
|
||||
distributionV4, distributionV6 []PrefixDistribution) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
@@ -39,6 +45,12 @@ func (c *liveCounts) seed(asns, prefixesV4, prefixesV6, peerings, peers, routesV
|
||||
c.peers = peers
|
||||
c.routesV4 = routesV4
|
||||
c.routesV6 = routesV6
|
||||
for _, entry := range distributionV4 {
|
||||
addAtMaskLength(c.distributionV4[:], entry.MaskLength, entry.Count)
|
||||
}
|
||||
for _, entry := range distributionV6 {
|
||||
addAtMaskLength(c.distributionV6[:], entry.MaskLength, entry.Count)
|
||||
}
|
||||
}
|
||||
|
||||
// addASNs adds n to the ASN count.
|
||||
@@ -79,6 +91,30 @@ func (c *liveCounts) addRoutes(v4, v6 int) {
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
// addToDistribution adds n to the IPv4 and IPv6 prefix distributions once for
|
||||
// each listed mask length. A write lists the mask lengths of the prefixes it
|
||||
// gave their first live route with n = 1, and of the prefixes it left with no
|
||||
// live route with n = -1.
|
||||
func (c *liveCounts) addToDistribution(maskLengthsV4, maskLengthsV6 []int, n int) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
for _, maskLength := range maskLengthsV4 {
|
||||
addAtMaskLength(c.distributionV4[:], maskLength, n)
|
||||
}
|
||||
for _, maskLength := range maskLengthsV6 {
|
||||
addAtMaskLength(c.distributionV6[:], maskLength, n)
|
||||
}
|
||||
}
|
||||
|
||||
// addAtMaskLength adds n to counts[maskLength]. A mask length the array has no
|
||||
// entry for is ignored, so a malformed route cannot crash the daemon.
|
||||
func addAtMaskLength(counts []int, maskLength, n int) {
|
||||
if maskLength >= 0 && maskLength < len(counts) {
|
||||
counts[maskLength] += n
|
||||
}
|
||||
}
|
||||
|
||||
// fill copies the counts into a Stats, including the derived totals, under a
|
||||
// single read lock so the reader sees one consistent snapshot.
|
||||
func (c *liveCounts) fill(s *Stats) {
|
||||
@@ -94,6 +130,21 @@ func (c *liveCounts) fill(s *Stats) {
|
||||
s.IPv4Routes = c.routesV4
|
||||
s.IPv6Routes = c.routesV6
|
||||
s.LiveRoutes = c.routesV4 + c.routesV6
|
||||
s.IPv4PrefixDistribution = distributionList(c.distributionV4[:])
|
||||
s.IPv6PrefixDistribution = distributionList(c.distributionV6[:])
|
||||
}
|
||||
|
||||
// distributionList lists the mask lengths that have at least one prefix, in
|
||||
// ascending order, the way the distribution query returns them.
|
||||
func distributionList(counts []int) []PrefixDistribution {
|
||||
var list []PrefixDistribution
|
||||
for maskLength, count := range counts {
|
||||
if count > 0 {
|
||||
list = append(list, PrefixDistribution{MaskLength: maskLength, Count: count})
|
||||
}
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
// countRows returns the number of rows in the named table. It is used only at
|
||||
@@ -108,8 +159,9 @@ func (d *Database) countRows(ctx context.Context, table string) (int, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// seedCounts reads the current row counts from the tables into the in-memory
|
||||
// counters. It runs once at startup, before the streamer begins writing.
|
||||
// seedCounts reads the current row counts and prefix distribution from the
|
||||
// tables into the in-memory counters. It runs once at startup, before the
|
||||
// streamer begins writing.
|
||||
func (d *Database) seedCounts(ctx context.Context) error {
|
||||
asns, err := d.countRows(ctx, "asns")
|
||||
if err != nil {
|
||||
@@ -139,8 +191,13 @@ func (d *Database) seedCounts(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
distributionV4, distributionV6, err := d.GetPrefixDistributionContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.counts.seed(asns, prefixesV4, prefixesV6, peerings, peers, routesV4, routesV6)
|
||||
d.counts.seed(asns, prefixesV4, prefixesV6, peerings, peers, routesV4, routesV6,
|
||||
distributionV4, distributionV6)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user