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

+12 -42
View File
@@ -62,6 +62,9 @@ type Orchestrator struct {
// next Tick does not start the same work twice.
inflight sync.Map
wg sync.WaitGroup
// hb is the in-memory heartbeat state (see heartbeat.go).
hb heartbeatState
}
// Wait blocks until all background work started by Tick has finished.
@@ -99,6 +102,7 @@ func New(d *db.DB, osClient openstack.FloatingIPClient, cfg *config.ControlAPI,
Log: log,
ScanPageSize: cfg.OpenStack.ListPageSize,
hb: heartbeatState{startedAt: time.Now()},
}
}
@@ -395,6 +399,12 @@ func (o *Orchestrator) RecordCheckIfOpen(ctx context.Context, c db.Check) (bool,
return o.DB.UpsertCheckIfOpen(ctx, c)
}
// RecordChecksIfOpen is RecordCheckIfOpen for a batch, written in one
// transaction; written[i] reports whether checks[i] was stored.
func (o *Orchestrator) RecordChecksIfOpen(ctx context.Context, checks []db.Check) ([]bool, error) {
return o.DB.UpsertChecksIfOpen(ctx, checks)
}
// RecordCheck is RecordCheckIfOpen for callers that do not need to know
// whether the result was dropped.
func (o *Orchestrator) RecordCheck(ctx context.Context, c db.Check) error {
@@ -419,8 +429,8 @@ func (o *Orchestrator) MarkSiteComplete(ctx context.Context, ipID int64, siteInd
func (o *Orchestrator) releaseValidatorPorts(ctx context.Context, validators []db.Validator) {
var wg sync.WaitGroup
for _, v := range validators {
if v.OSPortID == "" {
continue
if v.OSPortID == "" || v.State == db.ValidatorDiagnostic {
continue // a diagnostic hold keeps its floating ip attached
}
wg.Add(1)
go func(v db.Validator) {
@@ -893,46 +903,6 @@ func (o *Orchestrator) sweepExpiredLeases(ctx context.Context) error {
return nil
}
// sweepStaleHeartbeats marks validators unreachable if they haven't
// heartbeated within HeartbeatTimeoutSeconds. It does not itself reclaim
// their in-flight IP — that happens independently via lease expiry, so a
// validator that stops heartbeating but whose lease hasn't yet expired
// still finishes its current check window if it recovers in time.
func (o *Orchestrator) SweepStaleHeartbeats(ctx context.Context) error {
cutoff := db.Now().Add(-time.Duration(o.Cfg.HeartbeatTimeoutSeconds) * time.Second)
stale, err := o.DB.ListStaleHeartbeats(ctx, cutoff)
if err != nil {
return err
}
for _, v := range stale {
if err := o.DB.MarkValidatorUnreachable(ctx, v.ValidatorID); err != nil {
o.Log.Error("mark validator unreachable", "validator", v.ValidatorID, "err", err)
continue
}
o.event(ctx, "control-api", "", nil, "validator_unreachable", fmt.Sprintf(`{"validator_id":%q}`, v.ValidatorID))
}
return nil
}
// SweepStaleSiteHeartbeats marks prober sites unreachable if they haven't
// heartbeated within HeartbeatTimeoutSeconds — mirrors SweepStaleHeartbeats
// exactly, for sites instead of validators.
func (o *Orchestrator) SweepStaleSiteHeartbeats(ctx context.Context) error {
cutoff := db.Now().Add(-time.Duration(o.Cfg.HeartbeatTimeoutSeconds) * time.Second)
stale, err := o.DB.ListStaleSiteHeartbeats(ctx, cutoff)
if err != nil {
return err
}
for _, s := range stale {
if err := o.DB.MarkSiteUnreachable(ctx, s.SiteID); err != nil {
o.Log.Error("mark site unreachable", "site_id", s.SiteID, "err", err)
continue
}
o.event(ctx, "control-api", "", nil, "site_unreachable", fmt.Sprintf(`{"site_id":%q}`, s.SiteID))
}
return nil
}
// RecordEvent is the exported entry point httpapi uses to log
// agent/prober-reported audit events (config_received, fip_changed,
// error, etc.) through the same path as internally generated events