Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for grouping cluster restarts #308

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions lib/thin/controllers/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Controllers
class Cluster < Controller
# Cluster only options that should not be passed in the command sent
# to the indiviual servers.
CLUSTER_OPTIONS = [:servers, :only, :onebyone, :wait]
CLUSTER_OPTIONS = [:servers, :only, :onebyone, :xbyx, :wait]

# Maximum wait time for the server to be restarted
DEFAULT_WAIT_TIME = 30 # seconds
Expand All @@ -32,6 +32,7 @@ def log_file; @options[:log] end
def size; @options[:servers] end
def only; @options[:only] end
def onebyone; @options[:onebyone] end
def xbyx; @options[:xbyx] end
def wait; @options[:wait] end

def swiftiply?
Expand Down Expand Up @@ -64,18 +65,41 @@ def stop_server(number)

# Stop and start the servers.
def restart
unless onebyone
# Let's do a normal restart by defaults
stop
sleep 0.1 # Let's breath a bit shall we ?
start
else
if onebyone
#Stop/Start each server, on by one
with_each_server do |n|
stop_server(n)
sleep 0.1 # Let's breath a bit shall we ?
start_server(n)
wait_until_server_started(n)
end
elsif !xbyx.nil? and xbyx > 0
#Stop up to xbyx servers at a time in the cluster, then start them.
#Repeat until all servers have been restarted. Allows us to speed up large clusters while maintaining service
q=[]
#build total queue in reverse order so we can pop servers off the end
with_each_server do |n|
q=[n]+q
end
while q.length > 0
bq=[]
#build our batch queue to process
while bq.length < xbyx and q.length > 0
bq << q.pop
end
bq.each do |server|
stop_server(server)
end
sleep 0.1 #if xbyx is small give just a moment for process to finish
bq.each do |server|
start_server(server)
end
end
else
# Let's do a normal restart by default
stop
sleep 0.1 # Let's breath a bit shall we ?
start
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/thin/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def parser
opts.on("-C", "--config FILE", "Load options from config file") { |file| @options[:config] = file }
opts.on( "--all [DIR]", "Send command to each config files in DIR") { |dir| @options[:all] = dir } if Thin.linux?
opts.on("-O", "--onebyone", "Restart the cluster one by one (only works with restart command)") { @options[:onebyone] = true }
opts.on("-X", "--xbyx NUM", "Restart the cluster in batches up to X servers at a time (only works with restart command)") { |num| @options[:xbyx] = num.to_i }
opts.on("-w", "--wait NUM", "Maximum wait time for server to be started in seconds (use with -O)") { |time| @options[:wait] = time.to_i }
end

Expand Down