Skip to content

Commit

Permalink
Merge pull request #4791 from hashicorp/f-fix-resource-type
Browse files Browse the repository at this point in the history
Change CPU/Disk/MemoryMB to int everywhere in new resource structs
  • Loading branch information
preetapan committed Oct 16, 2018
2 parents 6307142 + b7de957 commit fb3b8c0
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 80 deletions.
6 changes: 3 additions & 3 deletions api/allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions api/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions client/driver/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion client/fingerprint/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/fingerprint/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/fingerprint/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/fingerprint/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions client/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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:
Expand All @@ -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 {
Expand All @@ -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
Expand Down
16 changes: 7 additions & 9 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"io/ioutil"
golog "log"
"net"
"os"
"path/filepath"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit fb3b8c0

Please sign in to comment.