Class: Fluent::MysqlReplicatorInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_mysql_replicator.rb

Instance Method Summary collapse

Constructor Details

#initializeMysqlReplicatorInput

Returns a new instance of MysqlReplicatorInput.



5
6
7
8
9
# File 'lib/fluent/plugin/in_mysql_replicator.rb', line 5

def initialize
  require 'mysql2'
  require 'digest/sha1'
  super
end

Instance Method Details

#configure(conf) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/fluent/plugin/in_mysql_replicator.rb', line 23

def configure(conf)
  super
  @interval = Config.time_value(@interval)

  if @tag.nil?
    raise Fluent::ConfigError, "mysql_replicator: missing 'tag' parameter. Please add following line into config like 'tag replicator.mydatabase.mytable.${event}.${primary_key}'"
  end

  $log.info "adding mysql_replicator worker. :tag=>#{tag} :query=>#{@query} :interval=>#{@interval}sec :enable_delete=>#{enable_delete}"
end

#emit_record(tag, record) ⇒ Object



109
110
111
# File 'lib/fluent/plugin/in_mysql_replicator.rb', line 109

def emit_record(tag, record)
  Engine.emit(tag, Engine.now, record)
end

#format_tag(tag, param) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/fluent/plugin/in_mysql_replicator.rb', line 101

def format_tag(tag, param)
  pattern = {'${event}' => param[:event].to_s, '${primary_key}' => @primary_key}
  tag.gsub(/\${[a-z_]+(\[[0-9]+\])?}/, pattern) do
    $log.warn "mysql_replicator: missing placeholder. :tag=>#{tag} :placeholder=>#{$1}" unless pattern.include?($1)
    pattern[$1]
  end
end

#get_connectionObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/fluent/plugin/in_mysql_replicator.rb', line 124

def get_connection
  begin
    return Mysql2::Client.new({
      :host => @host,
      :port => @port,
      :username => @username,
      :password => @password,
      :database => @database,
      :encoding => @encoding,
      :reconnect => true,
      :stream => true,
      :cache_rows => false
    })
  rescue Exception => e
    $log.warn "mysql_replicator: #{e}"
    sleep @interval
    retry
  end
end

#hash_delete_by_list(hash, deleted_keys) ⇒ Object



97
98
99
# File 'lib/fluent/plugin/in_mysql_replicator.rb', line 97

def hash_delete_by_list (hash, deleted_keys)
  deleted_keys.each{|k| hash.delete(k)}
end

#pollObject



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
89
90
91
92
93
94
95
# File 'lib/fluent/plugin/in_mysql_replicator.rb', line 52

def poll
  table_hash = Hash.new
  ids = Array.new
  loop do
    start_time = Time.now
    previous_ids = ids
    current_ids = Array.new
    query(@query).each do |row|
      current_ids << row[@primary_key]
      current_hash = Digest::SHA1.hexdigest(row.flatten.join)
      row.each {|k, v| row[k] = v.to_s if v.is_a?(Time) || v.is_a?(Date)}
      if row[@primary_key].nil?
        $log.error "mysql_replicator: missing primary_key. :tag=>#{tag} :primary_key=>#{primary_key}"
        break
      end
      if !table_hash.include?(row[@primary_key])
        tag = format_tag(@tag, {:event => :insert})
        emit_record(tag, row)
      elsif table_hash[row[@primary_key]] != current_hash
        tag = format_tag(@tag, {:event => :update})
        emit_record(tag, row)
      end
      table_hash[row[@primary_key]] = current_hash
    end
    ids = current_ids
    if @enable_delete
      if previous_ids.empty?
        deleted_ids = [*1...current_ids.max] - current_ids
      else
        deleted_ids = previous_ids - current_ids
      end
      if deleted_ids.count > 0
        hash_delete_by_list(table_hash, deleted_ids)
        deleted_ids.each do |id| 
          tag = format_tag(@tag, {:event => :delete})
          emit_record(tag, {@primary_key => id})
        end
      end
    end
    elapsed_time = sprintf("%0.02f", Time.now - start_time)
    $log.info "mysql_replicator: finished execution :tag=>#{tag} :elapsed_time=>#{elapsed_time} sec"
    sleep @interval
  end
end

#query(query) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/fluent/plugin/in_mysql_replicator.rb', line 113

def query(query)
  @mysql ||= get_connection
  begin
    return @mysql.query(query)
  rescue Exception => e
    $log.warn "mysql_replicator: #{e}"
    sleep @interval
    retry
  end
end

#runObject



42
43
44
45
46
47
48
49
50
# File 'lib/fluent/plugin/in_mysql_replicator.rb', line 42

def run
  begin
    poll
  rescue StandardError => e
    $log.error "mysql_replicator: failed to execute query."
    $log.error "error: #{e.message}"
    $log.error e.backtrace.join("\n")
  end
end

#shutdownObject



38
39
40
# File 'lib/fluent/plugin/in_mysql_replicator.rb', line 38

def shutdown
  Thread.kill(@thread)
end

#startObject



34
35
36
# File 'lib/fluent/plugin/in_mysql_replicator.rb', line 34

def start
  @thread = Thread.new(&method(:run))
end