Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
53 changes: 53 additions & 0 deletions internal/engine/compile_cache_lru_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package engine

import (
"fmt"
"testing"
)

// The snapshot-compile cache is an LRU: reaching capacity evicts exactly one
// entry, the least recently used, rather than clearing the whole cache. A
// wholesale clear would throw away a dashboard's stable, repeatedly-run
// statements every time a burst of one-off queries crossed the bound.
func TestCompileCacheEvictsOneEntryNotAll(t *testing.T) {
d, _ := newBatchCacheTest(t)
ctx := t.Context()
// Distinct statement text per index keys a distinct cache entry.
query := func(i int) string { return fmt.Sprintf("SELECT %d FROM spans", i) }

// Fill the cache to capacity; query(0) is the least recently used.
for i := 0; i < snapshotCacheCapacity; i++ {
if _, err := d.compileSnapshot(ctx, d.DB, query(i)); err != nil {
t.Fatal(err)
}
}
// One more entry overflows the cache, evicting exactly the one LRU entry.
if _, err := d.compileSnapshot(ctx, d.DB, query(snapshotCacheCapacity)); err != nil {
t.Fatal(err)
}

// Every entry except the evicted query(0) must still be cached. Re-reading
// them is a hit and never evicts, so order among survivors is irrelevant.
hits0, misses0 := d.SnapshotCacheStats()
for i := 1; i <= snapshotCacheCapacity; i++ {
if _, err := d.compileSnapshot(ctx, d.DB, query(i)); err != nil {
t.Fatal(err)
}
}
hits1, misses1 := d.SnapshotCacheStats()
if misses1 != misses0 {
t.Fatalf("surviving entries should all hit, but %d missed (wholesale clear?)", misses1-misses0)
}
if hits1-hits0 != int64(snapshotCacheCapacity) {
t.Fatalf("expected %d hits from survivors, got %d", snapshotCacheCapacity, hits1-hits0)
}

// The single oldest entry, and only it, was evicted: a fresh miss.
if _, err := d.compileSnapshot(ctx, d.DB, query(0)); err != nil {
t.Fatal(err)
}
_, misses2 := d.SnapshotCacheStats()
if misses2 != misses1+1 {
t.Fatalf("query(0) should have been the one evicted; misses %d->%d", misses1, misses2)
}
}
20 changes: 19 additions & 1 deletion internal/engine/duck.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package engine

import (
"container/list"
"context"
"database/sql"
"database/sql/driver"
Expand Down Expand Up @@ -39,8 +40,17 @@ type Duck struct {
cachePublishMu sync.Mutex
cachePublishing map[string]bool
cacheTransfers []*batchCacheTransfer
// snapshotMu guards the snapshot-compile cache: the most-recently-used list
// and its key index. Snapshot compilation is costly (serialize/rewrite/
// deserialize through the engine), so compiled relation aliases are kept by
// their now window-stable statement text and evicted one entry at a time.
snapshotMu sync.Mutex
snapshotQueries map[string]compiledSnapshot
snapshotLRU *list.List // of *snapshotCacheEntry, most recently used first
snapshotQueries map[string]*list.Element
// snapshotHits and snapshotMisses count the compile cache's lookups so a
// benchmark or test can observe its effectiveness; see SnapshotCacheStats.
snapshotHits atomic.Int64
snapshotMisses atomic.Int64
// DB is the read pool. Reads scan immutable Parquet concurrently, so it is
// sized from the machine.
DB *sql.DB
Expand Down Expand Up @@ -238,6 +248,14 @@ func (d *Duck) runPendingCheckpoint(ctx context.Context) error {
return nil
}

// SnapshotCacheStats reports the cumulative snapshot-compile cache hits and
// misses since start. A miss is a statement whose compiled relation aliases
// had to be built; a hit reused them. It lets a benchmark or test measure the
// cache hit rate, which window-stable statement text is meant to raise.
func (d *Duck) SnapshotCacheStats() (hits, misses int64) {
return d.snapshotHits.Load(), d.snapshotMisses.Load()
}

const (
// Rollup progress keys in rollup_state. readCacheVersion drops that table
// with the rollups, so a version change also restarts their backfill; the
Expand Down
105 changes: 75 additions & 30 deletions internal/engine/file_snapshot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package engine

import (
"container/list"
"context"
"database/sql"
"encoding/json"
Expand Down Expand Up @@ -344,43 +345,87 @@ type compiledSnapshot struct {
metricNames []string
}

// snapshotCacheCapacity bounds the compile cache. Reaching it evicts the single
// least-recently-used entry, so a burst of one-off statements cannot discard
// the dashboard's stable, repeatedly-run ones.
const snapshotCacheCapacity = 128

// snapshotCacheEntry is one cached compilation, held in the LRU list under its
// statement text.
type snapshotCacheEntry struct {
key string
compiled compiledSnapshot
}

// cachedSnapshot returns a compilation for query and moves it to the front of
// the LRU when present.
func (d *Duck) cachedSnapshot(query string) (compiledSnapshot, bool) {
d.snapshotMu.Lock()
defer d.snapshotMu.Unlock()
element, ok := d.snapshotQueries[query]
if !ok {
return compiledSnapshot{}, false
}
d.snapshotLRU.MoveToFront(element)
return element.Value.(*snapshotCacheEntry).compiled, true
}

// storeSnapshot records compiled under query at the front of the LRU, evicting
// the least-recently-used entry once over capacity.
func (d *Duck) storeSnapshot(query string, compiled compiledSnapshot) {
d.snapshotMu.Lock()
defer d.snapshotMu.Unlock()
if d.snapshotQueries == nil {
d.snapshotQueries = map[string]*list.Element{}
d.snapshotLRU = list.New()
}
if element, ok := d.snapshotQueries[query]; ok {
element.Value.(*snapshotCacheEntry).compiled = compiled
d.snapshotLRU.MoveToFront(element)
return
}
d.snapshotQueries[query] = d.snapshotLRU.PushFront(&snapshotCacheEntry{key: query, compiled: compiled})
for d.snapshotLRU.Len() > snapshotCacheCapacity {
oldest := d.snapshotLRU.Back()
d.snapshotLRU.Remove(oldest)
delete(d.snapshotQueries, oldest.Value.(*snapshotCacheEntry).key)
}
}

// compileSnapshot compiles relation aliases once with the native parser. Per-read
// scopes become ordinary CTEs, avoiding repeated JSON SQL parse/deserialize work.
// The window bounds ride as bind parameters, so a panel's statement text is
// identical across time windows and this cache hits instead of recompiling.
func (d *Duck) compileSnapshot(ctx context.Context, db snapshotSQL, query string) (compiledSnapshot, error) {
d.snapshotMu.Lock()
compiled, ok := d.snapshotQueries[query]
d.snapshotMu.Unlock()
if !ok {
aliases := map[string]string{}
names := []string{"trace_candidates", "trace_tail", "resource_inventory", "receiver_rollup", "receiver_points", "resource_rollup", "receiver_entity_rollup"}
for _, s := range telemetry.Signals {
names = append(names, "telemetry."+s.Name, s.Name)
}
for _, name := range names {
alias := "__pitot_snapshot_" + strings.ReplaceAll(name, ".", "_")
aliases[name] = "SELECT * FROM " + alias
}
used := map[string]string{}
rewritten, err := compileRelations(ctx, db, query, aliases, used)
if compiled, ok := d.cachedSnapshot(query); ok {
d.snapshotHits.Add(1)
return compiled, nil
}
d.snapshotMisses.Add(1)
aliases := map[string]string{}
names := []string{"trace_candidates", "trace_tail", "resource_inventory", "receiver_rollup", "receiver_points", "resource_rollup", "receiver_entity_rollup"}
for _, s := range telemetry.Signals {
names = append(names, "telemetry."+s.Name, s.Name)
}
for _, name := range names {
alias := "__pitot_snapshot_" + strings.ReplaceAll(name, ".", "_")
aliases[name] = "SELECT * FROM " + alias
}
used := map[string]string{}
rewritten, err := compileRelations(ctx, db, query, aliases, used)
if err != nil {
return compiledSnapshot{}, err
}
// The AST records only rewritten table references, never SQL literals.
compiled := compiledSnapshot{query: rewritten, names: used}
if _, ok := used["metrics"]; ok {
root, err := parseSQLTree(ctx, db, query)
if err != nil {
return compiledSnapshot{}, err
}
// The AST records only rewritten table references, never SQL literals.
compiled = compiledSnapshot{query: rewritten, names: used}
if _, ok := used["metrics"]; ok {
root, err := parseSQLTree(ctx, db, query)
if err != nil {
return compiledSnapshot{}, err
}
compiled.metricNames = singleMetricNames(root)
}
d.snapshotMu.Lock()
if d.snapshotQueries == nil || len(d.snapshotQueries) >= 128 {
d.snapshotQueries = map[string]compiledSnapshot{}
}
d.snapshotQueries[query] = compiled
d.snapshotMu.Unlock()
compiled.metricNames = singleMetricNames(root)
}
d.storeSnapshot(query, compiled)
return compiled, nil
}

Expand Down
16 changes: 15 additions & 1 deletion internal/query/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,20 @@ func compileAggregateQuery(p *Panel, measures []Measure, filters []Filter, scope
}
hasTotals := bucket != "" && (p.Viz == "stat" || p.Viz == "gauge")
quantiles := map[int]Measure{}
// Rolled rate measures read the covered-seconds window bounds as bind
// parameters. They appear in the measure projection, which the engine
// renders after the base CTE's own window placeholders and before no other
// placeholder, so these arrive after args in textual order. Only rate
// embeds the expression, so its parameters ride along exactly when the
// placeholder survived into the measure text.
var measureArgs []any
for _, m := range measures {
expr := measureExprSQL(m, sig, seconds, partition)
var coveredArgs []any
if rolled {
expr = rollupMeasureSQL(m, coveredSecondsSQL(scope, bucket, hasTotals), partition)
var covered string
covered, coveredArgs = coveredSecondsSQL(scope, bucket, hasTotals)
expr = rollupMeasureSQL(m, covered, partition)
} else if hasTotals {
expr = strings.ReplaceAll(expr, "count(*) / "+sqlFloat(seconds), "count(*) / (CASE WHEN grouping("+bucket+")=1 THEN "+sqlFloat(scope.End.Sub(scope.Start).Seconds())+" ELSE "+sqlFloat(seconds)+" END)")
}
Expand All @@ -266,12 +276,16 @@ func compileAggregateQuery(p *Panel, measures []Measure, filters []Filter, scope
expr = "NULL::DOUBLE"
}
selects = append(selects, expr+" AS "+quoteIdent(m.Alias))
if rolled && strings.Contains(expr, "?") {
measureArgs = append(measureArgs, coveredArgs...)
}
unit := m.Unit
if unit == "none" {
unit = "" // The catalog cannot infer a physical unit for this field.
}
columns = append(columns, Column{Name: m.Alias, Type: "number", Role: "measure", Unit: unit})
}
args = append(args, measureArgs...)
var b strings.Builder
var ranking *Ranking
rankingFloor := false
Expand Down
Loading
Loading