From 5e67b37aadc625155aa9c3e5e8ba3eea02d829c3 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 16 Oct 2018 15:34:32 -0700 Subject: [PATCH] use int64 --- api/allocations.go | 6 ++-- api/nodes.go | 6 ++-- client/driver/env/env.go | 12 ++++---- client/fingerprint/cpu.go | 2 +- client/fingerprint/cpu_test.go | 2 +- client/fingerprint/memory.go | 2 +- client/fingerprint/storage.go | 2 +- client/gc.go | 8 +++--- command/agent/agent.go | 16 +++++------ nomad/structs/structs.go | 52 ++++++++++++++++------------------ scheduler/generic_sched.go | 3 +- scheduler/rank.go | 7 ++--- scheduler/reconcile_test.go | 6 ++-- scheduler/system_sched.go | 3 +- scheduler/util.go | 5 ++-- 15 files changed, 61 insertions(+), 71 deletions(-) diff --git a/api/allocations.go b/api/allocations.go index c82fdd2e5..cdeb3e8a0 100644 --- a/api/allocations.go +++ b/api/allocations.go @@ -171,15 +171,15 @@ type AllocatedTaskResources struct { } type AllocatedSharedResources struct { - DiskMB int + DiskMB int64 } type AllocatedCpuResources struct { - CpuShares int + CpuShares int64 } type AllocatedMemoryResources struct { - MemoryMB int + MemoryMB int64 } // AllocIndexSort reverse sorts allocs by CreateIndex. diff --git a/api/nodes.go b/api/nodes.go index a8ce431a2..234cf8cd8 100644 --- a/api/nodes.go +++ b/api/nodes.go @@ -471,15 +471,15 @@ type NodeResources struct { } type NodeCpuResources struct { - TotalShares int + TotalShares int64 } type NodeMemoryResources struct { - MemoryMB int + MemoryMB int64 } type NodeDiskResources struct { - DiskMB int + DiskMB int64 } type NodeReservedResources struct { diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 6f531b346..63c245504 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 int - memLimit int + 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.FormatInt(int64(b.memLimit), 10) + envMap[MemLimit] = strconv.FormatInt(b.memLimit, 10) } if b.cpuLimit != 0 { - envMap[CpuLimit] = strconv.FormatInt(int64(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 = task.Resources.MemoryMB - b.cpuLimit = 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 { diff --git a/client/fingerprint/cpu.go b/client/fingerprint/cpu.go index 7c21f3018..e317b7e06 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: totalCompute, + CpuShares: int64(totalCompute), }, } } diff --git a/client/fingerprint/cpu_test.go b/client/fingerprint/cpu_test.go index e2eccb8e8..1088ab95e 100644 --- a/client/fingerprint/cpu_test.go +++ b/client/fingerprint/cpu_test.go @@ -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) } } diff --git a/client/fingerprint/memory.go b/client/fingerprint/memory.go index b18903008..938310232 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: totalMemory / bytesInMB, + MemoryMB: int64(totalMemory / bytesInMB), }, } } diff --git a/client/fingerprint/storage.go b/client/fingerprint/storage.go index a3192aac2..edf14e247 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: int(free / bytesPerMegabyte), + DiskMB: int64(free / bytesPerMegabyte), }, } resp.Detected = true diff --git a/client/gc.go b/client/gc.go index f71dd0502..33084aa1d 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 += alloc.Resources.DiskMB + totalResource.DiskMB += int64(alloc.Resources.DiskMB) } } @@ -284,7 +284,7 @@ func (a *AllocGarbageCollector) MakeRoomFor(allocations []*structs.Allocation) e } } - var diskCleared int + var diskCleared int64 for { select { case <-a.shutdownCh: @@ -322,11 +322,11 @@ func (a *AllocGarbageCollector) MakeRoomFor(allocations []*structs.Allocation) e alloc := ar.Alloc() // COMPAT(0.11): Remove in 0.11 - var allocDiskMB int + var allocDiskMB int64 if alloc.AllocatedResources != nil { allocDiskMB = alloc.AllocatedResources.Shared.DiskMB } else { - allocDiskMB = alloc.Resources.DiskMB + allocDiskMB = int64(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 65cdba3ce..9c91f6e40 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -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 = a.config.Client.Reserved.CPU - res.Memory.MemoryMB = a.config.Client.Reserved.MemoryMB - res.Disk.DiskMB = 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 diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 02e6075c5..c5cd06dde 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -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: n.Reserved.CPU, + CpuShares: int64(n.Reserved.CPU), }, Memory: AllocatedMemoryResources{ - MemoryMB: n.Reserved.MemoryMB, + MemoryMB: int64(n.Reserved.MemoryMB), }, }, Shared: AllocatedSharedResources{ - DiskMB: n.Reserved.DiskMB, + DiskMB: int64(n.Reserved.DiskMB), }, } } @@ -1626,14 +1622,14 @@ func (n *Node) ComparableResources() *ComparableResources { return &ComparableResources{ Flattened: AllocatedTaskResources{ Cpu: AllocatedCpuResources{ - CpuShares: n.Resources.CPU, + CpuShares: int64(n.Resources.CPU), }, Memory: AllocatedMemoryResources{ - MemoryMB: n.Resources.MemoryMB, + MemoryMB: int64(n.Resources.MemoryMB), }, }, Shared: AllocatedSharedResources{ - DiskMB: 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 int + 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 int + 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 int + 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 int + CpuShares int64 } // NodeReservedMemoryResources captures the reserved memory resources of the node. type NodeReservedMemoryResources struct { - MemoryMB int + MemoryMB int64 } // NodeReservedDiskResources captures the reserved disk resources of the node. type NodeReservedDiskResources struct { - DiskMB int + DiskMB int64 } // NodeReservedNetworkResources captures the reserved network resources of the node. @@ -2771,7 +2767,7 @@ func (a *AllocatedTaskResources) Subtract(delta *AllocatedTaskResources) { // AllocatedSharedResources are the set of resources allocated to a task group. type AllocatedSharedResources struct { - DiskMB int + DiskMB int64 } func (a *AllocatedSharedResources) Add(delta *AllocatedSharedResources) { @@ -2792,7 +2788,7 @@ func (a *AllocatedSharedResources) Subtract(delta *AllocatedSharedResources) { // AllocatedCpuResources captures the allocated CPU resources. type AllocatedCpuResources struct { - CpuShares int + CpuShares int64 } func (a *AllocatedCpuResources) Add(delta *AllocatedCpuResources) { @@ -2813,7 +2809,7 @@ func (a *AllocatedCpuResources) Subtract(delta *AllocatedCpuResources) { // AllocatedMemoryResources captures the allocated memory resources. type AllocatedMemoryResources struct { - MemoryMB int + MemoryMB int64 } func (a *AllocatedMemoryResources) Add(delta *AllocatedMemoryResources) { @@ -7361,14 +7357,14 @@ func (a *Allocation) ComparableResources() *ComparableResources { return &ComparableResources{ Flattened: AllocatedTaskResources{ Cpu: AllocatedCpuResources{ - CpuShares: resources.CPU, + CpuShares: int64(resources.CPU), }, Memory: AllocatedMemoryResources{ - MemoryMB: resources.MemoryMB, + MemoryMB: int64(resources.MemoryMB), }, }, Shared: AllocatedSharedResources{ - DiskMB: resources.DiskMB, + DiskMB: int64(resources.DiskMB), }, } } diff --git a/scheduler/generic_sched.go b/scheduler/generic_sched.go index 14e386240..cf6ce977a 100644 --- a/scheduler/generic_sched.go +++ b/scheduler/generic_sched.go @@ -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: tg.EphemeralDisk.SizeMB, + DiskMB: int64(tg.EphemeralDisk.SizeMB), }, } diff --git a/scheduler/rank.go b/scheduler/rank.go index 7bf25d473..d2e70a4e6 100644 --- a/scheduler/rank.go +++ b/scheduler/rank.go @@ -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: 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: task.Resources.CPU, + CpuShares: int64(task.Resources.CPU), }, Memory: structs.AllocatedMemoryResources{ - MemoryMB: task.Resources.MemoryMB, + MemoryMB: int64(task.Resources.MemoryMB), }, } diff --git a/scheduler/reconcile_test.go b/scheduler/reconcile_test.go index 9569ccb92..72e77677a 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: 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: task.Resources.CPU, + CpuShares: int64(task.Resources.CPU), }, Memory: structs.AllocatedMemoryResources{ - MemoryMB: task.Resources.MemoryMB, + MemoryMB: int64(task.Resources.MemoryMB), }, Networks: networks, } diff --git a/scheduler/system_sched.go b/scheduler/system_sched.go index fa3df0927..60202eb68 100644 --- a/scheduler/system_sched.go +++ b/scheduler/system_sched.go @@ -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: missing.TaskGroup.EphemeralDisk.SizeMB, + DiskMB: int64(missing.TaskGroup.EphemeralDisk.SizeMB), }, } diff --git a/scheduler/util.go b/scheduler/util.go index 5a1e0f128..85a93298f 100644 --- a/scheduler/util.go +++ b/scheduler/util.go @@ -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: 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: newTG.EphemeralDisk.SizeMB, + DiskMB: int64(newTG.EphemeralDisk.SizeMB), }, } newAlloc.Metrics = ctx.Metrics()