161 lines
5.2 KiB
Go
161 lines
5.2 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// DiagnosticHold is an address held on a validator for manual diagnostics.
|
|
type DiagnosticHold struct {
|
|
Address string `json:"address"`
|
|
ValidatorID string `json:"validator_id"`
|
|
FIPID string `json:"fip_id"`
|
|
Note string `json:"note"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
// ClaimDiagnostic moves an idle validator to the diagnostic state and records
|
|
// the hold, in one transaction. ErrNotFound: no such validator; ErrBusy: the
|
|
// validator is not idle; ErrConflict: the address or validator is already held.
|
|
func (d *DB) ClaimDiagnostic(ctx context.Context, validatorID, address, fipID, note string) error {
|
|
return d.claimDiagnostic(ctx, validatorID, address, fipID, note, nil)
|
|
}
|
|
|
|
func (d *DB) claimDiagnostic(ctx context.Context, validatorID, address, fipID, note string, item *DiagnosticOperationItem) error {
|
|
tx, err := d.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
now := timeToDB(Now())
|
|
var active bool
|
|
err = tx.QueryRowContext(ctx, `
|
|
SELECT EXISTS(SELECT 1 FROM ip_queue WHERE ip_address=? AND state IN (?, ?, ?, ?))
|
|
`, address, IPAssigningFIP, IPAwaitingSelfCheck, IPChecking, IPAggregating).Scan(&active)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if active {
|
|
return fmt.Errorf("address %q is being checked: %w", address, ErrBusy)
|
|
}
|
|
var held bool
|
|
if err := tx.QueryRowContext(ctx, `
|
|
SELECT EXISTS(SELECT 1 FROM diagnostic_holds WHERE address=? OR validator_id=?)
|
|
`, address, validatorID).Scan(&held); err != nil {
|
|
return err
|
|
}
|
|
if held {
|
|
return fmt.Errorf("address %q or validator %q is already held: %w", address, validatorID, ErrConflict)
|
|
}
|
|
var expectedPort string
|
|
if item != nil {
|
|
expectedPort = item.PortID
|
|
}
|
|
res, err := tx.ExecContext(ctx, `
|
|
UPDATE validators SET state=?, updated_at=?
|
|
WHERE validator_id=? AND state=? AND current_ip_id IS NULL AND (?='' OR os_port_id=?)
|
|
`, ValidatorDiagnostic, now, validatorID, ValidatorIdle, expectedPort, expectedPort)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n, _ := res.RowsAffected(); n != 1 {
|
|
var one int
|
|
err := tx.QueryRowContext(ctx, `SELECT 1 FROM validators WHERE validator_id=?`, validatorID).Scan(&one)
|
|
if err == sql.ErrNoRows {
|
|
return fmt.Errorf("validator %q: %w", validatorID, ErrNotFound)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return fmt.Errorf("validator %q is no longer idle on the selected port: %w", validatorID, ErrBusy)
|
|
}
|
|
var pendingID *int64
|
|
if item != nil {
|
|
pendingID = &item.ID
|
|
if _, err := tx.ExecContext(ctx, `
|
|
UPDATE diagnostic_operation_items SET validator_id=?, port_id=?, fip_id=? WHERE id=? AND status='running'
|
|
`, validatorID, item.PortID, fipID, item.ID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `
|
|
INSERT INTO diagnostic_holds (address, validator_id, fip_id, note, created_at, pending_item_id)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
`, address, validatorID, fipID, note, now, pendingID); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// ReleaseDiagnostic deletes the hold of an address and returns the validator
|
|
// to idle. ErrNotFound if the address is not held.
|
|
func (d *DB) ReleaseDiagnostic(ctx context.Context, address string) (*DiagnosticHold, error) {
|
|
tx, err := d.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
h, err := scanHold(tx.QueryRowContext(ctx, holdSelect+` WHERE address=? AND pending_item_id IS NULL`, address))
|
|
if err == sql.ErrNoRows {
|
|
return nil, fmt.Errorf("address %q is not on diagnostic: %w", address, ErrNotFound)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `DELETE FROM diagnostic_holds WHERE address=? AND pending_item_id IS NULL`, address); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := resetDiagnosticValidator(ctx, tx, h.ValidatorID); err != nil {
|
|
return nil, err
|
|
}
|
|
return h, tx.Commit()
|
|
}
|
|
|
|
func resetDiagnosticValidator(ctx context.Context, tx *sql.Tx, validatorID string) error {
|
|
_, err := tx.ExecContext(ctx, `
|
|
UPDATE validators SET state=?, updated_at=? WHERE validator_id=? AND state=?
|
|
`, ValidatorIdle, timeToDB(Now()), validatorID, ValidatorDiagnostic)
|
|
return err
|
|
}
|
|
|
|
const holdSelect = `SELECT address, validator_id, fip_id, note, created_at FROM diagnostic_holds`
|
|
|
|
func (d *DB) HasDiagnosticHold(ctx context.Context, address string) (bool, error) {
|
|
var exists bool
|
|
err := d.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM diagnostic_holds WHERE address=?)`, address).Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
func scanHold(row rowScanner) (*DiagnosticHold, error) {
|
|
var h DiagnosticHold
|
|
var created string
|
|
if err := row.Scan(&h.Address, &h.ValidatorID, &h.FIPID, &h.Note, &created); err != nil {
|
|
return nil, err
|
|
}
|
|
var err error
|
|
if h.CreatedAt, err = dbToTime(created); err != nil {
|
|
return nil, err
|
|
}
|
|
return &h, nil
|
|
}
|
|
|
|
// ListDiagnosticHolds returns confirmed holds, oldest first.
|
|
func (d *DB) ListDiagnosticHolds(ctx context.Context) ([]DiagnosticHold, error) {
|
|
rows, err := d.QueryContext(ctx, holdSelect+` WHERE pending_item_id IS NULL ORDER BY id`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []DiagnosticHold{}
|
|
for rows.Next() {
|
|
h, err := scanHold(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, *h)
|
|
}
|
|
return out, rows.Err()
|
|
}
|