Merge pull request #494 from hashicorp/b-dynamic-port-conflict

Remove shared reference to DynamicPorts across allocs
This commit is contained in:
Alex Dadgar 2015-11-23 18:26:37 -08:00
commit 94ab5c321a
3 changed files with 30 additions and 7 deletions

View File

@ -95,11 +95,14 @@ func (idx *NetworkIndex) AddReserved(n *NetworkResource) (collide bool) {
used = make(map[int]struct{}) used = make(map[int]struct{})
idx.UsedPorts[n.IP] = used idx.UsedPorts[n.IP] = used
} }
for _, port := range n.ReservedPorts {
if _, ok := used[port.Value]; ok { for _, ports := range [][]Port{n.ReservedPorts, n.DynamicPorts} {
collide = true for _, port := range ports {
} else { if _, ok := used[port.Value]; ok {
used[port.Value] = struct{}{} 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 { if _, ok := idx.UsedPorts[ipStr][randPort]; ok {
goto PICK 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 offer.DynamicPorts[i].Value = randPort
} }

View File

@ -645,6 +645,10 @@ func (n *NetworkResource) Copy() *NetworkResource {
newR.ReservedPorts = make([]Port, len(n.ReservedPorts)) newR.ReservedPorts = make([]Port, len(n.ReservedPorts))
copy(newR.ReservedPorts, 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 return newR
} }

View File

@ -60,6 +60,19 @@ func TestServiceSched_JobRegister(t *testing.T) {
t.Fatalf("bad: %#v", out) 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) h.AssertEvalStatus(t, structs.EvalStatusComplete)
} }