Run per-validator OpenStack work in parallel so all validators start at once

The orchestrator claimed idle validators one by one and associated each
floating IP synchronously (~30 s per address), so 20 validators started
about 30 s apart. Aggregation/disassociation and lease reclaim were
sequential in the same way.

The slow OpenStack calls now run in one goroutine per address, guarded by
an in-flight set against duplicates. control-api runs Tick in Async mode
(Tick does not wait); tests keep the waiting mode.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5.5 committed 2026-10-01 20:37:32 +03:00
1 parent aff8fe38b5
commit c0e300f71c
5 files changed
+115 -14

No files matched your search

+59 -13
View File
@@ -49,6 +49,36 @@ type Orchestrator struct {
// scan is the background floating-IP scan job (see scanjob.go); its zero
// value is ready to use.
scan scanJob
// 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
// default, used by tests) Tick runs the same work in parallel but waits
// for all of it before returning.
Async bool
// inflight holds the keys of work already running in a goroutine, so the
// next Tick does not start the same work twice.
inflight sync.Map
wg sync.WaitGroup
}
// Wait blocks until all background work started by Tick has finished.
func (o *Orchestrator) Wait() { o.wg.Wait() }
// spawn runs fn in its own goroutine unless work with the same key is
// already running. It returns immediately; callers that need the result
// wait with o.wg (see runAll).
func (o *Orchestrator) spawn(key string, fn func()) {
if _, busy := o.inflight.LoadOrStore(key, struct{}{}); busy {
return
}
o.wg.Add(1)
go func() {
defer o.wg.Done()
defer o.inflight.Delete(key)
fn()
}()
}
// New constructs an Orchestrator. Egress check types/targets, prober sites,
@@ -79,6 +109,10 @@ func (o *Orchestrator) leaseTTL() time.Duration {
// validators, sweep the checking window for ready-to-aggregate IPs, and
// reclaim expired leases. Intended to be called on a fixed interval
// (Cfg.PollIntervalSeconds) by the caller (cmd/control-api/main.go).
//
// The slow part of every step (calls to OpenStack) runs per address in its
// own goroutine, so validators never wait for each other. In Async mode
// Tick does not wait for those goroutines; otherwise it waits for them.
func (o *Orchestrator) Tick(ctx context.Context) {
if err := o.assignIdleValidators(ctx); err != nil {
o.Log.Error("assign idle validators", "err", err)
@@ -89,6 +123,9 @@ func (o *Orchestrator) Tick(ctx context.Context) {
if err := o.sweepExpiredLeases(ctx); err != nil {
o.Log.Error("sweep expired leases", "err", err)
}
if !o.Async {
o.wg.Wait()
}
}
// assignIdleValidators claims the next queued IP for every currently idle
@@ -108,9 +145,12 @@ func (o *Orchestrator) assignIdleValidators(ctx context.Context) error {
continue // no work available for this validator right now
}
o.Log.Info("claimed ip", "validator", v.ValidatorID, "ip", item.IPAddress, "ip_id", item.ID)
if err := o.associateFIP(ctx, v.ValidatorID, v.OSPortID, item); err != nil {
o.Log.Error("associate fip", "validator", v.ValidatorID, "ip", item.IPAddress, "err", err)
}
v, item := v, item
o.spawn("assign:"+v.ValidatorID, func() {
if err := o.associateFIP(ctx, v.ValidatorID, v.OSPortID, item); err != nil {
o.Log.Error("associate fip", "validator", v.ValidatorID, "ip", item.IPAddress, "err", err)
}
})
}
return nil
}
@@ -453,9 +493,12 @@ func (o *Orchestrator) sweepCheckingWindow(ctx context.Context) error {
if !ready {
continue
}
if err := o.aggregateAndRelease(ctx, item); err != nil {
o.Log.Error("aggregate and release", "ip_id", item.ID, "err", err)
}
item := item
o.spawn(fmt.Sprintf("aggregate:%d", item.ID), func() {
if err := o.aggregateAndRelease(ctx, item); err != nil {
o.Log.Error("aggregate and release", "ip_id", item.ID, "err", err)
}
})
}
return nil
}
@@ -606,14 +649,17 @@ func (o *Orchestrator) sweepExpiredLeases(ctx context.Context) error {
if item.OwnerValidatorID != nil {
validatorID = *item.OwnerValidatorID
}
o.Log.Info("lease expired, reclaiming", "ip_id", item.ID, "ip", item.IPAddress, "validator", validatorID)
if item.FIPID != "" {
if err := o.OS.DisassociateFloatingIP(ctx, item.FIPID); err != nil {
o.Log.Error("disassociate fip on lease reclaim", "ip_id", item.ID, "err", err)
item, validatorID := item, validatorID
o.spawn(fmt.Sprintf("lease:%d", item.ID), func() {
o.Log.Info("lease expired, reclaiming", "ip_id", item.ID, "ip", item.IPAddress, "validator", validatorID)
if item.FIPID != "" {
if err := o.OS.DisassociateFloatingIP(ctx, item.FIPID); err != nil {
o.Log.Error("disassociate fip on lease reclaim", "ip_id", item.ID, "err", err)
}
}
}
o.event(ctx, "control-api", "", &item.ID, "lease_expired", fmt.Sprintf(`{"validator_id":%q}`, validatorID))
o.requeueOrFail(ctx, item.ID, validatorID, "lease expired")
o.event(ctx, "control-api", "", &item.ID, "lease_expired", fmt.Sprintf(`{"validator_id":%q}`, validatorID))
o.requeueOrFail(ctx, item.ID, validatorID, "lease expired")
})
}
return nil
}