Class: Gizzard::Migrator

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

Constant Summary collapse

BALANCE_TOLERANCE =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(existing_map, config_templates, default_total_shards, config) ⇒ Migrator

Returns a new instance of Migrator.



23
24
25
26
27
28
29
30
31
32
# File 'lib/gizzard/migrator.rb', line 23

def initialize(existing_map, config_templates, default_total_shards, config)
  @configured_templates = config_templates
  @existing_map = existing_map

  @existing_templates = existing_map.keys
  @total_shards = @existing_map.values.map { |a| a.length }.inject { |a, b| a + b } || default_total_shards
  @config = config

  derive_changes
end

Instance Attribute Details

#configured_templatesObject (readonly)

Returns the value of attribute configured_templates.



18
19
20
# File 'lib/gizzard/migrator.rb', line 18

def configured_templates
  @configured_templates
end

#existing_mapObject (readonly)

Returns the value of attribute existing_map.



18
19
20
# File 'lib/gizzard/migrator.rb', line 18

def existing_map
  @existing_map
end

#existing_templatesObject (readonly)

Returns the value of attribute existing_templates.



18
19
20
# File 'lib/gizzard/migrator.rb', line 18

def existing_templates
  @existing_templates
end

#new_templatesObject (readonly)

populated via derive_changes



21
22
23
# File 'lib/gizzard/migrator.rb', line 21

def new_templates
  @new_templates
end

#similar_templatesObject (readonly)

populated via derive_changes



21
22
23
# File 'lib/gizzard/migrator.rb', line 21

def similar_templates
  @similar_templates
end

#total_shardsObject (readonly)

Returns the value of attribute total_shards.



18
19
20
# File 'lib/gizzard/migrator.rb', line 18

def total_shards
  @total_shards
end

#unchanged_templatesObject (readonly)

populated via derive_changes



21
22
23
# File 'lib/gizzard/migrator.rb', line 21

def unchanged_templates
  @unchanged_templates
end

#unrecognized_templatesObject (readonly)

populated via derive_changes



21
22
23
# File 'lib/gizzard/migrator.rb', line 21

def unrecognized_templates
  @unrecognized_templates
end

Instance Method Details

#cleanup!(nameserver) ⇒ Object



46
47
48
# File 'lib/gizzard/migrator.rb', line 46

def cleanup!(nameserver)
  transformations.each {|t| t.cleanup! nameserver, @config }
end

#copy!(nameserver) ⇒ Object



38
39
40
# File 'lib/gizzard/migrator.rb', line 38

def copy!(nameserver)
  transformations.each {|t| t.copy! nameserver, @config }
end

#prepare!(nameserver) ⇒ Object



34
35
36
# File 'lib/gizzard/migrator.rb', line 34

def prepare!(nameserver)
  transformations.each {|t| t.prepare! nameserver, @config }
end

#transformationsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gizzard/migrator.rb', line 50

def transformations
  return @transformations if @transformations

  # no changes
  return @transformations = [] if similar_templates.empty? and unrecognized_templates.empty? and new_templates.empty?

  configured_map = configured_templates.inject({}) {|h, t| h.update t => [] }

  @transformations = []

  if existing_templates.empty?
    # no forwardings exist, we must populate the forwarding index.
    forwardings = generate_new_forwardings(total_shards)

    # add the new shard ids to a member of the configured map. will
    # be rebalanced later.
    configured_map.values.first.concat forwardings.values

    @transformations << ForwardingTransformation.new(@config.table_id, forwardings.inject({}) {|f, (b, e)| f.update b => @config.shard_name(e) })
  end

  # map the unchanged templates straight over
  move_unchanged(existing_map, configured_map)

  # map similar templates over to their new versions
  move_similar(existing_map, configured_map)

  # move shards from unrecognized templates to new templates (or
  # existing ones)
  move_unrecognized_to_new(existing_map, configured_map)

  # rebalance
  rebalance_shards(configured_map)

  # transformation generation
  @transformations = generate_transformations(existing_map, configured_map) + @transformations
end

#wait_for_copies(nameserver) ⇒ Object



42
43
44
# File 'lib/gizzard/migrator.rb', line 42

def wait_for_copies(nameserver)
  transformations.each {|t| t.wait_for_copies nameserver, @config }
end