Class: Fluent::PostgresReplicatorInput

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

Instance Method Summary collapse

Constructor Details

#initializePostgresReplicatorInput



14
15
16
17
18
# File 'lib/fluent/plugin/in_postgres_replicator.rb', line 14

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

Instance Method Details

#configure(conf) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fluent/plugin/in_postgres_replicator.rb', line 20

def configure(conf)
  super
  @interval = Fluent::Config.time_value(@interval)
  if @primary_keys.nil?
    raise Fluent::ConfigError, "primary_keys MUST be specified"
  end
  if @tag.nil?
    raise Fluent::ConfigError, "tag MUST be specified"
  end
  @primary_keys = @primary_keys.split(/\s*,\s*/)
end

#emit_record(tag, record) ⇒ Object



107
108
109
# File 'lib/fluent/plugin/in_postgres_replicator.rb', line 107

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

#format_tag(tag, param) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/fluent/plugin/in_postgres_replicator.rb', line 99

def format_tag(tag, param)
  pattern = {'${event}' => param[:event].to_s, '${primary_keys}' => @primary_keys.join('_')}
  tag.gsub(/(\${[a-z_]+})/) do
    log.warn "placeholder value is not found. :tag=>#{tag} :placeholder=>#{$1}" unless pattern.include?($1)
    pattern[$1]
  end
end

#get_connectionObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fluent/plugin/in_postgres_replicator.rb', line 111

def get_connection
  begin
    return PG::Connection.new({
      :host => @host,
      :port => @port,
      :user => @username,
      :password => @password,
      :dbname => @database
    })
  rescue Exception => e
    log.warn "failed to get connection and will retry. error: #{e}"
    sleep @interval
    retry
  end
end

#pollObject



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
# File 'lib/fluent/plugin/in_postgres_replicator.rb', line 49

def poll
  hash_values = Hash.new
  conn = get_connection()
  loop do
    rows_count = 0
    start_time = Time.now
    rows, conn = query(@sql, conn)
    rows.each do |row|
      row_ids = Array.new
      @primary_keys.each do |primary_key|
        if !row[primary_key].nil?
          row_ids << row[primary_key]
        end
      end
      if row_ids.size != @primary_keys.size
        log.error "primary_keys column value is something wrong. :tag=>#{@tag} :primary_keys=>#{@primary_keys}"
        break
      end

      hash_value_id = row_ids.join('_')
      hash_value = Digest::SHA1.hexdigest(row.flatten.join)
      if !hash_values.include?(hash_value_id)
        tag = format_tag(@tag, {:event => :insert})
        emit_record(tag, row)
      elsif hash_values[hash_value_id] != hash_value
        tag = format_tag(@tag, {:event => :update})
        emit_record(tag, row)
      end
      hash_values[hash_value_id] = hash_value
      rows_count += 1
    end
    conn.close
    elapsed_time = sprintf('%0.02f', Time.now - start_time)
    log.info "success to execute replicator. :tag=>#{@tag} :rows_count=>#{rows_count} :elapsed_time=>#{elapsed_time} sec"
    sleep @interval
  end

end

#query(sql, conn = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/fluent/plugin/in_postgres_replicator.rb', line 88

def query(sql, conn = nil)
  begin
    conn = (conn.nil? || conn.finished?) ? get_connection : conn
    return conn.query(sql), conn
  rescue Exception => e
    log.warn "failed to execute query and will retry. error: #{e}"
    sleep @interval
    retry
  end
end

#runObject



40
41
42
43
44
45
46
47
# File 'lib/fluent/plugin/in_postgres_replicator.rb', line 40

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

#shutdownObject



36
37
38
# File 'lib/fluent/plugin/in_postgres_replicator.rb', line 36

def shutdown
  Thread.kill(@thread)
end

#startObject



32
33
34
# File 'lib/fluent/plugin/in_postgres_replicator.rb', line 32

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