control-api: reduce SQLite disk writes; add diagnostic mode

Disk writes:
- SQLite pragmas in DSN: wal_autocheckpoint=10000, journal_size_limit=64MB, cache_size=64MB
- migration 0013: drop redundant idx_checks_registry_cycle
- results batched: UpsertChecksIfOpen in one transaction; agent flushes every 5s and before complete
- heartbeat kept in memory, persisted on state change or every 60s; startup grace for stale sweeps

Diagnostic mode:
- POST /api/v1/admin/diagnostics/attach|detach, GET /api/v1/admin/diagnostics
- migration 0014: diagnostic_holds; validator state "diagnostic"
- held addresses are not queued, held validators skip sweeps, port release and delete

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Opus 5.5 committed 2026-11-03 07:01:24 +03:00
1 parent d0ab6b94f9
commit 55d383bd34
41 files changed
+1214 -94

No files matched your search

+35 -12
View File
@@ -8,6 +8,7 @@ import (
"database/sql"
_ "embed"
"fmt"
"strings"
"time"
_ "modernc.org/sqlite"
@@ -49,6 +50,12 @@ var checkRunsSchema string
//go:embed migrations/0012_self_check_failures.sql
var selfCheckFailuresSchema string
//go:embed migrations/0013_drop_redundant_checks_index.sql
var dropChecksIndexSchema string
//go:embed migrations/0014_diagnostic_holds.sql
var diagnosticHoldsSchema string
// migrations is the ordered list of schema versions. Each entry's SQL is
// applied, in order, for any version greater than the database's current
// PRAGMA user_version — so a fresh database walks the whole list and an
@@ -69,6 +76,28 @@ var migrations = []struct {
{10, verdictIntegritySchema},
{11, checkRunsSchema},
{12, selfCheckFailuresSchema},
{13, dropChecksIndexSchema},
{14, diagnosticHoldsSchema},
}
// connPragmas are applied to every new connection. A larger WAL checkpoint
// interval and cache cut random writes on the HDD-backed volume.
var connPragmas = []string{
"busy_timeout(5000)",
"journal_mode(WAL)",
"synchronous(NORMAL)",
"foreign_keys(ON)",
"wal_autocheckpoint(10000)",
"journal_size_limit(67108864)",
"cache_size(-65536)",
}
func dsn(path string) string {
q := make([]string, len(connPragmas))
for i, p := range connPragmas {
q[i] = "_pragma=" + p
}
return path + "?" + strings.Join(q, "&")
}
type DB struct {
@@ -79,24 +108,18 @@ type DB struct {
// pragmas suited to a single-writer WAL workload, and runs any pending
// schema migrations.
func Open(ctx context.Context, path string) (*DB, error) {
sqlDB, err := sql.Open("sqlite", path+"?_pragma=busy_timeout(5000)")
sqlDB, err := sql.Open("sqlite", dsn(path))
if err != nil {
return nil, fmt.Errorf("open sqlite: %w", err)
}
// Control API is the sole writer; one connection avoids SQLITE_BUSY
// entirely for writes while still allowing concurrent reads via WAL.
sqlDB.SetMaxOpenConns(1)
for _, pragma := range []string{
"PRAGMA journal_mode=WAL",
"PRAGMA synchronous=NORMAL",
"PRAGMA foreign_keys=ON",
"PRAGMA busy_timeout=5000",
} {
if _, err := sqlDB.ExecContext(ctx, pragma); err != nil {
sqlDB.Close()
return nil, fmt.Errorf("apply pragma %q: %w", pragma, err)
}
// Pragmas are in the DSN, so they apply to every new connection; this
// ping opens the first one and reports a bad pragma early.
if err := sqlDB.PingContext(ctx); err != nil {
sqlDB.Close()
return nil, fmt.Errorf("open sqlite: %w", err)
}
d := &DB{DB: sqlDB}