From 3ca71ae935714fee816f8f8ea8ee1a58c61bf902 Mon Sep 17 00:00:00 2001 From: Preetha Appan Date: Tue, 16 Oct 2018 16:21:42 -0500 Subject: [PATCH] Change CPU/Disk/MemoryMB to int everywhere in new resource structs --- api/allocations.go | 6 +- api/nodes.go | 6 +- client/driver/env/env.go | 12 ++-- client/fingerprint/cpu.go | 2 +- client/fingerprint/memory.go | 2 +- client/fingerprint/storage.go | 2 +- client/gc.go | 12 ++-- command/agent/agent.go | 6 +- nomad/structs/structs.go | 116 ++++++++++++++++++++++++++++------ scheduler/generic_sched.go | 2 +- scheduler/rank.go | 6 +- scheduler/reconcile_test.go | 6 +- scheduler/system_sched.go | 2 +- scheduler/util.go | 4 +- 14 files changed, 132 insertions(+), 52 deletions(-) diff --git a/api/allocations.go b/api/allocations.go index 371e4feeb..c82fdd2e5 100644 --- a/api/allocations.go +++ b/api/allocations.go @@ -171,15 +171,15 @@ type AllocatedTaskResources struct { } type AllocatedSharedResources struct { - DiskMB uint64 + DiskMB int } type AllocatedCpuResources struct { - CpuShares uint64 + CpuShares int } type AllocatedMemoryResources struct { - MemoryMB uint64 + MemoryMB int } // AllocIndexSort reverse sorts allocs by CreateIndex. diff --git a/api/nodes.go b/api/nodes.go index beb109c2e..a8ce431a2 100644 --- a/api/nodes.go +++ b/api/nodes.go @@ -471,15 +471,15 @@ type NodeResources struct { } type NodeCpuResources struct { - TotalShares uint64 + TotalShares int } type NodeMemoryResources struct { - MemoryMB uint64 + MemoryMB int } type NodeDiskResources struct { - DiskMB uint64 + DiskMB int } type NodeReservedResources struct { diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 00811344c..6f531b346 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -207,8 +207,8 @@ type Builder struct { // secretsDir from task's perspective; eg /secrets secretsDir string - cpuLimit uint64 - memLimit uint64 + cpuLimit int + memLimit int taskName string allocIndex int datacenter string @@ -272,10 +272,10 @@ func (b *Builder) Build() *TaskEnv { // Add the resource limits if b.memLimit != 0 { - envMap[MemLimit] = strconv.FormatUint(b.memLimit, 10) + envMap[MemLimit] = strconv.FormatInt(int64(b.memLimit), 10) } if b.cpuLimit != 0 { - envMap[CpuLimit] = strconv.FormatUint(b.cpuLimit, 10) + envMap[CpuLimit] = strconv.FormatInt(int64(b.cpuLimit), 10) } // Add the task metadata @@ -377,8 +377,8 @@ func (b *Builder) setTask(task *structs.Task) *Builder { b.cpuLimit = 0 b.networks = []*structs.NetworkResource{} } else { - b.memLimit = uint64(task.Resources.MemoryMB) - b.cpuLimit = uint64(task.Resources.CPU) + b.memLimit = task.Resources.MemoryMB + b.cpuLimit = task.Resources.CPU // Copy networks to prevent sharing b.networks = make([]*structs.NetworkResource, len(task.Resources.Networks)) for i, n := range task.Resources.Networks { diff --git a/client/fingerprint/cpu.go b/client/fingerprint/cpu.go index c7ebeac96..7c21f3018 100644 --- a/client/fingerprint/cpu.go +++ b/client/fingerprint/cpu.go @@ -31,7 +31,7 @@ func (f *CPUFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cst resp.NodeResources = &structs.NodeResources{ Cpu: structs.NodeCpuResources{ - CpuShares: uint64(totalCompute), + CpuShares: totalCompute, }, } } diff --git a/client/fingerprint/memory.go b/client/fingerprint/memory.go index d90725ce6..b18903008 100644 --- a/client/fingerprint/memory.go +++ b/client/fingerprint/memory.go @@ -51,7 +51,7 @@ func (f *MemoryFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp * resp.NodeResources = &structs.NodeResources{ Memory: structs.NodeMemoryResources{ - MemoryMB: uint64(totalMemory / bytesInMB), + MemoryMB: totalMemory / bytesInMB, }, } } diff --git a/client/fingerprint/storage.go b/client/fingerprint/storage.go index 479b55e94..a3192aac2 100644 --- a/client/fingerprint/storage.go +++ b/client/fingerprint/storage.go @@ -53,7 +53,7 @@ func (f *StorageFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp } resp.NodeResources = &structs.NodeResources{ Disk: structs.NodeDiskResources{ - DiskMB: free / bytesPerMegabyte, + DiskMB: int(free / bytesPerMegabyte), }, } resp.Detected = true diff --git a/client/gc.go b/client/gc.go index e802c03c1..f71dd0502 100644 --- a/client/gc.go +++ b/client/gc.go @@ -266,7 +266,7 @@ func (a *AllocGarbageCollector) MakeRoomFor(allocations []*structs.Allocation) e if alloc.AllocatedResources != nil { totalResource.Add(&alloc.AllocatedResources.Shared) } else { - totalResource.DiskMB += uint64(alloc.Resources.DiskMB) + totalResource.DiskMB += alloc.Resources.DiskMB } } @@ -279,12 +279,12 @@ func (a *AllocGarbageCollector) MakeRoomFor(allocations []*structs.Allocation) e } else { availableForAllocations = hostStats.AllocDirStats.Available - uint64(a.config.ReservedDiskMB*MB) } - if totalResource.DiskMB*MB < availableForAllocations { + if uint64(totalResource.DiskMB*MB) < availableForAllocations { return nil } } - var diskCleared uint64 + var diskCleared int for { select { case <-a.shutdownCh: @@ -302,7 +302,7 @@ func (a *AllocGarbageCollector) MakeRoomFor(allocations []*structs.Allocation) e } if allocDirStats != nil { - if allocDirStats.Available >= totalResource.DiskMB*MB { + if allocDirStats.Available >= uint64(totalResource.DiskMB*MB) { break } } else { @@ -322,11 +322,11 @@ func (a *AllocGarbageCollector) MakeRoomFor(allocations []*structs.Allocation) e alloc := ar.Alloc() // COMPAT(0.11): Remove in 0.11 - var allocDiskMB uint64 + var allocDiskMB int if alloc.AllocatedResources != nil { allocDiskMB = alloc.AllocatedResources.Shared.DiskMB } else { - allocDiskMB = uint64(alloc.Resources.DiskMB) + allocDiskMB = alloc.Resources.DiskMB } // Destroy the alloc runner and wait until it exits diff --git a/command/agent/agent.go b/command/agent/agent.go index 1493e5134..65cdba3ce 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -434,9 +434,9 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) { res = new(structs.NodeReservedResources) conf.Node.ReservedResources = res } - res.Cpu.CpuShares = uint64(a.config.Client.Reserved.CPU) - res.Memory.MemoryMB = uint64(a.config.Client.Reserved.MemoryMB) - res.Disk.DiskMB = uint64(a.config.Client.Reserved.DiskMB) + res.Cpu.CpuShares = a.config.Client.Reserved.CPU + res.Memory.MemoryMB = a.config.Client.Reserved.MemoryMB + res.Disk.DiskMB = a.config.Client.Reserved.DiskMB res.Networks.ReservedHostPorts = a.config.Client.Reserved.ReservedPorts conf.Version = a.config.Version diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index a0cbfbe78..02e6075c5 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -1600,14 +1600,14 @@ func (n *Node) ComparableReservedResources() *ComparableResources { return &ComparableResources{ Flattened: AllocatedTaskResources{ Cpu: AllocatedCpuResources{ - CpuShares: uint64(n.Reserved.CPU), + CpuShares: n.Reserved.CPU, }, Memory: AllocatedMemoryResources{ - MemoryMB: uint64(n.Reserved.MemoryMB), + MemoryMB: n.Reserved.MemoryMB, }, }, Shared: AllocatedSharedResources{ - DiskMB: uint64(n.Reserved.DiskMB), + DiskMB: n.Reserved.DiskMB, }, } } @@ -1626,14 +1626,14 @@ func (n *Node) ComparableResources() *ComparableResources { return &ComparableResources{ Flattened: AllocatedTaskResources{ Cpu: AllocatedCpuResources{ - CpuShares: uint64(n.Resources.CPU), + CpuShares: n.Resources.CPU, }, Memory: AllocatedMemoryResources{ - MemoryMB: uint64(n.Resources.MemoryMB), + MemoryMB: n.Resources.MemoryMB, }, }, Shared: AllocatedSharedResources{ - DiskMB: uint64(n.Resources.DiskMB), + DiskMB: n.Resources.DiskMB, }, } } @@ -2263,7 +2263,7 @@ func (n *NodeResources) Equals(o *NodeResources) bool { type NodeCpuResources struct { // CpuShares is the CPU shares available. This is calculated by number of // cores multiplied by the core frequency. - CpuShares uint64 + CpuShares int } func (n *NodeCpuResources) Merge(o *NodeCpuResources) { @@ -2295,7 +2295,7 @@ func (n *NodeCpuResources) Equals(o *NodeCpuResources) bool { // NodeMemoryResources captures the memory resources of the node type NodeMemoryResources struct { // MemoryMB is the total available memory on the node - MemoryMB uint64 + MemoryMB int } func (n *NodeMemoryResources) Merge(o *NodeMemoryResources) { @@ -2327,7 +2327,7 @@ func (n *NodeMemoryResources) Equals(o *NodeMemoryResources) bool { // NodeDiskResources captures the disk resources of the node type NodeDiskResources struct { // DiskMB is the total available disk space on the node - DiskMB uint64 + DiskMB int } func (n *NodeDiskResources) Merge(o *NodeDiskResources) { @@ -2601,17 +2601,17 @@ func (n *NodeReservedResources) Comparable() *ComparableResources { // NodeReservedCpuResources captures the reserved CPU resources of the node. type NodeReservedCpuResources struct { - CpuShares uint64 + CpuShares int } // NodeReservedMemoryResources captures the reserved memory resources of the node. type NodeReservedMemoryResources struct { - MemoryMB uint64 + MemoryMB int } // NodeReservedDiskResources captures the reserved disk resources of the node. type NodeReservedDiskResources struct { - DiskMB uint64 + DiskMB int } // NodeReservedNetworkResources captures the reserved network resources of the node. @@ -2731,9 +2731,47 @@ func (a *AllocatedTaskResources) Add(delta *AllocatedTaskResources) { } } +// Comparable turns AllocatedTaskResources into ComparableResources +// as a helper step in preemption +func (a *AllocatedTaskResources) Comparable() *ComparableResources { + ret := &ComparableResources{ + Flattened: AllocatedTaskResources{ + Cpu: AllocatedCpuResources{ + CpuShares: a.Cpu.CpuShares, + }, + Memory: AllocatedMemoryResources{ + MemoryMB: a.Memory.MemoryMB, + }, + }, + } + if len(a.Networks) > 0 { + for _, net := range a.Networks { + ret.Flattened.Networks = append(ret.Flattened.Networks, net) + } + } + return ret +} + +func (a *AllocatedTaskResources) Subtract(delta *AllocatedTaskResources) { + if delta == nil { + return + } + + a.Cpu.Subtract(&delta.Cpu) + a.Memory.Subtract(&delta.Memory) + + for _, n := range delta.Networks { + // Find the matching interface by IP or CIDR + idx := a.NetIndex(n) + if idx != -1 { + a.Networks[idx].MBits -= delta.Networks[idx].MBits + } + } +} + // AllocatedSharedResources are the set of resources allocated to a task group. type AllocatedSharedResources struct { - DiskMB uint64 + DiskMB int } func (a *AllocatedSharedResources) Add(delta *AllocatedSharedResources) { @@ -2744,9 +2782,17 @@ func (a *AllocatedSharedResources) Add(delta *AllocatedSharedResources) { a.DiskMB += delta.DiskMB } +func (a *AllocatedSharedResources) Subtract(delta *AllocatedSharedResources) { + if delta == nil { + return + } + + a.DiskMB -= delta.DiskMB +} + // AllocatedCpuResources captures the allocated CPU resources. type AllocatedCpuResources struct { - CpuShares uint64 + CpuShares int } func (a *AllocatedCpuResources) Add(delta *AllocatedCpuResources) { @@ -2757,9 +2803,17 @@ func (a *AllocatedCpuResources) Add(delta *AllocatedCpuResources) { a.CpuShares += delta.CpuShares } +func (a *AllocatedCpuResources) Subtract(delta *AllocatedCpuResources) { + if delta == nil { + return + } + + a.CpuShares -= delta.CpuShares +} + // AllocatedMemoryResources captures the allocated memory resources. type AllocatedMemoryResources struct { - MemoryMB uint64 + MemoryMB int } func (a *AllocatedMemoryResources) Add(delta *AllocatedMemoryResources) { @@ -2770,6 +2824,14 @@ func (a *AllocatedMemoryResources) Add(delta *AllocatedMemoryResources) { a.MemoryMB += delta.MemoryMB } +func (a *AllocatedMemoryResources) Subtract(delta *AllocatedMemoryResources) { + if delta == nil { + return + } + + a.MemoryMB -= delta.MemoryMB +} + // ComparableResources is the set of resources allocated to a task group but // not keyed by Task, making it easier to compare. type ComparableResources struct { @@ -2786,6 +2848,24 @@ func (c *ComparableResources) Add(delta *ComparableResources) { c.Shared.Add(&delta.Shared) } +func (c *ComparableResources) Subtract(delta *ComparableResources) { + if delta == nil { + return + } + + c.Flattened.Subtract(&delta.Flattened) + c.Shared.Subtract(&delta.Shared) +} + +func (c *ComparableResources) Copy() *ComparableResources { + if c == nil { + return nil + } + newR := new(ComparableResources) + *newR = *c + return newR +} + // Superset checks if one set of resources is a superset of another. This // ignores network resources, and the NetworkIndex should be used for that. func (c *ComparableResources) Superset(other *ComparableResources) (bool, string) { @@ -7281,14 +7361,14 @@ func (a *Allocation) ComparableResources() *ComparableResources { return &ComparableResources{ Flattened: AllocatedTaskResources{ Cpu: AllocatedCpuResources{ - CpuShares: uint64(resources.CPU), + CpuShares: resources.CPU, }, Memory: AllocatedMemoryResources{ - MemoryMB: uint64(resources.MemoryMB), + MemoryMB: resources.MemoryMB, }, }, Shared: AllocatedSharedResources{ - DiskMB: uint64(resources.DiskMB), + DiskMB: resources.DiskMB, }, } } diff --git a/scheduler/generic_sched.go b/scheduler/generic_sched.go index 3c46c6f66..14e386240 100644 --- a/scheduler/generic_sched.go +++ b/scheduler/generic_sched.go @@ -482,7 +482,7 @@ func (s *GenericScheduler) computePlacements(destructive, place []placementResul resources := &structs.AllocatedResources{ Tasks: option.TaskResources, Shared: structs.AllocatedSharedResources{ - DiskMB: uint64(tg.EphemeralDisk.SizeMB), + DiskMB: tg.EphemeralDisk.SizeMB, }, } diff --git a/scheduler/rank.go b/scheduler/rank.go index ad83cc5a2..7bf25d473 100644 --- a/scheduler/rank.go +++ b/scheduler/rank.go @@ -193,17 +193,17 @@ OUTER: Tasks: make(map[string]*structs.AllocatedTaskResources, len(iter.taskGroup.Tasks)), Shared: structs.AllocatedSharedResources{ - DiskMB: uint64(iter.taskGroup.EphemeralDisk.SizeMB), + DiskMB: iter.taskGroup.EphemeralDisk.SizeMB, }, } for _, task := range iter.taskGroup.Tasks { // Allocate the resources taskResources := &structs.AllocatedTaskResources{ Cpu: structs.AllocatedCpuResources{ - CpuShares: uint64(task.Resources.CPU), + CpuShares: task.Resources.CPU, }, Memory: structs.AllocatedMemoryResources{ - MemoryMB: uint64(task.Resources.MemoryMB), + MemoryMB: task.Resources.MemoryMB, }, } diff --git a/scheduler/reconcile_test.go b/scheduler/reconcile_test.go index c0503b24c..9569ccb92 100644 --- a/scheduler/reconcile_test.go +++ b/scheduler/reconcile_test.go @@ -51,7 +51,7 @@ func allocUpdateFnInplace(existing *structs.Allocation, _ *structs.Job, newTG *s newAlloc.AllocatedResources = &structs.AllocatedResources{ Tasks: map[string]*structs.AllocatedTaskResources{}, Shared: structs.AllocatedSharedResources{ - DiskMB: uint64(newTG.EphemeralDisk.SizeMB), + DiskMB: newTG.EphemeralDisk.SizeMB, }, } @@ -60,10 +60,10 @@ func allocUpdateFnInplace(existing *structs.Allocation, _ *structs.Job, newTG *s networks := existing.AllocatedResources.Tasks[task.Name].Copy().Networks newAlloc.AllocatedResources.Tasks[task.Name] = &structs.AllocatedTaskResources{ Cpu: structs.AllocatedCpuResources{ - CpuShares: uint64(task.Resources.CPU), + CpuShares: task.Resources.CPU, }, Memory: structs.AllocatedMemoryResources{ - MemoryMB: uint64(task.Resources.MemoryMB), + MemoryMB: task.Resources.MemoryMB, }, Networks: networks, } diff --git a/scheduler/system_sched.go b/scheduler/system_sched.go index 2387a1461..fa3df0927 100644 --- a/scheduler/system_sched.go +++ b/scheduler/system_sched.go @@ -318,7 +318,7 @@ func (s *SystemScheduler) computePlacements(place []allocTuple) error { resources := &structs.AllocatedResources{ Tasks: option.TaskResources, Shared: structs.AllocatedSharedResources{ - DiskMB: uint64(missing.TaskGroup.EphemeralDisk.SizeMB), + DiskMB: missing.TaskGroup.EphemeralDisk.SizeMB, }, } diff --git a/scheduler/util.go b/scheduler/util.go index 12a0469f1..5a1e0f128 100644 --- a/scheduler/util.go +++ b/scheduler/util.go @@ -553,7 +553,7 @@ func inplaceUpdate(ctx Context, eval *structs.Evaluation, job *structs.Job, newAlloc.AllocatedResources = &structs.AllocatedResources{ Tasks: option.TaskResources, Shared: structs.AllocatedSharedResources{ - DiskMB: uint64(update.TaskGroup.EphemeralDisk.SizeMB), + DiskMB: update.TaskGroup.EphemeralDisk.SizeMB, }, } newAlloc.Metrics = ctx.Metrics() @@ -829,7 +829,7 @@ func genericAllocUpdateFn(ctx Context, stack Stack, evalID string) allocUpdateTy newAlloc.AllocatedResources = &structs.AllocatedResources{ Tasks: option.TaskResources, Shared: structs.AllocatedSharedResources{ - DiskMB: uint64(newTG.EphemeralDisk.SizeMB), + DiskMB: newTG.EphemeralDisk.SizeMB, }, } newAlloc.Metrics = ctx.Metrics()