Skip to content

Commit

Permalink
Revert "Fix schedule()"
Browse files Browse the repository at this point in the history
This reverts commit 2e9bb96.
  • Loading branch information
site0801 committed Sep 8, 2023
1 parent 1bafd86 commit f7965ba
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions server/pkg/api/server_add_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"math/rand"
"net/url"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -207,9 +208,15 @@ func schedule(targets []targetHost, limitOverCommit uint64) (*targetHost, error)
return nil, ErrNoValidHost
}

minTargets := getMinTargets(schedulableTargets)
// 1. use lowest over-commit instance
// 2. check limit of over-commit
sort.SliceStable(schedulableTargets, func(i, j int) bool {
// lowest percentOverCommit is first
return schedulableTargets[i].percentOverCommit < schedulableTargets[j].percentOverCommit
})

return &minTargets[rand.Intn(len(minTargets))], nil
index := rand.Intn(len(schedulableTargets))
return &schedulableTargets[index], nil
}

// parseAlias parse user input
Expand Down Expand Up @@ -248,19 +255,3 @@ func parseAlias(input string) (*api.InstanceSource, error) {
Alias: input,
}, nil
}

func getMinTargets(hosts []targetHost) []targetHost {
var minTargets []targetHost
// use lowest over-commit instance
// if there is more than one the lowest ones, it picks up from them randomly
minTargetOverCommit := hosts[0].percentOverCommit
for _, t := range hosts {
if minTargetOverCommit > t.percentOverCommit {
minTargetOverCommit = t.percentOverCommit
minTargets = []targetHost{t}
} else if minTargetOverCommit == t.percentOverCommit {
minTargets = append(minTargets, t)
}
}
return minTargets
}

0 comments on commit f7965ba

Please sign in to comment.