Scan floating IPs in the background, page by page, so thousands of addresses work
The "Scan Floating IP" button failed with a client timeout: the project now holds ~6.4k floating IPs and the scan listed them all in one unpaginated, timeout-less Neutron request on the HTTP request context. openstack: ListFreeFloatingIPs reads marker-based pages (fields= keeps them small) with per-page retry/backoff on transport errors, 5xx and 429, and every request now has a timeout (also ends hangs inside the orchestrator tick). orchestrator: the scan is a single-flight background job on the process context with progress (clearing/listing/enqueuing/done/error), dry_run, full discovery before anything is enqueued, then SubmitIPs in chunks of 500 in ascending IP order; a failed read leaves the queue untouched. The auto-cycle gets a "scanning" phase that polls the job, so the control loop and autoCycleMu are never held across OpenStack/DB work; it recovers after a restart and waits for (instead of adopting) a scan started by someone else. db: migration 0009 (indexes), paged ListIPsPage/ListRegistryPage, GROUP BY counters, EXISTS completion check, set-based ClearAllIPs. API: POST /admin/ips/scan -> 202 (dry_run, wait), GET /admin/ips/scan, paging and filters on /admin/ips and /admin/registry (bare arrays without limit), results_by_overall in /admin/status. dashboard: scan progress panel and dry-run button, paginated /ips and /registry with server-side filters, Overview on counters and capped lists with progress/ETA, "select all N by filter", hx-params fix for per-row buttons, real counts in confirmations. Also: docs (API, USAGE, DASHBOARD, README), plan and review under docs/changes/, bin/ rebuilt with new SHA256SUMS. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
debf2afed2
commit
aff8fe38b5
61 files changed
+5833
-536
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -497,6 +498,263 @@ func deleteIPTx(ctx context.Context, tx *sql.Tx, ipID int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ClearAllIPs deletes every ip_queue row in one short, set-based
|
||||
// transaction (five statements regardless of the queue size) and returns the
|
||||
// deleted addresses in queue order. It does exactly what deleteIPTx does per
|
||||
// row: frees every validator that still points at a doomed row, detaches
|
||||
// checks/events (their registry_id keeps the history), drops the ephemeral
|
||||
// ip_site_checks progress flags and finally the rows themselves.
|
||||
// Disassociating attached floating IPs is the caller's (orchestrator's) job.
|
||||
func (d *DB) ClearAllIPs(ctx context.Context) ([]string, error) {
|
||||
tx, err := d.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
rows, err := tx.QueryContext(ctx, `SELECT ip_address FROM ip_queue ORDER BY sequence`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deleted := []string{}
|
||||
for rows.Next() {
|
||||
var addr string
|
||||
if err := rows.Scan(&addr); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
deleted = append(deleted, addr)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
rows.Close()
|
||||
|
||||
now := timeToDB(Now())
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
UPDATE validators SET state=?, current_ip_id=NULL, updated_at=?
|
||||
WHERE current_ip_id IS NOT NULL
|
||||
`, ValidatorIdle, now); err != nil {
|
||||
return nil, fmt.Errorf("free owning validators: %w", err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `UPDATE checks SET ip_id=NULL WHERE ip_id IS NOT NULL`); err != nil {
|
||||
return nil, fmt.Errorf("detach checks: %w", err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `UPDATE events SET ip_id=NULL WHERE ip_id IS NOT NULL`); err != nil {
|
||||
return nil, fmt.Errorf("detach events: %w", err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM ip_site_checks`); err != nil {
|
||||
return nil, fmt.Errorf("delete ip_site_checks: %w", err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM ip_queue`); err != nil {
|
||||
return nil, fmt.Errorf("delete ip_queue rows: %w", err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
// FIPRef identifies a queue row that currently holds a Neutron floating-IP
|
||||
// association.
|
||||
type FIPRef struct {
|
||||
IPID int64
|
||||
IPAddress string
|
||||
FIPID string
|
||||
}
|
||||
|
||||
// ListFIPRefs returns every queue row with an attached floating IP (fip_id
|
||||
// set) — typically at most one per validator — so a bulk clear can
|
||||
// disassociate them without loading the whole queue.
|
||||
func (d *DB) ListFIPRefs(ctx context.Context) ([]FIPRef, error) {
|
||||
rows, err := d.QueryContext(ctx, `SELECT id, ip_address, fip_id FROM ip_queue WHERE fip_id<>'' ORDER BY id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []FIPRef
|
||||
for rows.Next() {
|
||||
var r FIPRef
|
||||
if err := rows.Scan(&r.IPID, &r.IPAddress, &r.FIPID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// ListFIPRefsByAddresses is ListFIPRefs restricted to the given addresses
|
||||
// (unknown addresses and rows without an attached floating IP are simply
|
||||
// absent), using a handful of IN (...) queries instead of one lookup per
|
||||
// address.
|
||||
func (d *DB) ListFIPRefsByAddresses(ctx context.Context, addresses []string) ([]FIPRef, error) {
|
||||
const chunk = 500
|
||||
var out []FIPRef
|
||||
for start := 0; start < len(addresses); start += chunk {
|
||||
end := start + chunk
|
||||
if end > len(addresses) {
|
||||
end = len(addresses)
|
||||
}
|
||||
part := addresses[start:end]
|
||||
args := make([]any, len(part))
|
||||
for i, a := range part {
|
||||
args[i] = a
|
||||
}
|
||||
rows, err := d.QueryContext(ctx,
|
||||
`SELECT id, ip_address, fip_id FROM ip_queue WHERE fip_id<>'' AND ip_address IN (`+placeholders(len(part))+`)`, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
var r FIPRef
|
||||
if err := rows.Scan(&r.IPID, &r.IPAddress, &r.FIPID); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
rows.Close()
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func placeholders(n int) string {
|
||||
if n <= 0 {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSuffix(strings.Repeat("?,", n), ",")
|
||||
}
|
||||
|
||||
// CountIPsByState returns how many ip_queue rows are in each state, plus the
|
||||
// grand total, via a single GROUP BY (no row loading).
|
||||
func (d *DB) CountIPsByState(ctx context.Context) (map[string]int, int, error) {
|
||||
rows, err := d.QueryContext(ctx, `SELECT state, COUNT(*) FROM ip_queue GROUP BY state`)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
counts := map[string]int{}
|
||||
total := 0
|
||||
for rows.Next() {
|
||||
var state string
|
||||
var n int
|
||||
if err := rows.Scan(&state, &n); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
counts[state] = n
|
||||
total += n
|
||||
}
|
||||
return counts, total, rows.Err()
|
||||
}
|
||||
|
||||
// CountIPsByResult returns how many ip_queue rows carry each non-empty
|
||||
// overall_result (pass/partial/fail/cancelled), via a single GROUP BY.
|
||||
func (d *DB) CountIPsByResult(ctx context.Context) (map[string]int, error) {
|
||||
rows, err := d.QueryContext(ctx, `
|
||||
SELECT overall_result, COUNT(*) FROM ip_queue WHERE overall_result<>'' GROUP BY overall_result
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
counts := map[string]int{}
|
||||
for rows.Next() {
|
||||
var res string
|
||||
var n int
|
||||
if err := rows.Scan(&res, &n); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
counts[res] = n
|
||||
}
|
||||
return counts, rows.Err()
|
||||
}
|
||||
|
||||
// AnyNonTerminalIP reports whether any ip_queue row is still unfinished (its
|
||||
// state is none of done/failed/occupied).
|
||||
func (d *DB) AnyNonTerminalIP(ctx context.Context) (bool, error) {
|
||||
var any bool
|
||||
err := d.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM ip_queue WHERE state NOT IN (?, ?, ?))`,
|
||||
IPDone, IPFailed, IPOccupied).Scan(&any)
|
||||
return any, err
|
||||
}
|
||||
|
||||
// Order values for IPFilter.Order.
|
||||
const (
|
||||
IPOrderSequence = "sequence"
|
||||
IPOrderAggregatedAtDesc = "aggregated_at_desc"
|
||||
)
|
||||
|
||||
// IPFilter narrows ListIPsPage. The zero value matches everything, ordered by
|
||||
// queue sequence.
|
||||
type IPFilter struct {
|
||||
States []string // any of these states (empty = all)
|
||||
Query string // substring of ip_address
|
||||
Result string // overall_result equals this (pass|partial|fail|cancelled)
|
||||
Order string // IPOrderSequence (default) | IPOrderAggregatedAtDesc
|
||||
}
|
||||
|
||||
// ListIPsPage returns one page (limit/offset) of ip_queue rows matching f,
|
||||
// plus the total number of matching rows. limit <= 0 means no limit.
|
||||
func (d *DB) ListIPsPage(ctx context.Context, f IPFilter, limit, offset int) ([]IPQueueItem, int, error) {
|
||||
var conds []string
|
||||
var args []any
|
||||
if len(f.States) > 0 {
|
||||
conds = append(conds, "state IN ("+placeholders(len(f.States))+")")
|
||||
for _, s := range f.States {
|
||||
args = append(args, s)
|
||||
}
|
||||
}
|
||||
if f.Query != "" {
|
||||
conds = append(conds, "instr(ip_address, ?) > 0")
|
||||
args = append(args, f.Query)
|
||||
}
|
||||
if f.Result != "" {
|
||||
conds = append(conds, "overall_result = ?")
|
||||
args = append(args, f.Result)
|
||||
}
|
||||
where := ""
|
||||
if len(conds) > 0 {
|
||||
where = "WHERE " + strings.Join(conds, " AND ") + " "
|
||||
}
|
||||
|
||||
var total int
|
||||
if err := d.QueryRowContext(ctx, `SELECT COUNT(*) FROM ip_queue `+where, args...).Scan(&total); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
order := "ORDER BY sequence"
|
||||
if f.Order == IPOrderAggregatedAtDesc {
|
||||
// Timestamps are stored as RFC3339Nano, which trims trailing zeros
|
||||
// and so does not sort correctly as text; strftime normalizes them to
|
||||
// a fixed millisecond layout. NULL aggregated_at sorts last in DESC.
|
||||
order = "ORDER BY strftime('%Y-%m-%d %H:%M:%f', aggregated_at) DESC, sequence"
|
||||
}
|
||||
q := ipQueueSelect + where + order
|
||||
qargs := append([]any(nil), args...)
|
||||
if limit > 0 {
|
||||
q += " LIMIT ? OFFSET ?"
|
||||
qargs = append(qargs, limit, offset)
|
||||
}
|
||||
rows, err := d.QueryContext(ctx, q, qargs...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items, err := scanIPQueueItems(rows)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if items == nil {
|
||||
items = []IPQueueItem{}
|
||||
}
|
||||
return items, total, nil
|
||||
}
|
||||
|
||||
func (d *DB) SetEgressComplete(ctx context.Context, ipID int64) error {
|
||||
_, err := d.ExecContext(ctx, `UPDATE ip_queue SET egress_complete=1, updated_at=? WHERE id=?`, timeToDB(Now()), ipID)
|
||||
return err
|
||||
|
||||
Reference in new issue
Block a user