Class: Fluent::MetricSenseOutput::Backends::RDBTSDBBackend

Inherits:
Fluent::MetricSenseOutput::Backend show all
Defined in:
lib/fluent/plugin/backends/rdb_tsdb_backend.rb

Constant Summary collapse

INSERT_SUPPRESS_RING_BUFFER_SIZE =
64
ROW_TIME_WINDOW =

def shutdown end

60*24
ROW_TIME_WINDOW_BITS =
11
ROW_TIME_WINDOW_MASK =
(1<<ROW_TIME_WINDOW_BITS)-1

Constants inherited from Fluent::MetricSenseOutput::Backend

Fluent::MetricSenseOutput::Backend::UpdateMode

Instance Attribute Summary

Attributes inherited from Fluent::MetricSenseOutput::Backend

#log

Instance Method Summary collapse

Methods inherited from Fluent::MetricSenseOutput::Backend

#shutdown

Constructor Details

#initializeRDBTSDBBackend

Returns a new instance of RDBTSDBBackend.



29
30
31
32
33
34
# File 'lib/fluent/plugin/backends/rdb_tsdb_backend.rb', line 29

def initialize
  super
  require 'sequel'
  @insup_ring = []
  @insup_ring_index = 0
end

Instance Method Details

#configure(conf) ⇒ Object



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
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fluent/plugin/backends/rdb_tsdb_backend.rb', line 36

def configure(conf)
  super

  @rdb_read_url ||= @rdb_url

  @metric_tag_table = "#{@rdb_table_prefix}_metric_tags"
  @segment_value_table = "#{@rdb_table_prefix}_segment_values"
  @data_table = "#{@rdb_table_prefix}_data"
  @metric_view = "#{@rdb_table_prefix}_metrics"
  @metric_json_view = "#{@rdb_table_prefix}_json"

  #sql_standard_concat = lambda {|array| array.join(' || ') }
  #sql_standard_surround = lambda {|expr| "'\"' || #{expr} || '\"'" }

  case @rdb_url
  when /^mysql/i
    @sql_type = :mysql
    @sql_autoincr_type = "INT"
    @sql_autoincr_ref_type = "INT"
    @sql_autoincr_suffix = " AUTO_INCREMENT"
    @sql_value_type = "SMALLINT"
    @sql_name_type = "VARCHAR(255)"
    @sql_time_type = "INT"
    @sql_insert_ignore = "INSERT IGNORE"
    @sql_insert_returns_last_id = true
    #@sql_concat = lambda {|array| "CONCAT(#{array.join(', ')})" }
    #@sql_surround = lambda {|expr| "CONCAT('\"', #{expr}, '\"')" }
  when /^postgres/i
    @sql_type = :postgresql
    @sql_autoincr_type = "SERIAL"
    @sql_autoincr_ref_type = "INT"
    @sql_autoincr_suffix = ""
    @sql_value_type = "SMALLINT"
    @sql_name_type = "VARCHAR(255)"
    @sql_time_type = "INT"
    @sql_insert_ignore = "INSERT"
    @sql_insert_returns_last_id = false
    #@sql_concat = sql_standard_concat
    #@sql_surround = sql_standard_surround
  when /^sqlite/i
    @sql_type = :sqlite
    @sql_autoincr_type = "INTEGER"
    @sql_autoincr_ref_type = "INTEGER"
    @sql_autoincr_suffix = " AUTOINCREMENT"
    @sql_value_type = "INTEGER"
    @sql_name_type = "TEXT"
    @sql_time_type = "INTEGER"
    @sql_insert_ignore = "INSERT OR IGNORE"
    @sql_insert_returns_last_id = false
    #@sql_concat = sql_standard_concat
    #@sql_surround = sql_standard_surround
  else
    @sql_type = :unknown
    @sql_autoincr_type = "INT"
    @sql_autoincr_ref_type = "INT"
    @sql_autoincr_suffix = " AUTO_INCREMENT"
    @sql_value_type = "SMALLINT"
    @sql_name_type = "VARCHAR(255)"
    @sql_time_type = "INT"
    @sql_insert_ignore = "INSERT IGNORE"
    @sql_insert_returns_last_id = false
    #@sql_concat = sql_standard_concat
    #@sql_surround = sql_standard_surround
  end
end

#ensure_connect(&block) ⇒ Object



296
297
298
299
300
301
302
303
# File 'lib/fluent/plugin/backends/rdb_tsdb_backend.rb', line 296

def ensure_connect(&block)
  db = Sequel.connect(@rdb_url, :max_connections=>1)
  begin
    block.call(db)
  ensure
    db.disconnect
  end
end

#get_metric_id(db, tag, seg_key) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/fluent/plugin/backends/rdb_tsdb_backend.rb', line 252

def get_metric_id(db, tag, seg_key)
  key = "#{tag}\0#{seg_key}"
  id = @metric_names[key]
  return id if id

  begin
    id = db["INSERT INTO `#{@metric_tag_table}` (metric_name,segment_name) VALUES (?,?)", tag, seg_key||''].update
    if @sql_insert_returns_last_id
      @metric_names[key] = id
      return id
    end
    reload_metric_names!(db)
    return @metric_names[key]

  rescue => e
    reload_metric_names!(db)
    id = @metric_names[key]
    return id if id
    raise e
  end
end

#get_segment_id(db, seg_val) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/fluent/plugin/backends/rdb_tsdb_backend.rb', line 274

def get_segment_id(db, seg_val)
  key = seg_val ? seg_val.to_s : ''
  id = @segment_names[key]
  return id if id

  begin
    id = db["INSERT INTO `#{@segment_value_table}` (name) VALUES (?)", key].update
    if @sql_insert_returns_last_id
      @segment_names[key] = id
      return id
    end
    reload_segment_names!(db)
    return @segment_names[key]

  rescue => e
    reload_segment_names!(db)
    id = @segment_names[key]
    return id if id
    raise e
  end
end

#reload_metric_names!(db) ⇒ Object



235
236
237
238
239
240
241
242
# File 'lib/fluent/plugin/backends/rdb_tsdb_backend.rb', line 235

def reload_metric_names!(db)
  map = {}
  db.fetch("SELECT id, metric_name, segment_name FROM `#{@metric_tag_table}`") {|row|
    key = "#{row[:metric_name]}\0#{row[:segment_name]}"
    map[key] = row[:id]
  }
  @metric_names = map
end

#reload_segment_names!(db) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/fluent/plugin/backends/rdb_tsdb_backend.rb', line 244

def reload_segment_names!(db)
  map = {}
  db.fetch("SELECT id, name FROM `#{@segment_value_table}`") {|row|
    map[row[:name]] = row[:id]
  }
  @segment_names = map
end

#startObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
# File 'lib/fluent/plugin/backends/rdb_tsdb_backend.rb', line 102

def start
  ensure_connect do |db|
    db.run %[
      CREATE TABLE IF NOT EXISTS `#{@metric_tag_table}` (
        id #{@sql_autoincr_type} PRIMARY KEY#{@sql_autoincr_suffix},
        metric_name #{@sql_name_type} NOT NULL,
        segment_name #{@sql_name_type} NOT NULL,
        UNIQUE (metric_name, segment_name)
      );]

    db.run %[
      CREATE TABLE IF NOT EXISTS `#{@segment_value_table}` (
        id #{@sql_autoincr_type} PRIMARY KEY#{@sql_autoincr_suffix},
        name #{@sql_name_type} NOT NULL,
        UNIQUE (name)
      );]

    minutes = (0..59).to_a.map {|m| "m#{m} #{@sql_value_type} NOT NULL DEFAULT 0" }.join(', ')
    db.run %[
      CREATE TABLE IF NOT EXISTS `#{@data_table}` (
        base_time #{@sql_time_type} NOT NULL,
        metric_id #{@sql_autoincr_ref_type} NOT NULL,
        segment_id #{@sql_autoincr_ref_type},
        #{minutes},
        PRIMARY KEY (base_time, metric_id, segment_id)
      );]

    if @sql_type == :postgresql
      # ignore duplication error on data_table
      db.run %[
        CREATE OR REPLACE RULE ignore_duplicated_insert AS ON INSERT TO `#{@data_table}`
        WHERE NEW.base_time = OLD.base_time AND NEW.metric_id = OLD.metric_id AND NEW.segment_id = OLD.segment_id
        DO INSTEAD NOTHING;]
    end

    #minutes = (0..59).to_a.map {|m| "m#{m}" }.join(', ')
    #db.run %[
    #  CREATE VIEW IF NOT EXISTS `#{@metric_view}` AS
    #  SELECT
    #    base_time * 60 AS time,
    #    M.metric_name AS metric_name,
    #    CASE M.segment_name WHEN '' THEN NULL ELSE M.segment_name END AS segment_name,
    #    S.name AS segment_value,
    #    #{minutes}
    #  FROM `#{@data_table}` T
    #  LEFT JOIN `#{@metric_tag_table}` M ON T.metric_id = M.id
    #  LEFT JOIN `#{@segment_value_table}` S ON T.segment_id = S.id;]

    #minutes = (0..59).to_a.map {|m| ["m#{m}", "','"] }.flatten!
    #minutes.pop
    #minutes = @sql_concat.call(["'['"]+minutes+["']'"])
    #db.run %[
    #  CREATE VIEW IF NOT EXISTS `#{@metric_json_view}` AS
    #  SELECT
    #    base_time * 60 AS time,
    #    #{@sql_surround.call("M.metric_name")} AS metric_name,
    #    CASE WHEN M.segment_name IS NULL OR M.segment_name = '' THEN 'null' ELSE #{@sql_surround.call("M.segment_name")} END AS segment_name,
    #    CASE WHEN S.name IS NULL OR S.name = '' THEN 'null' ELSE #{@sql_surround.call("S.name")} END AS segment_value,
    #    #{minutes}
    #  FROM `#{@data_table}` T
    #  LEFT JOIN `#{@metric_tag_table}` M ON T.metric_id = M.id
    #  LEFT JOIN `#{@segment_value_table}` S ON T.segment_id = S.id;]

    reload_metric_names!(db)
    reload_segment_names!(db)
  end
end

#write(data) ⇒ Object



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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/fluent/plugin/backends/rdb_tsdb_backend.rb', line 177

def write(data)
  ensure_connect do |db|
    # group by row_key (base_time,metric_id,segment_id)
    rows = {}
    data.each {|tag,time,value,seg_key,seg_val,mode|
      # TODO update_mode is not supported yet
      base_time = time / ROW_TIME_WINDOW
      metric_id = get_metric_id(db, tag, seg_key)
      segment_id = get_segment_id(db, seg_val) if seg_val

      row_key = [base_time, metric_id, segment_id]
      minutes = (rows[row_key] ||= [])
      minutes << ((value << ROW_TIME_WINDOW_BITS) | (time % 60))
    }

    # insert rows if not exist
    if @sql_type == :sqlite
      # sqlite3 < 1.3.7 doesn't allow multiple rows at once
      rows.keys.each {|row_key|
        next if @insup_ring.include?(row_key)
        db["#{@sql_insert_ignore} INTO `#{@data_table}` (base_time,metric_id,segment_id) VALUES (?,?,?)", *row_key].update
        rid = @insup_ring_index = (@insup_ring_index + 1) % INSERT_SUPPRESS_RING_BUFFER_SIZE
        @insup_ring[rid] = row_key
      }
    else
      insert_sql = "#{@sql_insert_ignore} INTO `#{@data_table}` (base_time,metric_id,segment_id) VALUES " + (["(?,?,?)"] * rows.size).join(', ')
      insert_params = [insert_sql]
      rows.keys.each {|row_key|
        next if @insup_ring.include?(row_key)
        insert_params.concat(row_key)
        rid = @insup_ring_index = (@insup_ring_index + 1) % INSERT_SUPPRESS_RING_BUFFER_SIZE
        @insup_ring[rid] = row_key
      }
      db[*insert_params].update
    end

    # increment values
    db.transaction do
      rows.each_pair {|row_key,minutes|
        update_sql = "UPDATE `#{@data_table}` SET "
        update_params = [update_sql]

        values_sql = []
        minutes.each {|m|
          value = m >> ROW_TIME_WINDOW_BITS
          minute = m & ROW_TIME_WINDOW_MASK
          values_sql << "m#{minute} = m#{minute} + ?"
          update_params << value
        }.join(', ')
        update_sql << values_sql.join(', ') << " WHERE base_time=? AND metric_id=? AND segment_id=?"
        update_params.concat(row_key)

        db[*update_params].update
      }
    end
  end
end