Skip to content

Commit

Permalink
#196 - returns default/empty values for sentinel commands instead of …
Browse files Browse the repository at this point in the history
…failing future
  • Loading branch information
Maris Ruskulis committed Dec 3, 2017
1 parent 55573df commit 7367072
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
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"
}
}
}

0 comments on commit 7367072

Please sign in to comment.