-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set difference when picking random ports #1526
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,6 +183,8 @@ func (idx *NetworkIndex) AssignNetwork(ask *NetworkResource) (out *NetworkResour | |
return | ||
} | ||
|
||
used := idx.UsedPorts[ipStr] | ||
|
||
// Check if any of the reserved ports are in use | ||
for _, port := range ask.ReservedPorts { | ||
// Guard against invalid port | ||
|
@@ -192,7 +194,6 @@ func (idx *NetworkIndex) AssignNetwork(ask *NetworkResource) (out *NetworkResour | |
} | ||
|
||
// Check if in use | ||
used := idx.UsedPorts[ipStr] | ||
if used != nil && used.Check(uint(port.Value)) { | ||
err = fmt.Errorf("reserved port collision") | ||
return | ||
|
@@ -208,28 +209,25 @@ func (idx *NetworkIndex) AssignNetwork(ask *NetworkResource) (out *NetworkResour | |
DynamicPorts: ask.DynamicPorts, | ||
} | ||
|
||
// Check if we need to generate any ports | ||
for i := 0; i < len(ask.DynamicPorts); i++ { | ||
attempts := 0 | ||
PICK: | ||
attempts++ | ||
if attempts > maxRandPortAttempts { | ||
err = fmt.Errorf("dynamic port selection failed") | ||
return | ||
} | ||
// Try to stochastically pick the dynamic ports as it is faster and | ||
// lower memory usage. | ||
var dynPorts []int | ||
var dynErr error | ||
dynPorts, dynErr = getDynamicPortsStochastic(used, ask) | ||
if err == nil { | ||
goto BUILD_OFFER | ||
} | ||
|
||
randPort := MinDynamicPort + rand.Intn(MaxDynamicPort-MinDynamicPort) | ||
used := idx.UsedPorts[ipStr] | ||
if used != nil && used.Check(uint(randPort)) { | ||
goto PICK | ||
} | ||
// Fall back to the precise method if the random sampling failed. | ||
dynPorts, dynErr = getDynamicPortsPrecise(used, ask) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this overwrite the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I want it to. We only get here if the stochastic method failed |
||
if dynErr != nil { | ||
err = dynErr | ||
return | ||
} | ||
|
||
for _, ports := range [][]Port{offer.ReservedPorts, offer.DynamicPorts} { | ||
if isPortReserved(ports, randPort) { | ||
goto PICK | ||
} | ||
} | ||
offer.DynamicPorts[i].Value = randPort | ||
BUILD_OFFER: | ||
for i, port := range dynPorts { | ||
offer.DynamicPorts[i].Value = port | ||
} | ||
|
||
// Stop, we have an offer! | ||
|
@@ -240,10 +238,86 @@ func (idx *NetworkIndex) AssignNetwork(ask *NetworkResource) (out *NetworkResour | |
return | ||
} | ||
|
||
// getDynamicPortsPrecise takes the nodes used port bitmap which may be nil if | ||
// no ports have been allocated yet, the network ask and returns a set of unused | ||
// ports to fullfil the ask's DynamicPorts or an error if it failed. An error | ||
// means the ask can not be satisfied as the method does a precise search. | ||
func getDynamicPortsPrecise(nodeUsed Bitmap, ask *NetworkResource) ([]int, error) { | ||
// Create a copy of the used ports and apply the new reserves | ||
var usedSet Bitmap | ||
var err error | ||
if nodeUsed != nil { | ||
usedSet, err = nodeUsed.Copy() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
usedSet, err = NewBitmap(maxValidPort) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
for _, port := range ask.ReservedPorts { | ||
usedSet.Set(uint(port.Value)) | ||
} | ||
|
||
// Get the indexes of the unset | ||
availablePorts := usedSet.IndexesInRange(false, MinDynamicPort, MaxDynamicPort) | ||
|
||
// Randomize the amount we need | ||
numDyn := len(ask.DynamicPorts) | ||
if len(availablePorts) < numDyn { | ||
return nil, fmt.Errorf("dynamic port selection failed") | ||
} | ||
|
||
for i := 0; i < numDyn; i++ { | ||
j := rand.Intn(numDyn) | ||
availablePorts[i], availablePorts[j] = availablePorts[j], availablePorts[i] | ||
} | ||
|
||
return availablePorts[:numDyn], nil | ||
} | ||
|
||
// getDynamicPortsStochastic takes the nodes used port bitmap which may be nil if | ||
// no ports have been allocated yet, the network ask and returns a set of unused | ||
// ports to fullfil the ask's DynamicPorts or an error if it failed. An error | ||
// does not mean the ask can not be satisfied as the method has a fixed amount | ||
// of random probes and if these fail, the search is aborted. | ||
func getDynamicPortsStochastic(nodeUsed Bitmap, ask *NetworkResource) ([]int, error) { | ||
var reserved, dynamic []int | ||
for _, port := range ask.ReservedPorts { | ||
reserved = append(reserved, port.Value) | ||
} | ||
|
||
for i := 0; i < len(ask.DynamicPorts); i++ { | ||
attempts := 0 | ||
PICK: | ||
attempts++ | ||
if attempts > maxRandPortAttempts { | ||
return nil, fmt.Errorf("stochastic dynamic port selection failed") | ||
} | ||
|
||
randPort := MinDynamicPort + rand.Intn(MaxDynamicPort-MinDynamicPort) | ||
if nodeUsed != nil && nodeUsed.Check(uint(randPort)) { | ||
goto PICK | ||
} | ||
|
||
for _, ports := range [][]int{reserved, dynamic} { | ||
if isPortReserved(ports, randPort) { | ||
goto PICK | ||
} | ||
} | ||
dynamic = append(dynamic, randPort) | ||
} | ||
|
||
return dynamic, nil | ||
} | ||
|
||
// IntContains scans an integer slice for a value | ||
func isPortReserved(haystack []Port, needle int) bool { | ||
func isPortReserved(haystack []int, needle int) bool { | ||
for _, item := range haystack { | ||
if item.Value == needle { | ||
if item == needle { | ||
return true | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you get
fmt.Errorf("stochastic dynamic port selection failed")
you are jumping overgetDynamicPortsPrecise