Module: Fluent::FlydataSync

Included in:
MysqlBinlogFlydataInput
Defined in:
lib/flydata/fluent-plugins/flydata_plugin_ext/flydata_sync.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/flydata/fluent-plugins/flydata_plugin_ext/flydata_sync.rb', line 11

def self.included(base)
  base.class_eval do
    include FlushSupport
    prepend TransactionSupport

    config_param :data_entry_name, :string, default: nil  # data entry name
    config_param :data_entry_type, :string, default: nil  # data entry type
    config_param :tables, :string
    config_param :tables_append_only, :string
    config_param :pk_override, :hash, default: {}
    config_param :table_attributes, :array, default: []
    config_param :tag, :string
    config_param :position_file, :string, default: 'position.binlog.pos'
  end
end

Instance Method Details

#build_data_entry(base_object = {}) ⇒ Object


90
91
92
93
94
95
96
# File 'lib/flydata/fluent-plugins/flydata_plugin_ext/flydata_sync.rb', line 90

def build_data_entry(base_object = {})
  de = base_object || {}
  de['name'] = @data_entry_name
  de['type'] = @data_entry_type
  de.merge!(@data_entry_preferences || {})
  de
end

#configure(conf) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/flydata/fluent-plugins/flydata_plugin_ext/flydata_sync.rb', line 27

def configure(conf)
  super

  @source_position_file = self.class::SOURCE_POSITION_FILE_CLASS.new(@position_file)
  unless @source_position_file.exists?
    raise "No position file(#{@source_position_file.path}). Initial synchronization is required before starting."
  end

  load_custom_conf # preference module needs to be included
  @data_entry = build_data_entry
  @source = Flydata::Source.create(@data_entry)

  @sync_fm = Flydata::SyncFileManager.new(@data_entry, @source)
  sent_position_file_path = @sync_fm.sent_source_pos_path(@position_file)
  @sent_position_file = self.class::SOURCE_POSITION_FILE_CLASS.new(sent_position_file_path)

  # Create positions dir
  positions_path = @sync_fm.table_positions_dir_path
  Dir.mkdir positions_path unless File.exists? positions_path

  @tables = @tables.to_s.split(/(?:\s*,\s*|\s+)/)
  @omit_events = Hash.new
  @tables_append_only = @tables_append_only.to_s.split(/(?:\s*,\s*|\s+)/)
  @tables_append_only.each do |table|
    @tables << table unless @tables.include?(table)
    @omit_events[table] = [:delete, :truncate_table]
  end

  # Remove tables that do not have pos files
  new_tables = @sync_fm.get_new_table_list(@tables, "pos")
  @tables -= new_tables
  $log.info "Not watching these tables: #{new_tables.join(", ")}"

  # Set table revisions
  @table_revs = @tables.inject({}) do |h, table_name|
    h[table_name] = @sync_fm.table_rev(table_name)
    h
  end

  # Re-construct pk_override & table_attributes
  #   @pk_override should include both USER_PK_OVERRIDE and UK_AS_PK_OVERRIDE.
  #   If mismatch happens here, @pk_override value should come from a local conf file.
  #TODO support UK_AS_PK_OVERRIDE for tables existing only in local conf, not registered on the web.

  pk_override_from_table_attrs = Flydata::TableAttribute::generate_pk_override(@table_attributes)

  if @pk_override != pk_override_from_table_attrs
    # @pk_override in a local conf file only includes USER_PK_OVERRIDE. Update table_attributes.
    Flydata::TableAttribute::delete_attribute(@table_attributes, Flydata::TableAttribute::USER_PK_OVERRIDE)
    Flydata::TableAttribute::save_attribute(@table_attributes, @pk_override, Flydata::TableAttribute::USER_PK_OVERRIDE)

    @pk_override = Flydata::TableAttribute::generate_pk_override(@table_attributes)
    $log.info "Update primary key override. Using #{@pk_override}"

    prefs = @data_entry['mysql_data_entry_preference'] || @data_entry['postgresql_data_entry_preference']
    prefs['table_attributes'] = @table_attributes
    prefs['pk_override'] = @pk_override
  end

  $log.info("Source position - resume_pos:'#{@source_position_file.read rescue IOError}' " +
            "sent_pos:'#{@sent_position_file.read rescue nil}'")
end