Run manual diagnostic attachments in the background
This commit is contained in:
1 parent
d845a5e4b6
commit
89ac4d5bcd
40 files changed
+1560
-58
No files matched your search
@@ -56,33 +56,13 @@ func (o *Orchestrator) DiagnosticAttach(ctx context.Context, items []DiagnosticI
|
||||
|
||||
func (o *Orchestrator) diagnosticAttachOne(ctx context.Context, it DiagnosticItem, note string) DiagnosticResult {
|
||||
addr := it.Address
|
||||
holds, err := o.DB.ListDiagnosticHolds(ctx)
|
||||
v, fip, reason, err := o.prepareDiagnosticAttach(ctx, it, "")
|
||||
if err != nil {
|
||||
return rejected(addr, "internal error: "+err.Error())
|
||||
}
|
||||
for _, h := range holds {
|
||||
if h.Address == addr {
|
||||
return rejected(addr, "address is already on diagnostic")
|
||||
}
|
||||
}
|
||||
if item, err := o.DB.GetIPByAddress(ctx, addr); err == nil && activeQueueStates[item.State] {
|
||||
return rejected(addr, "address is being checked ("+item.State+")")
|
||||
}
|
||||
fip, err := o.OS.GetFloatingIPByAddress(ctx, addr)
|
||||
if err != nil {
|
||||
if openstack.IsNotFound(err) {
|
||||
return rejected(addr, "floating ip not found in the project")
|
||||
}
|
||||
return rejected(addr, "cloud lookup failed: "+err.Error())
|
||||
}
|
||||
|
||||
v, reason := o.pickDiagnosticValidator(ctx, it.ValidatorID)
|
||||
if v == nil {
|
||||
if reason != "" {
|
||||
return rejected(addr, reason)
|
||||
}
|
||||
if fip.PortID != "" && fip.PortID != v.OSPortID {
|
||||
return rejected(addr, "floating ip is attached to another port")
|
||||
}
|
||||
if err := o.DB.ClaimDiagnostic(ctx, v.ValidatorID, addr, fip.ID, note); err != nil {
|
||||
return rejected(addr, "validator "+v.ValidatorID+" cannot be claimed: "+err.Error())
|
||||
}
|
||||
@@ -97,6 +77,36 @@ func (o *Orchestrator) diagnosticAttachOne(ctx context.Context, it DiagnosticIte
|
||||
return DiagnosticResult{Address: addr, Status: DiagAttached, ValidatorID: v.ValidatorID, FIPID: fip.ID}
|
||||
}
|
||||
|
||||
func (o *Orchestrator) prepareDiagnosticAttach(ctx context.Context, it DiagnosticItem, operationID string) (*db.Validator, *openstack.FloatingIP, string, error) {
|
||||
addr := it.Address
|
||||
held, err := o.DB.HasDiagnosticHold(ctx, addr)
|
||||
if err != nil {
|
||||
return nil, nil, "", err
|
||||
}
|
||||
if held {
|
||||
return nil, nil, "address is already on diagnostic", nil
|
||||
}
|
||||
if item, err := o.DB.GetIPByAddress(ctx, addr); err == nil && activeQueueStates[item.State] {
|
||||
return nil, nil, "address is being checked (" + item.State + ")", nil
|
||||
}
|
||||
fip, err := o.lookupDiagnosticFIP(ctx, operationID, addr)
|
||||
if err != nil {
|
||||
if openstack.IsNotFound(err) {
|
||||
return nil, nil, "floating ip not found in the project", nil
|
||||
}
|
||||
return nil, nil, "cloud lookup failed: " + err.Error(), nil
|
||||
}
|
||||
|
||||
v, reason := o.pickDiagnosticValidator(ctx, it.ValidatorID)
|
||||
if v == nil {
|
||||
return nil, nil, reason, nil
|
||||
}
|
||||
if fip.PortID != "" && fip.PortID != v.OSPortID {
|
||||
return nil, nil, "floating ip is attached to another port", nil
|
||||
}
|
||||
return v, fip, "", nil
|
||||
}
|
||||
|
||||
// pickDiagnosticValidator returns the requested validator if it is idle, or
|
||||
// the first idle one with a port when none is requested.
|
||||
func (o *Orchestrator) pickDiagnosticValidator(ctx context.Context, id string) (*db.Validator, string) {
|
||||
@@ -180,8 +190,12 @@ func (o *Orchestrator) DiagnosticList(ctx context.Context) ([]db.DiagnosticHold,
|
||||
}
|
||||
|
||||
func (o *Orchestrator) diagnosticEvent(ctx context.Context, typ, addr, validatorID, fipID, note string) {
|
||||
o.event(ctx, "control-api", validatorID, nil, typ, diagnosticPayload(addr, validatorID, fipID, note))
|
||||
}
|
||||
|
||||
func diagnosticPayload(addr, validatorID, fipID, note string) string {
|
||||
payload, _ := json.Marshal(map[string]string{
|
||||
"address": addr, "validator_id": validatorID, "fip_id": fipID, "note": note,
|
||||
})
|
||||
o.event(ctx, "control-api", validatorID, nil, typ, string(payload))
|
||||
return string(payload)
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package orchestrator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"cloudipvalidator/internal/db"
|
||||
"cloudipvalidator/internal/openstack"
|
||||
)
|
||||
|
||||
func (o *Orchestrator) WakeDiagnosticOperations() {
|
||||
select {
|
||||
case o.diagnosticWake <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// RunDiagnosticOperations owns the single worker. Persistent running items
|
||||
// are picked up on startup along with work accepted while the worker is busy.
|
||||
func (o *Orchestrator) RunDiagnosticOperations(ctx context.Context) {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
for ctx.Err() == nil {
|
||||
item, err := o.DB.NextDiagnosticOperationItem(ctx)
|
||||
if err == nil {
|
||||
err = o.applyDiagnosticOperationItem(ctx, item)
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if !errors.Is(err, sql.ErrNoRows) && ctx.Err() == nil {
|
||||
o.Log.Error("process diagnostic operation item", "operation_id", item.OperationID, "ip", item.Address, "err", err)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
case <-o.diagnosticWake:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Orchestrator) lookupDiagnosticFIP(ctx context.Context, operationID, address string) (*openstack.FloatingIP, error) {
|
||||
start := time.Now()
|
||||
fip, err := o.OS.GetFloatingIPByAddress(ctx, address)
|
||||
o.logDiagnosticStage(operationID, address, "lookup", start, err)
|
||||
return fip, err
|
||||
}
|
||||
|
||||
func (o *Orchestrator) logDiagnosticStage(operationID, address, stage string, start time.Time, err error) {
|
||||
o.Log.Info("diagnostic stage", "operation_id", operationID, "ip", address, "stage", stage, "duration_ms", time.Since(start).Milliseconds(), "err", err)
|
||||
}
|
||||
|
||||
func (o *Orchestrator) applyDiagnosticOperationItem(ctx context.Context, item db.DiagnosticOperationItem) error {
|
||||
var fip *openstack.FloatingIP
|
||||
if item.FIPID == "" {
|
||||
validator, found, reason, err := o.prepareDiagnosticAttach(ctx, DiagnosticItem{
|
||||
Address: item.Address, ValidatorID: item.RequestedValidator,
|
||||
}, item.OperationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if reason != "" {
|
||||
return o.DB.FinishDiagnosticOperationItem(ctx, item, reason, nil)
|
||||
}
|
||||
reserved := item
|
||||
reserved.ValidatorID, reserved.PortID, reserved.FIPID = validator.ValidatorID, validator.OSPortID, found.ID
|
||||
start := time.Now()
|
||||
err = o.DB.ReserveDiagnosticOperationItem(ctx, reserved)
|
||||
o.logDiagnosticStage(item.OperationID, item.Address, "reserve", start, err)
|
||||
if errors.Is(err, db.ErrBusy) || errors.Is(err, db.ErrConflict) || errors.Is(err, db.ErrNotFound) {
|
||||
return o.DB.FinishDiagnosticOperationItem(ctx, item, err.Error(), nil)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
item, fip = reserved, found
|
||||
} else {
|
||||
// A previous process could have exited after Neutron applied the PUT.
|
||||
found, err := o.lookupDiagnosticFIP(ctx, item.OperationID, item.Address)
|
||||
if err != nil {
|
||||
if openstack.IsNotFound(err) {
|
||||
return o.DB.FinishDiagnosticOperationItem(ctx, item, "floating ip no longer exists", nil)
|
||||
}
|
||||
return o.retryDiagnosticVerification(ctx, item)
|
||||
}
|
||||
fip = found
|
||||
}
|
||||
|
||||
if fip.ID != item.FIPID || (fip.PortID != "" && fip.PortID != item.PortID) {
|
||||
return o.DB.FinishDiagnosticOperationItem(ctx, item, "floating ip changed or is attached to another port", nil)
|
||||
}
|
||||
if fip.PortID != item.PortID {
|
||||
start := time.Now()
|
||||
err := o.OS.AssociateFloatingIP(ctx, item.FIPID, item.PortID)
|
||||
o.logDiagnosticStage(item.OperationID, item.Address, "associate", start, err)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
found, lookupErr := o.lookupDiagnosticFIP(ctx, item.OperationID, item.Address)
|
||||
if lookupErr != nil && !openstack.IsNotFound(lookupErr) {
|
||||
return o.retryDiagnosticVerification(ctx, item)
|
||||
}
|
||||
if lookupErr == nil && found.ID == item.FIPID && found.PortID == "" && openstack.IsRetryableRequestError(err) {
|
||||
return o.retryDiagnosticVerification(ctx, item)
|
||||
}
|
||||
if lookupErr != nil || found.ID != item.FIPID || found.PortID != item.PortID {
|
||||
return o.DB.FinishDiagnosticOperationItem(ctx, item, "associate floating ip: "+err.Error(), nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
event := db.Event{
|
||||
SourceType: "control-api", SourceID: item.ValidatorID, EventType: "diagnostic_attached",
|
||||
Payload: diagnosticPayload(item.Address, item.ValidatorID, item.FIPID, item.Note), OccurredAt: db.Now(),
|
||||
}
|
||||
if err := o.DB.FinishDiagnosticOperationItem(ctx, item, "", &event); err != nil {
|
||||
return err
|
||||
}
|
||||
o.Log.Info("diagnostic hold attached", "operation_id", item.OperationID, "ip", item.Address, "validator", item.ValidatorID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *Orchestrator) retryDiagnosticVerification(ctx context.Context, item db.DiagnosticOperationItem) error {
|
||||
return o.DB.RetryDiagnosticOperationItem(ctx, item.ID, "Проверяем состояние привязки в OpenStack", 2*time.Second)
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package orchestrator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"cloudipvalidator/internal/db"
|
||||
"cloudipvalidator/internal/openstack"
|
||||
)
|
||||
|
||||
func TestDiagnosticOperationPartialSuccess(t *testing.T) {
|
||||
o, d, ctx, _ := diagSetup(t)
|
||||
id := "diagnostic-partial"
|
||||
items := []db.DiagnosticOperationItem{
|
||||
{Address: "1.1.1.1", RequestedValidator: "validator-2"},
|
||||
{Address: "9.9.9.9"},
|
||||
}
|
||||
for range 2 {
|
||||
if err := d.CreateDiagnosticOperation(ctx, id, "trace", "same request", items); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := d.CreateDiagnosticOperation(ctx, id, "", "changed request", items); !errors.Is(err, db.ErrConflict) {
|
||||
t.Fatalf("different request with the same ID: %v", err)
|
||||
}
|
||||
for _, expected := range items {
|
||||
item, err := d.NextDiagnosticOperationItem(ctx)
|
||||
if err != nil || item.Address != expected.Address || item.Note != "trace" {
|
||||
t.Fatalf("claim was %+v, err=%v", item, err)
|
||||
}
|
||||
if err := o.applyDiagnosticOperationItem(ctx, item); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
op, err := d.GetDiagnosticOperation(ctx, id)
|
||||
if err != nil || len(op.Items) != 2 || op.Items[0].Status != DiagAttached ||
|
||||
op.Items[0].ValidatorID != "validator-2" || op.Items[1].Status != DiagRejected || op.Items[1].Reason == "" {
|
||||
t.Fatalf("operation was %+v, err=%v", op, err)
|
||||
}
|
||||
}
|
||||
|
||||
type diagnosticLostResponse struct {
|
||||
*openstack.MockClient
|
||||
lookups int
|
||||
associations int
|
||||
}
|
||||
|
||||
func (c *diagnosticLostResponse) GetFloatingIPByAddress(ctx context.Context, addr string) (*openstack.FloatingIP, error) {
|
||||
c.lookups++
|
||||
if c.lookups == 2 {
|
||||
return nil, errors.New("Neutron temporarily unreachable")
|
||||
}
|
||||
return c.MockClient.GetFloatingIPByAddress(ctx, addr)
|
||||
}
|
||||
|
||||
func (c *diagnosticLostResponse) AssociateFloatingIP(ctx context.Context, fipID, portID string) error {
|
||||
c.associations++
|
||||
if err := c.MockClient.AssociateFloatingIP(ctx, fipID, portID); err != nil {
|
||||
return err
|
||||
}
|
||||
return errors.New("lost response after successful PUT")
|
||||
}
|
||||
|
||||
func TestDiagnosticOperationRecoversAppliedReservationAfterRestart(t *testing.T) {
|
||||
o, d, ctx, mock := diagSetup(t)
|
||||
cloud := &diagnosticLostResponse{MockClient: mock}
|
||||
o.OS = cloud
|
||||
id := "diagnostic-restart"
|
||||
if err := d.CreateDiagnosticOperation(ctx, id, "", "attach", []db.DiagnosticOperationItem{
|
||||
{Address: "1.1.1.1", RequestedValidator: "validator-1"},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
item, err := d.NextDiagnosticOperationItem(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := o.applyDiagnosticOperationItem(ctx, item); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
op, err := d.GetDiagnosticOperation(ctx, id)
|
||||
if err != nil || len(op.Items) != 1 || op.Items[0].Status != "running" || op.Items[0].Reason == "" {
|
||||
t.Fatalf("unconfirmed operation was %+v, err=%v", op, err)
|
||||
}
|
||||
if holds, err := o.DiagnosticList(ctx); err != nil || len(holds) != 0 {
|
||||
t.Fatalf("unconfirmed hold exposed: %+v, err=%v", holds, err)
|
||||
}
|
||||
if result := o.DiagnosticDetach(ctx, []string{"1.1.1.1"}, false); result[0].Status != DiagRejected {
|
||||
t.Fatalf("detach accepted before confirmation: %+v", result)
|
||||
}
|
||||
if _, err := d.ExecContext(ctx, `UPDATE diagnostic_operation_items SET retry_at=0 WHERE id=?`, item.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// A fresh orchestrator uses only persistent state and the current Neutron port.
|
||||
restarted := &Orchestrator{DB: d, OS: cloud, Log: o.Log}
|
||||
item, err = d.NextDiagnosticOperationItem(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := restarted.applyDiagnosticOperationItem(ctx, item); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
op, err = d.GetDiagnosticOperation(ctx, id)
|
||||
if err != nil || len(op.Items) != 1 || op.Items[0].Status != DiagAttached || cloud.associations != 1 {
|
||||
t.Fatalf("operation was %+v, PUT count=%d, err=%v", op, cloud.associations, err)
|
||||
}
|
||||
events, err := d.ListRecentEvents(ctx, 10)
|
||||
if err != nil || len(events) != 1 || events[0].EventType != "diagnostic_attached" {
|
||||
t.Fatalf("events: %+v, err=%v", events, err)
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,8 @@ type Orchestrator struct {
|
||||
// value is ready to use.
|
||||
scan scanJob
|
||||
|
||||
diagnosticWake chan struct{}
|
||||
|
||||
// Async makes Tick return without waiting for the slow per-address work
|
||||
// (OpenStack calls): each address is handled in its own goroutine, so one
|
||||
// slow cloud call never delays another validator. With Async=false (the
|
||||
@@ -104,8 +106,9 @@ func New(d *db.DB, osClient openstack.FloatingIPClient, cfg *config.ControlAPI,
|
||||
Agg: cfg.Aggregation,
|
||||
Log: log,
|
||||
|
||||
ScanPageSize: cfg.OpenStack.ListPageSize,
|
||||
hb: heartbeatState{startedAt: time.Now()},
|
||||
ScanPageSize: cfg.OpenStack.ListPageSize,
|
||||
diagnosticWake: make(chan struct{}, 1),
|
||||
hb: heartbeatState{startedAt: time.Now()},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user