rollout-zk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Eric Lindvall
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # Zookeeper storage adapter for rollout
2
+
3
+ I've implemented a zookeeper-based storage adapter for [rollout][] that does
4
+ not require any network roundtrips to check if a feature is active for a user.
5
+
6
+ It uses zookeeper watches to store the entire dataset in memory (which will
7
+ generally be very small) and updates the local cache as soon as anything
8
+ changes.
9
+
10
+ If you haven't migrated to rollout v2, you can just use this `:legacy_storage`
11
+ option and things will migrate to redis. If you have alerady migrated, it
12
+ would take some manual work (that I think could be fairly easy to automate).
13
+
14
+
15
+ ## Usage
16
+
17
+ ``` ruby
18
+ $rollout = Rollout.new(
19
+ Rollout::Zookeeper::Storage.new($zookeeper, "/rollout/users"),
20
+ :legacy_storage => $redis, :migrate => true)
21
+ ```
22
+
23
+ ## Items of note
24
+
25
+ * it uses the [zk][] gem
26
+ * all settings are stored is a single znode
27
+ * this will automatically migrate everything properly if you haven't migrated to rollout v2 yet
28
+
29
+ [rollout]: https://github.com/jamesgolick/rollout
30
+ [zk]: https://github.com/slyphon/zk
31
+ [pull request]: https://github.com/jamesgolick/rollout/pull/31
@@ -0,0 +1,151 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/test_*.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Generate RCov test coverage and open in your browser"
56
+ task :coverage do
57
+ require 'rcov'
58
+ sh "rm -fr coverage"
59
+ sh "rcov test/test_*.rb"
60
+ sh "open coverage/index.html"
61
+ end
62
+
63
+ require 'rake/rdoctask'
64
+ Rake::RDocTask.new do |rdoc|
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "#{name} #{version}"
67
+ rdoc.rdoc_files.include('README*')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
70
+
71
+ desc "Open an irb session preloaded with this library"
72
+ task :console do
73
+ sh "irb -rubygems -r ./lib/#{name}.rb"
74
+ end
75
+
76
+ #############################################################################
77
+ #
78
+ # Custom tasks (add your own tasks here)
79
+ #
80
+ #############################################################################
81
+
82
+
83
+
84
+ #############################################################################
85
+ #
86
+ # Packaging tasks
87
+ #
88
+ #############################################################################
89
+
90
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
91
+ task :release => :build do
92
+ unless `git branch` =~ /^\* master$/
93
+ puts "You must be on the master branch to release!"
94
+ exit!
95
+ end
96
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
97
+ sh "git tag v#{version}"
98
+ sh "git push origin master"
99
+ sh "git push origin v#{version}"
100
+ sh "gem push pkg/#{name}-#{version}.gem"
101
+ end
102
+
103
+ desc "Build #{gem_file} into the pkg directory"
104
+ task :build => :gemspec do
105
+ sh "mkdir -p pkg"
106
+ sh "gem build #{gemspec_file}"
107
+ sh "mv #{gem_file} pkg"
108
+ end
109
+
110
+ desc "Generate #{gemspec_file}"
111
+ task :gemspec => :validate do
112
+ # read spec file and split out manifest section
113
+ spec = File.read(gemspec_file)
114
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
115
+
116
+ # replace name version and date
117
+ replace_header(head, :name)
118
+ replace_header(head, :version)
119
+ replace_header(head, :date)
120
+ #comment this out if your rubyforge_project has a different name
121
+ replace_header(head, :rubyforge_project)
122
+
123
+ # determine file list from git ls-files
124
+ files = `git ls-files`.
125
+ split("\n").
126
+ sort.
127
+ reject { |file| file =~ /^\./ }.
128
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
129
+ map { |file| " #{file}" }.
130
+ join("\n")
131
+
132
+ # piece file back together and write
133
+ manifest = " s.files = %w[\n#{files}\n ]\n"
134
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
135
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
136
+ puts "Updated #{gemspec_file}"
137
+ end
138
+
139
+ desc "Validate #{gemspec_file}"
140
+ task :validate do
141
+ prefix = name[/^([^-]*)/, 1]
142
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}", "lib/#{prefix}"]
143
+ unless libfiles.empty?
144
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
145
+ exit!
146
+ end
147
+ unless Dir['VERSION*'].empty?
148
+ puts "A `VERSION` file at root level violates Gem best practices."
149
+ exit!
150
+ end
151
+ end
@@ -0,0 +1,5 @@
1
+ require 'rollout/zookeeper/storage'
2
+
3
+ module RolloutZk
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,197 @@
1
+ require 'zk'
2
+ require 'yajl'
3
+
4
+ module Rollout::Zookeeper
5
+ class DistributedHashtable
6
+ def initialize(zk, path, options = {})
7
+ @zk = zk
8
+ @path = path
9
+ @mutex = Mutex.new
10
+ @callbacks = []
11
+ @on_error = options[:on_error] || proc { |ex| }
12
+
13
+ configure_watches
14
+ end
15
+
16
+ def on_change(&block)
17
+ @mutex.synchronize do
18
+ @callbacks << block
19
+ end
20
+
21
+ block.call
22
+ end
23
+
24
+ def fire
25
+ callbacks = @mutex.synchronize do
26
+ @callbacks.dup
27
+ end
28
+
29
+ callbacks.each do |cb|
30
+ begin
31
+ cb.call
32
+ rescue => e
33
+ @on_error.call(e)
34
+ end
35
+ end
36
+ end
37
+
38
+ def [](key)
39
+ @mutex.synchronize do
40
+ if @hashtable
41
+ return @hashtable[key]
42
+ end
43
+ end
44
+ end
45
+
46
+ def []=(key, value)
47
+ result = @mutex.synchronize do
48
+ update do |hashtable|
49
+ hashtable[key] = value
50
+ end
51
+ end
52
+
53
+ fire
54
+
55
+ return result
56
+ end
57
+
58
+ def has_key?(key)
59
+ @mutex.synchronize do
60
+ if @hashtable
61
+ @hashtable.has_key?(key)
62
+ end
63
+ end
64
+ end
65
+
66
+ def delete(key)
67
+ result = @mutex.synchronize do
68
+ update do |hashtable|
69
+ hashtable.delete(key)
70
+ end
71
+ end
72
+
73
+ fire
74
+
75
+ return result
76
+ end
77
+
78
+ def merge(other)
79
+ result = @mutex.synchronize do
80
+ update do |hashtable|
81
+ hashtable.merge(other)
82
+ end
83
+ end
84
+
85
+ fire
86
+
87
+ return result
88
+ end
89
+
90
+ def to_h
91
+ @mutex.synchronize do
92
+ if @hashtable
93
+ @hashtable.dup
94
+ else
95
+ {}
96
+ end
97
+ end
98
+ end
99
+
100
+ def each(&block)
101
+ to_h.each(&block)
102
+ end
103
+
104
+ def length
105
+ @mutex.synchronize do
106
+ if @hashtable
107
+ @hashtable.length
108
+ else
109
+ 0
110
+ end
111
+ end
112
+ end
113
+
114
+ def empty?
115
+ length == 0
116
+ end
117
+ alias_method :blank?, :empty?
118
+
119
+ def read
120
+ @mutex.synchronize do
121
+ begin
122
+ current, _ = @zk.get(@path, :watch => true)
123
+ @hashtable = Yajl::Parser.parse(current)
124
+ rescue ::ZK::Exceptions::NoNode
125
+ if @zk.exists?(@path, :watch => true)
126
+ retry
127
+ else
128
+ @hashtable = Hash.new
129
+ end
130
+ end
131
+ end
132
+
133
+ fire
134
+ end
135
+
136
+ def update(&block)
137
+ return update_exists(&block)
138
+ rescue ::ZK::Exceptions::NoNode
139
+ begin
140
+ return update_initial(&block)
141
+ rescue ::ZK::Exceptions::NodeExists
142
+ return update_exists(&block)
143
+ end
144
+ end
145
+
146
+ def update_exists(&block)
147
+ begin
148
+ current, stat = @zk.get(@path, :watch => true)
149
+ hashtable = Yajl::Parser.parse(current)
150
+
151
+ result = block.call(hashtable)
152
+
153
+ @zk.set(@path, Yajl::Encoder.encode(hashtable), :version => stat.version)
154
+ @hashtable = hashtable
155
+
156
+ return result
157
+ rescue ::ZK::Exceptions::BadVersion
158
+ sleep 0.1 + rand
159
+ retry
160
+ end
161
+ end
162
+
163
+ def update_initial(&block)
164
+ begin
165
+ hashtable = Hash.new
166
+
167
+ result = block.call(hashtable)
168
+
169
+ @zk.create(@path, Yajl::Encoder.encode(hashtable))
170
+ @hashtable = hashtable
171
+
172
+ return result
173
+ rescue ::ZK::Exceptions::NoNode
174
+ @zk.mkdir_p(File.dirname(@path))
175
+ retry
176
+ end
177
+ end
178
+
179
+ def configure_watches
180
+ @register ||= @zk.register(@path) do
181
+ read
182
+ end
183
+
184
+ @on_connected ||= @zk.on_connected do
185
+ read
186
+ end
187
+
188
+ begin
189
+ read
190
+ rescue ::ZK::Exceptions::OperationTimeOut, ::Zookeeper::Exceptions::ContinuationTimeoutError, ::Zookeeper::Exceptions::NotConnected => e
191
+ # Ignore these, we'll get them next time
192
+
193
+ @on_error.call(e)
194
+ end
195
+ end
196
+ end
197
+ end
@@ -0,0 +1,17 @@
1
+ require 'rollout/zookeeper/distributed_hashtable'
2
+
3
+ module Rollout::Zookeeper
4
+ class Storage
5
+ def initialize(zk, path, options = {})
6
+ @cache = ::Rollout::Zookeeper::DistributedHashtable.new(zk, path, options)
7
+ end
8
+
9
+ def get(key)
10
+ @cache[key]
11
+ end
12
+
13
+ def set(key, value)
14
+ @cache[key] = value.to_s
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,71 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'rollout-zk'
16
+ s.version = '1.0.0'
17
+ s.date = '2013-09-10'
18
+
19
+ ## Make sure your summary is short. The description may be as long
20
+ ## as you like.
21
+ s.summary = "A ZooKeeper storage adapter for rollout."
22
+ s.description = "A ZooKeeper storage adapter for rollout."
23
+
24
+ ## List the primary authors. If there are a bunch of authors, it's probably
25
+ ## better to set the email to an email list or something. If you don't have
26
+ ## a custom homepage, consider using your GitHub URL or the like.
27
+ s.authors = ["Eric Lindvall"]
28
+ s.email = 'eric@papertrailapp.com'
29
+ s.homepage = 'https://github.com/papertrail/rollout-zk'
30
+
31
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
32
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
33
+ s.require_paths = %w[lib]
34
+
35
+
36
+ ## Specify any RDoc options here. You'll want to add your README and
37
+ ## LICENSE files to the extra_rdoc_files list.
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.extra_rdoc_files = %w[README.md LICENSE]
40
+
41
+ ## List your runtime dependencies here. Runtime dependencies are those
42
+ ## that are needed for an end user to actually USE your code.
43
+ s.add_dependency('rollout', [">= 2.0"])
44
+ s.add_dependency('zk')
45
+
46
+ ## List your development dependencies here. Development dependencies are
47
+ ## those that are only needed during development
48
+ # s.add_development_dependency('DEVDEPNAME', [">= 1.1.0", "< 2.0.0"])
49
+
50
+ ## Leave this section as-is. It will be automatically generated from the
51
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
52
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
53
+ # = MANIFEST =
54
+ s.files = %w[
55
+ Gemfile
56
+ LICENSE
57
+ README.md
58
+ Rakefile
59
+ lib/rollout-zk.rb
60
+ lib/rollout/zookeeper/distributed_hashtable.rb
61
+ lib/rollout/zookeeper/storage.rb
62
+ rollout-zk.gemspec
63
+ test/storage_test.rb
64
+ test/test_helper.rb
65
+ ]
66
+ # = MANIFEST =
67
+
68
+ ## Test files will be grabbed from the file list. Make sure the path glob
69
+ ## matches what you actually use.
70
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
71
+ end
File without changes
File without changes
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rollout-zk
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Eric Lindvall
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-09-10 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rollout
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ version: "2.0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: zk
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ version_requirements: *id002
45
+ description: A ZooKeeper storage adapter for rollout.
46
+ email: eric@papertrailapp.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - README.md
53
+ - LICENSE
54
+ files:
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - lib/rollout-zk.rb
60
+ - lib/rollout/zookeeper/distributed_hashtable.rb
61
+ - lib/rollout/zookeeper/storage.rb
62
+ - rollout-zk.gemspec
63
+ - test/storage_test.rb
64
+ - test/test_helper.rb
65
+ has_rdoc: true
66
+ homepage: https://github.com/papertrail/rollout-zk
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --charset=UTF-8
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.6
92
+ signing_key:
93
+ specification_version: 2
94
+ summary: A ZooKeeper storage adapter for rollout.
95
+ test_files:
96
+ - test/test_helper.rb