Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default/Validate resources. #739

Merged
merged 4 commits into from
Feb 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion api/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ func assertWriteMeta(t *testing.T, wm *WriteMeta) {

func testJob() *Job {
task := NewTask("task1", "exec").
Require(&Resources{MemoryMB: 256})
Require(&Resources{
CPU: 100,
MemoryMB: 256,
DiskMB: 25,
IOPS: 10,
})

group := NewTaskGroup("group1", 1).
AddTask(task)
Expand Down
6 changes: 5 additions & 1 deletion command/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ func testServer(

func testJob(jobID string) *api.Job {
task := api.NewTask("task1", "exec").
Require(&api.Resources{MemoryMB: 256})
Require(&api.Resources{
MemoryMB: 256,
DiskMB: 20,
CPU: 100,
})

group := api.NewTaskGroup("group1", 1).
AddTask(task)
Expand Down
4 changes: 4 additions & 0 deletions jobspec/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,10 @@ func parseResources(result *structs.Resources, list *ast.ObjectList) error {
result.Networks = []*structs.NetworkResource{&r}
}

// Combine the parsed resources with a default resource block.
min := structs.DefaultResources()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this logic should be in the jobs endpoint? Users who are submitting the jobs via the http API can optionally get the default resources if they don't specify

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked about defaulting with Armon a bit and the consensus is the api endpoint should do as little defaulting as possible

min.Merge(result)
*result = *min
return nil
}

Expand Down
4 changes: 4 additions & 0 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func TestParse(t *testing.T) {
Resources: &structs.Resources{
CPU: 500,
MemoryMB: 128,
DiskMB: 10,
IOPS: 1,
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
MBits: 100,
Expand All @@ -132,6 +134,8 @@ func TestParse(t *testing.T) {
Resources: &structs.Resources{
CPU: 500,
MemoryMB: 128,
DiskMB: 10,
IOPS: 30,
},
Constraints: []*structs.Constraint{
&structs.Constraint{
Expand Down
1 change: 1 addition & 0 deletions jobspec/test-fixtures/basic.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ job "binstore-storagelocker" {
resources {
cpu = 500
memory = 128
IOPS = 30
}
constraint {
attribute = "kernel.arch"
Expand Down
1 change: 1 addition & 0 deletions nomad/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func Job() *structs.Job {
Resources: &structs.Resources{
CPU: 500,
MemoryMB: 256,
DiskMB: 100,
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
MBits: 50,
Expand Down
75 changes: 72 additions & 3 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,60 @@ type Resources struct {
Networks []*NetworkResource
}

// DefaultResources returns the minimum resources a task can use and be valid.
func DefaultResources() *Resources {
return &Resources{
CPU: 100,
MemoryMB: 10,
DiskMB: 10,
IOPS: 1,
}
}

// Merge merges this resource with another resource.
func (r *Resources) Merge(other *Resources) {
if other.CPU != 0 {
r.CPU = other.CPU
}
if other.MemoryMB != 0 {
r.MemoryMB = other.MemoryMB
}
if other.DiskMB != 0 {
r.DiskMB = other.DiskMB
}
if other.IOPS != 0 {
r.IOPS = other.IOPS
}
if len(other.Networks) != 0 {
r.Networks = other.Networks
}
}

// MeetsMinResources returns an error if the resources specified are less than
// the minimum allowed.
func (r *Resources) MeetsMinResources() error {
var mErr multierror.Error
if r.CPU < 100 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum CPU value is 100; got %d", r.CPU))
}
if r.MemoryMB < 10 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum MemoryMB value is 10; got %d", r.MemoryMB))
}
if r.DiskMB < 10 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum DiskMB value is 10; got %d", r.DiskMB))
}
if r.IOPS < 0 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum IOPS value is 0; got %d", r.IOPS))
}
for i, n := range r.Networks {
if err := n.MeetsMinResources(); err != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("network resource at index %d failed: %v", i, err))
}
}

return mErr.ErrorOrNil()
}

// Copy returns a deep copy of the resources
func (r *Resources) Copy() *Resources {
newR := new(Resources)
Expand Down Expand Up @@ -676,6 +730,16 @@ type NetworkResource struct {
DynamicPorts []Port // Dynamically assigned ports
}

// MeetsMinResources returns an error if the resources specified are less than
// the minimum allowed.
func (n *NetworkResource) MeetsMinResources() error {
var mErr multierror.Error
if n.MBits < 1 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum MBits value is 1; got %d", n.MBits))
}
return mErr.ErrorOrNil()
}

// Copy returns a deep copy of the network resource
func (n *NetworkResource) Copy() *NetworkResource {
newR := new(NetworkResource)
Expand Down Expand Up @@ -1545,12 +1609,17 @@ func (t *Task) Validate() error {
if t.Driver == "" {
mErr.Errors = append(mErr.Errors, errors.New("Missing task driver"))
}
if t.Resources == nil {
mErr.Errors = append(mErr.Errors, errors.New("Missing task resources"))
}
if t.KillTimeout.Nanoseconds() < 0 {
mErr.Errors = append(mErr.Errors, errors.New("KillTimeout must be a positive value"))
}

// Validate the resources.
if t.Resources == nil {
mErr.Errors = append(mErr.Errors, errors.New("Missing task resources"))
} else if err := t.Resources.MeetsMinResources(); err != nil {
mErr.Errors = append(mErr.Errors, err)
}

for idx, constr := range t.Constraints {
if err := constr.Validate(); err != nil {
outer := fmt.Errorf("Constraint %d validation failed: %s", idx+1, err)
Expand Down
11 changes: 8 additions & 3 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,14 @@ func TestTask_Validate(t *testing.T) {
}

task = &Task{
Name: "web",
Driver: "docker",
Resources: &Resources{},
Name: "web",
Driver: "docker",
Resources: &Resources{
CPU: 100,
DiskMB: 100,
MemoryMB: 100,
IOPS: 10,
},
}
err = task.Validate()
if err != nil {
Expand Down