Skip to content
Browse files

timeout code

  • Loading branch information...
1 parent 6a30845 commit 131c673529b785df16b2616303dddc85f3159073 Kyle Maxwell committed Oct 6, 2010
Showing with 101 additions and 20 deletions.
  1. +69 −15 lib/gizzard/commands.rb
  2. +32 −5 lib/gizzmo.rb
View
84 lib/gizzard/commands.rb
@@ -1,4 +1,5 @@
require "pp"
+require "digest/md5"
module Gizzard
class Command
include Thrift
@@ -390,24 +391,77 @@ def run
class ReportCommand < ShardCommand
def run
- regex = @argv.first
- help!("regex is a required option") unless regex
- regex = Regexp.compile(regex)
- service.list_hostnames.map do |host|
- puts host
- counts = {}
- service.shards_for_hostname(host).each do |shard|
- id = shard.id.to_unix
- if key = id[regex, 1] || id[regex, 0]
- counts[key] ||= 0
- counts[key] += 1
- end
- end
- counts.sort.each do |k, v|
- puts " %3d %s" % [v, k]
+ things = @argv.map do |shard|
+ parse(down(ShardId.parse(shard))).join("\n")
+ end
+
+ group(things).each do |string, things|
+ puts "=== " + ::Digest::MD5.hexdigest(string)[0..10] + ": #{things.length}" + " ===================="
+ puts string
+ end
+
+ # def run
+ # @roots = []
+ # argv.each do |arg|
+ # @id = ShardId.parse(arg)
+ # @roots += roots_of(@id)
+ # end
+ # @roots.uniq.each do |root|
+ # output root.to_unix
+ # down(root, 1)
+ # end
+ # end
+ #
+ # def roots_of(id)
+ # links = service.list_upward_links(id)
+ # if links.empty?
+ # [id]
+ # else
+ # links.map { |link| roots_of(link.up_id) }.flatten
+ # end
+ # end
+ #
+ # def down(id, depth = 0)
+ # service.list_downward_links(id).map do |link|
+ # printable = " " * depth + link.down_id.to_unix
+ # output printable
+ # down(link.down_id, depth + 1)
+ # end
+ # end
+ end
+
+ def group(arr)
+ arr.inject({}) do |m, e|
+ m[e] ||= []
+ m[e] << e
+ m
+ end
+ end
+
+ def parse(obj, id = nil, depth = 0)
+ case obj
+ when Hash
+ id, prefix = parse(obj.keys.first, id, depth)
+ [prefix] + parse(obj.values.first, id, depth + 1)
+ when String
+ host, prefix = obj.split("/")
+ host = "db" if host != "localhost"
+ id ||= prefix[/\w+ward_\d+_\d+/]
+ prefix = (" " * depth) + host + "/" + prefix.sub(id, "[ID]")
+ [id, prefix]
+ when Array
+ obj.map do |e|
+ parse e, id, depth
end
end
end
+
+ def down(id)
+ vals = service.list_downward_links(id).map do |link|
+ down(link.down_id)
+ end
+ {id.to_unix => vals}
+ end
end
class FindCommand < ShardCommand
View
37 lib/gizzmo.rb
@@ -78,10 +78,6 @@ def separators(opts, string)
opts.banner = "Usage: #{zero} wrap CLASS_NAME SHARD_ID_TO_WRAP [MORE SHARD_IDS...]"
separators(opts, DOC_STRINGS["wrap"])
end,
- 'report' => OptionParser.new do |opts|
- opts.banner = "Usage: #{zero} report RUBY_REGEX"
- separators(opts, DOC_STRINGS["report"])
- end,
'rebalance' => OptionParser.new do |opts|
opts.banner = "Usage: #{zero} rebalance"
separators(opts, DOC_STRINGS["rebalance"])
@@ -174,6 +170,12 @@ def separators(opts, string)
opts.banner = "Usage: #{zero} unlink PARENT_SHARD_ID CHILD_SHARD_ID"
separators(opts, DOC_STRINGS["unlink"])
end,
+
+ 'report' => OptionParser.new do |opts|
+ opts.banner = "Usage: #{zero} report"
+ separators(opts, DOC_STRINGS["report"])
+ end,
+
'lookup' => OptionParser.new do |opts|
opts.banner = "Usage: #{zero} lookup [options] TABLE_ID SOURCE"
separators(opts, DOC_STRINGS["lookup"])
@@ -254,6 +256,10 @@ def separators(opts, string)
opts.on("-r", "--retry=TIMES", "TIMES to retry the command") do |r|
global_options.retry = r
end
+
+ opts.on("-t", "--timeout=SECONDS", "SECONDS to let the command run") do |r|
+ global_options.timeout = r
+ end
opts.on("--subtree", "Render in subtree mode") do
global_options.render << "subtree"
@@ -328,11 +334,32 @@ def process_nested_parsers(global, subcommands)
argv << line.strip
end
+def custom_timeout(seconds)
+ if seconds
+ begin
+ require "rubygems"
+ require "system_timer"
+ SystemTimer.timeout_after(seconds) do
+ yield
+ end
+ rescue LoadError
+ require "timeout"
+ Timeout.timeout(seconds) do
+ yield
+ end
+ end
+ else
+ yield
+ end
+end
+
tries_left = global_options.retry.to_i + 1
begin
while (tries_left -= 1) >= 0
begin
- Gizzard::Command.run(subcommand_name, global_options, argv, subcommand_options, log)
+ custom_timeout(global_options.timeout) do
+ Gizzard::Command.run(subcommand_name, global_options, argv, subcommand_options, log)
+ end
break
rescue
if tries_left > 0

0 comments on commit 131c673

Please sign in to comment.
Something went wrong with that request. Please try again.