Class: Flydata::Command::Sync

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

Direct Known Subclasses

Mysql, Mysqlbinlog, Mysqldump, Psql

Constant Summary collapse

RUN_PROFILE =
false
INSERT_PROGRESS_INTERVAL =
1000
SERVER_DATA_PROCESSING_TIMEOUT =

seconds

3600
AUTO_CREATE_STATUS_START =

for sync_info file auto_create_status

'START'
AUTO_CREATE_STATUS_CREATED_TABLES =

AUTO_CREATE_STATUS_SENT_DDL = ‘SENT_DDL’

'CREATED_TABLES'
STATUS_START =

for dump.pos file

'START'
STATUS_PARSING =

only :source_pos is available at the begining of parse

'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, #dashboard_url, #data_port, #flydata, #initialize, #newline, #redshift_cluster, #redshift_console_url, #register_crontab, #retrieve_data_entries, #separator, #show_purpose_name, #source

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



50
51
52
# File 'lib/flydata/command/sync.rb', line 50

def ddl_tables
  @ddl_tables
end

#full_initial_syncObject (readonly)

true if full initial sync



50
51
52
# File 'lib/flydata/command/sync.rb', line 50

def full_initial_sync
  @full_initial_sync
end

#full_tablesObject (readonly)

true if full initial sync



50
51
52
# File 'lib/flydata/command/sync.rb', line 50

def full_tables
  @full_tables
end

#input_tablesObject (readonly)

true if full initial sync



50
51
52
# File 'lib/flydata/command/sync.rb', line 50

def input_tables
  @input_tables
end

#new_tablesObject (readonly)

true if full initial sync



50
51
52
# File 'lib/flydata/command/sync.rb', line 50

def new_tables
  @new_tables
end

Class Method Details

.slopObject

Command: flydata sync

- Arguments


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/flydata/command/sync.rb', line 61

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', 'Save dump result into a file.'
    on 's', 'dump-stream', 'Stream dump result to a pipe 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_clearObject

Command: flydata sync:clear

- Arguments


287
288
289
290
291
292
# File 'lib/flydata/command/sync.rb', line 287

def self.slop_clear
  Slop.new do
    on 'force-run', 'Run forcefully, ignoring exclusive run info'
    on 'y', 'yes', 'Skip command prompt assuming yes to all questions.  Use this for batch operation.'
  end
end

.slop_flushObject

Command: flydata sync:flush

- Arguments


95
96
97
98
99
100
101
102
103
# File 'lib/flydata/command/sync.rb', line 95

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.'
    on 'force-run', 'Run forcefully, ignoring exclusive run info'
    on 'server-timeout=', 'Server data processing timeout', as: Integer, default: SERVER_DATA_PROCESSING_TIMEOUT
    on 'dont-wait-upload', 'Do not wait for queue items to be processed.'
  end
end

.slop_generate_table_ddlObject

Command: flydata sync:generate_table_ddl

- Arguments


325
326
327
328
329
330
331
332
333
334
# File 'lib/flydata/command/sync.rb', line 325

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'
    on 'drop-append-only', 'Include queries to drop append-only tables'
    #no 'force-run' option. because stdout is often redirected to a file.
  end
end

.slop_repairObject

Command: flydata sync:repair

- Arguments


355
356
357
358
359
360
361
# File 'lib/flydata/command/sync.rb', line 355

def self.slop_repair
  Slop.new do
    on 'y', 'yes', 'Skip command prompt assuming yes to all questions.  Use this for batch operation.'
    on 'skip-start', 'Skip auto start after repair is completed.'
    on 'force-run', 'Run forcefully, ignoring exclusive run info'
  end
end

.slop_resetObject

Command: flydata sync:reset

- Arguments


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/flydata/command/sync.rb', line 127

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.'
    on 'a', 'all', 'Resets Sync for all tables'
    on 'i', 'init', 'Resets unfinished initial sync'
    on 'force-run', 'Run forcefully, ignoring exclusive run info'
    on 'f', 'force', "Resets tables including append only tables.  Use this option only when you want to stop sync for the append only table permanently."
    on 'include-unregistered', "Resets tables including unregistered tables.  Use this option only when resetting removed tables."
  end
end

.slop_resyncObject

Command: flydata sync:resync

- Arguments


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

def self.slop_resync
  resync_opts = self.slop_reset
  resync_opts.options.concat(Flydata::Command::Sender.slop_start.options)
  resync_opts
end

Instance Method Details

#_reset(recover_cmd, options) ⇒ Object



222
223
224
225
226
227
228
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 222

def _reset(recover_cmd, options)
  reset_client_only = options[:reset_client_only]
  delete_tbl_ddl = options[:delete_tbl_ddl]

  # Flush client buffer
  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 = <<EOS
  Data processing is taking more than expected.  Please contact [email protected] to check the system status.
  Once checked, you can continue your operation with the following command

    #{recover_cmd}

EOS
    ee.set_backtrace e.backtrace
    raise ee
  end

  # Cleanup tables on server
  de = data_entry
  cleanup_sync_server(de, @input_tables) unless reset_client_only
  sync_fm = create_sync_file_manager(de)

  # Delete local files
  sync_fm.delete_dump_files
  sync_fm.delete_table_position_files(*@input_tables)
  sync_fm.delete_table_rev_files(*@input_tables)
  sync_fm.delete_table_ddl_files(*@input_tables) if delete_tbl_ddl

  new_tables_after_reset = @unsynced_tables + @input_tables
  if @input_tables.empty? or @full_tables.empty? or @full_tables.all?{|ft| new_tables_after_reset.include?(ft)}
    sync_fm.delete_master_position_files
  end
  sync_fm.close

  log_info_stdout("Reset completed successfully.")
end

#check(options = {}) ⇒ Object

Command: flydata sync:check

- Entry method


375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/flydata/command/sync.rb', line 375

def check(options = {})
  context = source.source_pos
  status, corrupt_master_pos_files, pos_mismatch_tables, gap_tables =
    _check(context, 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 += " - Timeout while processing data\n"
    end
    if status.include? :STUCK_AT_UPLOAD
      message += " - Timeout while uploading data\n"
    end
    if status.include? :ABNORMAL_SHUTDOWN
      message += " - Agent was not shut down correctly\n"
    end
    if status.include? :CORRUPT_MASTER_POS
      message += " - Master source position is corrupted\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.  This may not be a real issue, caused by pending upload chunks.  Run sync:flush and try again.\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

#clearObject

Command: flydata sync:clear

- Entry method


296
297
298
299
300
301
302
303
304
305
306
# File 'lib/flydata/command/sync.rb', line 296

def clear
  sender = Flydata::Command::Sender.new(opts)
  sender.stop(auto_create: true)
  if ask_yes_no("This clears buffer and positions directory. Are you sure?")
    Flydata::SyncFileManager.clear_sync_client_resources
    log_info_stdout("Done!")
  end
rescue => e
  log_error_stderr("[error] Failed to clear - error:\"#{e}\"")
  raise e
end

#compat_checkObject

Command: flydata sync:compat_check

- Entry method


418
419
420
421
# File 'lib/flydata/command/sync.rb', line 418

def compat_check
  context = source.sync_generate_table_ddl(flydata.data_port.get)
  context.run_compatibility_check
end

#flush(*tables) ⇒ Object

Command: flydata sync:flush

- Entry method


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/flydata/command/sync.rb', line 107

def flush(*tables)
  begin
    flush_buffer_and_stop(tables, skip_flush: opts.skip_flush?,
                                  timeout: opts[:'server-timeout'],
                                  dont_wait_upload: opts.dont_wait_upload?)
  rescue ServerDataProcessingTimeout => e
    ee = ServerDataProcessingTimeout.new("Delayed Data Processing")
    ee.description = <<EOS
  Data processing is taking more than expected.  Please contact [email protected] to check the system status.

EOS
    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


338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/flydata/command/sync.rb', line 338

def generate_table_ddl(*tables)
  # Compatibility check
  de = data_entry
  context = source.sync_generate_table_ddl(flydata.data_port.get)
  context.run_compatibility_check

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

  unless generate_and_print_table_ddl(context, de)
    raise "There are no valid unsynced tables, if you want to just get ddl for all tables, please run \`flydata sync:generate_table_ddl --all-tables\`"
  end
end

#repairObject

Command: flydata sync:repair

- Entry method


365
366
367
368
369
370
# File 'lib/flydata/command/sync.rb', line 365

def repair
  need_to_start = _repair
  if need_to_start && !opts.skip_start?
    Flydata::Command::Sender.new.start
  end
end

#reset(*tables) ⇒ Object

Command: flydata sync:reset

- Entry method


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
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
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/flydata/command/sync.rb', line 141

def reset(*tables)
  # Set instance variables
  reset_init = opts[:init]
  if opts[:all]
    # Reset all tables regardless of sync_resumed
    tables = []
    reset_init = false
  end
  sync_resumed, auto_create = set_current_tables(tables, resume: !opts[:all], include_unregistered: opts.include_unregistered?)
  target_tables = opts[:all] ? @full_tables : @input_tables
  target_append_only_tables = target_tables & @append_only_tables
  target_full_sync_tables = target_tables - @append_only_tables

  return if !sync_resumed && reset_init

  # Suggest to add option/arguments
  if tables.empty? && !opts[:all] && !opts[:init]
    if sync_resumed
      log_info_stdout <<EOS
ERROR!  Argument or option must be specified.

  To reset unfinished initial sync, run the following command:

    flydata sync:reset --init

EOS
    else
      log_info_stdout <<EOS
ERROR!  Argument or option must be specified.

  To reset all tables:              flydata sync:reset --all
  To reset specific tables:         flydata sync:reset table1 table2 ...

EOS
    end
    return
  end

  # Suggest to use --init
  if sync_resumed && !tables.empty?
    log_info_stdout <<EOS
ERROR!  You cannot reset tables because the previous initial sync has not been completed.  Reset the unfinished initial sync first with the following command:

  flydata sync:reset --init

EOS
    return
  end

  # Suggest to use --force
  if target_append_only_tables.size > 0 && !opts[:force]
    log_info_stdout <<EOS
ERROR!  Reset failed because it includes append only table(s).  Sync can no longer continue if you reset an append only table.

  Append only table(s): #{target_append_only_tables.join(", ")}

If you really want to reset append-only tables, run the command with '--force' option.

EOS

    log_info_stdout <<EOS if !target_full_sync_tables.empty? && !sync_resumed
To reset all tables except for append only tables, run the following command.

  flydata sync:reset #{target_full_sync_tables.join(" ")}

EOS
    return
  end

  msg_tables = @input_tables.empty? ? '' : " for these tables : #{@input_tables.join(" ")}"
  msg_sync_type = sync_resumed ? "the current initial sync" : "the current sync"
  show_purpose_name
  return unless ask_yes_no("This resets #{msg_sync_type}#{msg_tables}.  Are you sure?")

  recover_cmd = "flydata sync:reset #{tables.empty? ? '' : tables.join(" ")}"

  _reset(recover_cmd, reset_client_only: opts.client?, delete_tbl_ddl: true)

end

#resync(*tables) ⇒ Object

Command: flydata sync:resync

- Entry method


276
277
278
279
280
281
282
283
# File 'lib/flydata/command/sync.rb', line 276

def resync(*tables)
  self.reset(*tables)
  sender = Flydata::Command::Sender.new(opts)
  sender.start(auto_create: true)
rescue => e
  log_error_stderr("[error] Failed to resync - error:\"#{e}\"")
  raise e
end

#run(*tables) ⇒ Object

Command: flydata sync

- Entry method


76
77
78
# File 'lib/flydata/command/sync.rb', line 76

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

#skipObject

Depricated Command: flydata sync:skip skip initial sync



311
312
313
314
315
316
317
318
319
320
# File 'lib/flydata/command/sync.rb', line 311

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

#try_initial_sync(options) ⇒ Object

Public method

- Called from Sender#start/restart


83
84
85
86
87
88
89
90
91
# File 'lib/flydata/command/sync.rb', line 83

def try_initial_sync(options)
  @opts = options[:slop_opts] if options[:slop_opts]
  handle_initial_sync(options) if source.sync.supported?
rescue Source::UnsupportedSourceError
  return
rescue => e
  log_error("[error] Unexpected error happened during initial sync. error:#{e}")
  raise e
end