-
Notifications
You must be signed in to change notification settings - Fork 140
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
base: master
Are you sure you want to change the base?
Add sticky port #291
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,20 +169,32 @@ class Broker(val id: Int = 0) { | |
) | ||
} | ||
|
||
private[kafka] def getSuitablePort(ports: util.List[Range]): Int = { | ||
if (ports.isEmpty) return -1 | ||
|
||
val ports_ = ports.sortBy(r => r.start) | ||
if (port == null) | ||
return ports_.get(0).start | ||
|
||
for (range <- ports_) { | ||
val overlap = range.overlap(port) | ||
if (overlap != null) | ||
return overlap.start | ||
private[kafka] def getSuitablePort(availablePorts: util.List[Range]): Int = { | ||
// no available ports to choose from | ||
if (availablePorts.isEmpty) return -1 | ||
|
||
// compute allowed usable ports based on broker config and offer | ||
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)) | ||
}.sortBy(_.start) | ||
|
||
// no port usable | ||
if (usablePorts.isEmpty) return -1 | ||
|
||
// try to stick to the previous port if possible | ||
if (stickiness.port != null) { | ||
val preferedPort = new Range(stickiness.port) | ||
for (range <- usablePorts) { | ||
val found = range.overlap(preferedPort) | ||
if (found != null) | ||
return found.start | ||
} | ||
} | ||
|
||
-1 | ||
// else return first usable port | ||
return usablePorts.get(0).start | ||
} | ||
|
||
/* | ||
|
@@ -198,8 +210,8 @@ class Broker(val id: Int = 0) { | |
|
||
def shouldStop: Boolean = !active && task != null && !task.stopping | ||
|
||
def registerStart(hostname: String): Unit = { | ||
stickiness.registerStart(hostname) | ||
def registerStart(hostname: String, port: Integer): Unit = { | ||
stickiness.registerStart(hostname, port) | ||
failover.resetFailures() | ||
} | ||
|
||
|
@@ -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 | ||
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. 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 = { | ||
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. prefer |
||
this.hostname = hostname | ||
this.port = port | ||
stopTime = null | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
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. Ditto on |
||
|
||
class StickinessSerializer extends StdSerializer[Stickiness](classOf[Stickiness]) { | ||
override def serialize(s: Stickiness, gen: JsonGenerator, provider: SerializerProvider): Unit = { | ||
provider.defaultSerializeValue(StickinessModel(s.period, s.stopTime, s.hostname), gen) | ||
provider.defaultSerializeValue(StickinessModel(s.period, s.stopTime, s.hostname, s.port), gen) | ||
} | ||
} | ||
|
||
|
@@ -188,6 +188,7 @@ class StickinessDeserializer extends StdDeserializer[Stickiness](classOf[Stickin | |
val model = p.readValueAs(classOf[StickinessModel]) | ||
val s = new Stickiness() | ||
s.hostname = model.hostname | ||
s.port = model.port | ||
s.stopTime = model.stopTime | ||
s.period = model.period | ||
s | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,8 @@ | |
"syslog": false, | ||
"stickiness": { | ||
"period": "10m", | ||
"hostname": "host1" | ||
"hostname": "host1", | ||
"port": 1234 | ||
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. Leave this file as it is, its to test backwards compat with older (pre 0.10.0) serialized JSON. |
||
}, | ||
"log4jOptions": "k1=v1,k2=v2", | ||
"options": "a=1,b=2", | ||
|
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.
this can be just
Option(range.overlap(port))