Skip to content

Commit

Permalink
Fix provisioning delay once again.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Nov 10, 2023
1 parent fc197c0 commit 62a11ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
6 changes: 0 additions & 6 deletions scheduler/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package scheduler

import (
"log/slog"
"sync"
"time"

"github.com/gammadia/alfred/proto"
Expand Down Expand Up @@ -46,15 +45,10 @@ type nodeState struct {

nodeName string
earliestStart time.Time

mutex sync.Mutex
}

func (ns *nodeState) UpdateStatus(status NodeStatus) {
if ns.status != status {
ns.mutex.Lock()
defer ns.mutex.Unlock()

ns.status = status
ns.scheduler.broadcast(EventNodeStatusUpdated{Node: ns.nodeName, Status: status})
}
Expand Down
19 changes: 10 additions & 9 deletions scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,10 @@ func (s *Scheduler) resizePool() {

for i := 0; i < nodesToCreate; i++ {
// Never queue the first node we create
queueNode := len(s.nodes) > 0 && time.Now().Before(s.earliestNextNodeStart)
s.earliestNextNodeStart = s.earliestNextNodeStart.Add(lo.Ternary(queueNode, s.config.ProvisioningDelay, 0))
queueNode := (len(s.nodes) > 0 || len(s.nodesQueue) > 0 || i > 0) && time.Now().Before(s.earliestNextNodeStart)

nodeName := namegen.Get()
nodeState := &nodeState{
ns := &nodeState{
scheduler: s,

node: nil,
Expand All @@ -342,21 +341,23 @@ func (s *Scheduler) resizePool() {
earliestStart: s.earliestNextNodeStart,
}

nodeState.log.Debug("Creating node", "earliestStart", nodeState.earliestStart, "status", nodeState.status)
s.broadcast(EventNodeCreated{Node: nodeName, Status: nodeState.status})
ns.log.Debug("Creating node", "earliestStart", ns.earliestStart, "status", ns.status)
s.broadcast(EventNodeCreated{Node: nodeName, Status: ns.status})

if queueNode {
s.nodesQueue = append(s.nodesQueue, nodeState)
s.nodesQueue = append(s.nodesQueue, ns)

wait := time.Until(s.earliestNextNodeStart)
nodeState.log.Debug("Wait before provisioning node", "wait", wait)
s.after(wait, func() {
s.requestTick("queued node should be ready to be provisioned")
})
} else {
s.nodes = append(s.nodes, nodeState)
go s.watchNodeProvisioning(nodeState)
s.nodes = append(s.nodes, ns)
go s.watchNodeProvisioning(ns)
}

// The next node should start with a delay
s.earliestNextNodeStart = s.earliestNextNodeStart.Add(s.config.ProvisioningDelay)
}
}

Expand Down

0 comments on commit 62a11ac

Please sign in to comment.