Class: Flydata::FileUtil::SyncFileManager
- Inherits:
-
Object
- Object
- Flydata::FileUtil::SyncFileManager
- Defined in:
- lib/flydata/sync_file_manager.rb
Constant Summary collapse
- DUMP_DIR =
ENV['FLYDATA_DUMP'] || File.join(FLYDATA_HOME, 'dump')
- BACKUP_DIR =
ENV['FLYDATA_BACKUP'] || File.join(FLYDATA_HOME, 'backup')
- TABLE_POSITIONS_DIR =
ENV['FLYDATA_TABLE_POSITIONS'] || File.join(FLYDATA_HOME, 'positions')
Instance Method Summary collapse
- #backup_dir ⇒ Object
- #backup_dump_dir ⇒ Object
- #binlog_path ⇒ Object
- #close ⇒ Object
- #delete_dump_file ⇒ Object
- #delete_table_binlog_pos(table_name) ⇒ Object
- #dump_file_path ⇒ Object
-
#dump_pos_path ⇒ Object
dump pos file for resume.
- #get_new_table_list(tables, file_type) ⇒ Object
- #get_table_binlog_pos(table_name) ⇒ Object
-
#increment_and_save_table_position(table_name) ⇒ Object
Read a sequence number from the table’s position file, increment the number and pass the number to a block.
- #increment_table_rev(table_name, base_rev) ⇒ Object
-
#initialize(data_entry) ⇒ SyncFileManager
constructor
A new instance of SyncFileManager.
- #install_table_binlog_files(tables) ⇒ Object
- #load_dump_pos ⇒ Object
- #load_sync_info ⇒ Object
- #mark_generated_tables(tables) ⇒ Object
-
#mysql_table_marshal_dump_path ⇒ Object
MysqlTable marshal file.
- #reset_table_position_files(tables) ⇒ Object
-
#save_binlog(binlog_pos) ⇒ Object
binlog.pos file.
- #save_dump_pos(status, table_name, last_pos, binlog_pos, state = nil, substate = nil) ⇒ Object
- #save_mysql_table_marshal_dump(mysql_table) ⇒ Object
- #save_sync_info(initial_sync, tables) ⇒ Object
- #save_table_binlog_pos(tables, binlog_pos) ⇒ Object
- #sync_info_file ⇒ Object
- #table_binlog_pos_init_paths(*tables) ⇒ Object
- #table_binlog_pos_paths(*tables) ⇒ Object
- #table_ddl_file_paths(*tables) ⇒ Object
- #table_position_file_paths(*tables) ⇒ Object
- #table_positions_dir_path ⇒ Object
- #table_rev(table_name) ⇒ Object
- #table_rev_file_path(table_name) ⇒ Object
- #table_rev_file_paths(*tables) ⇒ Object
Constructor Details
#initialize(data_entry) ⇒ SyncFileManager
Returns a new instance of SyncFileManager.
7 8 9 10 |
# File 'lib/flydata/sync_file_manager.rb', line 7 def initialize(data_entry) @data_entry = data_entry @table_position_files = {} # File objects keyed by table name end |
Instance Method Details
#backup_dir ⇒ Object
249 250 251 |
# File 'lib/flydata/sync_file_manager.rb', line 249 def backup_dir BACKUP_DIR end |
#backup_dump_dir ⇒ Object
241 242 243 244 245 246 247 |
# File 'lib/flydata/sync_file_manager.rb', line 241 def backup_dump_dir backup_dir = BACKUP_DIR.dup FileUtils.mkdir_p(backup_dir) unless Dir.exists?(backup_dir) dest_dir = File.join(backup_dir, Time.now.strftime("%Y%m%d%H%M%S")) FileUtils.mkdir(dest_dir) FileUtils.mv(Dir.glob("#{dump_dir}/*"), dest_dir) end |
#binlog_path ⇒ Object
82 83 84 |
# File 'lib/flydata/sync_file_manager.rb', line 82 def binlog_path File.join(FLYDATA_HOME, @data_entry['name'] + ".binlog.pos") end |
#close ⇒ Object
12 13 14 15 |
# File 'lib/flydata/sync_file_manager.rb', line 12 def close @table_position_files.values.each {|f| f.close } @table_position_files = {} end |
#delete_dump_file ⇒ Object
237 238 239 |
# File 'lib/flydata/sync_file_manager.rb', line 237 def delete_dump_file FileUtils.rm(dump_file_path) if File.exists?(dump_file_path) end |
#delete_table_binlog_pos(table_name) ⇒ Object
204 205 206 207 208 209 210 211 |
# File 'lib/flydata/sync_file_manager.rb', line 204 def delete_table_binlog_pos(table_name) file = File.join(table_positions_dir_path, table_name + ".binlog.pos") if File.exists?(file) FileUtils.rm(file, :force => true) else puts "#{file} does not exist. Something is wrong. Did you delete the file manually when flydata was running?" end end |
#dump_file_path ⇒ Object
17 18 19 |
# File 'lib/flydata/sync_file_manager.rb', line 17 def dump_file_path File.join(dump_dir, @data_entry['name']) + ".dump" end |
#dump_pos_path ⇒ Object
dump pos file for resume
22 23 24 |
# File 'lib/flydata/sync_file_manager.rb', line 22 def dump_pos_path dump_file_path + ".pos" end |
#get_new_table_list(tables, file_type) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/flydata/sync_file_manager.rb', line 54 def get_new_table_list(tables, file_type) table_positions_dir_path = ENV['FLYDATA_TABLE_POSITIONS'] || File.join(FLYDATA_HOME, 'positions') new_tables = [] tables.each do |table| new_tables << table unless File.exists?(File.join(table_positions_dir_path, "#{table}.#{file_type}")) end new_tables end |
#get_table_binlog_pos(table_name) ⇒ Object
167 168 169 170 171 |
# File 'lib/flydata/sync_file_manager.rb', line 167 def get_table_binlog_pos(table_name) file = File.join(table_positions_dir_path, table_name + ".binlog.pos") return nil unless File.exists?(file) File.open(file, 'r').readline end |
#increment_and_save_table_position(table_name) ⇒ Object
Read a sequence number from the table’s position file, increment the number and pass the number to a block. After executing the block, saves the value to the position file.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/flydata/sync_file_manager.rb', line 121 def increment_and_save_table_position(table_name) file = File.join(table_positions_dir_path, table_name + ".pos") retry_count = 0 begin @table_position_files[table_name] ||= File.open(file, "r+") rescue Errno::ENOENT raise if retry_count > 0 # Already retried. Must be a differentfile causing the error # File not exist. Create one with initial value of '0' File.open(file, "w") {|f| f.write('0') } retry_count += 1 retry end f = @table_position_files[table_name] seq = f.read seq = seq.to_i + 1 begin yield(seq) ensure # when an error happened in yield, the sequence number should remain # as is. For the next call to read the value correctly, the position # must be rewound. f.rewind end f.truncate(0) f.write(seq) f.flush f.rewind end |
#increment_table_rev(table_name, base_rev) ⇒ Object
195 196 197 198 199 200 201 202 |
# File 'lib/flydata/sync_file_manager.rb', line 195 def increment_table_rev(table_name, base_rev) file = table_rev_file_path(table_name) new_rev = base_rev + 1 File.open(file, "w") do |f| f.write(new_rev) end new_rev end |
#install_table_binlog_files(tables) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/flydata/sync_file_manager.rb', line 222 def install_table_binlog_files(tables) FileUtils.mkdir_p(table_positions_dir_path) unless Dir.exists?(table_positions_dir_path) tables.each do |table_name| file_name = table_name + ".binlog.pos" src_file = File.join(dump_dir, file_name) if ! File.exists?(src_file) raise "#{src_file} does not exist. Error!!" end FileUtils.mv(src_file, table_positions_dir_path) # save the position at initial sync. this is used for repair if # necessary. FileUtils.cp(File.join(table_positions_dir_path, file_name), File.join(table_positions_dir_path, file_name + ".init")) end end |
#load_dump_pos ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/flydata/sync_file_manager.rb', line 32 def load_dump_pos path = dump_pos_path return {} unless File.exists?(path) items = File.open(path, 'r').readline.split("\t") raise "Invalid dump.pos file: #{path}" unless items.length >= 5 && items.length <= 7 mysql_table = load_mysql_table_marshal_dump { status: items[0], table_name: items[1], last_pos: items[2].to_i, binlog_pos: {binfile: items[3], pos: items[4].to_i}, state: items[5], substate: items[6], mysql_table: mysql_table} end |
#load_sync_info ⇒ Object
160 161 162 163 164 165 |
# File 'lib/flydata/sync_file_manager.rb', line 160 def load_sync_info return nil unless File.exists?(sync_info_file) items = File.open(sync_info_file, 'r').readline.split("\t") { initial_sync: (items[0] == 'true'), tables: items[1].split(" ") } end |
#mark_generated_tables(tables) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/flydata/sync_file_manager.rb', line 43 def mark_generated_tables(tables) table_positions_dir_path = ENV['FLYDATA_TABLE_POSITIONS'] || File.join(FLYDATA_HOME, 'positions') #Create positions if dir does not exist unless File.directory?(table_positions_dir_path) FileUtils.mkdir_p(table_positions_dir_path) end tables.each do |tab| File.open(File.join(table_positions_dir_path, "#{tab}.generated_ddl"), 'w') {|f| f.write("1") } end end |
#mysql_table_marshal_dump_path ⇒ Object
MysqlTable marshal file
64 65 66 |
# File 'lib/flydata/sync_file_manager.rb', line 64 def mysql_table_marshal_dump_path dump_file_path + ".mysql_table" end |
#reset_table_position_files(tables) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/flydata/sync_file_manager.rb', line 86 def reset_table_position_files(tables) tables.each do |table_name| file = File.join(table_positions_dir_path, table_name + ".pos") File.open(file, "w") {|f| f.write('0') } end end |
#save_binlog(binlog_pos) ⇒ Object
binlog.pos file
75 76 77 78 79 80 |
# File 'lib/flydata/sync_file_manager.rb', line 75 def save_binlog(binlog_pos) path = binlog_path File.open(path, 'w') do |f| f.write(binlog_content(binlog_pos)) end end |
#save_dump_pos(status, table_name, last_pos, binlog_pos, state = nil, substate = nil) ⇒ Object
26 27 28 29 30 |
# File 'lib/flydata/sync_file_manager.rb', line 26 def save_dump_pos(status, table_name, last_pos, binlog_pos, state = nil, substate = nil) File.open(dump_pos_path, 'w') do |f| f.write(dump_pos_content(status, table_name, last_pos, binlog_pos, state, substate)) end end |
#save_mysql_table_marshal_dump(mysql_table) ⇒ Object
68 69 70 71 72 |
# File 'lib/flydata/sync_file_manager.rb', line 68 def save_mysql_table_marshal_dump(mysql_table) File.open(mysql_table_marshal_dump_path, 'w') do |f| f.write Marshal.dump(mysql_table) end end |
#save_sync_info(initial_sync, tables) ⇒ Object
154 155 156 157 158 |
# File 'lib/flydata/sync_file_manager.rb', line 154 def save_sync_info(initial_sync, tables) File.open(sync_info_file, "w") do |f| f.write([initial_sync, tables.join(" ")].join("\t")) end end |
#save_table_binlog_pos(tables, binlog_pos) ⇒ Object
213 214 215 216 217 218 219 220 |
# File 'lib/flydata/sync_file_manager.rb', line 213 def save_table_binlog_pos(tables, binlog_pos) tables.each do |table_name| file = File.join(dump_dir, table_name + ".binlog.pos") File.open(file, "w") do |f| f.write(binlog_content(binlog_pos)) end end end |
#sync_info_file ⇒ Object
150 151 152 |
# File 'lib/flydata/sync_file_manager.rb', line 150 def sync_info_file File.join(dump_dir, "sync.info") end |
#table_binlog_pos_init_paths(*tables) ⇒ Object
112 113 114 115 |
# File 'lib/flydata/sync_file_manager.rb', line 112 def table_binlog_pos_init_paths(*tables) tables.empty? ? Dir.glob(File.join(table_positions_dir_path, '*.binlog.pos.init')) : tables.map{|table| File.join(table_positions_dir_path, table + '.binlog.pos.init')} end |
#table_binlog_pos_paths(*tables) ⇒ Object
107 108 109 110 |
# File 'lib/flydata/sync_file_manager.rb', line 107 def table_binlog_pos_paths(*tables) tables.empty? ? Dir.glob(File.join(table_positions_dir_path, '*.binlog.pos')) : tables.map{|table| File.join(table_positions_dir_path, table + '.binlog.pos')} end |
#table_ddl_file_paths(*tables) ⇒ Object
102 103 104 105 |
# File 'lib/flydata/sync_file_manager.rb', line 102 def table_ddl_file_paths(*tables) tables.empty? ? Dir.glob(File.join(table_positions_dir_path, '*.generated_ddl')) : tables.map{|table| File.join(table_positions_dir_path, table + '.generated_ddl')} end |
#table_position_file_paths(*tables) ⇒ Object
97 98 99 100 |
# File 'lib/flydata/sync_file_manager.rb', line 97 def table_position_file_paths(*tables) tables.empty? ? Dir.glob(File.join(table_positions_dir_path, '*.pos')) : tables.map{|table| File.join(table_positions_dir_path, table + '.pos')} end |
#table_positions_dir_path ⇒ Object
93 94 95 |
# File 'lib/flydata/sync_file_manager.rb', line 93 def table_positions_dir_path TABLE_POSITIONS_DIR end |
#table_rev(table_name) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/flydata/sync_file_manager.rb', line 182 def table_rev(table_name) file = table_rev_file_path(table_name) return 1 unless File.exists?(file) #default revision is 1 File.open(file, "r+") do |f| seq = f.read if seq.empty? return 1 else return seq.to_i end end end |
#table_rev_file_path(table_name) ⇒ Object
173 174 175 |
# File 'lib/flydata/sync_file_manager.rb', line 173 def table_rev_file_path(table_name) File.join(table_positions_dir_path, table_name + ".rev") end |
#table_rev_file_paths(*tables) ⇒ Object
177 178 179 180 |
# File 'lib/flydata/sync_file_manager.rb', line 177 def table_rev_file_paths(*tables) tables.empty? ? Dir.glob(File.join(table_positions_dir_path, "*.rev")) : tables.map{|table| table_rev_file_path(table)} end |