alicorn 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "shoulda", "~> 3.0.1"
5
+ gem "rdoc", "~> 3.12"
6
+ gem "bundler", "~> 1.1.3"
7
+ gem "jeweler", "~> 1.8.3"
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.8.3)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rdoc
10
+ json (1.7.1)
11
+ rake (0.9.2.2)
12
+ rdoc (3.12)
13
+ json (~> 1.4)
14
+ shoulda (3.0.1)
15
+ shoulda-context (~> 1.0.0)
16
+ shoulda-matchers (~> 1.0.0)
17
+ shoulda-context (1.0.0)
18
+ shoulda-matchers (1.0.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bundler (~> 1.1.3)
25
+ jeweler (~> 1.8.3)
26
+ rdoc (~> 3.12)
27
+ shoulda (~> 3.0.1)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Ben Somers
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = alicorn
2
+
3
+ An experimental auto-scaler for Eric Wong's {unicorn}[http://unicorn.bogomips.org] webserver.
4
+
5
+ == Contributing to alicorn
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Ben Somers. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "alicorn"
18
+ gem.homepage = "http://github.com/bensomers/alicorn"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{standalone auto-scaler for unicorn}
21
+ gem.description = %Q{highly configurable dumb auto-scaler for managing unicorn web servers}
22
+ gem.email = "somers.ben@gmail.com"
23
+ gem.authors = ["Ben Somers"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ # require 'rcov/rcovtask'
36
+ # Rcov::RcovTask.new do |test|
37
+ # test.libs << 'test'
38
+ # test.pattern = 'test/**/test_*.rb'
39
+ # test.verbose = true
40
+ # test.rcov_opts << '--exclude "gems/*"'
41
+ # end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "alicorn #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/.gitkeep ADDED
File without changes
data/bin/alicorn ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ $:.push File.join(File.dirname(__FILE__),'..','lib')
3
+ require 'optparse'
4
+ require 'alicorn'
5
+
6
+ options = {}
7
+ OptionParser.new do |opts|
8
+ opts.banner = "Alicorn is a tool for auto-scaling unicorn worker counts.
9
+ It relies on unicorn's built-in Raindrops middleware (so make sure you have
10
+ enabled it). It computes rough load scores based on separately averaged
11
+ data sets. , \n\nUsage: alicorn [options]"
12
+ opts.on( '-h', '--help', 'Display this screen' ) do
13
+ puts opts
14
+ exit
15
+ end
16
+
17
+ opts.on("--min-workers N", Integer, "the minimum number of workers to scale down to") do |v|
18
+ options[:min_workers] = v
19
+ end
20
+
21
+ opts.on("--max-workers N", Integer, "the maximum number of workers to scale up to") do |v|
22
+ options[:max_workers] = v
23
+ end
24
+
25
+ opts.on("--upward-step-size N", Integer, "the number of workers to add when scaling up") do |v|
26
+ options[:upward_step_size] = v
27
+ end
28
+
29
+ opts.on("--downward-step-size N", Integer, "the number of workers to remove when scaling down") do |v|
30
+ options[:downward_step_size] = v
31
+ end
32
+
33
+ opts.on("--upward-threshold (0-1)", Float, "the proportion of busy workers to trigger an upscale") do |v|
34
+ options[:upward_threshold] = v
35
+ end
36
+
37
+ opts.on("--downward-threshold (0-1)", Float, "the proportion of busy workers to trigger a downscale") do |v|
38
+ options[:downward_threshold] = v
39
+ end
40
+
41
+ opts.on("--url S", String, "raindrops URL to check, defaults to 127.0.0.1/_raindrops") do |v|
42
+ options[:url] = v
43
+ end
44
+
45
+ opts.on("--sample-count N", Integer, "number of data points to check before scaling") do |v|
46
+ options[:sample_count] = v
47
+ end
48
+
49
+ opts.on("--delay (seconds)", Float, "delay between checks") do |v|
50
+ options[:delay] = v
51
+ end
52
+
53
+ end.parse!
54
+ Alicorn::Scaler.new(options).scale!
data/lib/alicorn.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Alicorn; end
2
+
3
+ require 'alicorn/scaler'
@@ -0,0 +1,14 @@
1
+ class DataSet < Array
2
+ def avg
3
+ inject(:+) / size.to_i
4
+ end
5
+
6
+ def variance
7
+ sum=self.inject(0){|acc,i|acc +(i-avg)**2}
8
+ return(1/self.length.to_f*sum)
9
+ end
10
+
11
+ def stddev
12
+ Math.sqrt(self.variance)
13
+ end
14
+ end
@@ -0,0 +1,85 @@
1
+ require 'curl'
2
+ require 'alicorn/dataset'
3
+
4
+ class Alicorn::Scaler
5
+ attr_accessor :min_workers, :max_workers, :upward_step_size,
6
+ :downward_step_size, :raindrops_url, :delay, :sample_count, :app_name,
7
+ :master_pid, :worker_count, :calling, :writing, :active, :queued
8
+
9
+ def initialize(options)
10
+ self.min_workers = options[:min_workers] || 1
11
+ self.max_workers = options[:max_workers]
12
+ self.upward_threshold = options[:upward_threshold]
13
+ self.downward_threshold = options[:downward_threshold]
14
+ self.upward_step_size = options[:upward_step_size] || 1
15
+ self.downward_step_size = options[:downward_step_size] || 1
16
+ self.raindrops_url = options[:url] || "http://127.0.0.1/_raindrops"
17
+ self.delay = options[:delay] || 1
18
+ self.sample_count = options[:sample_count] || 30
19
+ self.app_name = options[:app_name] || "unicorn"
20
+ self.master_pid = find_master_pid(app_name)
21
+ self.worker_count = find_worker_count(app_name)
22
+ self.calling = DataSet.new
23
+ self.writing = DataSet.new
24
+ self.active = DataSet.new
25
+ self.queued = DataSet.new
26
+ end
27
+
28
+ def scale!
29
+ collect_data
30
+ upscale or downscale
31
+ end
32
+
33
+ protected
34
+
35
+ # planned algorithm: maintain worker count at 1.3*active
36
+ def upscale
37
+ return false if worker_count >= max_workers
38
+ return false if calling.all? { |sample| sample == 0 }
39
+ end
40
+
41
+ def downscale
42
+ return false if worker_count <= min_workers
43
+ end
44
+
45
+ private
46
+
47
+ def find_master_pid(app_name)
48
+ end
49
+
50
+ def find_worker_count(app_name)
51
+ end
52
+
53
+ def collect_data
54
+ p "Sampling #{sample_count} times"
55
+ sample_count.times do
56
+ raindrops = get_raindrops(raindrops_url)
57
+ calling << $1.to_i if raindrops.detect { |line| line.match(/calling: ([0-9]+)/) }
58
+ writing << $1.to_i if raindrops.detect { |line| line.match(/writing: ([0-9]+)/) }
59
+ active << $1.to_i if raindrops.detect { |line| line.match(/active: ([0-9]+)/) }
60
+ queued << $1.to_i if raindrops.detect { |line| line.match(/queued: ([0-9]+)/) }
61
+ sleep(delay)
62
+ end
63
+ p "Collected:"
64
+ p "calling:#{calling}"
65
+ p "calling avg:#{calling.avg}"
66
+ p "calling stddev:#{calling.stddev}"
67
+ p "writing:#{writing}"
68
+ p "writing avg:#{writing.avg}"
69
+ p "writing stddev:#{writing.stddev}"
70
+ p "active:#{active}"
71
+ p "active avg:#{active.avg}"
72
+ p "active stddev:#{active.stddev}"
73
+ p "queued:#{queued}"
74
+ p "queued avg:#{queued.avg}"
75
+ p "queued stddev:#{queued.stddev}"
76
+ end
77
+
78
+ def get_raindrops(url)
79
+ Curl::Easy.http_get(url).body_str.split("\n")
80
+ end
81
+
82
+ def send_signal(sig)
83
+ Process.kill(sig, master_pid)
84
+ end
85
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'unicorn_rider'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestUnicornRider < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alicorn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben Somers
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda
16
+ requirement: &70216366818240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.1
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70216366818240
25
+ - !ruby/object:Gem::Dependency
26
+ name: rdoc
27
+ requirement: &70216366817760 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.12'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70216366817760
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &70216366817280 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.1.3
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70216366817280
47
+ - !ruby/object:Gem::Dependency
48
+ name: jeweler
49
+ requirement: &70216366833140 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.8.3
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70216366833140
58
+ description: highly configurable dumb auto-scaler for managing unicorn web servers
59
+ email: somers.ben@gmail.com
60
+ executables:
61
+ - .gitkeep
62
+ - alicorn
63
+ extensions: []
64
+ extra_rdoc_files:
65
+ - LICENSE.txt
66
+ - README.rdoc
67
+ files:
68
+ - .document
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - LICENSE.txt
72
+ - README.rdoc
73
+ - Rakefile
74
+ - VERSION
75
+ - bin/.gitkeep
76
+ - bin/alicorn
77
+ - lib/alicorn.rb
78
+ - lib/alicorn/dataset.rb
79
+ - lib/alicorn/scaler.rb
80
+ - test/helper.rb
81
+ - test/test_alicorn.rb
82
+ homepage: http://github.com/bensomers/alicorn
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.17
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: standalone auto-scaler for unicorn
107
+ test_files: []