Skip to content

Commit

Permalink
Revert "Use all schedulable hosts"
Browse files Browse the repository at this point in the history
This reverts commit a171bd2.
  • Loading branch information
site0801 committed Sep 8, 2023
1 parent 99aab21 commit 98a1766
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
20 changes: 19 additions & 1 deletion server/pkg/api/server_add_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ func schedule(targets []targetHost, limitOverCommit uint64) (*targetHost, error)
return nil, ErrNoValidHost
}

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

return &minTargets[rand.Intn(len(minTargets))], nil
}

// parseAlias parse user input
Expand Down Expand Up @@ -270,3 +272,19 @@ 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
}
60 changes: 60 additions & 0 deletions server/pkg/api/server_add_instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package api

import (
"reflect"
"testing"
)

func Test_getMinTargets(t *testing.T) {
var (
hostA = targetHost{percentOverCommit: 1}
hostA2 = targetHost{percentOverCommit: 1}
hostB = targetHost{percentOverCommit: 2}
hostC = targetHost{percentOverCommit: 3}
)

type args struct {
hosts []targetHost
}
tests := []struct {
name string
args args
want []targetHost
}{
{
name: "Single host",
args: args{
hosts: []targetHost{hostA},
},
want: []targetHost{hostA},
},
{
name: "Multiple host",
args: args{
hosts: []targetHost{hostA, hostB, hostC},
},
want: []targetHost{hostA},
},
{
name: "Multiple host (random order)",
args: args{
hosts: []targetHost{hostC, hostA, hostB},
},
want: []targetHost{hostA},
},
{
name: "Multiple host result",
args: args{
hosts: []targetHost{hostA, hostA2, hostB, hostC},
},
want: []targetHost{hostA, hostA2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getMinTargets(tt.args.hosts); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getMinTargets() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 98a1766

Please sign in to comment.