2026-11-22 20:50:39 +03:00
|
|
|
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) 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 {
|
2026-11-22 21:26:19 +03:00
|
|
|
if openstack.IsRetryableRequestError(err) {
|
|
|
|
|
return o.retryDiagnosticVerification(ctx, item)
|
|
|
|
|
}
|
2026-11-22 20:50:39 +03:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if reason != "" {
|
2026-11-22 21:26:19 +03:00
|
|
|
return o.finishDiagnosticOperationItem(ctx, item, reason, nil)
|
2026-11-22 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2026-11-22 21:26:19 +03:00
|
|
|
return o.finishDiagnosticOperationItem(ctx, item, err.Error(), nil)
|
2026-11-22 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
item, fip = reserved, found
|
|
|
|
|
} else {
|
|
|
|
|
// A previous process could have exited after Neutron applied the PUT.
|
2026-11-22 21:26:19 +03:00
|
|
|
found, err := o.readFloatingIP(ctx, item.OperationID, item.Address, item.FIPID)
|
2026-11-22 20:50:39 +03:00
|
|
|
if err != nil {
|
|
|
|
|
if openstack.IsNotFound(err) {
|
2026-11-22 21:26:19 +03:00
|
|
|
return o.finishDiagnosticOperationItem(ctx, item, "floating ip no longer matches the reservation", nil)
|
2026-11-22 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
return o.retryDiagnosticVerification(ctx, item)
|
|
|
|
|
}
|
|
|
|
|
fip = found
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 21:26:19 +03:00
|
|
|
if fip.PortID != "" && fip.PortID != item.PortID {
|
|
|
|
|
return o.finishDiagnosticOperationItem(ctx, item, "floating ip is attached to another port", nil)
|
2026-11-22 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
}
|
2026-11-22 21:26:19 +03:00
|
|
|
found, lookupErr := o.readFloatingIP(ctx, item.OperationID, item.Address, item.FIPID)
|
2026-11-22 20:50:39 +03:00
|
|
|
if lookupErr != nil && !openstack.IsNotFound(lookupErr) {
|
|
|
|
|
return o.retryDiagnosticVerification(ctx, item)
|
|
|
|
|
}
|
2026-11-22 21:26:19 +03:00
|
|
|
if lookupErr == nil && found.PortID == "" && openstack.IsRetryableRequestError(err) {
|
2026-11-22 20:50:39 +03:00
|
|
|
return o.retryDiagnosticVerification(ctx, item)
|
|
|
|
|
}
|
2026-11-22 21:26:19 +03:00
|
|
|
if lookupErr != nil || found.PortID != item.PortID {
|
|
|
|
|
return o.finishDiagnosticOperationItem(ctx, item, "associate floating ip: "+err.Error(), nil)
|
2026-11-22 20:50:39 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(),
|
|
|
|
|
}
|
2026-11-22 21:26:19 +03:00
|
|
|
if err := o.finishDiagnosticOperationItem(ctx, item, "", &event); err != nil {
|
2026-11-22 20:50:39 +03:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
o.Log.Info("diagnostic hold attached", "operation_id", item.OperationID, "ip", item.Address, "validator", item.ValidatorID)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 21:26:19 +03:00
|
|
|
func (o *Orchestrator) finishDiagnosticOperationItem(ctx context.Context, item db.DiagnosticOperationItem, reason string, event *db.Event) error {
|
|
|
|
|
if err := o.DB.FinishDiagnosticOperationItem(ctx, item, reason, event); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
o.Log.Info("diagnostic attach result", "operation_id", item.OperationID, "ip", item.Address,
|
|
|
|
|
"duration_ms", time.Since(item.CreatedAt).Milliseconds(), "reason", reason)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 20:50:39 +03:00
|
|
|
func (o *Orchestrator) retryDiagnosticVerification(ctx context.Context, item db.DiagnosticOperationItem) error {
|
|
|
|
|
return o.DB.RetryDiagnosticOperationItem(ctx, item.ID, "Проверяем состояние привязки в OpenStack", 2*time.Second)
|
|
|
|
|
}
|