Class: Lhm::Invoker

Inherits:
Object
  • Object
show all
Includes:
SqlHelper
Defined in:
lib/lhm/invoker.rb

Overview

Copies an origin table to an altered destination table. Live activity is synchronized into the destination table using triggers.

Once the origin and destination tables have converged, origin is archived and replaced by destination.

Constant Summary collapse

LOCK_WAIT_TIMEOUT_DELTA =
10
INNODB_LOCK_WAIT_TIMEOUT_MAX =
1073741824.freeze
LOCK_WAIT_TIMEOUT_MAX =
31536000.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SqlHelper

#annotation, #idx_name, #idx_spec, #tagged, #version_string

Constructor Details

#initialize(origin, connection) ⇒ Invoker

Returns a new instance of Invoker.



24
25
26
27
# File 'lib/lhm/invoker.rb', line 24

def initialize(origin, connection)
  @connection = connection
  @migrator = Migrator.new(origin, connection)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



22
23
24
# File 'lib/lhm/invoker.rb', line 22

def connection
  @connection
end

#migratorObject (readonly)

Returns the value of attribute migrator.



22
23
24
# File 'lib/lhm/invoker.rb', line 22

def migrator
  @migrator
end

Instance Method Details

#run(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lhm/invoker.rb', line 48

def run(options = {})
  normalize_options(options)
  set_session_lock_wait_timeouts
  migration = @migrator.run
  entangler = Entangler.new(migration, @connection)

  entangler.run do
    options[:verifier] ||= Proc.new { |conn| triggers_still_exist?(conn, entangler) }
    Chunker.new(migration, @connection, options).run
    raise "Required triggers do not exist" unless triggers_still_exist?(@connection, entangler)
    if options[:atomic_switch]
      AtomicSwitcher.new(migration, @connection).run
    else
      LockedSwitcher.new(migration, @connection).run
    end
  end
end

#set_session_lock_wait_timeoutsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lhm/invoker.rb', line 29

def set_session_lock_wait_timeouts
  global_innodb_lock_wait_timeout = @connection.select_one("SHOW GLOBAL VARIABLES LIKE 'innodb_lock_wait_timeout'")
  global_lock_wait_timeout = @connection.select_one("SHOW GLOBAL VARIABLES LIKE 'lock_wait_timeout'")

  if global_innodb_lock_wait_timeout
    desired_innodb_lock_wait_timeout = global_innodb_lock_wait_timeout['Value'].to_i + LOCK_WAIT_TIMEOUT_DELTA
    if desired_innodb_lock_wait_timeout <= INNODB_LOCK_WAIT_TIMEOUT_MAX
      @connection.execute("SET SESSION innodb_lock_wait_timeout=#{desired_innodb_lock_wait_timeout}")
    end
  end

  if global_lock_wait_timeout
    desired_lock_wait_timeout = global_lock_wait_timeout['Value'].to_i + LOCK_WAIT_TIMEOUT_DELTA
    if desired_lock_wait_timeout <= LOCK_WAIT_TIMEOUT_MAX
      @connection.execute("SET SESSION lock_wait_timeout=#{desired_lock_wait_timeout}")
    end
  end
end

#triggers_still_exist?(conn, entangler) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/lhm/invoker.rb', line 66

def triggers_still_exist?(conn, entangler)
  triggers = conn.select_values("SHOW TRIGGERS LIKE '%#{migrator.origin.name}'").select { |name| name =~ /^lhmt/ }
  triggers.sort == entangler.expected_triggers.sort
end