Class: Flydata::Command::Sync

Inherits:
Base
  • Object
show all
Includes:
Helpers
Defined in:
lib/flydata/command/sync.rb

Direct Known Subclasses

Mysql, Mysqlbinlog, Mysqldump

Defined Under Namespace

Classes: SyncDataEntryError

Constant Summary collapse

RUN_PROFILE =
false
INSERT_PROGRESS_INTERVAL =
1000
SERVER_DATA_PROCESSING_TIMEOUT =

seconds

3600
STATUS_PARSING =

for dump.pos file

'PARSING'
STATUS_PARSED =

the value is different from the constant name on purpose for backward compatibility.

'WAITING'
STATUS_COMPLETE =
'COMPLETE'

Constants included from Helpers

Helpers::UNIT_PREFIX

Instance Attribute Summary collapse

Attributes inherited from Base

#opts

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

as_size, development?, env_mode, env_suffix, flydata_api_host_file, flydata_conf_file, flydata_version, format_menu_list, retry_on, to_command_class, usage_text

Methods inherited from Base

#ask_input_table_name, #ask_yes_no, #choose_one, #flydata, #initialize, #newline, #register_crontab, #retrieve_data_entries, #separator, #show_purpose_name

Methods included from ExclusiveRunnable

included

Methods included from Flydata::CommandLoggable

#before_logging, #log_error_stderr, #log_info_stdout, #log_warn_stderr

Constructor Details

This class inherits a constructor from Flydata::Command::Base

Instance Attribute Details

#ddl_tablesObject (readonly)

true if full initial sync



42
43
44
# File 'lib/flydata/command/sync.rb', line 42

def ddl_tables
  @ddl_tables
end

#full_initial_syncObject (readonly)

true if full initial sync



42
43
44
# File 'lib/flydata/command/sync.rb', line 42

def full_initial_sync
  @full_initial_sync
end

#full_tablesObject (readonly)

true if full initial sync



42
43
44
# File 'lib/flydata/command/sync.rb', line 42

def full_tables
  @full_tables
end

#input_tablesObject (readonly)

true if full initial sync



42
43
44
# File 'lib/flydata/command/sync.rb', line 42

def input_tables
  @input_tables
end

#new_tablesObject (readonly)

true if full initial sync



42
43
44
# File 'lib/flydata/command/sync.rb', line 42

def new_tables
  @new_tables
end

Class Method Details

.slopObject

Command: flydata sync

- Arguments


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/flydata/command/sync.rb', line 56

def self.slop
  Slop.new do
    on 'c', 'skip-cleanup', 'Skip server cleanup'
    on 'f', 'skip-flush', 'Skip server flush'
    on 'y', 'yes', 'Skip command prompt assuming yes to all questions.  Use this for batch operation.'
    on 'd', 'dump-file', 'Dump mysqldump into a file. Use this for debugging after making sure the free space.' # dummy for compatibility
    on 's', 'dump-stream', 'Dump mysqldump stream instead of saving dump file. It might cause timeout error if db size is larger than 10GB.'
    on 'n', 'no-flydata-start', 'Don\'t start the flydata agent after initial sync.'
    #TODO : This option is temp! Should remove soon.
    on 'ff', 'Skip checking query queue and flush'
  end
end

.slop_fix_binlogposObject

Command: flydata sync:fix_binlogpos

- Arguments


220
221
222
223
224
# File 'lib/flydata/command/sync.rb', line 220

def self.slop_fix_binlogpos
  Slop.new do
    on 'f', 'force', 'update sent binlog position file forcibly'
  end
end

.slop_flushObject

Command: flydata sync:flush

- Arguments


87
88
89
90
91
92
# File 'lib/flydata/command/sync.rb', line 87

def self.slop_flush
  Slop.new do
    on 'f', 'skip-flush', 'Skip server flush'
    on 'y', 'yes', 'Skip command prompt assuming yes to all questions.  Use this for batch operation.'
  end
end

.slop_generate_table_ddlObject

Command: flydata sync:generate_table_ddl

- Arguments


194
195
196
197
198
199
200
201
# File 'lib/flydata/command/sync.rb', line 194

def self.slop_generate_table_ddl
  Slop.new do
    on 'c', 'ctl-only', 'Only generate FlyData Control definitions'
    on 'y', 'yes', 'Skip command prompt assuming yes to all questions.  Use this for batch operation.'
    on 's', 'skip-primary-key-check', 'Skip primary key check when generating DDL'
    on 'all-tables', 'Generate all table schema'
  end
end

.slop_repairObject

Command: flydata sync:repair

- Arguments


269
270
271
272
273
# File 'lib/flydata/command/sync.rb', line 269

def self.slop_repair
  Slop.new do
    on 'y', 'yes', 'Skip command prompt assuming yes to all questions.  Use this for batch operation.'
  end
end

.slop_resetObject

Command: flydata sync:reset

- Arguments


114
115
116
117
118
119
# File 'lib/flydata/command/sync.rb', line 114

def self.slop_reset
  Slop.new do
    on 'c', 'client', 'Resets client only.'
    on 'y', 'yes', 'Skip command prompt assuming yes to all questions.  Use this for batch operation.'
  end
end

Instance Method Details

#check(options = {}) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/flydata/command/sync.rb', line 280

def check(options = {})
  status, pos_mismatch_tables, gap_tables = _check(options)

  if status.include? :OK
    message = "\nNo errors are found.  Sync is clean.\n"
  else
    message = "\nFollowing errors are found.\n"

    if status.include? :STUCK_AT_PROCESS
      message += " - Data is stuck while processing\n"
    end
    if status.include? :STUCK_AT_UPLOAD
      message += " - Data is stuck while uploading\n"
    end
    if status.include? :ABNORMAL_SHUTDOWN
      message += " - Agent was not shut down correctly\n"
    end
    if gap_tables
      message += " - Sync data is missing for the following table(s)\n"
      gap_tables.each do |bt|
        message += "     table:#{bt[:table]}\n"
      end
      message += "\n"
    end
    if pos_mismatch_tables
      message += " - Incorrect table position(s)\n"
      pos_mismatch_tables.each do |bt|
        message += "     table:#{bt[:table]}, agent position:#{bt[:agent_seq] ? bt[:agent_seq] : '(missing)'}, server position:#{bt[:server_seq]}\n"
      end
      message += "\n"
    end
  end
  log_info_stdout message
end

#fix_binlogposObject

Command: flydata sync:fix_binlogpos Set binlog path

- Entry method


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/flydata/command/sync.rb', line 229

def fix_binlogpos
  de = data_entry
  sync_fm = create_sync_file_manager(de)

  if File.exists?(sync_fm.sent_binlog_path) && !opts.force?
    log_info_stdout("Skip creating sent binlogpos because sent position file is exist already. (#{sync_fm.sent_binlog_path})")
    return
  end

  if Flydata::Command::Sender.new.process_exist?
    log_warn_stderr("flydata is running. flydata process needs to be stopped with 'flydata stop'.")
    return
  end

  binlog_info = sync_fm.load_binlog
  if binlog_info.nil?
    log_info_stdout("Skip creating sent binlogpos because binlog position file is empty or invalid. (#{sync_fm.sent_binlog_path})")
    return
  end

  say("Updating binlog position files...")
  log_info("Updating binlog position files... Original binlog_info:#{binlog_info}")

  # Update binlog.sent.pos file
  #   -1 is because the position in binlog.pos is the next event's position.
  #   on the other hand the position in sent position indicates already processed.
  binlog_info[:pos] -= 1
  log_info("Updating sent position file. #{binlog_info} -> #{sync_fm.sent_binlog_path}")
  sync_fm.save_sent_binlog(binlog_info)

  # Update binlog.pos file to start from head of the current binlog file
  new_binlog_info = binlog_info.dup.tap{|h| h[:pos] = 4}  # 4 is the first position of binlog file.
  log_info("Updating original position file. #{new_binlog_info} -> #{sync_fm.binlog_path}")
  sync_fm.save_binlog(new_binlog_info)
  log_info_stdout("Done!")
end

#flush(*tables) ⇒ Object

Command: flydata sync:flush

- Entry method


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/flydata/command/sync.rb', line 96

def flush(*tables)
  begin
    flush_buffer_and_stop(tables, skip_flush: opts.skip_flush?)
  rescue ServerDataProcessingTimeout => e
    ee = ServerDataProcessingTimeout.new("Delayed Data Processing")
    ee.description = "  Data processing is taking more than expected.  Please contact [email protected] to check the system status.\n\n"
    ee.set_backtrace e.backtrace
    raise ee
  end
  log_info_stdout("Buffers have been flushed and the sender process has been stopped.")
end

#generate_table_ddl(*tables) ⇒ Object

Command: flydata sync:generate_table_ddl

- Entry method


205
206
207
208
209
210
211
212
213
214
215
# File 'lib/flydata/command/sync.rb', line 205

def generate_table_ddl(*tables)
  # Compatibility check
  de = data_entry
  dp = flydata.data_port.get
  Flydata::MysqlCompatibilityCheck.new(dp, de['mysql_data_entry_preference']).check

  # Set instance variables
  set_current_tables(tables, include_all_tables: true)

  do_generate_table_ddl(de)
end

#repairObject



275
276
277
# File 'lib/flydata/command/sync.rb', line 275

def repair
  _repair
end

#reset(*tables) ⇒ Object

Command: flydata sync:reset

- Entry method


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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/flydata/command/sync.rb', line 123

def reset(*tables)
  show_purpose_name

  # Set instance variables
  sync_resumed = set_current_tables(tables, resume: true)

  message = ''
  if sync_resumed && !tables.empty?
    log_info_stdout "Initial sync is in progress.  In this case, you can only reset the initial sync.  To reset specific table(s), please resume and complete the initial sync by running the 'flydata start' command first.\nIf you'd like to reset the initial sync in progress, run the 'flydata reset' command with no arguments.\n"
    return
  end

  # Flush client buffer
  msg_tables = @input_tables.empty? ? '' : " for these tables : #{@input_tables.join(" ")}"
  msg_sync_type = sync_resumed ? "the current initial sync" : "the current sync"
  return unless ask_yes_no("This resets #{msg_sync_type}#{msg_tables}.  Are you sure?")
  sender = Flydata::Command::Sender.new
  sender.flush_client_buffer # TODO We should rather delete buffer files
  sender.stop

  begin
    wait_for_server_buffer(timeout: SERVER_DATA_PROCESSING_TIMEOUT, tables: target_tables_for_api)
  rescue ServerDataProcessingTimeout => e
    ee = ServerDataProcessingTimeout.new("Delayed Data Processing")
    ee.description = "  Data processing is taking more than expected.  Please contact [email protected] to check the system status.\n  Once checked, you can continue sync reset with the following command\n\n    flydata sync:reset \#{tables.empty? ? '' : tables.join(\" \")}\n\n"
    ee.set_backtrace e.backtrace
    raise ee
  end

  # Cleanup tables on server
  de = data_entry
  cleanup_sync_server(de, @input_tables) unless opts.client?
  sync_fm = create_sync_file_manager(de)

  # Delete local files
  tables_for_deleting = tables.empty? ? sync_fm.tables_from_positions_dir : @input_tables
  sync_fm.delete_table_control_files(*tables_for_deleting)

  new_tables_after_reset = @unsynced_tables + @input_tables
  delete_binlog =@input_tables.empty? or @full_tables.empty? or @full_tables.all?{|ft| new_tables_after_reset.include?(ft)}
  sync_fm.delete_non_table_control_files(delete_binlog)
  sync_fm.close
  log_info_stdout("Reset completed successfully.")
end

#run(*tables) ⇒ Object

Command: flydata sync

- Entry method


71
72
73
# File 'lib/flydata/command/sync.rb', line 71

def run(*tables)
  raise "Command 'flydata sync' has been deprecated.  Use 'flydata start' instead."
end

#skipObject

Depricated Command: flydata sync:skip skip initial sync



180
181
182
183
184
185
186
187
188
189
# File 'lib/flydata/command/sync.rb', line 180

def skip
  de = data_entry
  sync_fm = create_sync_file_manager(de)
  binlog_path = sync_fm.binlog_path
  sync_fm.close
  `touch #{binlog_path}`
  log_info_stdout("Created an empty binlog position file.")
  log_info_stdout("-> #{binlog_path}")
  log_info_stdout("Run 'flydata start' to start continuous sync.")
end

#try_mysql_sync(options) ⇒ Object

Public method

- Called from Sender#start/restart


78
79
80
81
82
83
# File 'lib/flydata/command/sync.rb', line 78

def try_mysql_sync(options)
  # Start initial sync
  handle_mysql_sync(nil, options)
rescue SyncDataEntryError
  return
end