Class: Tableflip::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/tableflip/executor.rb

Defined Under Namespace

Classes: BinaryString

Instance Method Summary collapse

Constructor Details

#initialize(strategy) ⇒ Executor

Instance Methods =====================================================



7
8
9
10
11
# File 'lib/tableflip/executor.rb', line 7

def initialize(strategy)
  @strategy = strategy

  @time_format = '%Y-%m-%d %H:%M:%S'
end

Instance Method Details

#awaitObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tableflip/executor.rb', line 17

def await
  @await ||= Hash.new { |h, k| h[k] = [ ] }

  fibers = @await[Fiber.current]

  fibers << Fiber.current

  yield if (block_given?)

  fibers.delete(Fiber.current)

  while (fibers.any?)
    Fiber.yield
  end
end

#deferObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tableflip/executor.rb', line 33

def defer
  parent_fiber = Fiber.current

  fibers = @await[parent_fiber]

  fiber = Fiber.new do
    yield if (block_given?)

    fibers.delete(Fiber.current)

    parent_fiber.resume
  end

  fibers << fiber

  EventMachine.next_tick do
    fiber.resume
  end
end

#do_query(db, query, *values) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/tableflip/executor.rb', line 161

def do_query(db, query, *values)
  fiber = Fiber.current
  query = query.gsub('?') do |s|
    escaper(db, values.shift)
  end

  if (@strategy.debug_queries?)
    puts "SQL> #{query}"
  end

  completed = false

  while (!completed)
    begin
      deferred = db.query(query)

      deferred.callback do |result|
        EventMachine.next_tick do
          completed = true

          fiber.resume(result)
        end
      end

      deferred.errback do |err|
        EventMachine.next_tick do
          completed = true

          fiber.resume(err)
        end
      end

      case (response = Fiber.yield)
      when Exception
        raise response
      else
        return response
      end

    rescue Mysql2::Error => e
      if (e.to_s.match(/MySQL server has gone away/))
        # Ignore
      else
        raise e
      end
    end
  end
end

#escaper(db, value) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/tableflip/executor.rb', line 142

def escaper(db, value)
  case (value)
  when nil
    'NULL'
  when BinaryString
    "0x%s" % value.unpack("H*")
  when Fixnum
    value
  when Date
    '"' + db.escape(value.strftime('%Y-%m-%d')) + '"'
  when DateTime, Time
    '"' + db.escape(value.utc.strftime('%Y-%m-%d %H:%M:%S')) + '"'
  when Array
    value.collect { |v| escaper(db, v) }.join(',')
  else
    '"' + db.escape(value.to_s) + '"'
  end
end

#execute!Object



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tableflip/executor.rb', line 53

def execute!
  require 'eventmachine'
  require 'em-synchrony'

  if (@strategy.message)
    puts @strategy.message
    exit(0)
  end

  tables = { }

  EventMachine.synchrony do
    if (@strategy.tables.include?(:__all__))
      source_db = Tableflip::DatabaseHandle.connect(
        @strategy.source_env,
        :encoding => @strategy.encoding
      )

      @strategy.tables.delete(:__all__)

      result = do_query(source_db, "SHOW TABLES")

      result.each do |row|
        table_name = row.first[1]

        case (table_name)
        when 'schema_migrations', /__changes/
          next
        end

        @strategy.tables << table_name
      end
    end

    await do
      @strategy.tables.each do |table|
        defer do
          queue = @strategy.actions.dup

          table_config = tables[table] = {
            :table => table,
            :queue => queue
          }
              
          while (action = queue.shift)
            log("#{table} [#{action}]")

            source_db = Tableflip::DatabaseHandle.connect(
              @strategy.source_env,
              :encoding => @strategy.encoding
            )

            case (action)
            when :tracking_add
              tracking_add(source_db, table_config)
            when :tracking_remove
              tracking_remove(source_db, table_config)
            when :tracking_seed
              tracking_seed(source_db, table_config)
            when :table_migrate
              @strategy.complete = false

              target_db = Tableflip::DatabaseHandle.connect(
                @strategy.target_env,
                :encoding => @strategy.encoding
              )
              table_migrate(source_db, target_db, table_config)
            when :table_report_status
              target_db = Tableflip::DatabaseHandle.connect(
                @strategy.target_env,
                :encoding => @strategy.encoding
              )
              table_report_status(source_db, target_db, table_config)
            when :table_count
              table_count(source_db, target_db, table_config)
            when :table_create_test
              table_create_test(source_db, table_config)
            when :table_fuzz
              table_fuzz(source_db, table_config, @strategy.fuzz_intensity)
            end
          end
        end
      end
    end

    EventMachine.stop_event_loop
  end
end

#log(message) ⇒ Object



13
14
15
# File 'lib/tableflip/executor.rb', line 13

def log(message)
  puts "[%s] %s" % [ Time.now.strftime(@time_format), message ]
end

#table_create_test(db, table_config) ⇒ Object



397
398
399
400
401
402
403
# File 'lib/tableflip/executor.rb', line 397

def table_create_test(db, table_config)
  table = table_config[:table]

  do_query(db, "CREATE TABLE `#{table}` (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), created_at DATETIME, updated_at DATETIME)")
rescue Mysql2::Error => e
  puts e.to_s
end

#table_exists?(db, table) ⇒ Boolean

Returns:

  • (Boolean)


210
211
212
213
214
215
216
217
# File 'lib/tableflip/executor.rb', line 210

def table_exists?(db, table)
  do_query(db, "SHOW FIELDS FROM `#{table}`")

  true

rescue Mysql2::Error
  false
end

#table_fuzz(db, table_config, count) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/tableflip/executor.rb', line 405

def table_fuzz(db, table_config, count)
  require 'securerandom'

  table = table_config[:table]

  EventMachine::PeriodicTimer.new(1) do
    unless (@inserting)
      @inserting = true

      Fiber.new do
        now = Time.now.utc.strftime('%Y-%m-%d %H:%M:%S')

        log("Adding #{count} rows to #{table}")

        count.times do
          do_query(db,
            "INSERT IGNORE INTO `#{table}` (id, name, created_at, updated_at) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE name=VALUES(name), updated_at=VALUES(updated_at)",
            SecureRandom.random_number(1<<20),
            SecureRandom.hex,
            now,
            now
          )
        end

        @inserting = false
      end.resume
    end
  end
end

#table_migrate(source_db, target_db, table_config) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/tableflip/executor.rb', line 299

def table_migrate(source_db, target_db, table_config)
  table = table_config[:table]
  changes_table = "#{table}__changes"

  result = do_query(source_db, "SELECT COUNT(*) AS rows FROM `#{changes_table}` WHERE claim IS NULL")
  count = table_config[:count] = result.first[:rows]

  log("#{table} has #{table_config[:count]} records to migrate.")

  next_claim = do_query(source_db, "SELECT MAX(claim) AS claim FROM `#{changes_table}`").first[:claim] || 0

  result = do_query(source_db, "SHOW FIELDS FROM `#{table}`")

  exclusions = Hash[
    @strategy.exclude_columns.collect do |column|
      [ column.to_sym, true ]
    end
  ]

  columns = [ ]
  binary_columns = { }

  result.each do |r|
    column = r[:Field].to_sym

    next if (exclusions[column])

    columns << column

    case (r[:Type].downcase)
    when 'tinyblob','blob','mediumblob','longblob','binary','varbinary'
      binary_columns[column] = true
    end
  end

  if (binary_columns.any?)
    log("#{table} has binary columns: #{binary_columns.keys.join(',')}")
  end

  @migrating ||= { }

  fiber = Fiber.current
  migrated = 0
  selected = 1

  loop do
    next_claim += 1
    do_query(source_db, "UPDATE `#{changes_table}` SET claim=? WHERE claim IS NULL LIMIT ?", next_claim, @strategy.block_size)

    result = do_query(source_db, "SELECT id FROM `#{changes_table}` WHERE claim=?", next_claim)

    id_block = result.to_a.collect { |r| r[:id] }

    if (id_block.length == 0)
      if (@strategy.persist?)
        EventMachine::Timer.new(1) do
          fiber.resume
        end

        Fiber.yield

        next
      else
        break
      end
    end

    log("Claim \##{next_claim} yields #{id_block.length} records.")

    selected = do_query(source_db, "SELECT * FROM `#{table}` WHERE id IN (?)", id_block)

    values = selected.collect do |row|
      "(%s)" % [
        escaper(
          source_db,
          columns.collect do |column|
            (binary_columns[column] and row[column]) ? BinaryString.new(row[column]) : row[column]
          end
        )
      ]
    end

    if (values.any?)
      case (@strategy.migrate_method)
      when :insert
        do_query(target_db, "INSERT IGNORE INTO `#{table}` (#{columns.collect { |c| "`#{c}`" }.join(',')}) VALUES #{values.join(',')}")
      else
        do_query(target_db, "REPLACE INTO `#{table}` (#{columns.collect { |c| "`#{c}`" }.join(',')}) VALUES #{values.join(',')}")
      end
    end

    selected = values.length
    migrated += values.length

    log("Migrated %d/%d records for #{table}" % [ migrated, count ])
  end
end

#table_report_status(source_db, target_db, table_config) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/tableflip/executor.rb', line 276

def table_report_status(source_db, target_db, table_config)
  table = table_config[:table]
  changes_table = "#{table}__changes"

  source_table_count = do_query(source_db, "SELECT COUNT(*) AS count FROM `#{table}`").first[:count]
  target_table_count = do_query(target_db, "SELECT COUNT(*) AS count FROM `#{table}`").first[:count]
  migrated_count = do_query(source_db, "SELECT COUNT(*) AS count FROM `#{changes_table}` WHERE claim IS NOT NULL").first[:count]
  tracked_count = do_query(source_db, "SELECT COUNT(*) AS count FROM `#{changes_table}`").first[:count]

  percentage = tracked_count > 0 ? (migrated_count.to_f * 100 / tracked_count) : 0.0

  log(
    "%s: %d/%d [%d/%d] (%.1f%%)" % [
      table,
      source_table_count,
      target_table_count,
      migrated_count,
      tracked_count,
      percentage
    ]
  )
end

#tracking_add(db, table_config) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/tableflip/executor.rb', line 219

def tracking_add(db, table_config)
  table = table_config[:table]
  changes_table = "#{table}__changes"

  if (table_exists?(db, changes_table))
    STDERR.puts("Table #{changes_table} already exists. Not recreated.")
  else
    do_query(db, "CREATE TABLE `#{changes_table}` (id INT PRIMARY KEY, claim INT, INDEX index_claim (claim))")
    do_query(db, "CREATE TRIGGER `#{table}__tai` AFTER INSERT ON `#{table}` FOR EACH ROW INSERT IGNORE INTO `#{changes_table}` (id) VALUES (NEW.id) ON DUPLICATE KEY UPDATE claim=NULL")
    do_query(db, "CREATE TRIGGER `#{table}__tau` AFTER UPDATE ON `#{table}` FOR EACH ROW INSERT IGNORE INTO `#{changes_table}` (id) VALUES (NEW.id) ON DUPLICATE KEY UPDATE claim=NULL")
  end
end

#tracking_remove(db, table_config) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/tableflip/executor.rb', line 232

def tracking_remove(db, table_config)
  table = table_config[:table]
  changes_table = "#{table}__changes"

  if (table_exists?(db, changes_table))
    do_query(db, "DROP TABLE IF EXISTS `#{table}__changes`")
    do_query(db, "DROP TRIGGER IF EXISTS `#{table}__tai`")
    do_query(db, "DROP TRIGGER IF EXISTS `#{table}__tau`")
  else
    STDERR.puts("Table #{changes_table} does not exist. Not removed.")
  end
end

#tracking_seed(db, table_config) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/tableflip/executor.rb', line 245

def tracking_seed(db, table_config)
  table = table_config[:table]
  changes_table = "#{table}__changes"

  result = do_query(db, "SELECT id FROM `#{table}` #{@strategy.where}")

  ids = result.collect { |r| r[:id] }
  GC.start

  if (ids.any?)
    log("Populating #{ids.length} entries into #{changes_table} from #{table}")

    ((ids.length / @strategy.block_size) + 1).times do |n|
      start_offset = @strategy.block_size * n
      id_block = ids[start_offset, @strategy.block_size]

      if (id_block and id_block.any?)
        query = "INSERT IGNORE INTO `#{changes_table}` (id) VALUES %s" % [
          id_block.collect { |id| "(%d)" % id }.join(',')
        ]

        do_query(db, query)

        log("%d/%d entries added to #{changes_table}" % [ start_offset + id_block.length, ids.length ])
      end
    end
  else
    log("No records to migrate from #{table}")
  end
end