Skip to content
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

Update port conflict checker to distinguish between conflicts and exact matches. #667

Merged
merged 1 commit into from Sep 9, 2014
Merged

Update port conflict checker to distinguish between conflicts and exact matches. #667

merged 1 commit into from Sep 9, 2014

Conversation

tasdomas
Copy link
Contributor

@tasdomas tasdomas commented Sep 3, 2014

This is a fix for #1359837 (https://bugs.launchpad.net/juju-core/+bug/1359837).

Opening a port that is already open should not result in an error.

@ericsnowcurrently
Copy link
Contributor

"LGTM" (rookie). Does a similar change have to happen in network/port.go?

@tasdomas
Copy link
Contributor Author

tasdomas commented Sep 3, 2014

No, network/port.go does not need to handle this - juju is moving from handling individual ports to operating on port ranges. Thanks for the review!

@@ -222,7 +225,7 @@ func (p *Ports) ClosePorts(portRange PortRange) error {
if existingPortsDef == portRange {
found = true
continue
} else if existingPortsDef.UnitName == portRange.UnitName && existingPortsDef.ConflictsWith(portRange) {
} else if existingPortsDef.UnitName == portRange.UnitName && existingPortsDef.CheckConflicts(portRange) == ErrPortsConflict {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is getting a bit too long for my taste, so how about:

} else if existingPortsDef.UnitName == portRange.UnitName &&
      existingPortsDef.CheckConflicts(portRange) == ErrPortsConflict {
...
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need the else

just open a new if condition here.

@davecheney
Copy link
Contributor

review done, sorry not lgtm yet.

}

return false
if a.ToPort >= b.FromPort && b.ToPort >= a.FromPort {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just trying to understand what we're checking here:

a := PortRange{FromPort: 1000, ToPort: 2000}
b := PortRange{FromPort: 1999, ToPort: 1001}

In the case above this if block will return ErrPortsConflict, but:

  • We're not checking ToPort <= FromPort (and possibly swapping them if so)
  • b as defined is not a valid range, but if b was {FromPort:1001, ToPort: 1999} (a valid range) ErrPortsConflict won't be returned, but it should as there is indeed conflict / overlap.

So I think there are about 10 cases to check:

  1. if a.FromPort > a.ToPort => "%d-%d is not a valid port range", a.FromPort, a.ToPort
  2. if b.FromPort > b.ToPort => "%d-%d is not a valid port range", b.FromPort, b.ToPort
  3. if a.FromPort == b.FromPort && a.ToPort == b.ToPort => ErrPortsMatch
  4. if a.FromPort < b.FromPort && a.ToPort > b.ToPort => ErrPortsConflict (a completely includes b)
  5. if b.FromPort < a.FromPort && b.ToPort > a.ToPort => ErrPortsConflict (b completely includes a)
  6. if a.FromPort < b.FromPort && a.ToPort < b.ToPort => ErrPortsConflict? (a overlaps partially with b)
  7. if b.FromPort < a.FromPort && b.ToPort < a.ToPort => ErrPortsConflict? (b overlaps partially with a)
  8. (only 2 cases left: a before b, b before a - in both cases no overlap or conflict) => nil

@dimitern
Copy link

dimitern commented Sep 4, 2014

A few thoughts and suggestions.

@tasdomas
Copy link
Contributor Author

tasdomas commented Sep 4, 2014

@dimitern, @davecheney - PTAL

"invalid port range",
state.PortRange{"wordpress/0", 100, 80, "TCP"},
MustPortRange("wordpress/0", 80, 80, "TCP"),
"100-80/tcp is not a valid port range: invalid port range 100-80",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather see "invalid port range: 100-80/tcp" only here as an error, which probably means if Validate() returns and error, just return it as-is in CheckConflicts().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Thanks!

@tasdomas
Copy link
Contributor Author

tasdomas commented Sep 4, 2014

@dimitern, @davecheney - PTAL

if !ports.canOpenPorts(portRange) {
return nil, fmt.Errorf("cannot open ports %v on machine %v due to conflict", portRange, machineId)
if err := ports.canOpenPorts(portRange); err != nil {
return nil, fmt.Errorf("cannot open ports %v on machine %v: %v", portRange, machineId, err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use errors.Annotatef(err, "cannot open ports %v on machine %q", portRange, machineId) ?

@tasdomas
Copy link
Contributor Author

tasdomas commented Sep 6, 2014

@dimitern - thanks for the review! PTAL

expected: "mismatched port ranges 100-200/tcp and 100-300/tcp",
},
}
expected: "mismatched port ranges 100-200/tcp and 100-300/tcp: port ranges 100-200/tcp \\(wordpress/0\\) and 100-300/tcp \\(wordpress/0\\) conflict",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message seems a bit too repetitive, I'd trim it like this "mismatched port ranges: 100-200/tcp (wordpress/0) and 100-300/tcp (wordpress/0) conflict", but this is just a suggestion, I can live with it.

@dimitern
Copy link

dimitern commented Sep 9, 2014

Apart from possibly rephrasing a minor eye-poker error message, now it looks much better, thanks for bearing with me! LGTM

@tasdomas
Copy link
Contributor Author

tasdomas commented Sep 9, 2014

$$fixes-1359837$$

@jujubot
Copy link
Collaborator

jujubot commented Sep 9, 2014

Status: merge request accepted. Url: http://juju-ci.vapour.ws:8080/job/github-merge-juju

jujubot added a commit that referenced this pull request Sep 9, 2014
Update port conflict checker to distinguish between conflicts and exact matches.

This is a fix for #1359837 (https://bugs.launchpad.net/juju-core/+bug/1359837).

Opening a port that is already open should not result in an error.
@jujubot jujubot merged commit 03f177a into juju:master Sep 9, 2014
@tasdomas tasdomas deleted the fix-existing-port-open branch September 16, 2014 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants