forked from acrosa/scala-redis
-
Notifications
You must be signed in to change notification settings - Fork 218
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
Sentinel support #79
Open
univerio
wants to merge
1
commit into
debasishg:master
Choose a base branch
from
univerio:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sentinel support #79
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.redis | ||
|
||
import java.net.InetSocketAddress | ||
|
||
abstract class NodeAddress { | ||
def addr: (String, Int) | ||
|
||
def onChange(callback: InetSocketAddress => Unit) | ||
override def toString = { | ||
val (host, port) = addr | ||
host + ":" + String.valueOf(port) | ||
} | ||
} | ||
|
||
class FixedAddress(host: String, port: Int) extends NodeAddress { | ||
val addr = (host, port) | ||
override def onChange(callback: InetSocketAddress => Unit) { } | ||
} | ||
|
||
class SentinelMonitoredMasterAddress(val sentinels: Seq[(String, Int)], val masterName: String) extends NodeAddress | ||
with Log { | ||
|
||
var master: Option[(String, Int)] = None | ||
|
||
override def addr = master.synchronized { | ||
master match { | ||
case Some((h, p)) => (h, p) | ||
case _ => throw new RuntimeException("All sentinels are down.") | ||
} | ||
} | ||
|
||
private var onChangeCallbacks: List[InetSocketAddress => Unit] = Nil | ||
|
||
override def onChange(callback: InetSocketAddress => Unit) = synchronized { | ||
onChangeCallbacks = callback :: onChangeCallbacks | ||
} | ||
|
||
private def fireCallbacks(addr: InetSocketAddress) = synchronized { | ||
onChangeCallbacks foreach (_(addr)) | ||
} | ||
|
||
def stopMonitoring() { | ||
sentinelListeners foreach (_.stop()) | ||
} | ||
|
||
private val sentinelClients = sentinels.map { case (h, p) => | ||
val client = new SentinelClient(h, p) | ||
master match { // this can be done without synchronization because the threads are not yet live | ||
case Some(_) => | ||
case None => | ||
try { | ||
master = client.getMasterAddrByName(masterName) | ||
} catch { | ||
case e: Throwable => error("Error connecting to sentinel.", e) | ||
} | ||
} | ||
client | ||
} | ||
private val sentinelListeners = sentinelClients map { client => | ||
val listener = new SentinelListener(client) | ||
new Thread(listener).start() | ||
listener | ||
} | ||
|
||
private class SentinelListener(val client: SentinelClient) extends Runnable { | ||
@volatile var running: Boolean = false | ||
|
||
def run() { | ||
running = true | ||
while (running) { | ||
try { | ||
client.synchronized { | ||
client.send("SUBSCRIBE", List("+switch-master"))(()) | ||
} | ||
new client.Consumer((msg: PubSubMessage) => | ||
msg match { | ||
case M(chan, msgText) => | ||
val tokens = msgText split ' ' | ||
val addr = tokens(3) | ||
val port = tokens(4).toInt | ||
master.synchronized { | ||
master = Some(addr, port) | ||
} | ||
fireCallbacks(new InetSocketAddress(addr, port)) | ||
case _ => | ||
}).run() // synchronously read, so we know when a disconnect happens | ||
} catch { | ||
case e: Throwable => error("Error connecting to sentinel.", e) | ||
} | ||
} | ||
} | ||
|
||
def stop() { | ||
client.synchronized { | ||
client.unsubscribe | ||
} | ||
running = false | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.redis | ||
|
||
class SentinelClient(override val host: String, override val port: Int) extends Redis | ||
with SentinelOperations | ||
with PubSub { | ||
|
||
lazy val addr: NodeAddress = new FixedAddress(host, port) // can only be fixed, not a dynamic master | ||
|
||
def this() = this("localhost", 26379) | ||
override def toString = host + ":" + String.valueOf(port) | ||
|
||
// publishing is not allowed on a sentinel's pub/sub channel | ||
override def publish(channel: String, msg: String): Option[Long] = | ||
throw new RuntimeException("Publishing is not supported on a sentinel.") | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.redis | ||
|
||
import serialization._ | ||
|
||
trait SentinelOperations { self: Redis => | ||
|
||
def masters[K,V](implicit format: Format, parseK: Parse[K], parseV: Parse[V]): Option[List[Option[Map[K,V]]]] = | ||
send("SENTINEL", List("MASTERS"))(asListOfListPairs[K,V].map(_.map(_.map(_.flatten.toMap)))) | ||
|
||
def slaves[K,V](name: String)(implicit format: Format, parseK: Parse[K], parseV: Parse[V]): | ||
Option[List[Option[Map[K,V]]]] = | ||
send("SENTINEL", List("SLAVES", name))(asListOfListPairs[K,V].map(_.map(_.map(_.flatten.toMap)))) | ||
|
||
def isMasterDownByAddr(host: String, port: Int): Option[(Boolean, String)] = | ||
send("SENTINEL", List("IS-MASTER-DOWN-BY-ADDR", host, port))(asList) match { | ||
case Some(List(Some(down), Some(leader))) => Some(down.toInt == 1, leader) | ||
case _ => None | ||
} | ||
|
||
def getMasterAddrByName(name: String): Option[(String, Int)] = | ||
send("SENTINEL", List("GET-MASTER-ADDR-BY-NAME", name))(asList[String]) match { | ||
case Some(List(Some(h), Some(p))) => Some(h, p.toInt) | ||
case _ => None | ||
} | ||
|
||
def reset(pattern: String): Option[Int] = | ||
send("SENTINEL", List("RESET", pattern))(asInt) | ||
|
||
def failover(name: String): Boolean = | ||
send("SENTINEL", List("FAILOVER", name))(asBoolean) | ||
|
||
} |
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
Oops, something went wrong.
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.
throw new MapOverflowException :)
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.
Heh, will probably need refactoring if it gets to
ListOfListOfListPairs
.