Class: Tapsoob::Push
Instance Attribute Summary
Attributes inherited from Operation
#database_url, #dump_path, #opts
Instance Method Summary
collapse
Methods inherited from Operation
#add_completed_table, #apply_table_filter, #catch_errors, #completed_tables, #completed_tables_mutex, #data?, #db, #default_chunksize, #exclude_tables, #exiting?, factory, #format_number, #indexes_first?, #initialize, #log, #parallel?, #parallel_workers, #resuming?, #schema?, #setup_signal_trap, #store_session, #stream_state, #stream_state=, #table_filter
Instance Method Details
#fetch_local_tables_info ⇒ Object
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
|
# File 'lib/tapsoob/operation.rb', line 651
def fetch_local_tables_info
tables_with_counts = {}
tbls = Dir.glob(File.join(dump_path, "schemas", "*")).map { |path| File.basename(path, ".rb") }
tbls.each do |table|
if File.exist?(File.join(dump_path, "data", "#{table}.json"))
total_rows = 0
File.readlines(File.join(dump_path, "data", "#{table}.json")).each do |line|
chunk = JSON.parse(line.strip)
total_rows += chunk["data"].size if chunk["data"]
end
tables_with_counts[table] = total_rows
else
tables_with_counts[table] = 0
end
end
apply_table_filter(tables_with_counts)
end
|
#file_prefix ⇒ Object
420
421
422
|
# File 'lib/tapsoob/operation.rb', line 420
def file_prefix
"push"
end
|
#local_tables_info ⇒ Object
634
635
636
|
# File 'lib/tapsoob/operation.rb', line 634
def local_tables_info
opts[:local_tables_info] ||= fetch_local_tables_info
end
|
#push_data ⇒ Object
501
502
503
504
505
506
507
508
509
510
511
|
# File 'lib/tapsoob/operation.rb', line 501
def push_data
log.info "Sending data"
log.info "#{tables.size} tables, #{format_number(record_count)} records"
if parallel?
push_data_parallel
else
push_data_serial
end
end
|
#push_data_from_file(stream, progress) ⇒ Object
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
# File 'lib/tapsoob/operation.rb', line 577
def push_data_from_file(stream, progress)
loop do
if exiting?
store_session
exit 0
end
row_size = 0
chunksize = stream.state[:chunksize]
begin
chunksize = Tapsoob::Utils.calculate_chunksize(chunksize) do |c|
stream.state[:chunksize] = c.to_i
encoded_data, row_size, elapsed_time = nil
d1 = c.time_delta do
encoded_data, row_size, elapsed_time = stream.fetch({ :type => "file", :source => dump_path })
end
data = nil
d2 = c.time_delta do
data = {
:state => stream.to_hash,
:checksum => Tapsoob::Utils.checksum(encoded_data).to_s,
:encoded_data => encoded_data
}
end
stream.fetch_data_to_database(data)
log.debug "row size: #{row_size}"
self.stream_state = stream.to_hash
c.idle_secs = (d1 + d2)
elapsed_time
end
rescue Tapsoob::CorruptedData => e
next
rescue Tapsoob::DuplicatePrimaryKeyError => e
stream.verify_stream
stream = JSON.generate({ :state => stream.to_hash })
next
end
stream.state[:chunksize] = chunksize
progress.inc(1) if progress
break if stream.complete?
end
progress.finish if progress
add_completed_table(stream.table_name)
self.stream_state = {}
end
|
#push_data_parallel ⇒ Object
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
# File 'lib/tapsoob/operation.rb', line 534
def push_data_parallel
log.info "Using #{parallel_workers} parallel workers"
multi_progress = opts[:progress] ? MultiProgressBar.new(parallel_workers) : nil
table_queue = Queue.new
tables.each do |table_name, count|
data_file = File.join(dump_path, "data", "#{table_name}.json")
next unless File.exist?(data_file) && count > 0
table_queue << [table_name, count]
end
workers = (1..parallel_workers).map do
Thread.new do
loop do
break if table_queue.empty?
table_name, count = table_queue.pop(true) rescue break
db[table_name.to_sym].truncate if @opts[:purge]
stream = Tapsoob::DataStream.factory(db, {
:table_name => table_name,
:chunksize => default_chunksize
}, {
:"skip-duplicates" => opts[:"skip-duplicates"] || false,
:"discard-identity" => opts[:"discard-identity"] || false,
:purge => opts[:purge] || false,
:debug => opts[:debug]
})
estimated_chunks = [(count.to_f / default_chunksize).ceil, 1].max
progress = multi_progress ? multi_progress.create_bar(table_name.to_s, estimated_chunks) : nil
push_data_from_file(stream, progress)
end
end
end
workers.each(&:join)
multi_progress.stop if multi_progress
end
|
#push_data_serial ⇒ Object
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
# File 'lib/tapsoob/operation.rb', line 513
def push_data_serial
tables.each do |table_name, count|
data_file = File.join(dump_path, "data", "#{table_name}.json")
next unless File.exist?(data_file) && count > 0
db[table_name.to_sym].truncate if @opts[:purge]
stream = Tapsoob::DataStream.factory(db, {
:table_name => table_name,
:chunksize => default_chunksize
}, {
:"skip-duplicates" => opts[:"skip-duplicates"] || false,
:"discard-identity" => opts[:"discard-identity"] || false,
:purge => opts[:purge] || false,
:debug => opts[:debug]
})
estimated_chunks = [(count.to_f / default_chunksize).ceil, 1].max
progress = (opts[:progress] ? ProgressBar.new(table_name.to_s, estimated_chunks) : nil)
push_data_from_file(stream, progress)
end
end
|
#push_indexes ⇒ Object
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
# File 'lib/tapsoob/operation.rb', line 442
def push_indexes
idxs = {}
table_idxs = Dir.glob(File.join(dump_path, "indexes", "*.json")).map { |path| File.basename(path, '.json') }
table_idxs.each do |table_idx|
index_file = File.join(dump_path, "indexes", "#{table_idx}.json")
idxs[table_idx] = File.readlines(index_file).map { |line| JSON.parse(line.strip) }
end
return unless idxs.size > 0
log.info "Sending indexes"
filtered_idxs = apply_table_filter(idxs).select { |table, indexes| indexes.size > 0 }
max_title_width = filtered_idxs.keys.map { |table| "#{table} indexes".length }.max || 14
filtered_idxs.each do |table, indexes|
progress = ProgressBar.new("#{table} indexes", indexes.size, STDOUT, max_title_width)
indexes.each do |idx|
Tapsoob::Utils.load_indexes(database_url, idx)
progress.inc(1)
end
progress.finish
end
end
|
#push_partial_data ⇒ Object
488
489
490
491
492
493
494
495
496
497
498
499
|
# File 'lib/tapsoob/operation.rb', line 488
def push_partial_data
return if stream_state == {}
table_name = stream_state[:table_name]
record_count = tables[table_name.to_s]
log.info "Resuming #{table_name}, #{format_number(record_count)} records"
stream = Tapsoob::DataStream.factory(db, stream_state)
chunksize = stream_state[:chunksize] || default_chunksize
estimated_chunks = [(record_count.to_f / chunksize).ceil, 1].max
progress = (opts[:progress] ? ProgressBar.new(table_name.to_s, estimated_chunks) : nil)
push_data_from_file(stream, progress)
end
|
#push_reset_sequences ⇒ Object
482
483
484
485
486
|
# File 'lib/tapsoob/operation.rb', line 482
def push_reset_sequences
log.info "Resetting sequences"
Tapsoob::Utils.schema_bin(:reset_db_sequences, database_url)
end
|
#push_schema ⇒ Object
469
470
471
472
473
474
475
476
477
478
479
480
|
# File 'lib/tapsoob/operation.rb', line 469
def push_schema
log.info "Sending schema"
progress = ProgressBar.new('Schema', tables.size)
tables.each do |table, count|
log.debug "Loading '#{table}' schema\n"
Tapsoob::Utils.load_schema(dump_path, db, table)
progress.inc(1)
end
progress.finish
end
|
#record_count ⇒ Object
647
648
649
|
# File 'lib/tapsoob/operation.rb', line 647
def record_count
@record_count ||= local_tables_info.values.inject(0) { |a,c| a += c }
end
|
#run ⇒ Object
428
429
430
431
432
433
434
435
436
437
438
439
440
|
# File 'lib/tapsoob/operation.rb', line 428
def run
catch_errors do
unless resuming?
push_schema if schema?
push_indexes if indexes_first? && schema?
end
setup_signal_trap
push_partial_data if data? && resuming?
push_data if data?
push_indexes if !indexes_first? && schema?
push_reset_sequences
end
end
|
#tables ⇒ Object
638
639
640
641
642
643
644
645
|
# File 'lib/tapsoob/operation.rb', line 638
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_hash ⇒ Object
424
425
426
|
# File 'lib/tapsoob/operation.rb', line 424
def to_hash
super.merge(:local_tables_info => local_tables_info)
end
|