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

+41 -5
View File
@@ -402,8 +402,42 @@ func fetchIPEcho(ctx context.Context, url string) (string, error) {
}
func (a *Agent) runChecks(ctx context.Context, assignment assignmentResp) {
var results []checkResultDTO
var traces sync.WaitGroup
// Results are sent in batches every results_flush_seconds and before
// complete, instead of one request (and one control-api commit) each.
var (
bufMu sync.Mutex
buf []checkResultDTO
)
flush := func() {
bufMu.Lock()
batch := buf
buf = nil
bufMu.Unlock()
if len(batch) > 0 {
a.postResults(ctx, batch)
}
}
stopFlush := make(chan struct{})
var flusher sync.WaitGroup
flusher.Add(1)
go func() {
defer flusher.Done()
every := time.Duration(a.cfg.Checks.ResultsFlushSeconds) * time.Second
if every <= 0 {
every = 5 * time.Second
}
t := time.NewTicker(every)
defer t.Stop()
for {
select {
case <-t.C:
flush()
case <-stopFlush:
return
}
}
}()
for _, ct := range assignment.CheckConfig {
for _, target := range ct.Targets {
var fn func(context.Context) checkrunner.Result
@@ -432,14 +466,13 @@ func (a *Agent) runChecks(ctx context.Context, assignment assignmentResp) {
// loopback-only local e2e harness) would collide on the
// checks table's UNIQUE(ip_id, attempt, source, type,
// target) key and silently overwrite each other.
results = append(results, checkResultDTO{
bufMu.Lock()
buf = append(buf, checkResultDTO{
IPID: assignment.IPID, CheckType: res.CheckType, Target: target,
Success: res.Success, LatencyMS: res.LatencyMS, Detail: res.Detail,
CheckedAt: res.CheckedAt.Format(time.RFC3339Nano),
})
// Report progressively rather than batching until the end, so
// a crash mid-run doesn't lose already-completed check results.
a.postResults(ctx, []checkResultDTO{results[len(results)-1]})
bufMu.Unlock()
if ct.Type == "https" && !res.Success && a.cfg.Checks.Traceroute.IsEnabled() {
traces.Add(1)
@@ -455,6 +488,9 @@ func (a *Agent) runChecks(ctx context.Context, assignment assignmentResp) {
// detaches the floating IP and the trace would leave from another address.
traces.Wait()
close(stopFlush)
flusher.Wait()
flush()
a.postComplete(ctx, assignment.IPID)
a.lastHandledIPID = assignment.IPID
}