Merge pull request #4791 from hashicorp/f-fix-resource-type

Change CPU/Disk/MemoryMB to int everywhere in new resource structs
This commit is contained in:
Preetha 2018-10-16 19:44:51 -04:00 committed by GitHub
commit dddde8445e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 149 additions and 80 deletions

View file

@ -171,15 +171,15 @@ type AllocatedTaskResources struct {
}
type AllocatedSharedResources struct {
DiskMB uint64
DiskMB int64
}
type AllocatedCpuResources struct {
CpuShares uint64
CpuShares int64
}
type AllocatedMemoryResources struct {
MemoryMB uint64
MemoryMB int64
}
// AllocIndexSort reverse sorts allocs by CreateIndex.

View file

@ -471,15 +471,15 @@ type NodeResources struct {
}
type NodeCpuResources struct {
TotalShares uint64
TotalShares int64
}
type NodeMemoryResources struct {
MemoryMB uint64
MemoryMB int64
}
type NodeDiskResources struct {
DiskMB uint64
DiskMB int64
}
type NodeReservedResources struct {

View file

@ -207,8 +207,8 @@ type Builder struct {
// secretsDir from task's perspective; eg /secrets
secretsDir string
cpuLimit uint64
memLimit uint64
cpuLimit int64
memLimit int64
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(b.memLimit, 10)
}
if b.cpuLimit != 0 {
envMap[CpuLimit] = strconv.FormatUint(b.cpuLimit, 10)
envMap[CpuLimit] = strconv.FormatInt(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 = int64(task.Resources.MemoryMB)
b.cpuLimit = int64(task.Resources.CPU)
// Copy networks to prevent sharing
b.networks = make([]*structs.NetworkResource, len(task.Resources.Networks))
for i, n := range task.Resources.Networks {

View file

@ -31,7 +31,7 @@ func (f *CPUFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cst
resp.NodeResources = &structs.NodeResources{
Cpu: structs.NodeCpuResources{
CpuShares: uint64(totalCompute),
CpuShares: int64(totalCompute),
},
}
}

View file

@ -100,7 +100,7 @@ func TestCPUFingerprint_OverrideCompute(t *testing.T) {
if response.Resources.CPU != cfg.CpuCompute {
t.Fatalf("expected override cpu of %d but found %d", cfg.CpuCompute, response.Resources.CPU)
}
if response.NodeResources.Cpu.CpuShares != uint64(cfg.CpuCompute) {
if response.NodeResources.Cpu.CpuShares != int64(cfg.CpuCompute) {
t.Fatalf("expected override cpu of %d but found %d", cfg.CpuCompute, response.NodeResources.Cpu.CpuShares)
}
}

View file

@ -51,7 +51,7 @@ func (f *MemoryFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *
resp.NodeResources = &structs.NodeResources{
Memory: structs.NodeMemoryResources{
MemoryMB: uint64(totalMemory / bytesInMB),
MemoryMB: int64(totalMemory / bytesInMB),
},
}
}

View file

@ -53,7 +53,7 @@ func (f *StorageFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp
}
resp.NodeResources = &structs.NodeResources{
Disk: structs.NodeDiskResources{
DiskMB: free / bytesPerMegabyte,
DiskMB: int64(free / bytesPerMegabyte),
},
}
resp.Detected = true

View file

@ -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 += int64(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 int64
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 int64
if alloc.AllocatedResources != nil {
allocDiskMB = alloc.AllocatedResources.Shared.DiskMB
} else {
allocDiskMB = uint64(alloc.Resources.DiskMB)
allocDiskMB = int64(alloc.Resources.DiskMB)
}
// Destroy the alloc runner and wait until it exits

View file

@ -4,6 +4,7 @@ import (
"fmt"
"io"
"io/ioutil"
golog "log"
"net"
"os"
"path/filepath"
@ -13,16 +14,13 @@ import (
"sync/atomic"
"time"
golog "log"
metrics "github.com/armon/go-metrics"
log "github.com/hashicorp/go-hclog"
uuidparse "github.com/hashicorp/go-uuid"
clientconfig "github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/lib"
log "github.com/hashicorp/go-hclog"
uuidparse "github.com/hashicorp/go-uuid"
"github.com/hashicorp/nomad/client"
clientconfig "github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/command/agent/consul"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad"
@ -434,9 +432,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 = int64(a.config.Client.Reserved.CPU)
res.Memory.MemoryMB = int64(a.config.Client.Reserved.MemoryMB)
res.Disk.DiskMB = int64(a.config.Client.Reserved.DiskMB)
res.Networks.ReservedHostPorts = a.config.Client.Reserved.ReservedPorts
conf.Version = a.config.Version

View file

@ -2,6 +2,7 @@ package structs
import (
"bytes"
"container/heap"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
@ -11,6 +12,7 @@ import (
"errors"
"fmt"
"io"
"math"
"net"
"net/url"
"os"
@ -22,26 +24,20 @@ import (
"strings"
"time"
"golang.org/x/crypto/blake2b"
"container/heap"
"math"
hcodec "github.com/hashicorp/go-msgpack/codec"
multierror "github.com/hashicorp/go-multierror"
psstructs "github.com/hashicorp/nomad/plugins/shared/structs"
"github.com/gorhill/cronexpr"
"github.com/hashicorp/consul/api"
hcodec "github.com/hashicorp/go-msgpack/codec"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-version"
"github.com/hashicorp/nomad/acl"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/helper/args"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/lib/kheap"
psstructs "github.com/hashicorp/nomad/plugins/shared/structs"
"github.com/mitchellh/copystructure"
"github.com/ugorji/go/codec"
"golang.org/x/crypto/blake2b"
)
var (
@ -1600,14 +1596,14 @@ func (n *Node) ComparableReservedResources() *ComparableResources {
return &ComparableResources{
Flattened: AllocatedTaskResources{
Cpu: AllocatedCpuResources{
CpuShares: uint64(n.Reserved.CPU),
CpuShares: int64(n.Reserved.CPU),
},
Memory: AllocatedMemoryResources{
MemoryMB: uint64(n.Reserved.MemoryMB),
MemoryMB: int64(n.Reserved.MemoryMB),
},
},
Shared: AllocatedSharedResources{
DiskMB: uint64(n.Reserved.DiskMB),
DiskMB: int64(n.Reserved.DiskMB),
},
}
}
@ -1626,14 +1622,14 @@ func (n *Node) ComparableResources() *ComparableResources {
return &ComparableResources{
Flattened: AllocatedTaskResources{
Cpu: AllocatedCpuResources{
CpuShares: uint64(n.Resources.CPU),
CpuShares: int64(n.Resources.CPU),
},
Memory: AllocatedMemoryResources{
MemoryMB: uint64(n.Resources.MemoryMB),
MemoryMB: int64(n.Resources.MemoryMB),
},
},
Shared: AllocatedSharedResources{
DiskMB: uint64(n.Resources.DiskMB),
DiskMB: int64(n.Resources.DiskMB),
},
}
}
@ -2263,7 +2259,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 int64
}
func (n *NodeCpuResources) Merge(o *NodeCpuResources) {
@ -2295,7 +2291,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 int64
}
func (n *NodeMemoryResources) Merge(o *NodeMemoryResources) {
@ -2327,7 +2323,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 int64
}
func (n *NodeDiskResources) Merge(o *NodeDiskResources) {
@ -2601,17 +2597,17 @@ func (n *NodeReservedResources) Comparable() *ComparableResources {
// NodeReservedCpuResources captures the reserved CPU resources of the node.
type NodeReservedCpuResources struct {
CpuShares uint64
CpuShares int64
}
// NodeReservedMemoryResources captures the reserved memory resources of the node.
type NodeReservedMemoryResources struct {
MemoryMB uint64
MemoryMB int64
}
// NodeReservedDiskResources captures the reserved disk resources of the node.
type NodeReservedDiskResources struct {
DiskMB uint64
DiskMB int64
}
// NodeReservedNetworkResources captures the reserved network resources of the node.
@ -2731,9 +2727,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 int64
}
func (a *AllocatedSharedResources) Add(delta *AllocatedSharedResources) {
@ -2744,9 +2778,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 int64
}
func (a *AllocatedCpuResources) Add(delta *AllocatedCpuResources) {
@ -2757,9 +2799,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 int64
}
func (a *AllocatedMemoryResources) Add(delta *AllocatedMemoryResources) {
@ -2770,6 +2820,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 +2844,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 +7357,14 @@ func (a *Allocation) ComparableResources() *ComparableResources {
return &ComparableResources{
Flattened: AllocatedTaskResources{
Cpu: AllocatedCpuResources{
CpuShares: uint64(resources.CPU),
CpuShares: int64(resources.CPU),
},
Memory: AllocatedMemoryResources{
MemoryMB: uint64(resources.MemoryMB),
MemoryMB: int64(resources.MemoryMB),
},
},
Shared: AllocatedSharedResources{
DiskMB: uint64(resources.DiskMB),
DiskMB: int64(resources.DiskMB),
},
}
}

View file

@ -1047,5 +1047,4 @@ func checkAttributeConstraint(ctx Context, operand string, lVal, rVal *psstructs
return false
}
return false
}

View file

@ -1687,11 +1687,11 @@ func TestDeviceChecker(t *testing.T) {
"cores_clock": psstructs.NewIntAttribute(800, psstructs.UnitMHz),
},
Instances: []*structs.NodeDevice{
&structs.NodeDevice{
{
ID: uuid.Generate(),
Healthy: true,
},
&structs.NodeDevice{
{
ID: uuid.Generate(),
Healthy: true,
},
@ -1703,11 +1703,11 @@ func TestDeviceChecker(t *testing.T) {
Type: "gpu",
Name: "1080ti",
Instances: []*structs.NodeDevice{
&structs.NodeDevice{
{
ID: uuid.Generate(),
Healthy: false,
},
&structs.NodeDevice{
{
ID: uuid.Generate(),
Healthy: false,
},
@ -1719,11 +1719,11 @@ func TestDeviceChecker(t *testing.T) {
Type: "gpu",
Name: "GT640",
Instances: []*structs.NodeDevice{
&structs.NodeDevice{
{
ID: uuid.Generate(),
Healthy: true,
},
&structs.NodeDevice{
{
ID: uuid.Generate(),
Healthy: false,
},

View file

@ -6,7 +6,6 @@ import (
log "github.com/hashicorp/go-hclog"
memdb "github.com/hashicorp/go-memdb"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/structs"
@ -482,7 +481,7 @@ func (s *GenericScheduler) computePlacements(destructive, place []placementResul
resources := &structs.AllocatedResources{
Tasks: option.TaskResources,
Shared: structs.AllocatedSharedResources{
DiskMB: uint64(tg.EphemeralDisk.SizeMB),
DiskMB: int64(tg.EphemeralDisk.SizeMB),
},
}

View file

@ -2,7 +2,6 @@ package scheduler
import (
"fmt"
"math"
"github.com/hashicorp/nomad/nomad/structs"
@ -193,17 +192,17 @@ OUTER:
Tasks: make(map[string]*structs.AllocatedTaskResources,
len(iter.taskGroup.Tasks)),
Shared: structs.AllocatedSharedResources{
DiskMB: uint64(iter.taskGroup.EphemeralDisk.SizeMB),
DiskMB: int64(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: int64(task.Resources.CPU),
},
Memory: structs.AllocatedMemoryResources{
MemoryMB: uint64(task.Resources.MemoryMB),
MemoryMB: int64(task.Resources.MemoryMB),
},
}

View file

@ -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: int64(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: int64(task.Resources.CPU),
},
Memory: structs.AllocatedMemoryResources{
MemoryMB: uint64(task.Resources.MemoryMB),
MemoryMB: int64(task.Resources.MemoryMB),
},
Networks: networks,
}

View file

@ -5,7 +5,6 @@ import (
log "github.com/hashicorp/go-hclog"
memdb "github.com/hashicorp/go-memdb"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/structs"
)
@ -318,7 +317,7 @@ func (s *SystemScheduler) computePlacements(place []allocTuple) error {
resources := &structs.AllocatedResources{
Tasks: option.TaskResources,
Shared: structs.AllocatedSharedResources{
DiskMB: uint64(missing.TaskGroup.EphemeralDisk.SizeMB),
DiskMB: int64(missing.TaskGroup.EphemeralDisk.SizeMB),
},
}

View file

@ -7,7 +7,6 @@ import (
log "github.com/hashicorp/go-hclog"
memdb "github.com/hashicorp/go-memdb"
"github.com/hashicorp/nomad/nomad/structs"
)
@ -553,7 +552,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: int64(update.TaskGroup.EphemeralDisk.SizeMB),
},
}
newAlloc.Metrics = ctx.Metrics()
@ -829,7 +828,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: int64(newTG.EphemeralDisk.SizeMB),
},
}
newAlloc.Metrics = ctx.Metrics()