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

+33 -1
View File
@@ -28,8 +28,40 @@ func (d *DB) UpsertCheck(ctx context.Context, c Check) error {
// false (and writes nothing) when the result was dropped. The state test and
// the write are one statement, so they cannot interleave with SetAggregating.
func (d *DB) UpsertCheckIfOpen(ctx context.Context, c Check) (bool, error) {
return upsertCheckIfOpen(ctx, d, c)
}
// UpsertChecksIfOpen stores a batch of results in one transaction, applying
// the UpsertCheckIfOpen rule to each. written[i] reports whether checks[i] was
// stored (false: dropped).
func (d *DB) UpsertChecksIfOpen(ctx context.Context, checks []Check) ([]bool, error) {
written := make([]bool, len(checks))
if len(checks) == 0 {
return written, nil
}
tx, err := d.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
for i, c := range checks {
if written[i], err = upsertCheckIfOpen(ctx, tx, c); err != nil {
return nil, err
}
}
if err := tx.Commit(); err != nil {
return nil, err
}
return written, nil
}
type execer interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}
func upsertCheckIfOpen(ctx context.Context, x execer, c Check) (bool, error) {
now := timeToDB(Now())
res, err := d.ExecContext(ctx, `
res, err := x.ExecContext(ctx, `
INSERT INTO checks (registry_id, cycle_id, ip_id, ip_address, attempt_number, validator_id,
source, check_type, target, success, latency_ms, detail, checked_at, created_at, recorded_at, run_id)
SELECT q.registry_id, q.cycle_id, q.id, ?, q.attempt_number, COALESCE(NULLIF(?, ''), q.owner_validator_id, ''),