Search in sources :

Example 1 with FailoverAbortedException

use of redis.clients.jedis.exceptions.FailoverAbortedException in project jedis by xetorthio.

the class JedisSentinelTestUtil method waitForNewPromotedMaster.

public static HostAndPort waitForNewPromotedMaster(final String masterName, final Jedis sentinelJedis, final Jedis commandJedis) throws InterruptedException {
    final AtomicReference<String> newmaster = new AtomicReference<String>("");
    sentinelJedis.psubscribe(new JedisPubSub() {

        @Override
        public void onPMessage(String pattern, String channel, String message) {
            if (channel.equals("+switch-master")) {
                newmaster.set(message);
                punsubscribe();
            } else if (channel.startsWith("-failover-abort")) {
                punsubscribe();
                throw new FailoverAbortedException("Unfortunately sentinel cannot failover..." + " reason(channel) : " + channel + " / message : " + message);
            }
        }

        @Override
        public void onPSubscribe(String pattern, int subscribedChannels) {
            commandJedis.sentinelFailover(masterName);
        }
    }, "*");
    String[] chunks = newmaster.get().split(" ");
    HostAndPort newMaster = new HostAndPort(chunks[3], Integer.parseInt(chunks[4]));
    return newMaster;
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) AtomicReference(java.util.concurrent.atomic.AtomicReference) FailoverAbortedException(redis.clients.jedis.exceptions.FailoverAbortedException) JedisPubSub(redis.clients.jedis.JedisPubSub)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)1 HostAndPort (redis.clients.jedis.HostAndPort)1 JedisPubSub (redis.clients.jedis.JedisPubSub)1 FailoverAbortedException (redis.clients.jedis.exceptions.FailoverAbortedException)1