From 197228121eb7574f3e02354edbf1fb8f32f23338 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Mon, 23 Nov 2015 16:32:30 -0800 Subject: [PATCH 1/2] Remove shared reference to network resources across allocs --- nomad/structs/network.go | 20 +++++++++++++------- nomad/structs/structs.go | 4 ++++ scheduler/generic_sched_test.go | 13 +++++++++++++ scheduler/rank.go | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/nomad/structs/network.go b/nomad/structs/network.go index 33327a50b8a..1cbcce85572 100644 --- a/nomad/structs/network.go +++ b/nomad/structs/network.go @@ -95,11 +95,14 @@ func (idx *NetworkIndex) AddReserved(n *NetworkResource) (collide bool) { used = make(map[int]struct{}) idx.UsedPorts[n.IP] = used } - for _, port := range n.ReservedPorts { - if _, ok := used[port.Value]; ok { - collide = true - } else { - used[port.Value] = struct{}{} + + for _, ports := range [][]Port{n.ReservedPorts, n.DynamicPorts} { + for _, port := range ports { + if _, ok := used[port.Value]; ok { + collide = true + } else { + used[port.Value] = struct{}{} + } } } @@ -179,8 +182,11 @@ func (idx *NetworkIndex) AssignNetwork(ask *NetworkResource) (out *NetworkResour if _, ok := idx.UsedPorts[ipStr][randPort]; ok { goto PICK } - if isPortReserved(offer.ReservedPorts, randPort) { - goto PICK + + for _, ports := range [][]Port{offer.ReservedPorts, offer.DynamicPorts} { + if isPortReserved(ports, randPort) { + goto PICK + } } offer.DynamicPorts[i].Value = randPort } diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index d1c5c666d44..df7aa5f9ddd 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -645,6 +645,10 @@ func (n *NetworkResource) Copy() *NetworkResource { newR.ReservedPorts = make([]Port, len(n.ReservedPorts)) copy(newR.ReservedPorts, n.ReservedPorts) } + if n.DynamicPorts != nil { + newR.DynamicPorts = make([]Port, len(n.DynamicPorts)) + copy(newR.DynamicPorts, n.DynamicPorts) + } return newR } diff --git a/scheduler/generic_sched_test.go b/scheduler/generic_sched_test.go index e3e87557804..33abef8a498 100644 --- a/scheduler/generic_sched_test.go +++ b/scheduler/generic_sched_test.go @@ -60,6 +60,19 @@ func TestServiceSched_JobRegister(t *testing.T) { t.Fatalf("bad: %#v", out) } + // Ensure different ports were used. + used := make(map[int]struct{}) + for _, alloc := range out { + for _, resource := range alloc.TaskResources { + for _, port := range resource.Networks[0].DynamicPorts { + if _, ok := used[port.Value]; ok { + t.Fatalf("Port collision %v", port.Value) + } + used[port.Value] = struct{}{} + } + } + } + h.AssertEvalStatus(t, structs.EvalStatusComplete) } diff --git a/scheduler/rank.go b/scheduler/rank.go index 1677187ade8..38d0cf35fe6 100644 --- a/scheduler/rank.go +++ b/scheduler/rank.go @@ -188,7 +188,7 @@ OUTER: // Check if we need a network resource if len(taskResources.Networks) > 0 { - ask := taskResources.Networks[0] + ask := taskResources.Networks[0].Copy() offer, err := netIdx.AssignNetwork(ask) if offer == nil { iter.ctx.Metrics().ExhaustedNode(option.Node, From 7ca2bac7b4f96de75723b6597ad846f44f2c9cf4 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Mon, 23 Nov 2015 16:36:12 -0800 Subject: [PATCH 2/2] Remove unnecessary copy --- scheduler/rank.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scheduler/rank.go b/scheduler/rank.go index 38d0cf35fe6..1677187ade8 100644 --- a/scheduler/rank.go +++ b/scheduler/rank.go @@ -188,7 +188,7 @@ OUTER: // Check if we need a network resource if len(taskResources.Networks) > 0 { - ask := taskResources.Networks[0].Copy() + ask := taskResources.Networks[0] offer, err := netIdx.AssignNetwork(ask) if offer == nil { iter.ctx.Metrics().ExhaustedNode(option.Node,