From 736707222475e18b9bf0d66d1490a56967d189cb Mon Sep 17 00:00:00 2001 From: Maris Ruskulis Date: Sun, 3 Dec 2017 17:23:46 +0200 Subject: [PATCH] #196 - returns default/empty values for sentinel commands instead of failing future --- src/main/scala/redis/commands/Sentinel.scala | 12 ++++++++---- src/test/scala/redis/RedisSpec.scala | 3 +++ src/test/scala/redis/SentinelSpec.scala | 6 +++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/scala/redis/commands/Sentinel.scala b/src/main/scala/redis/commands/Sentinel.scala index 324bb5fc..168a0c42 100644 --- a/src/main/scala/redis/commands/Sentinel.scala +++ b/src/main/scala/redis/commands/Sentinel.scala @@ -5,28 +5,29 @@ 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)) @@ -34,4 +35,7 @@ trait Sentinel extends Request { def failover(master: String): Future[Boolean] = send(SenMasterFailover(master)) + private def recoverWith[R](r: R): PartialFunction[Throwable, R] = { + case NonFatal(_) => r + } } diff --git a/src/test/scala/redis/RedisSpec.scala b/src/test/scala/redis/RedisSpec.scala index 4cd3909f..1e10015e 100644 --- a/src/test/scala/redis/RedisSpec.scala +++ b/src/test/scala/redis/RedisSpec.scala @@ -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() diff --git a/src/test/scala/redis/SentinelSpec.scala b/src/test/scala/redis/SentinelSpec.scala index 1d916e62..fc1207de 100644 --- a/src/test/scala/redis/SentinelSpec.scala +++ b/src/test/scala/redis/SentinelSpec.scala @@ -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" + } + } }