Retry validator-agent/prober registration; show validator hostname

Both binaries registered with control-api exactly once at startup and
exited (os.Exit(1)) on any failure — including control-api simply not
being up yet (no ordering guarantee between the two at boot/redeploy) or
the admin not having added this validator_id/site_id to the config yet.
Run() now retries registration with capped exponential backoff (3s->30s)
until it succeeds or the process is asked to shut down, instead of
crashing; registerWithRetry is identical in agentcore and probercore
since their Run/register shape already was.

Separately, the admin dashboard's Validators page had no hostname column
even though the agent already reports one on register (mirroring the
prober) and control-api already persists it — only the admin-config read
DTO (validatorDTO in httpapi and dashboard) dropped it before it reached
the template. Added hostname + last_heartbeat_at to that DTO end-to-end
and a Хост/Heartbeat column to validators.html, matching sites.html.

Rebuilt bin/{control-api,admin-dashboard,prober,validator-agent} and
bin/SHA256SUMS per docs/SETUP.md's documented build recipe.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5 committed 2026-09-18 10:43:00 +03:00
1 parent 399c64e801
commit 95f8066eed
15 files changed
+320 -23

No files matched your search

+38 -6
View File
@@ -8,7 +8,6 @@ package probercore
import (
"context"
"fmt"
"log/slog"
"os"
"time"
@@ -22,6 +21,13 @@ type Prober struct {
cfg *config.Prober
client *apiclient.Client
log *slog.Logger
// registerRetryInitial/Max govern the backoff used while waiting for a
// successful registration (see registerWithRetry): control-api may not
// be up yet at prober boot, or may reject an unconfigured site_id until
// an admin adds it — the prober should keep waiting rather than exit.
registerRetryInitial time.Duration
registerRetryMax time.Duration
}
func New(cfg *config.Prober, log *slog.Logger) *Prober {
@@ -30,15 +36,17 @@ func New(cfg *config.Prober, log *slog.Logger) *Prober {
timeout = 10 * time.Second
}
return &Prober{
cfg: cfg,
client: apiclient.New(cfg.ControlAPIURL, timeout+5*time.Second),
log: log,
cfg: cfg,
client: apiclient.New(cfg.ControlAPIURL, timeout+5*time.Second),
log: log,
registerRetryInitial: 3 * time.Second,
registerRetryMax: 30 * time.Second,
}
}
func (p *Prober) Run(ctx context.Context) error {
if err := p.register(ctx); err != nil {
return fmt.Errorf("register: %w", err)
if err := p.registerWithRetry(ctx); err != nil {
return err
}
interval := time.Duration(p.cfg.PollIntervalSeconds) * time.Second
@@ -70,6 +78,30 @@ func (p *Prober) register(ctx context.Context) error {
return nil
}
// registerWithRetry retries register with capped exponential backoff until
// it succeeds or ctx is cancelled — see agentcore.Agent.registerWithRetry
// for the identical rationale (control-api may start later, or an admin may
// add this site_id to its config later).
func (p *Prober) registerWithRetry(ctx context.Context) error {
delay := p.registerRetryInitial
for {
err := p.register(ctx)
if err == nil {
return nil
}
p.log.Warn("registration failed, will retry", "err", err, "retry_in", delay)
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(delay):
}
delay *= 2
if delay > p.registerRetryMax {
delay = p.registerRetryMax
}
}
}
type assignment struct {
IPID int64 `json:"ip_id"`
IPAddress string `json:"ip_address"`
+81
View File
@@ -7,6 +7,7 @@ import (
"net/http/httptest"
"os"
"sync"
"sync/atomic"
"testing"
"time"
@@ -95,6 +96,86 @@ func TestPollOnceSkipsAssignmentsWhenHeartbeatFails(t *testing.T) {
}
}
// TestRegisterWithRetrySucceedsAfterSiteConfigured mirrors an admin adding
// this prober's site_id to control-api's config after the prober has
// already started: registration is rejected with 400 until then, and the
// prober must keep retrying rather than give up.
func TestRegisterWithRetrySucceedsAfterSiteConfigured(t *testing.T) {
var attempts int32
mux := http.NewServeMux()
mux.HandleFunc("POST /api/v1/probers/register", func(w http.ResponseWriter, r *http.Request) {
n := atomic.AddInt32(&attempts, 1)
if n < 3 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":"unknown site_id: site-1"}`))
return
}
w.WriteHeader(http.StatusOK)
})
ts := httptest.NewServer(mux)
defer ts.Close()
p := &Prober{
cfg: &config.Prober{SiteID: "site-1", ControlAPIURL: ts.URL},
client: apiclient.New(ts.URL, 5*time.Second),
log: testLogger(),
registerRetryInitial: time.Millisecond,
registerRetryMax: 5 * time.Millisecond,
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := p.registerWithRetry(ctx); err != nil {
t.Fatalf("expected eventual success, got err: %v", err)
}
if got := atomic.LoadInt32(&attempts); got != 3 {
t.Fatalf("expected exactly 3 attempts, got %d", got)
}
}
// TestRegisterWithRetryStopsOnCancel confirms a prober waiting on an
// unreachable/unconfigured control-api can still be shut down promptly.
func TestRegisterWithRetryStopsOnCancel(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("POST /api/v1/probers/register", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
})
ts := httptest.NewServer(mux)
defer ts.Close()
p := &Prober{
cfg: &config.Prober{SiteID: "site-1", ControlAPIURL: ts.URL},
client: apiclient.New(ts.URL, 5*time.Second),
log: testLogger(),
registerRetryInitial: 10 * time.Millisecond,
registerRetryMax: 10 * time.Millisecond,
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(30 * time.Millisecond)
cancel()
}()
var err error
done := make(chan struct{})
go func() {
defer close(done)
err = p.registerWithRetry(ctx)
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("registerWithRetry did not return after ctx cancellation")
}
if err != context.Canceled {
t.Fatalf("expected context.Canceled, got %v", err)
}
}
// TestExtraChecksForPort confirms only ports 443 (TLS) and 22 (SSH) get an
// extra protocol check beyond the baseline TCPConnect every port gets.
func TestExtraChecksForPort(t *testing.T) {