Keep one address per validator; fix heartbeat handling and queue clear
A mass check on 2026-10-02 stalled 7 of 20 validators and sent 42 addresses to fail without a single check. A validator busy with slow checks went silent, was marked unreachable, and its next heartbeat put it back to idle while it still held the address; it was handed a second one, whose association never ran (the in-flight guard was keyed by validator), and both waited for their leases to expire. - Heartbeat/re-register return an unreachable validator to assigned when it still holds an address, else idle. - A validator is released only from the address it currently holds (ReleaseFIP, RequeueOrFail, MarkFIPOccupied, FreeValidator); an unreachable validator stays unreachable until its next heartbeat, so a dead validator is no longer handed a new address every lease period. - ClaimNextQueued refuses a validator that still has an address; a ReconcileValidators pass on every tick repairs rows that disagree with the queue. - Association guard is keyed by address, not validator. - The agent sends heartbeats from their own goroutine. - Clear queue / delete: detach only floating IPs of unfinished rows (done, failed and occupied rows kept their fip_id and made a clear issue >1000 sequential cloud calls: 256 s), at most 8 in parallel; the operation no longer dies with the client connection (10 minute limit). Includes the incident analysis and the plan under analysis/ and docs/changes/, and rebuilt bin/control-api and bin/validator-agent. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cf4a883363
commit
0532baff09
17 files changed
+1293
-62
No files matched your search
@@ -27,24 +27,32 @@ func (d *DB) RegisterValidator(ctx context.Context, validatorID, hostname, osPor
|
||||
}
|
||||
// A brand-new row already lands in ValidatorIdle via the INSERT branch;
|
||||
// a re-registering validator that was 'unregistered' or 'unreachable'
|
||||
// (but not mid-assignment) should also come back to idle.
|
||||
// comes back: to idle when it holds no address, to assigned when it still
|
||||
// does (its current_ip_id is kept, so it must not be handed another).
|
||||
_, err = d.ExecContext(ctx, `
|
||||
UPDATE validators SET state=?, updated_at=?
|
||||
UPDATE validators SET
|
||||
state = CASE WHEN current_ip_id IS NULL THEN ? ELSE ? END,
|
||||
updated_at=?
|
||||
WHERE validator_id=? AND state IN (?, ?)
|
||||
`, ValidatorIdle, now, validatorID, ValidatorUnregistered, ValidatorUnreachable)
|
||||
`, ValidatorIdle, ValidatorAssigned, now, validatorID, ValidatorUnregistered, ValidatorUnreachable)
|
||||
if err != nil {
|
||||
return fmt.Errorf("register validator (reactivate): %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Heartbeat records a sign of life. A validator that was marked unreachable
|
||||
// returns to idle if it holds no address, but to assigned if it still does:
|
||||
// it was only silent (for example busy with slow checks), and handing it a
|
||||
// second address while it works on the first would leave the second one
|
||||
// without an owner that can ever pick it up.
|
||||
func (d *DB) Heartbeat(ctx context.Context, validatorID string) error {
|
||||
now := timeToDB(Now())
|
||||
res, err := d.ExecContext(ctx, `
|
||||
UPDATE validators SET last_heartbeat_at=?, updated_at=?,
|
||||
state = CASE WHEN state=? THEN ? ELSE state END
|
||||
state = CASE WHEN state=? THEN (CASE WHEN current_ip_id IS NULL THEN ? ELSE ? END) ELSE state END
|
||||
WHERE validator_id=?
|
||||
`, now, now, ValidatorUnreachable, ValidatorIdle, validatorID)
|
||||
`, now, now, ValidatorUnreachable, ValidatorIdle, ValidatorAssigned, validatorID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("heartbeat: %w", err)
|
||||
}
|
||||
@@ -138,16 +146,59 @@ func (d *DB) MarkValidatorUnreachable(ctx context.Context, validatorID string) e
|
||||
return err
|
||||
}
|
||||
|
||||
// FreeValidator returns a validator to idle with no assigned IP. Used after
|
||||
// an IP finishes (success or failure) or is reclaimed by the lease sweep.
|
||||
func (d *DB) FreeValidator(ctx context.Context, validatorID string) error {
|
||||
_, err := d.ExecContext(ctx, `
|
||||
UPDATE validators SET state=?, current_ip_id=NULL, updated_at=?
|
||||
WHERE validator_id=?
|
||||
`, ValidatorIdle, timeToDB(Now()), validatorID)
|
||||
// freeValidatorSQL releases a validator from the address it holds. It only
|
||||
// applies when the validator's current address is the one being released
|
||||
// (args: now, validator id, ip id): a late release of an old address must not
|
||||
// free a validator that has already moved on to another one. An unreachable
|
||||
// validator stays unreachable until its next heartbeat, so a dead validator
|
||||
// is not handed new addresses just because its lease was reclaimed.
|
||||
var freeValidatorSQL = fmt.Sprintf(`
|
||||
UPDATE validators SET current_ip_id=NULL,
|
||||
state = CASE WHEN state='%s' THEN state ELSE '%s' END,
|
||||
updated_at=?
|
||||
WHERE validator_id=? AND current_ip_id=?`, ValidatorUnreachable, ValidatorIdle)
|
||||
|
||||
// FreeValidator releases a validator from the given address (see
|
||||
// freeValidatorSQL). Used after an IP finishes (success or failure) or is
|
||||
// reclaimed by the lease sweep.
|
||||
func (d *DB) FreeValidator(ctx context.Context, validatorID string, ipID int64) error {
|
||||
_, err := d.ExecContext(ctx, freeValidatorSQL, timeToDB(Now()), validatorID, ipID)
|
||||
return err
|
||||
}
|
||||
|
||||
// ReconcileValidators repairs validators whose state disagrees with the
|
||||
// queue: a validator pointing at an address that no longer exists, is
|
||||
// finished, or belongs to another validator is released; an "assigned"
|
||||
// validator that holds nothing goes back to idle. The invariants normally
|
||||
// hold by construction (every change is one transaction); this heals what a
|
||||
// crash or an older version left behind. It returns the number of validators
|
||||
// repaired.
|
||||
func (d *DB) ReconcileValidators(ctx context.Context) (int64, error) {
|
||||
now := timeToDB(Now())
|
||||
res, err := d.ExecContext(ctx, fmt.Sprintf(`
|
||||
UPDATE validators SET current_ip_id=NULL,
|
||||
state = CASE WHEN state='%s' THEN state ELSE '%s' END,
|
||||
updated_at=?
|
||||
WHERE current_ip_id IS NOT NULL AND NOT EXISTS (
|
||||
SELECT 1 FROM ip_queue q
|
||||
WHERE q.id = validators.current_ip_id
|
||||
AND q.owner_validator_id = validators.validator_id
|
||||
AND q.state NOT IN ('%s','%s','%s'))`,
|
||||
ValidatorUnreachable, ValidatorIdle, IPDone, IPFailed, IPOccupied), now)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("reconcile validators: %w", err)
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
res, err = d.ExecContext(ctx, `
|
||||
UPDATE validators SET state=?, updated_at=?
|
||||
WHERE state=? AND current_ip_id IS NULL`, ValidatorIdle, now, ValidatorAssigned)
|
||||
if err != nil {
|
||||
return n, fmt.Errorf("reconcile validators: %w", err)
|
||||
}
|
||||
m, _ := res.RowsAffected()
|
||||
return n + m, nil
|
||||
}
|
||||
|
||||
// AdminCreateValidator registers a brand-new validator via the admin API.
|
||||
// Unlike RegisterValidator (used by the agent's self-registration call),
|
||||
// this refuses to upsert over an existing row.
|
||||
|
||||
Reference in new issue
Block a user