Skip to content

Commit

Permalink
basic round-robin strategy works
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Kallen committed Apr 16, 2009
1 parent cf1f569 commit 28de084
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 22 deletions.
26 changes: 15 additions & 11 deletions joke_cluster.rb 100644 → 100755
@@ -1,15 +1,19 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby


options = { require 'optparse'
:count => 10,
:base_port => 1000
}
OptionParser.new do |opts|
opts.on('-n', "--number COUNT", Integer) { |count| options[:count] = count }
opts.on('-p', "--port PORT", Integer) { |count| options[:port] = port }
end.parse!


options[:count].times do |count| begin
port = options[:base_port] + count $options = {
System.fork("./joke_server.rb -p#{port}") :count => 10,
:base_port => 10000
}
OptionParser.new do |opts|
opts.on('-n', "--number COUNT", Integer) { |count| $options[:count] = count }
opts.on('-p', "--port PORT", Integer) { |count| $options[:port] = port }
end.parse!
end

$options[:count].times do |count|
port = $options[:base_port] + count + 1
fork { exec("./joke_server.rb -p#{port}") }
end end
12 changes: 11 additions & 1 deletion joke_server.rb
Expand Up @@ -4,6 +4,16 @@
require 'eventmachine' require 'eventmachine'
require 'activesupport' require 'activesupport'
require 'statosaurus' require 'statosaurus'
require 'optparse'

begin
$options = {
:port => 10000
}
OptionParser.new do |opts|
opts.on('-p', "--port PORT", Integer) { |port| $options[:port] = port }
end.parse!
end


begin begin
logfile = File.join(File.dirname(__FILE__), 'log', File.basename(__FILE__) + '.log') logfile = File.join(File.dirname(__FILE__), 'log', File.basename(__FILE__) + '.log')
Expand All @@ -23,5 +33,5 @@ def receive_data(data)
end end


EM.run do EM.run do
EM.start_server "0.0.0.0", 10001, JokeServer EM.start_server "0.0.0.0", $options[:port], JokeServer
end end
119 changes: 119 additions & 0 deletions log/joke_server.rb.log
Expand Up @@ -1015,3 +1015,122 @@
2009-04-16T16:27:29-07:00 12361-1239924449-3298 0.100142 0.000100 0.000100 2009-04-16T16:27:29-07:00 12361-1239924449-3298 0.100142 0.000100 0.000100
2009-04-16T16:27:29-07:00 12361-1239924449-9976 0.100207 0.000100 0.000100 2009-04-16T16:27:29-07:00 12361-1239924449-9976 0.100207 0.000100 0.000100
2009-04-16T16:27:29-07:00 12361-1239924449-4605 0.100215 0.000100 0.000100 2009-04-16T16:27:29-07:00 12361-1239924449-4605 0.100215 0.000100 0.000100
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
2009-04-16T16:39:16-07:00 12846-1239925155-708 0.100338 0.000100 0.000100
2009-04-16T16:39:16-07:00 12846-1239925156-1278 0.100214 0.000100 0.000100
# Fields: job_user job_sys job_real
2009-04-16T16:39:30-07:00 12870-1239925170-2160 0.100368 0.000100 0.000100
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
2009-04-16T16:40:12-07:00 12895-1239925212-6214 0.100339 0.000100 0.000100
2009-04-16T16:40:15-07:00 12894-1239925215-4828 0.100345 0.000100 0.000100
2009-04-16T16:40:15-07:00 12894-1239925215-2173 0.100226 0.000100 0.000100
2009-04-16T16:40:33-07:00 12894-1239925233-3088 0.100229 0.000100 0.000100
2009-04-16T16:40:33-07:00 12894-1239925233-8899 0.100226 0.000100 0.000100
2009-04-16T16:40:37-07:00 12895-1239925237-4962 0.100226 0.000100 0.000100
2009-04-16T16:40:37-07:00 12895-1239925237-1147 0.100260 0.000100 0.000100
2009-04-16T16:41:26-07:00 12894-1239925286-6518 0.100212 0.000100 0.000100
2009-04-16T16:41:27-07:00 12894-1239925287-1483 0.100209 0.000100 0.000100
2009-04-16T16:41:31-07:00 12894-1239925291-8667 0.100153 0.000100 0.000100
2009-04-16T16:41:31-07:00 12894-1239925291-5620 0.100219 0.000100 0.000100
2009-04-16T16:41:31-07:00 12894-1239925291-4415 0.100219 0.000100 0.000100
2009-04-16T16:41:31-07:00 12894-1239925291-2708 0.100152 0.000100 0.000100
2009-04-16T16:41:32-07:00 12894-1239925291-8906 0.100138 0.000100 0.000100
2009-04-16T16:41:32-07:00 12894-1239925292-1911 0.100116 0.000100 0.000100
2009-04-16T16:41:32-07:00 12894-1239925292-1758 0.100220 0.000100 0.000100
2009-04-16T16:41:32-07:00 12894-1239925292-3637 0.100155 0.000100 0.000100
2009-04-16T16:41:32-07:00 12894-1239925292-9207 0.100225 0.000100 0.000100
2009-04-16T16:41:32-07:00 12894-1239925292-3630 0.100225 0.000100 0.000100
2009-04-16T16:41:32-07:00 12894-1239925292-2795 0.100224 0.000100 0.000100
2009-04-16T16:41:33-07:00 12894-1239925292-7685 0.100229 0.000100 0.000100
2009-04-16T16:41:33-07:00 12894-1239925293-5809 0.100225 0.000100 0.000100
2009-04-16T16:41:33-07:00 12894-1239925293-1905 0.100220 0.000100 0.000100
2009-04-16T16:41:33-07:00 12894-1239925293-3587 0.100162 0.000100 0.000100
2009-04-16T16:41:33-07:00 12894-1239925293-8865 0.100231 0.000100 0.000100
2009-04-16T16:41:33-07:00 12894-1239925293-2388 0.100231 0.000100 0.000100
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
2009-04-16T16:46:06-07:00 13011-1239925566-8856 0.100394 0.000100 0.000100
2009-04-16T16:46:06-07:00 13011-1239925566-1744 0.100225 0.000100 0.000100
2009-04-16T16:46:53-07:00 13011-1239925613-2870 0.100212 0.000100 0.000100
2009-04-16T16:46:53-07:00 13011-1239925613-2494 0.100187 0.000100 0.000100
2009-04-16T16:46:54-07:00 13011-1239925614-7975 0.100219 0.000100 0.000100
2009-04-16T16:47:01-07:00 13011-1239925621-8650 0.100230 0.000100 0.000100
2009-04-16T16:47:02-07:00 13011-1239925621-908 0.100213 0.000100 0.000100
2009-04-16T16:47:02-07:00 13011-1239925622-6468 0.100211 0.000100 0.000100
2009-04-16T16:47:02-07:00 13011-1239925622-9970 0.100220 0.000100 0.000100
2009-04-16T16:47:02-07:00 13011-1239925622-535 0.100212 0.000100 0.000100
2009-04-16T16:47:02-07:00 13011-1239925622-1591 0.100211 0.000100 0.000100
2009-04-16T16:47:02-07:00 13011-1239925622-6253 0.100240 0.000100 0.000100
2009-04-16T16:47:17-07:00 13011-1239925637-6547 0.100195 0.000100 0.000100
2009-04-16T16:47:17-07:00 13011-1239925637-3158 0.100210 0.000100 0.000100
2009-04-16T16:47:18-07:00 13011-1239925638-2852 0.100141 0.000100 0.000100
2009-04-16T16:47:18-07:00 13011-1239925638-9842 0.100227 0.000100 0.000100
2009-04-16T16:47:18-07:00 13011-1239925638-7160 0.100141 0.000100 0.000100
2009-04-16T16:47:18-07:00 13011-1239925638-6601 0.100230 0.000100 0.000100
2009-04-16T16:47:18-07:00 13011-1239925638-9143 0.100138 0.000100 0.000100
2009-04-16T16:48:11-07:00 13012-1239925691-2445 0.100336 0.000100 0.000100
2009-04-16T16:48:11-07:00 13013-1239925691-8829 0.100288 0.000100 0.000100
2009-04-16T16:48:15-07:00 13014-1239925695-7215 0.100323 0.000100 0.000100
2009-04-16T16:48:15-07:00 13015-1239925695-9791 0.100333 0.000100 0.000100
2009-04-16T16:48:15-07:00 13016-1239925695-5056 0.100319 0.000100 0.000100
2009-04-16T16:48:15-07:00 13017-1239925695-4129 0.100328 0.000100 0.000100
2009-04-16T16:48:16-07:00 13018-1239925695-5641 0.100329 0.000100 0.000100
2009-04-16T16:48:16-07:00 13019-1239925696-3832 0.100329 0.000100 0.000100
2009-04-16T16:48:16-07:00 13020-1239925696-5993 0.100254 0.000100 0.000100
2009-04-16T16:48:20-07:00 13011-1239925700-6747 0.100222 0.000100 0.000100
2009-04-16T16:48:20-07:00 13012-1239925700-8170 0.100224 0.000100 0.000100
2009-04-16T16:48:21-07:00 13013-1239925700-1664 0.100209 0.000100 0.000100
2009-04-16T16:48:21-07:00 13014-1239925701-9194 0.100210 0.000100 0.000100
2009-04-16T16:48:21-07:00 13015-1239925701-9609 0.100152 0.000100 0.000100
2009-04-16T16:48:21-07:00 13016-1239925701-19 0.100209 0.000100 0.000100
59 changes: 59 additions & 0 deletions log/proxy.rb.log
Expand Up @@ -1051,3 +1051,62 @@
2009-04-16T16:27:29-07:00 12557-1239924449-6347 0.100660 0.000100 0.000100 2009-04-16T16:27:29-07:00 12557-1239924449-6347 0.100660 0.000100 0.000100
2009-04-16T16:27:29-07:00 12557-1239924449-4902 0.100738 0.000100 0.000100 2009-04-16T16:27:29-07:00 12557-1239924449-4902 0.100738 0.000100 0.000100
2009-04-16T16:27:29-07:00 12557-1239924449-3981 0.100748 0.000100 0.000100 2009-04-16T16:27:29-07:00 12557-1239924449-3981 0.100748 0.000100 0.000100
# Fields: job_user job_sys job_real
2009-04-16T16:41:26-07:00 12957-1239925286-8835 0.101271 0.000100 0.000100
2009-04-16T16:41:27-07:00 12957-1239925287-234 0.100824 0.000100 0.000100
2009-04-16T16:41:31-07:00 12957-1239925291-2472 0.100781 0.000100 0.000100
2009-04-16T16:41:31-07:00 12957-1239925291-7186 0.100838 0.010000 0.000100
2009-04-16T16:41:31-07:00 12957-1239925291-5982 0.100797 0.000100 0.000100
2009-04-16T16:41:31-07:00 12957-1239925291-6320 0.100805 0.000100 0.000100
2009-04-16T16:41:32-07:00 12957-1239925291-5506 0.100718 0.000100 0.000100
2009-04-16T16:41:32-07:00 12957-1239925292-6261 0.100634 0.000100 0.000100
2009-04-16T16:41:32-07:00 12957-1239925292-2691 0.100844 0.000100 0.000100
2009-04-16T16:41:32-07:00 12957-1239925292-4712 0.100829 0.000100 0.000100
2009-04-16T16:41:32-07:00 12957-1239925292-3262 0.100909 0.000100 0.000100
2009-04-16T16:41:32-07:00 12957-1239925292-7012 0.100860 0.000100 0.000100
2009-04-16T16:41:32-07:00 12957-1239925292-3317 0.100862 0.000100 0.000100
2009-04-16T16:41:33-07:00 12957-1239925292-1327 0.100856 0.000100 0.000100
2009-04-16T16:41:33-07:00 12957-1239925293-4489 0.100876 0.000100 0.000100
2009-04-16T16:41:33-07:00 12957-1239925293-8423 0.100784 0.000100 0.000100
2009-04-16T16:41:33-07:00 12957-1239925293-264 0.100710 0.000100 0.000100
2009-04-16T16:41:33-07:00 12957-1239925293-2051 0.100791 0.000100 0.000100
2009-04-16T16:41:33-07:00 12957-1239925293-8061 0.100845 0.000100 0.000100
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
# Fields: job_user job_sys job_real
2009-04-16T16:46:06-07:00 13084-1239925566-1625 0.101503 0.000100 0.000100
2009-04-16T16:46:06-07:00 13084-1239925566-482 0.100900 0.000100 0.000100
# Fields: job_user job_sys job_real
2009-04-16T16:46:53-07:00 13098-1239925613-6409 0.101122 0.000100 0.000100
2009-04-16T16:46:53-07:00 13098-1239925613-2836 0.100761 0.000100 0.000100
2009-04-16T16:46:54-07:00 13098-1239925614-8939 0.100772 0.000100 0.000100
2009-04-16T16:47:01-07:00 13098-1239925621-3879 0.100803 0.000100 0.000100
2009-04-16T16:47:02-07:00 13098-1239925621-9017 0.100876 0.000100 0.000100
2009-04-16T16:47:02-07:00 13098-1239925622-6255 0.100838 0.000100 0.000100
2009-04-16T16:47:02-07:00 13098-1239925622-9546 0.100874 0.000100 0.000100
2009-04-16T16:47:02-07:00 13098-1239925622-7153 0.100819 0.000100 0.000100
2009-04-16T16:47:02-07:00 13098-1239925622-4154 0.100833 0.000100 0.000100
2009-04-16T16:47:02-07:00 13098-1239925622-1997 0.100876 0.000100 0.000100
2009-04-16T16:47:17-07:00 13098-1239925637-8284 0.100784 0.000100 0.000100
2009-04-16T16:47:17-07:00 13098-1239925637-2968 0.100832 0.000100 0.000100
2009-04-16T16:47:18-07:00 13098-1239925638-2824 0.100779 0.000100 0.000100
2009-04-16T16:47:18-07:00 13098-1239925638-7028 0.101075 0.000100 0.000100
2009-04-16T16:47:18-07:00 13098-1239925638-6971 0.100683 0.000100 0.000100
2009-04-16T16:47:18-07:00 13098-1239925638-6240 0.100881 0.000100 0.000100
2009-04-16T16:47:18-07:00 13098-1239925638-8436 0.100833 0.000100 0.000100
# Fields: job_user job_sys job_real
2009-04-16T16:48:11-07:00 13116-1239925691-63 0.102476 0.000100 0.000100
2009-04-16T16:48:11-07:00 13116-1239925691-4691 0.101021 0.000100 0.000100
2009-04-16T16:48:15-07:00 13116-1239925695-8327 0.101195 0.000100 0.000100
2009-04-16T16:48:15-07:00 13116-1239925695-5897 0.101188 0.000100 0.000100
2009-04-16T16:48:15-07:00 13116-1239925695-574 0.101152 0.000100 0.000100
2009-04-16T16:48:15-07:00 13116-1239925695-737 0.101181 0.000100 0.000100
2009-04-16T16:48:16-07:00 13116-1239925695-6931 0.101162 0.000100 0.000100
2009-04-16T16:48:16-07:00 13116-1239925696-565 0.101141 0.000100 0.000100
2009-04-16T16:48:16-07:00 13116-1239925696-3873 0.101114 0.000100 0.000100
2009-04-16T16:48:20-07:00 13116-1239925700-5869 0.100864 0.000100 0.000100
2009-04-16T16:48:20-07:00 13116-1239925700-5325 0.100883 0.000100 0.000100
2009-04-16T16:48:21-07:00 13116-1239925700-1977 0.100956 0.000100 0.000100
2009-04-16T16:48:21-07:00 13116-1239925701-9921 0.100853 0.000100 0.000100
2009-04-16T16:48:21-07:00 13116-1239925701-3803 0.100742 0.000100 0.000100
2009-04-16T16:48:21-07:00 13116-1239925701-6483 0.101034 0.000100 0.010000
11 changes: 5 additions & 6 deletions proxy.rb
@@ -1,20 +1,19 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby


['rubygems', 'activesupport', 'eventmachine', 'socket', 'optparse', 'statosaurus'].each { |dependency| require dependency } ['rubygems', 'activesupport', 'eventmachine', 'socket', 'optparse', 'statosaurus'].each { |dependency| require dependency }
require 'proxy/server' ['proxy/server', 'proxy/balancers/first', 'proxy/balancers/round_robin'].each { |dependency| require dependency }
require 'proxy/balancers/first'


begin begin
$options = { $options = {
:balancer => First, :balancer => First,
:port => 10000, :port => 10000,
:count => 1, :count => 10,
:host => "0.0.0.0" :host => "0.0.0.0"
} }
OptionParser.new do |opts| OptionParser.new do |opts|
opts.on('-s', "--balancer BALANCER", String) { |balancer| $options[:balancer] ||= balancer.constantize } opts.on('-b', "--balancer BALANCER", String) { |balancer| $options[:balancer] = balancer.constantize }
opts.on('-n', "--number COUNT", Integer) { |count| $options[:count] ||= count } opts.on('-n', "--number COUNT", Integer) { |count| $options[:count] = count }
opts.on('-p', "--port PORT", Integer) { |port| $options[:port] ||= port } opts.on('-p', "--port PORT", Integer) { |port| $options[:port] = port }
end.parse! end.parse!
end end


Expand Down
4 changes: 0 additions & 4 deletions proxy/balancers/first.rb
@@ -1,10 +1,6 @@
require 'proxy/balancers/balancer' require 'proxy/balancers/balancer'


class First < Balancer class First < Balancer
def initialize(servers)
@servers = servers
end

def forward(data) def forward(data)
servers.first.call(data) servers.first.call(data)
end end
Expand Down
7 changes: 7 additions & 0 deletions proxy/balancers/random.rb
@@ -0,0 +1,7 @@
require 'proxy/balancers/balancer'

class Random < Balancer
def forward(data)
servers.rand.call(data)
end
end
16 changes: 16 additions & 0 deletions proxy/balancers/round_robin.rb
@@ -0,0 +1,16 @@
require 'proxy/balancers/balancer'

class RoundRobin < Balancer
def forward(data)
next_server.call(data)
end

private
def next_server
Thread.exclusive do
@current ||= 0
@current = (@current + 1) % servers.size
servers[@current]
end
end
end

0 comments on commit 28de084

Please sign in to comment.