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

+112
View File
@@ -0,0 +1,112 @@
package db
import (
"context"
"database/sql"
"fmt"
"time"
)
// DiagnosticHold is an address held on a validator for manual diagnostics.
type DiagnosticHold struct {
Address string `json:"address"`
ValidatorID string `json:"validator_id"`
FIPID string `json:"fip_id"`
Note string `json:"note"`
CreatedAt time.Time `json:"created_at"`
}
// ClaimDiagnostic moves an idle validator to the diagnostic state and records
// the hold, in one transaction. ErrNotFound: no such validator; ErrBusy: the
// validator is not idle; ErrConflict: the address or validator is already held.
func (d *DB) ClaimDiagnostic(ctx context.Context, validatorID, address, fipID, note string) error {
tx, err := d.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
now := timeToDB(Now())
res, err := tx.ExecContext(ctx, `
UPDATE validators SET state=?, updated_at=?
WHERE validator_id=? AND state=? AND current_ip_id IS NULL
`, ValidatorDiagnostic, now, validatorID, ValidatorIdle)
if err != nil {
return err
}
if n, _ := res.RowsAffected(); n != 1 {
var one int
err := tx.QueryRowContext(ctx, `SELECT 1 FROM validators WHERE validator_id=?`, validatorID).Scan(&one)
if err == sql.ErrNoRows {
return fmt.Errorf("validator %q: %w", validatorID, ErrNotFound)
}
if err != nil {
return err
}
return fmt.Errorf("validator %q is not idle: %w", validatorID, ErrBusy)
}
if _, err := tx.ExecContext(ctx, `
INSERT INTO diagnostic_holds (address, validator_id, fip_id, note, created_at) VALUES (?, ?, ?, ?, ?)
`, address, validatorID, fipID, note, now); err != nil {
return fmt.Errorf("address %q or validator %q is already held: %w", address, validatorID, ErrConflict)
}
return tx.Commit()
}
// ReleaseDiagnostic deletes the hold of an address and returns the validator
// to idle. ErrNotFound if the address is not held.
func (d *DB) ReleaseDiagnostic(ctx context.Context, address string) (*DiagnosticHold, error) {
tx, err := d.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
h, err := scanHold(tx.QueryRowContext(ctx, holdSelect+` WHERE address=?`, address))
if err == sql.ErrNoRows {
return nil, fmt.Errorf("address %q is not on diagnostic: %w", address, ErrNotFound)
}
if err != nil {
return nil, err
}
if _, err := tx.ExecContext(ctx, `DELETE FROM diagnostic_holds WHERE address=?`, address); err != nil {
return nil, err
}
if _, err := tx.ExecContext(ctx, `
UPDATE validators SET state=?, updated_at=? WHERE validator_id=? AND state=?
`, ValidatorIdle, timeToDB(Now()), h.ValidatorID, ValidatorDiagnostic); err != nil {
return nil, err
}
return h, tx.Commit()
}
const holdSelect = `SELECT address, validator_id, fip_id, note, created_at FROM diagnostic_holds`
func scanHold(row rowScanner) (*DiagnosticHold, error) {
var h DiagnosticHold
var created string
if err := row.Scan(&h.Address, &h.ValidatorID, &h.FIPID, &h.Note, &created); err != nil {
return nil, err
}
var err error
if h.CreatedAt, err = dbToTime(created); err != nil {
return nil, err
}
return &h, nil
}
// ListDiagnosticHolds returns all holds, oldest first.
func (d *DB) ListDiagnosticHolds(ctx context.Context) ([]DiagnosticHold, error) {
rows, err := d.QueryContext(ctx, holdSelect+` ORDER BY id`)
if err != nil {
return nil, err
}
defer rows.Close()
out := []DiagnosticHold{}
for rows.Next() {
h, err := scanHold(rows)
if err != nil {
return nil, err
}
out = append(out, *h)
}
return out, rows.Err()
}