Class: Gizzard::AddPartitionCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/gizzard/commands.rb

Instance Attribute Summary

Attributes inherited from Command

#argv, #buffer, #command_options, #global_options, #job_injector, #manager

Instance Method Summary collapse

Methods inherited from Command

classify, #confirm!, #get_base_name, #help!, #initialize, make_job_injector, make_manager, #output, #require_tables, #require_template_options, run

Constructor Details

This class inherits a constructor from Gizzard::Command

Instance Method Details

#runObject



937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
# File 'lib/gizzard/commands.rb', line 937

def run
  require_tables
  require_template_options

  scheduler_options = command_options.scheduler_options || {}
  manifest          = manager.manifest(*global_options.tables)
  copy_wrapper      = scheduler_options[:copy_wrapper]
  be_quiet          = global_options.force && command_options.quiet
  transformations   = {}

  scheduler_options[:quiet] = be_quiet

  puts "Note: All partitions, including existing ones, will be weighted evenly." unless be_quiet

  add_templates_and_weights = {}

  @argv.each do |template_s|
    to     = ShardTemplate.parse(template_s)

    add_templates_and_weights[to] = ShardTemplate::DEFAULT_WEIGHT
  end

  orig_templates_and_weights = manifest.templates.inject({}) do |h, (template, forwardings)|
    h[template] = ShardTemplate::DEFAULT_WEIGHT; h
  end

  dest_templates_and_weights = orig_templates_and_weights.merge(add_templates_and_weights)

  transformations = global_options.tables.inject({}) do |all, table|
    trees      = manifest.trees.reject {|(f, s)| f.table_id != table }
    rebalancer = Rebalancer.new(trees, dest_templates_and_weights, copy_wrapper)

    all.update(rebalancer.transformations) {|t,a,b| a.merge b }
  end

  if transformations.empty?
    puts "Nothing to do!"
    exit
  end

  base_name = get_base_name(transformations)

  unless be_quiet
    transformations.each do |transformation, trees|
      puts transformation.inspect
      puts "Applied to #{trees.length} shards"
      #trees.keys.sort.each {|f| puts "  #{f.inspect}" }
    end
    puts ""
  end

  confirm!

  Gizzard.schedule! manager,
                    base_name,
                    transformations,
                    scheduler_options
end