Class: Taps::Push

Inherits:
Operation show all
Defined in:
lib/taps/operation.rb

Instance Attribute Summary

Attributes inherited from Operation

#database_url, #opts, #remote_url, #session_uri

Instance Method Summary collapse

Methods inherited from Operation

#apply_table_filter, #catch_errors, #close_session, #completed_tables, #compression_disabled?, #db, #default_chunksize, #exiting?, factory, #format_number, #http_headers, #indexes_first?, #initialize, #log, #resuming?, #safe_database_url, #safe_remote_url, #safe_url, #server, #session_resource, #set_session, #setup_signal_trap, #store_session, #stream_state, #stream_state=, #table_filter, #verify_server

Constructor Details

This class inherits a constructor from Taps::Operation

Instance Method Details

#fetch_local_tables_infoObject



547
548
549
550
551
552
553
# File 'lib/taps/operation.rb', line 547

def fetch_local_tables_info
  tables_with_counts = {}
  db.tables.each do |table|
    tables_with_counts[table] = db[table.to_sym.identifier].count
  end
  apply_table_filter(tables_with_counts)
end

#file_prefixObject



387
388
389
# File 'lib/taps/operation.rb', line 387

def file_prefix
  "push"
end

#local_tables_infoObject



530
531
532
# File 'lib/taps/operation.rb', line 530

def local_tables_info
  opts[:local_tables_info] ||= fetch_local_tables_info
end

#push_dataObject



457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/taps/operation.rb', line 457

def push_data
  puts "Sending data"

  puts "#{tables.size} tables, #{format_number(record_count)} records"

  tables.each do |table_name, count|
    stream = Taps::DataStream.factory(db,
      :table_name => table_name,
      :chunksize => default_chunksize)
    progress = ProgressBar.new(table_name.to_s, count)
    push_data_from_table(stream, progress)
  end
end

#push_data_from_table(stream, progress) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/taps/operation.rb', line 471

def push_data_from_table(stream, progress)
  loop do
    if exiting?
      store_session
      exit 0
    end

    row_size = 0
    chunksize = stream.state[:chunksize]

    begin
      chunksize = Taps::Utils.calculate_chunksize(chunksize) do |c|
        stream.state[:chunksize] = c
        encoded_data, row_size, elapsed_time = stream.fetch
        break if stream.complete?

        data = {
          :state => stream.to_hash,
          :checksum => Taps::Utils.checksum(encoded_data).to_s
        }

        begin
          content, content_type = Taps::Multipart.create do |r|
            r.attach :name => :encoded_data,
              :payload => encoded_data,
              :content_type => 'application/octet-stream'
            r.attach :name => :json,
              :payload => data.to_json,
              :content_type => 'application/json'
          end
          session_resource['push/table'].post(content, http_headers(:content_type => content_type))
          self.stream_state = stream.to_hash
        rescue => e
          Taps::Utils.reraise_server_exception(e)
        end

        elapsed_time
      end
    rescue Taps::CorruptedData => e
      # retry the same data, it got corrupted somehow.
      next
    rescue Taps::DuplicatePrimaryKeyError => e
      # verify the stream and retry it
      stream = stream.verify_remote_stream(session_resource['push/verify_stream'], http_headers)
      next
    end
    stream.state[:chunksize] = chunksize

    progress.inc(row_size)

    stream.increment(row_size)
    break if stream.complete?
  end

  progress.finish
  completed_tables << stream.table_name.to_s
  self.stream_state = {}
end

#push_indexesObject



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/taps/operation.rb', line 409

def push_indexes
  idxs = JSON.parse(Taps::Utils.schema_bin(:indexes_individual, database_url))

  return unless idxs.size > 0

  puts "Sending indexes"

  apply_table_filter(idxs).each do |table, indexes|
    next unless indexes.size > 0
    progress = ProgressBar.new(table, indexes.size)
    indexes.each do |idx|
      session_resource['push/indexes'].post(idx, http_headers)
      progress.inc(1)
    end
    progress.finish
  end
end

#push_partial_dataObject



446
447
448
449
450
451
452
453
454
455
# File 'lib/taps/operation.rb', line 446

def push_partial_data
  return if stream_state == {}

  table_name = stream_state[:table_name]
  record_count = tables[table_name.to_s]
  puts "Resuming #{table_name}, #{format_number(record_count)} records"
  progress = ProgressBar.new(table_name.to_s, record_count)
  stream = Taps::DataStream.factory(db, stream_state)
  push_data_from_table(stream, progress)
end

#push_reset_sequencesObject



440
441
442
443
444
# File 'lib/taps/operation.rb', line 440

def push_reset_sequences
  puts "Resetting sequences"

  session_resource['push/reset_sequences'].post('', http_headers)
end

#push_schemaObject



427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/taps/operation.rb', line 427

def push_schema
  puts "Sending schema"

  progress = ProgressBar.new('Schema', tables.size)
  tables.each do |table, count|
    schema_data = Taps::Utils.schema_bin(:dump_table, database_url, table)
    log.debug "Table: #{table}\n#{schema_data}\n"
    session_resource['push/schema'].post(schema_data, http_headers)
    progress.inc(1)
  end
  progress.finish
end

#record_countObject



543
544
545
# File 'lib/taps/operation.rb', line 543

def record_count
  @record_count ||= local_tables_info.values.inject(0) { |a,c| a += c }
end

#runObject



395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/taps/operation.rb', line 395

def run
  catch_errors do
    unless resuming?
      push_schema
      push_indexes if indexes_first?
    end
    setup_signal_trap
    push_partial_data if resuming?
    push_data
    push_indexes unless indexes_first?
    push_reset_sequences
  end
end

#tablesObject



534
535
536
537
538
539
540
541
# File 'lib/taps/operation.rb', line 534

def tables
  h = {}
  local_tables_info.each do |table_name, count|
    next if completed_tables.include?(table_name.to_s)
    h[table_name.to_s] = count
  end
  h
end

#to_hashObject



391
392
393
# File 'lib/taps/operation.rb', line 391

def to_hash
  super.merge(:local_tables_info => local_tables_info)
end