Merge pull request #10203 from hashicorp/f-cpu-cores

Reserved Cores [1/4]: Structs and scheduler implementation
This commit is contained in:
Nick Ethier
2021-03-29 14:05:54 -04:00
committed by GitHub
19 changed files with 791 additions and 72 deletions

View File

@@ -652,6 +652,7 @@ func TestJobs_Canonicalize(t *testing.T) {
},
Resources: &Resources{
CPU: intToPtr(500),
Cores: intToPtr(0),
MemoryMB: intToPtr(256),
Networks: []*NetworkResource{
{

View File

@@ -508,7 +508,9 @@ type NodeResources struct {
}
type NodeCpuResources struct {
CpuShares int64
CpuShares int64
TotalCpuCores uint16
ReservableCpuCores []uint16
}
type NodeMemoryResources struct {

View File

@@ -8,6 +8,7 @@ import (
// a given task or task group.
type Resources struct {
CPU *int `hcl:"cpu,optional"`
Cores *int `hcl:"cores,optional"`
MemoryMB *int `mapstructure:"memory" hcl:"memory,optional"`
DiskMB *int `mapstructure:"disk" hcl:"disk,optional"`
Networks []*NetworkResource `hcl:"network,block"`
@@ -24,9 +25,21 @@ type Resources struct {
// where they are not provided.
func (r *Resources) Canonicalize() {
defaultResources := DefaultResources()
if r.CPU == nil {
r.CPU = defaultResources.CPU
if r.Cores == nil {
r.Cores = defaultResources.Cores
// only set cpu to the default value if it and cores is not defined
if r.CPU == nil {
r.CPU = defaultResources.CPU
}
}
// CPU will be set to the default if cores is nil above.
// If cpu is nil here then cores has been set and cpu should be 0
if r.CPU == nil {
r.CPU = intToPtr(0)
}
if r.MemoryMB == nil {
r.MemoryMB = defaultResources.MemoryMB
}
@@ -42,6 +55,7 @@ func (r *Resources) Canonicalize() {
func DefaultResources() *Resources {
return &Resources{
CPU: intToPtr(100),
Cores: intToPtr(0),
MemoryMB: intToPtr(300),
}
}
@@ -54,6 +68,7 @@ func DefaultResources() *Resources {
func MinResources() *Resources {
return &Resources{
CPU: intToPtr(1),
Cores: intToPtr(0),
MemoryMB: intToPtr(10),
}
}

55
api/resources_test.go Normal file
View File

@@ -0,0 +1,55 @@
package api
import (
"reflect"
"testing"
"github.com/kr/pretty"
)
func TestResources_Canonicalize(t *testing.T) {
testCases := []struct {
name string
input *Resources
expected *Resources
}{
{
name: "empty",
input: &Resources{},
expected: DefaultResources(),
},
{
name: "cores",
input: &Resources{
Cores: intToPtr(2),
MemoryMB: intToPtr(1024),
},
expected: &Resources{
CPU: intToPtr(0),
Cores: intToPtr(2),
MemoryMB: intToPtr(1024),
},
},
{
name: "cpu",
input: &Resources{
CPU: intToPtr(500),
MemoryMB: intToPtr(1024),
},
expected: &Resources{
CPU: intToPtr(500),
Cores: intToPtr(0),
MemoryMB: intToPtr(1024),
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.input.Canonicalize()
if !reflect.DeepEqual(tc.input, tc.expected) {
t.Fatalf("Name: %v, Diffs:\n%v", tc.name, pretty.Diff(tc.expected, tc.input))
}
})
}
}