Skip to content

Instantly share code, notes, and snippets.

@mardambey
Created August 24, 2013 19:05
Show Gist options
  • Save mardambey/6329886 to your computer and use it in GitHub Desktop.
Save mardambey/6329886 to your computer and use it in GitHub Desktop.
Testing JedisSentinelPool subscribed to Redis Sentinel following +switch-master to find the new master.
package redis.clients.jedis;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
public class JedisSentinelPoolRunner {
private static Logger log = Logger.getLogger(JedisSentinelPoolRunner.class.getName());
public static void main(String[] argv) {
Set<String> sentinels = new HashSet<String>();
sentinels.add("localhost:26379");
final JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels);
Thread t = new Thread() {
public void run() {
while (true) {
Jedis j = null;
try {
j= pool.getResource();
try {
log.info("ping response: " + j.ping());
pool.returnResource(j);
} catch (Exception e) {
throw e;
}
} catch (Exception e) {
log.severe("Caught exception: " + e.getMessage());
if (j != null) pool.returnBrokenResource(j);
}
try { Thread.sleep(5000); } catch (Exception e) {}
}
}
};
t.start();
while (true) {
try { Thread.sleep(10); } catch (Exception e) {}
}
}
}
24-Aug-2013 2:46:26 PM redis.clients.jedis.JedisSentinelPool log
INFO: Got master running at 127.0.0.1:6380. Starting sentinel listeners.
24-Aug-2013 2:46:26 PM redis.clients.jedis.JedisSentinelPool log
INFO: Created pool: 127.0.0.1:6380
24-Aug-2013 2:46:26 PM redis.clients.jedis.JedisSentinelPoolRunner$1 run
INFO: ping response: PONG
24-Aug-2013 2:46:31 PM redis.clients.jedis.JedisSentinelPoolRunner$1 run
INFO: ping response: PONG
...
...
24-Aug-2013 2:47:06 PM redis.clients.jedis.JedisSentinelPoolRunner$1 run
SEVERE: Caught exception: It seems like server has closed the connection.
24-Aug-2013 2:47:11 PM redis.clients.jedis.JedisSentinelPoolRunner$1 run
SEVERE: Caught exception: Could not get a resource from the pool
24-Aug-2013 2:47:16 PM redis.clients.jedis.JedisSentinelPoolRunner$1 run
SEVERE: Caught exception: Could not get a resource from the pool
24-Aug-2013 2:47:17 PM redis.clients.jedis.JedisSentinelPool log
INFO: Sentinel localhost:26379 published: mymaster 127.0.0.1 6380 127.0.0.1 6379.
24-Aug-2013 2:47:17 PM redis.clients.jedis.JedisSentinelPool log
INFO: Created pool: 127.0.0.1:6379
24-Aug-2013 2:47:21 PM redis.clients.jedis.JedisSentinelPoolRunner$1 run
INFO: ping response: PONG
24-Aug-2013 2:47:26 PM redis.clients.jedis.JedisSentinelPoolRunner$1 run
INFO: ping response: PONG
...
...
[21011] 24 Aug 14:47:04.850 # +sdown master mymaster 127.0.0.1 6380
[21011] 24 Aug 14:47:04.850 # +odown master mymaster 127.0.0.1 6380 #quorum 1/1
[21011] 24 Aug 14:47:04.850 # +failover-triggered master mymaster 127.0.0.1 6380
[21011] 24 Aug 14:47:04.850 # +failover-state-wait-start master mymaster 127.0.0.1 6380 #starting in 11971 milliseconds
[21011] 24 Aug 14:47:16.880 # +failover-state-select-slave master mymaster 127.0.0.1 6380
[21011] 24 Aug 14:47:16.981 # +selected-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
[21011] 24 Aug 14:47:16.981 * +failover-state-send-slaveof-noone slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
[21011] 24 Aug 14:47:17.081 * +failover-state-wait-promotion slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
[21011] 24 Aug 14:47:17.884 # +promoted-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
[21011] 24 Aug 14:47:17.884 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6380
[21011] 24 Aug 14:47:17.985 # +failover-end master mymaster 127.0.0.1 6380
[21011] 24 Aug 14:47:17.985 # +switch-master mymaster 127.0.0.1 6380 127.0.0.1 6379
[21011] 24 Aug 14:47:20.992 # +sdown slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
1450 pts/1 Sl 0:00 \_ ./src/redis-server redis.2.conf
21646 pts/1 Sl 0:00 \_ ./src/redis-server redis.1.conf
kill 21450
[21450 | signal handler] (%1377370022) Received SIGTERM, scheduling shutdown...
[21450] 24 Aug 14:47:02.344 # User requested shutdown...
[21450] 24 Aug 14:47:02.344 # Redis is now ready to exit, bye bye...
[21646] 24 Aug 14:47:02.891 * Connecting to MASTER...
[21646] 24 Aug 14:47:02.891 * MASTER <-> SLAVE sync started
[21646] 24 Aug 14:47:02.891 # Error condition on socket for SYNC: Connection refused
[21646] 24 Aug 14:47:03.892 * Connecting to MASTER...
[21646] 24 Aug 14:47:03.892 * MASTER <-> SLAVE sync started
[21646] 24 Aug 14:47:03.892 # Error condition on socket for SYNC: Connection refused
...
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment