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

Add sticky port #291

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add sticky port #291

wants to merge 1 commit into from

Conversation

shangd
Copy link
Contributor

@shangd shangd commented Mar 10, 2017

This change will make broker remember previous port and prefer to restart on same port.

It is slightly different to the current hostname stickiness in that it is soft-stickiness, if the port is not available then it simply chooses the first port offered (same as current behavior) rather than rejecting the offer. It does not use the stickiness period to avoid overloading the config with unnecessary complexity.

Basically the existing hostname stickiness and config will behave exactly the same, but it will always pick previous port if available before choosing other ports.

@steveniemitz
Copy link
Contributor

In general this looks good, I'm curious though what the use case for this is? Is there a reason you need the port to be sticky? I worry that strengthening the assumption that a port may not change (although not specifically set as so) could cause friction/confusion for new users.

@@ -289,12 +301,14 @@ object Broker {
class Stickiness(_period: Period = new Period("10m")) {
var period: Period = _period
@volatile var hostname: String = null
@volatile var port: Integer = null
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer an Option[Int] here rather than a nullable to make it more idiomatic scala.

@volatile var stopTime: Date = null

def expires: Date = if (stopTime != null) new Date(stopTime.getTime + period.ms) else null

def registerStart(hostname: String): Unit = {
def registerStart(hostname: String, port: Integer): Unit = {
Copy link
Contributor

Choose a reason for hiding this comment

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

prefer Int over Integer

@@ -175,11 +175,11 @@ class RangeDeserializer extends StdDeserializer[Range](classOf[Range]) {
}
}

case class StickinessModel(period: Period, stopTime: Date, hostname: String)
case class StickinessModel(period: Period, stopTime: Date, hostname: String, port: Integer)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto on Option[Int] over Integer

@@ -19,7 +19,8 @@
"syslog": false,
"stickiness": {
"period": "10m",
"hostname": "host1"
"hostname": "host1",
"port": 1234
Copy link
Contributor

Choose a reason for hiding this comment

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

Leave this file as it is, its to test backwards compat with older (pre 0.10.0) serialized JSON.

val usablePorts: List[Range] =
if (port == null) availablePorts.toList.sortBy(_.start)
else availablePorts.toList.flatMap { range =>
if (range.overlap(port) == null) None else Some(range.overlap(port))
Copy link
Contributor

Choose a reason for hiding this comment

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

this can be just Option(range.overlap(port))

@shangd
Copy link
Contributor Author

shangd commented Mar 15, 2017

@steveniemitz Thanks for the review, the reason we implemented this was that we wanted to minimize the need for clients to re-lookup the brokers from our service discovery + re-init the client after the brokers have been restarted, which may take a while in some cases. With sticky address the client can usually reconnect as soon as the broker is back up.

I think it's quite reasonable to the user that they can either force the port they want to use in the config or they can let the framework pick an arbitrary port which may or may not be the same. I know it is a bit inconsistent when the hostname stickiness is enforced and the port one is not, but we thought it was practically more convenient. I'll update the doc to explain the differences so people don't get confused.

I'll make the Option changes you suggested when I got some time (quite busy atm) and come back in a few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants