Skip to content

Commit

Permalink
Execution load balancing (#692)
Browse files Browse the repository at this point in the history
refactor: Execution load balancing

Co-authored-by: Andrey Golev <[email protected]>
  • Loading branch information
andreygolev and Andrey Golev authored Mar 10, 2020
1 parent bd37c02 commit 5cfd9fc
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions dkron/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"expvar"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -735,6 +736,7 @@ func (a *Agent) join(addrs []string, replay bool) (n int, err error) {

func (a *Agent) processFilteredNodes(job *Job) ([]string, map[string]string, error) {
var nodes []string
var candidates []string
tags := make(map[string]string)

// Actually copy the map
Expand Down Expand Up @@ -763,13 +765,25 @@ func (a *Agent) processFilteredNodes(job *Job) ([]string, map[string]string, err
if member.Status == serf.StatusAlive {
for mtk, mtv := range member.Tags {
if mtk == jtk && mtv == tv {
if len(nodes) < count {
nodes = append(nodes, member.Name)
}
candidates = append(candidates, member.Name)
}
}
}
}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(candidates), func(i, j int) {
candidates[i], candidates[j] = candidates[j], candidates[i]
})

for i := 1; i <= count; i++ {
if len(candidates) == 0 {
break
}
nodes = append(nodes, candidates[0])
candidates = candidates[1:]
}
candidates = nil

}
}

Expand Down

0 comments on commit 5cfd9fc

Please sign in to comment.