redis-sentinel 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/lib/redis-sentinel.rb +2 -0
- data/lib/redis-sentinel/client.rb +84 -0
- data/lib/redis-sentinel/version.rb +5 -0
- data/redis-sentinel.gemspec +22 -0
- data/spec/redis-sentinel/client_spec.rb +40 -0
- data/spec/spec_helper.rb +10 -0
- metadata +91 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Richard Huang
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Redis::Sentinel
|
2
|
+
|
3
|
+
monkey patch [redis-rb][0] to support redis sentinel.
|
4
|
+
|
5
|
+
it subscribes message with channel "+switch-master", when message
|
6
|
+
received, it will disconnect current connection and connect to new
|
7
|
+
master server.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'redis-sentinel'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install redis-sentinel
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Specify the sentinel servers and master name
|
26
|
+
|
27
|
+
Redis.new(master_name: "master1", sentinels: [{host: "localhost", port: 26379}, {host: "localhost", port: 26380}])
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
36
|
+
|
37
|
+
[0]: https://github.com/redis/redis-rb
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require "redis"
|
2
|
+
|
3
|
+
class Redis::Client
|
4
|
+
class_eval do
|
5
|
+
def initiliaze_with_sentinel(options={})
|
6
|
+
@master_name = options.delete(:master_name) || options.delete("master_name")
|
7
|
+
@sentinels = options.delete(:sentinels) || options.delete("sentinels")
|
8
|
+
@watcher = Thread.new { watch_sentinel } if sentinel?
|
9
|
+
initialize_without_sentinel(options)
|
10
|
+
end
|
11
|
+
|
12
|
+
alias initialize_without_sentinel initialize
|
13
|
+
alias initialize initiliaze_with_sentinel
|
14
|
+
|
15
|
+
def connect_with_sentinel
|
16
|
+
discover_master if sentinel?
|
17
|
+
connect_without_sentinel
|
18
|
+
end
|
19
|
+
|
20
|
+
alias connect_without_sentinel connect
|
21
|
+
alias connect connect_with_sentinel
|
22
|
+
|
23
|
+
def sentinel?
|
24
|
+
@master_name && @sentinels
|
25
|
+
end
|
26
|
+
|
27
|
+
def try_next_sentinel
|
28
|
+
@sentinels << @sentinels.shift
|
29
|
+
if @logger && @logger.debug?
|
30
|
+
@logger.debug? "Trying next sentinel: #{@sentinels[0][:host]}:#{@sentinels[0][:port]}"
|
31
|
+
end
|
32
|
+
return @sentinels[0]
|
33
|
+
end
|
34
|
+
|
35
|
+
def discover_master
|
36
|
+
masters = []
|
37
|
+
|
38
|
+
while true
|
39
|
+
sentinel = Redis.new(@sentinels[0])
|
40
|
+
|
41
|
+
begin
|
42
|
+
host, port = sentinel.sentinel("get-master-addr-by-name", @master_name)
|
43
|
+
if !host && !port
|
44
|
+
raise Redis::ConnectionError("No master named: #{@master_name}")
|
45
|
+
end
|
46
|
+
@options.merge!(host: host, port: port.to_i)
|
47
|
+
|
48
|
+
break
|
49
|
+
rescue Redis::CannotConnectError
|
50
|
+
try_next_sentinel
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def watch_sentinel
|
56
|
+
while true
|
57
|
+
sentinel = Redis.new(@sentinels[0])
|
58
|
+
|
59
|
+
begin
|
60
|
+
sentinel.psubscribe("*") do |on|
|
61
|
+
on.pmessage do |pattern, channel, message|
|
62
|
+
next if channel != "+switch-master"
|
63
|
+
|
64
|
+
master_name, old_host, old_port, new_host, new_port = message.split(" ")
|
65
|
+
|
66
|
+
next if master_name != @master_name
|
67
|
+
|
68
|
+
@options.merge!(host: new_host, port: new_port.to_i)
|
69
|
+
|
70
|
+
if @logger && @logger.debug?
|
71
|
+
@logger.debug "Failover: #{old_host}:#{old_port} => #{new_host}:#{new_port}"
|
72
|
+
end
|
73
|
+
|
74
|
+
disconnect
|
75
|
+
end
|
76
|
+
end
|
77
|
+
rescue Redis::CannotConnectError
|
78
|
+
try_next_sentinel
|
79
|
+
sleep 1
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'redis-sentinel/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "redis-sentinel"
|
8
|
+
gem.version = Redis::Sentinel::VERSION
|
9
|
+
gem.authors = ["Richard Huang"]
|
10
|
+
gem.email = ["flyerhzm@gmail.com"]
|
11
|
+
gem.description = %q{monkey patch redis-rb to support redis sentinel}
|
12
|
+
gem.summary = %q{monkey patch redis-rb to support redis sentinel}
|
13
|
+
gem.homepage = "https://github.com/flyerhzm/redis-sentinel"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "redis"
|
21
|
+
gem.add_development_dependency "rspec"
|
22
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Redis::Client do
|
4
|
+
context "#sentinel?" do
|
5
|
+
it "should be true if passing sentiels and master_name options" do
|
6
|
+
expect(Redis::Client.new(master_name: "master", sentinels: [{host: "localhost", port: 26379}, {host: "localhost", port: 26380}])).to be_sentinel
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not be true if not passing sentinels and maser_name options" do
|
10
|
+
expect(Redis::Client.new).not_to be_sentinel
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should not be true if passing sentinels option but not master_name option" do
|
14
|
+
expect(Redis::Client.new(sentinels: [{host: "localhost", port: 26379}, {host: "localhost", port: 26380}])).not_to be_sentinel
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should not be true if passing master_name option but not sentinels option" do
|
18
|
+
expect(Redis::Client.new(master_name: "master")).not_to be_sentinel
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "#try_next_sentinel" do
|
23
|
+
let(:client) { Redis::Client.new(master_name: "master", sentinels: [{host: "localhost", port: 26379}, {host: "localhost", port: 26380}]) }
|
24
|
+
|
25
|
+
it "should return next sentinel server" do
|
26
|
+
expect(client.try_next_sentinel).to eq({host: "localhost", port: 26380})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "#discover_master" do
|
31
|
+
let(:client) { Redis::Client.new(master_name: "master", sentinels: [{host: "localhost", port: 26379}, {host: "localhost", port: 26380}]) }
|
32
|
+
before { Redis.any_instance.should_receive(:sentinel).with("get-master-addr-by-name", "master").and_return(["remote.server", 8888]) }
|
33
|
+
|
34
|
+
it "should update options" do
|
35
|
+
client.discover_master
|
36
|
+
expect(client.host).to eq "remote.server"
|
37
|
+
expect(client.port).to eq 8888
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-sentinel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Richard Huang
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: redis
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: monkey patch redis-rb to support redis sentinel
|
47
|
+
email:
|
48
|
+
- flyerhzm@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/redis-sentinel.rb
|
59
|
+
- lib/redis-sentinel/client.rb
|
60
|
+
- lib/redis-sentinel/version.rb
|
61
|
+
- redis-sentinel.gemspec
|
62
|
+
- spec/redis-sentinel/client_spec.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
homepage: https://github.com/flyerhzm/redis-sentinel
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.24
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: monkey patch redis-rb to support redis sentinel
|
88
|
+
test_files:
|
89
|
+
- spec/redis-sentinel/client_spec.rb
|
90
|
+
- spec/spec_helper.rb
|
91
|
+
has_rdoc:
|