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:
1 parent
d0ab6b94f9
commit
55d383bd34
41 files changed
+1214
-94
No files matched your search
@@ -0,0 +1,191 @@
|
||||
package orchestrator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cloudipvalidator/internal/db"
|
||||
"cloudipvalidator/internal/openstack"
|
||||
)
|
||||
|
||||
func diagSetup(t *testing.T) (*Orchestrator, *db.DB, context.Context, *openstack.MockClient) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
o, d, mock := newTestOrchestrator(t, 180)
|
||||
mock.Seed("fip-a", "1.1.1.1", "svc")
|
||||
mock.Seed("fip-b", "2.2.2.2", "svc")
|
||||
_ = d.RegisterValidator(ctx, "validator-1", "host", "port-1", "v")
|
||||
_ = d.RegisterValidator(ctx, "validator-2", "host", "port-2", "v")
|
||||
return o, d, ctx, mock
|
||||
}
|
||||
|
||||
func validatorState(t *testing.T, d *db.DB, id string) string {
|
||||
t.Helper()
|
||||
v, err := d.GetValidator(context.Background(), id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return v.State
|
||||
}
|
||||
|
||||
func TestDiagnosticAttachDetach(t *testing.T) {
|
||||
o, d, ctx, _ := diagSetup(t)
|
||||
if err := d.SeedQueue(ctx, []string{"1.1.1.1"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res := o.DiagnosticAttach(ctx, []DiagnosticItem{{Address: "1.1.1.1", ValidatorID: "validator-1"}}, "trace")
|
||||
if len(res) != 1 || res[0].Status != DiagAttached || res[0].ValidatorID != "validator-1" {
|
||||
t.Fatalf("attach: %+v", res)
|
||||
}
|
||||
if s := validatorState(t, d, "validator-1"); s != db.ValidatorDiagnostic {
|
||||
t.Fatalf("state = %s, want diagnostic", s)
|
||||
}
|
||||
if fips, _ := o.OS.ListFloatingIPsByPort(ctx, "port-1"); len(fips) != 1 {
|
||||
t.Fatalf("fips on port-1 = %v", fips)
|
||||
}
|
||||
|
||||
// The held address is queued but never handed out; the other validator stays idle.
|
||||
o.Tick(ctx)
|
||||
if ip, _ := d.GetIPByAddress(ctx, "1.1.1.1"); ip.State != db.IPQueued {
|
||||
t.Fatalf("held address state = %s, want queued", ip.State)
|
||||
}
|
||||
// Reconcile and heartbeat leave the diagnostic state alone.
|
||||
if err := o.ValidatorHeartbeat(ctx, "validator-1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s := validatorState(t, d, "validator-1"); s != db.ValidatorDiagnostic {
|
||||
t.Fatalf("after heartbeat/reconcile state = %s", s)
|
||||
}
|
||||
if list, _ := o.DiagnosticList(ctx); len(list) != 1 || list[0].Note != "trace" {
|
||||
t.Fatalf("list: %+v", list)
|
||||
}
|
||||
|
||||
res = o.DiagnosticDetach(ctx, nil, true)
|
||||
if len(res) != 1 || res[0].Status != DiagDetached {
|
||||
t.Fatalf("detach: %+v", res)
|
||||
}
|
||||
if s := validatorState(t, d, "validator-1"); s != db.ValidatorIdle {
|
||||
t.Fatalf("state = %s, want idle", s)
|
||||
}
|
||||
if fips, _ := o.OS.ListFloatingIPsByPort(ctx, "port-1"); len(fips) != 0 {
|
||||
t.Fatalf("fip still attached: %v", fips)
|
||||
}
|
||||
o.Tick(ctx)
|
||||
if ip, _ := d.GetIPByAddress(ctx, "1.1.1.1"); ip.State == db.IPQueued {
|
||||
t.Fatal("released address was not claimed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiagnosticRejections(t *testing.T) {
|
||||
o, d, ctx, _ := diagSetup(t)
|
||||
// Busy validator.
|
||||
if _, err := d.ExecContext(ctx, `UPDATE validators SET state='assigned' WHERE validator_id='validator-1'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res := o.DiagnosticAttach(ctx, []DiagnosticItem{{Address: "1.1.1.1", ValidatorID: "validator-1"}}, "")
|
||||
if res[0].Status != DiagRejected {
|
||||
t.Fatalf("busy validator: %+v", res)
|
||||
}
|
||||
// Unknown address and a cloud error roll back the claim.
|
||||
res = o.DiagnosticAttach(ctx, []DiagnosticItem{{Address: "9.9.9.9"}}, "")
|
||||
if res[0].Status != DiagRejected {
|
||||
t.Fatalf("unknown fip: %+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiagnosticCloudErrorRollsBack(t *testing.T) {
|
||||
o, d, ctx, mock := diagSetup(t)
|
||||
mock.AssociateFailures = map[string]error{"fip-a": errors.New("neutron down")}
|
||||
res := o.DiagnosticAttach(ctx, []DiagnosticItem{{Address: "1.1.1.1", ValidatorID: "validator-1"}}, "")
|
||||
if res[0].Status != DiagRejected {
|
||||
t.Fatalf("attach: %+v", res)
|
||||
}
|
||||
if s := validatorState(t, d, "validator-1"); s != db.ValidatorIdle {
|
||||
t.Fatalf("state = %s, want idle after rollback", s)
|
||||
}
|
||||
if list, _ := o.DiagnosticList(ctx); len(list) != 0 {
|
||||
t.Fatalf("hold left behind: %+v", list)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSweepAndReleasePortsSkipDiagnosticValidator(t *testing.T) {
|
||||
o, d, ctx, _ := diagSetup(t)
|
||||
if err := d.SeedQueue(ctx, []string{"1.1.1.1"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res := o.DiagnosticAttach(ctx, []DiagnosticItem{{Address: "1.1.1.1", ValidatorID: "validator-1"}}, ""); res[0].Status != DiagAttached {
|
||||
t.Fatalf("attach: %+v", res)
|
||||
}
|
||||
old := db.Now().Add(-time.Hour).UTC().Format(time.RFC3339Nano)
|
||||
if _, err := d.ExecContext(ctx, `UPDATE validators SET last_heartbeat_at=?`, old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o.EndStartupGrace()
|
||||
if err := o.SweepStaleHeartbeats(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s := validatorState(t, d, "validator-1"); s != db.ValidatorDiagnostic {
|
||||
t.Fatalf("sweep changed diagnostic validator to %s", s)
|
||||
}
|
||||
if s := validatorState(t, d, "validator-2"); s != db.ValidatorUnreachable {
|
||||
t.Fatalf("validator-2 = %s, want unreachable", s)
|
||||
}
|
||||
|
||||
vs := o.validatorsByID(ctx, "validator-1")
|
||||
o.releaseValidatorPorts(ctx, vs)
|
||||
if fips, _ := o.OS.ListFloatingIPsByPort(ctx, "port-1"); len(fips) != 1 {
|
||||
t.Fatalf("releaseValidatorPorts detached a held fip: %v", fips)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeartbeatPersistThrottle(t *testing.T) {
|
||||
o, d, ctx, _ := diagSetup(t)
|
||||
stored := func() string {
|
||||
var s string
|
||||
d.QueryRowContext(ctx, `SELECT COALESCE(last_heartbeat_at,'') FROM validators WHERE validator_id='validator-1'`).Scan(&s)
|
||||
return s
|
||||
}
|
||||
if err := o.ValidatorHeartbeat(ctx, "validator-1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first := stored()
|
||||
if first == "" {
|
||||
t.Fatal("first heartbeat was not written")
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
if err := o.ValidatorHeartbeat(ctx, "validator-1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stored() != first {
|
||||
t.Fatal("second heartbeat within the interval was written to the database")
|
||||
}
|
||||
// Memory is newer, and the admin overlay shows it.
|
||||
v, _ := d.GetValidator(ctx, "validator-1")
|
||||
o.OverlayValidatorHeartbeat(v)
|
||||
if !v.LastHeartbeatAt.After(mustParse(t, first)) {
|
||||
t.Fatal("overlay did not show the in-memory heartbeat")
|
||||
}
|
||||
// A state change is written at once.
|
||||
_ = d.MarkValidatorUnreachable(ctx, "validator-1")
|
||||
if err := o.ValidatorHeartbeat(ctx, "validator-1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s := validatorState(t, d, "validator-1"); s != db.ValidatorIdle {
|
||||
t.Fatalf("state = %s, want idle", s)
|
||||
}
|
||||
if err := o.ValidatorHeartbeat(ctx, "nobody"); err == nil {
|
||||
t.Fatal("unknown validator must fail")
|
||||
}
|
||||
}
|
||||
|
||||
func mustParse(t *testing.T, s string) time.Time {
|
||||
t.Helper()
|
||||
tm, err := time.Parse(time.RFC3339Nano, s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return tm
|
||||
}
|
||||
Reference in new issue
Block a user