fluent-plugin-sql 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d602eab48f2e5a82afd2805ea57198d5517bb1b7
4
- data.tar.gz: fec9b91b4f26b49f9f8262adbf86e0a29b6b9c6a
3
+ metadata.gz: 3e23b9247528a135033b20b8948e2c7c8553e8a7
4
+ data.tar.gz: 3f6b03e77450c220f17851b50c1f799e80f1e4e8
5
5
  SHA512:
6
- metadata.gz: 4e3909a2229451959f1ce21d69febb6ca102dae935da5944feb9895510f655fa0349b4f9b737bfdeba02533de55f7ac4dffab32f7cfe1e8beb80bb40864aa600
7
- data.tar.gz: 82ec1c90fc5b4a0436a20235f2839d892a94eef18e3154823526b322a1f0547f0fe0b3019538b9e07a9b473e0b0581af65471452e3ec905524c21c94c5cdd4fd
6
+ metadata.gz: e6259bf6a96579679fa63f3cd7af978a8f5c23134a41438ba81f0c7d0744b3fd883f761c326156907e85d4b8f1d7d122f8841fbdd8905999f397f0a12a9f8ab2
7
+ data.tar.gz: 2eefbca9a6c289f8286bfe98472c43abe6cb5343c056f59f1490e67af65a2709157c1f7007e53fba102ef17f584352e2c139983768ec2a75b4bb9b11f1362b3f
data/.travis.yml CHANGED
@@ -1,4 +1,20 @@
1
1
  sudo: false
2
+ language: ruby
3
+
4
+ addons:
5
+ postgresql: "9.4"
6
+
7
+ rvm:
8
+ - 2.0.0
9
+ - 2.1
10
+ - 2.2
11
+ - 2.3.0
2
12
 
3
13
  before_install:
4
14
  - gem install bundler
15
+
16
+ before_script:
17
+ - psql -U postgres -c "CREATE ROLE fluentd WITH LOGIN ENCRYPTED PASSWORD 'fluentd';"
18
+ - psql -U postgres -c "CREATE DATABASE fluentd_test OWNER fluentd;"
19
+
20
+ script: bundle exec rake test
data/Rakefile CHANGED
@@ -2,13 +2,13 @@
2
2
  require 'bundler'
3
3
  Bundler::GemHelper.install_tasks
4
4
 
5
- #require 'rake/testtask'
6
- #
7
- #Rake::TestTask.new(:test) do |test|
8
- # test.libs << 'lib' << 'test'
9
- # test.test_files = FileList['test/*.rb']
10
- # test.verbose = true
11
- #end
5
+ require 'rake/testtask'
6
+
7
+ Rake::TestTask.new(:test) do |test|
8
+ test.libs << 'lib' << 'test'
9
+ test.test_files = FileList['test/*/*.rb']
10
+ test.verbose = true
11
+ end
12
12
 
13
13
  task :default => [:build]
14
14
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.5.1
@@ -21,4 +21,8 @@ Gem::Specification.new do |gem|
21
21
  gem.add_dependency 'activerecord', "~> 4.2"
22
22
  gem.add_dependency 'activerecord-import', "~> 0.7"
23
23
  gem.add_development_dependency "rake", ">= 0.9.2"
24
+ gem.add_development_dependency "test-unit", "~> 3.1.0"
25
+ gem.add_development_dependency "test-unit-rr"
26
+ gem.add_development_dependency "test-unit-notify"
27
+ gem.add_development_dependency "pg"
24
28
  end
@@ -237,6 +237,8 @@ module Fluent
237
237
 
238
238
  def shutdown
239
239
  @stop_flag = true
240
+ $log.debug "Waiting for thread to finish"
241
+ @thread.join
240
242
  end
241
243
 
242
244
  def thread_main
@@ -96,7 +96,7 @@ module Fluent
96
96
  }
97
97
  begin
98
98
  @model.import(records)
99
- rescue ActiveRecord::StatementInvalid, ActiveRecord::ThrowResult, ActiveRecord::Import::MissingColumnError => e
99
+ rescue ActiveRecord::StatementInvalid, ActiveRecord::Import::MissingColumnError => e
100
100
  # ignore other exceptions to use Fluentd retry mechanizm
101
101
  @log.warn "Got deterministic error. Fallback to one-by-one import", :error => e.message, :error_class => e.class
102
102
  one_by_one_import(records)
@@ -108,7 +108,7 @@ module Fluent
108
108
  retries = 0
109
109
  begin
110
110
  @model.import([record])
111
- rescue ActiveRecord::StatementInvalid, ActiveRecord::ThrowResult, ActiveRecord::Import::MissingColumnError => e
111
+ rescue ActiveRecord::StatementInvalid, ActiveRecord::Import::MissingColumnError => e
112
112
  @log.error "Got deterministic error again. Dump a record", :error => e.message, :error_class => e.class, :record => record
113
113
  rescue => e
114
114
  retries += 1
@@ -189,7 +189,7 @@ module Fluent
189
189
  end
190
190
 
191
191
  SQLOutput.const_set("BaseModel_#{rand(1 << 31)}", @base_model)
192
- @base_model.establish_connection(config)
192
+ ActiveRecord::Base.establish_connection(config)
193
193
 
194
194
  # ignore tables if TableElement#init failed
195
195
  @tables.reject! do |te|
@@ -215,15 +215,15 @@ module Fluent
215
215
  end
216
216
 
217
217
  def write(chunk)
218
- conn = @base_model.connection
219
- conn.active? || conn.reconnect!
218
+ ActiveRecord::Base.connection_pool.with_connection do
220
219
 
221
- @tables.each { |table|
222
- if table.pattern.match(chunk.key)
223
- return table.import(chunk)
224
- end
225
- }
226
- @default_table.import(chunk)
220
+ @tables.each { |table|
221
+ if table.pattern.match(chunk.key)
222
+ return table.import(chunk)
223
+ end
224
+ }
225
+ @default_table.import(chunk)
226
+ end
227
227
  end
228
228
 
229
229
  private
@@ -0,0 +1,24 @@
1
+ require "active_record"
2
+ ActiveRecord::Base.establish_connection(host: "localhost",
3
+ port: 5432,
4
+ username: "fluentd",
5
+ password: "fluentd",
6
+ adapter: "postgresql",
7
+ database: "fluentd_test")
8
+ ActiveRecord::Schema.define(version: 20160225030107) do
9
+ create_table "logs", force: :cascade do |t|
10
+ t.string "host"
11
+ t.string "ident"
12
+ t.string "pid"
13
+ t.text "message"
14
+ t.datetime "created_at", null: false
15
+ t.datetime "updated_at", null: false
16
+ end
17
+
18
+ create_table "messages", force: :cascade do |t|
19
+ t.string "message"
20
+ t.datetime "created_at", null: false
21
+ t.datetime "updated_at", null: false
22
+ end
23
+ end
24
+
data/test/helper.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "test/unit"
2
+ require "test/unit/rr"
3
+ require "test/unit/notify"
4
+ require "fluent/test"
5
+ require "fluent/plugin/out_sql"
6
+ require "fluent/plugin/in_sql"
7
+
8
+ load "fixtures/schema.rb"
@@ -0,0 +1,86 @@
1
+ require "helper"
2
+
3
+ class SqlInputTest < Test::Unit::TestCase
4
+ def setup
5
+ Fluent::Test.setup
6
+ end
7
+
8
+ def teardown
9
+ end
10
+
11
+ CONFIG = %[
12
+ adapter postgresql
13
+ host localhost
14
+ port 5432
15
+ database fluentd_test
16
+
17
+ username fluentd
18
+ password fluentd
19
+
20
+ tag_prefix db
21
+
22
+ <table>
23
+ table messages
24
+ tag logs
25
+ update_column updated_at
26
+ time_column updated_at
27
+ </table>
28
+ ]
29
+
30
+ def create_driver(conf = CONFIG)
31
+ Fluent::Test::InputTestDriver.new(Fluent::SQLInput).configure(conf)
32
+ end
33
+
34
+ def test_configure
35
+ d = create_driver
36
+ expected = {
37
+ host: "localhost",
38
+ port: 5432,
39
+ adapter: "postgresql",
40
+ database: "fluentd_test",
41
+ username: "fluentd",
42
+ password: "fluentd",
43
+ tag_prefix: "db"
44
+ }
45
+ actual = {
46
+ host: d.instance.host,
47
+ port: d.instance.port,
48
+ adapter: d.instance.adapter,
49
+ database: d.instance.database,
50
+ username: d.instance.username,
51
+ password: d.instance.password,
52
+ tag_prefix: d.instance.tag_prefix
53
+ }
54
+ assert_equal(expected, actual)
55
+ tables = d.instance.instance_variable_get(:@tables)
56
+ assert_equal(1, tables.size)
57
+ messages = tables.first
58
+ assert_equal("messages", messages.table)
59
+ assert_equal("logs", messages.tag)
60
+ end
61
+
62
+ def test_message
63
+ d = create_driver(CONFIG + "select_interval 1")
64
+ Message.create!(message: "message 1")
65
+ Message.create!(message: "message 2")
66
+ Message.create!(message: "message 3")
67
+
68
+ d.run
69
+
70
+ assert_equal("db.logs", d.emits[0][0])
71
+ expected = [
72
+ [d.emits[0][1], "message 1"],
73
+ [d.emits[1][1], "message 2"],
74
+ [d.emits[2][1], "message 3"],
75
+ ]
76
+ actual = [
77
+ [Time.parse(d.emits[0][2]["updated_at"]).to_i, d.emits[0][2]["message"]],
78
+ [Time.parse(d.emits[1][2]["updated_at"]).to_i, d.emits[1][2]["message"]],
79
+ [Time.parse(d.emits[2][2]["updated_at"]).to_i, d.emits[2][2]["message"]],
80
+ ]
81
+ assert_equal(expected, actual)
82
+ end
83
+
84
+ class Message < ActiveRecord::Base
85
+ end
86
+ end
@@ -0,0 +1,112 @@
1
+ require "helper"
2
+
3
+ class SqlOutputTest < Test::Unit::TestCase
4
+ def setup
5
+ Fluent::Test.setup
6
+ end
7
+
8
+ def teardown
9
+ end
10
+
11
+ CONFIG = %[
12
+ host localhost
13
+ port 5432
14
+ adapter postgresql
15
+
16
+ database fluentd_test
17
+ username fluentd
18
+ password fluentd
19
+
20
+ remove_tag_prefix db
21
+
22
+ <table>
23
+ table logs
24
+ column_mapping timestamp:created_at,host:host,ident:ident,pid:pid,message:message
25
+ </table>
26
+ ]
27
+
28
+ def create_driver(conf = CONFIG)
29
+ Fluent::Test::BufferedOutputTestDriver.new(Fluent::SQLOutput).configure(conf)
30
+ end
31
+
32
+ def test_configure
33
+ d = create_driver
34
+ expected = {
35
+ host: "localhost",
36
+ port: 5432,
37
+ adapter: "postgresql",
38
+ database: "fluentd_test",
39
+ username: "fluentd",
40
+ password: "fluentd",
41
+ remove_tag_suffix: /^db/
42
+ }
43
+ actual = {
44
+ host: d.instance.host,
45
+ port: d.instance.port,
46
+ adapter: d.instance.adapter,
47
+ database: d.instance.database,
48
+ username: d.instance.username,
49
+ password: d.instance.password,
50
+ remove_tag_suffix: d.instance.remove_tag_prefix
51
+ }
52
+ assert_equal(expected, actual)
53
+ assert_empty(d.instance.tables)
54
+ default_table = d.instance.instance_variable_get(:@default_table)
55
+ assert_equal("logs", default_table.table)
56
+ end
57
+
58
+ def test_emit
59
+ d = create_driver
60
+ time = Time.parse("2011-01-02 13:14:15 UTC").to_i
61
+
62
+ d.emit({"message" => "message1"}, time)
63
+ d.emit({"message" => "message2"}, time)
64
+
65
+ d.run
66
+
67
+ default_table = d.instance.instance_variable_get(:@default_table)
68
+ model = default_table.instance_variable_get(:@model)
69
+ assert_equal(2, model.all.count)
70
+ messages = model.pluck(:message).sort
71
+ assert_equal(["message1", "message2"], messages)
72
+ end
73
+
74
+ class Fallback < self
75
+ def test_simple
76
+ d = create_driver
77
+ time = Time.parse("2011-01-02 13:14:15 UTC").to_i
78
+
79
+ d.emit({"message" => "message1"}, time)
80
+ d.emit({"message" => "message2"}, time)
81
+
82
+ d.run do
83
+ default_table = d.instance.instance_variable_get(:@default_table)
84
+ model = default_table.instance_variable_get(:@model)
85
+ mock(model).import(anything).at_least(1) do
86
+ raise ActiveRecord::Import::MissingColumnError.new("dummy_table", "dummy_column")
87
+ end
88
+ mock(default_table).one_by_one_import(anything)
89
+ end
90
+ end
91
+
92
+ def test_limit
93
+ d = create_driver
94
+ time = Time.parse("2011-01-02 13:14:15 UTC").to_i
95
+
96
+ d.emit({"message" => "message1"}, time)
97
+ d.emit({"message" => "message2"}, time)
98
+
99
+ d.run do
100
+ default_table = d.instance.instance_variable_get(:@default_table)
101
+ model = default_table.instance_variable_get(:@model)
102
+ mock(model).import([anything, anything]).once do
103
+ raise ActiveRecord::Import::MissingColumnError.new("dummy_table", "dummy_column")
104
+ end
105
+ mock(model).import([anything]).times(12) do
106
+ raise StandardError
107
+ end
108
+ assert_equal(5, default_table.instance_variable_get(:@num_retries))
109
+ end
110
+ end
111
+ end
112
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadayuki Furuhashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-18 00:00:00.000000000 Z
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd
@@ -72,6 +72,62 @@ dependencies:
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
74
  version: 0.9.2
75
+ - !ruby/object:Gem::Dependency
76
+ name: test-unit
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 3.1.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 3.1.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: test-unit-rr
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: test-unit-notify
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: pg
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
75
131
  description: SQL input/output plugin for Fluentd event collector
76
132
  email: frsyuki@gmail.com
77
133
  executables: []
@@ -86,6 +142,10 @@ files:
86
142
  - fluent-plugin-sql.gemspec
87
143
  - lib/fluent/plugin/in_sql.rb
88
144
  - lib/fluent/plugin/out_sql.rb
145
+ - test/fixtures/schema.rb
146
+ - test/helper.rb
147
+ - test/plugin/test_in_sql.rb
148
+ - test/plugin/test_out_sql.rb
89
149
  homepage: https://github.com/frsyuki/fluent-plugin-sql
90
150
  licenses:
91
151
  - Apache-2.0
@@ -106,8 +166,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
166
  version: '0'
107
167
  requirements: []
108
168
  rubyforge_project:
109
- rubygems_version: 2.2.2
169
+ rubygems_version: 2.5.1
110
170
  signing_key:
111
171
  specification_version: 4
112
172
  summary: SQL input/output plugin for Fluentd event collector
113
- test_files: []
173
+ test_files:
174
+ - test/fixtures/schema.rb
175
+ - test/helper.rb
176
+ - test/plugin/test_in_sql.rb
177
+ - test/plugin/test_out_sql.rb