Created
August 3, 2015 19:41
-
-
Save amoslanka/12db1d844b0a07508b22 to your computer and use it in GitHub Desktop.
Adjustment to Redis client's Seninel example in order to confirm that blocking operations are booted during a failover. (https://github.com/redis/redis-rb/blob/98e3e7a516fc9b4609bc8ab482605f835a4de621/examples/sentinel.rb)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'redis' | |
# This example creates a master-slave setup with a sentinel, then connects to | |
# it and sends write commands in a loop. | |
# | |
# After 30 seconds, the master dies. You will be able to see how a new master | |
# is elected and things continue to work as if nothing happened. | |
# | |
# To run this example: | |
# | |
# $ ruby -I./lib examples/sentinel.rb | |
# | |
at_exit do | |
begin | |
Process.kill(:INT, $redises) | |
rescue Errno::ESRCH | |
end | |
Process.waitall | |
end | |
$redises = spawn("examples/sentinel/start") | |
Sentinels = [{:host => "127.0.0.1", :port => 26379}, | |
{:host => "127.0.0.1", :port => 26380}] | |
r = Redis.new(:url => "redis://master1", :sentinels => Sentinels, :role => :master) | |
# Set keys into a loop. | |
# | |
# The example traps errors so that you can actually try to failover while | |
# running the script to see redis-rb reconfiguring. | |
(0..1000000).each{|i| | |
begin | |
# Still, write to the cluster (for replication purposes) | |
r.set(i,i) | |
$stdout.write("SET (#{i} times)\n") if i % 100 == 0 | |
$stdout.write("BLPOP #{i}\n") | |
# This should block as long as the connection is open | |
r.blpop("foo", 0) | |
rescue => e | |
$stdout.write("E\n") | |
$stdout.write("#{e.message}\n\n") | |
end | |
sleep(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment