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

implements #196 #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/main/scala/redis/commands/Sentinel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,37 @@ import redis.actors.ReplyErrorException
import redis.api._

import scala.concurrent.Future
import scala.util.control.NonFatal

trait Sentinel extends Request {

def masters(): Future[Seq[Map[String, String]]] =
send(SenMasters())
send(SenMasters()) recover recoverWith(Seq.empty)

def slaves(master: String): Future[Seq[Map[String, String]]] =
send(SenSlaves(master))
send(SenSlaves(master)) recover recoverWith(Seq.empty)

def isMasterDown(master: String): Future[Option[Boolean]] = {
send(SenMasterInfo(master)) map { response =>
Some(!(response("name") == master && response("flags") == "master"))
} recoverWith {
case ReplyErrorException(message) if message.startsWith("ERR No such master with that name") => Future.successful(None)
}
} recover recoverWith(None)
}

def getMasterAddr(master: String): Future[Option[(String, Int)]] =
send(SenGetMasterAddr(master)) map {
case Some(Seq(ip, port)) => Some((ip, port.toInt))
case _ => None
}
} recover recoverWith(None)

def resetMaster(pattern: String): Future[Boolean] =
send(SenResetMaster(pattern))

def failover(master: String): Future[Boolean] =
send(SenMasterFailover(master))

private def recoverWith[R](r: R): PartialFunction[Throwable, R] = {
case NonFatal(_) => r
}
}
3 changes: 3 additions & 0 deletions src/test/scala/redis/RedisSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ abstract class RedisSentinelClients(val masterName: String = "mymaster") extends
SentinelMonitoredRedisClient(master = masterName,
sentinels = Seq((redisHost, sentinelPort1), (redisHost, sentinelPort2)))

def failingSentinelMonitoredRedisClient =
SentinelMonitoredRedisClient(master = masterName,
sentinels = Seq((redisHost, 29292), (redisHost, sentinelPort1), (redisHost, sentinelPort2)))

val redisManager = new RedisManager()

Expand Down
6 changes: 5 additions & 1 deletion src/test/scala/redis/SentinelSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class SentinelSpec(implicit ee: ExecutionEnv) extends RedisSentinelClients("Sent
"reset master" in {
Await.result(sentinelClient.resetMaster(masterName), timeOut)
}
}

"skip failed nodes" in {
val failingClient = failingSentinelMonitoredRedisClient
Await.result(failingClient.ping(), timeOut) mustEqual "PONG"
}
}
}