fluent-plugin-postgres-replicator 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +17 -2
- data/README.md +24 -21
- data/fluent-plugin-postgres-replicator.gemspec +1 -1
- data/lib/fluent/plugin/in_postgres_replicator.rb +17 -15
- data/test/plugin/test_in_postgres_replicator.rb +26 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0800a5552e7c84208560aa4e71ae507af0c826a
|
4
|
+
data.tar.gz: 0b17c898e047944522ef5f97fb6f10c3f4631701
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce731ae495459c72229c84760df4c76963b08618f0d55300156b7d8303a838b0b8609d2b6d2ece8e27e0d01273eedd9b195bef6c74c215311b9cbf3149cf3d22
|
7
|
+
data.tar.gz: 3c98a0b8018703aa6e748c5b62eae1ddca8f4fe951c0c430e7594e5624d81b726ac1307766550d27b3b724eb274f0ce7ac0d838d11cee04a5803c9f89ffc7379
|
data/.travis.yml
CHANGED
@@ -1,4 +1,19 @@
|
|
1
1
|
language: ruby
|
2
|
+
|
2
3
|
rvm:
|
3
|
-
-
|
4
|
-
|
4
|
+
- 1.9.3
|
5
|
+
- 2.0.0
|
6
|
+
- 2.1
|
7
|
+
- 2.2
|
8
|
+
|
9
|
+
addons:
|
10
|
+
postgresql: "9.4"
|
11
|
+
|
12
|
+
services:
|
13
|
+
- postgresql
|
14
|
+
|
15
|
+
before_script:
|
16
|
+
- psql -c 'create database pg_repli_test_db;' -U postgres
|
17
|
+
- psql -c 'create table pg_repli_test_table (id int8 primary key, total int8) ;' -U postgres -d pg_repli_test_db
|
18
|
+
- psql -c 'insert into pg_repli_test_table values (1, 10) ;' -U postgres -d pg_repli_test_db
|
19
|
+
- psql -c 'insert into pg_repli_test_table values (2, 20) ;' -U postgres -d pg_repli_test_db
|
data/README.md
CHANGED
@@ -1,34 +1,37 @@
|
|
1
|
-
# Fluent::Plugin::
|
1
|
+
# Fluent::Plugin::PostgresReplicator, a plugin for [Fluentd](http://www.fluentd.org)
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/fluent-plugin-postgres-replicator)
|
4
|
+
[](https://travis-ci.org/innossh/fluent-plugin-postgres-replicator)
|
4
5
|
|
5
|
-
|
6
|
+
Fluentd input plugin to track insert/update event from PostgreSQL.
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
9
|
-
|
10
|
+
```sh
|
11
|
+
# requirements
|
12
|
+
$ apt-get install libpq-dev
|
10
13
|
|
11
|
-
|
12
|
-
gem 'fluent-plugin-postgres-replicator'
|
14
|
+
$ gem install fluent-plugin-postgres-replicator
|
13
15
|
```
|
14
16
|
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install fluent-plugin-postgres-replicator
|
22
|
-
|
23
17
|
## Usage
|
24
18
|
|
25
|
-
|
19
|
+
In your Fluentd configuration, use `type postgres_replicator`.
|
20
|
+
Default values would look like this:
|
26
21
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
22
|
+
```
|
23
|
+
<source>
|
24
|
+
type postgres_replicator
|
25
|
+
host localhost
|
26
|
+
username pipeline
|
27
|
+
password pipeline
|
28
|
+
database pipeline
|
29
|
+
query SELECT hour, project, total_pages from wiki_stats;
|
30
|
+
primary_keys hour,project
|
31
|
+
interval 1m
|
32
|
+
tag replicator.pipeline.wiki_stats.${event}.${primary_keys}
|
33
|
+
</source>
|
34
|
+
```
|
32
35
|
|
33
36
|
## Contributing
|
34
37
|
|
@@ -36,5 +39,5 @@ Bug reports and pull requests are welcome.
|
|
36
39
|
|
37
40
|
## License
|
38
41
|
|
39
|
-
- Copyright (c)
|
42
|
+
- Copyright (c) 2016 innossh
|
40
43
|
- [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
|
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |gem|
|
6
6
|
gem.name = "fluent-plugin-postgres-replicator"
|
7
|
-
gem.version = "0.0.
|
7
|
+
gem.version = "0.0.2"
|
8
8
|
gem.authors = ["innossh"]
|
9
9
|
gem.email = ["innossh@users.noreply.github.com"]
|
10
10
|
|
@@ -6,7 +6,7 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
|
|
6
6
|
config_param :username, :string, :default => 'root'
|
7
7
|
config_param :password, :string, :default => nil, :secret => true
|
8
8
|
config_param :database, :string, :default => nil
|
9
|
-
config_param :
|
9
|
+
config_param :sql, :string, :default => nil
|
10
10
|
config_param :primary_keys, :string, :default => nil
|
11
11
|
config_param :interval, :string, :default => '10s'
|
12
12
|
config_param :tag, :string, :default => nil
|
@@ -26,7 +26,7 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
|
|
26
26
|
if @tag.nil?
|
27
27
|
raise Fluent::ConfigError, "tag MUST be specified"
|
28
28
|
end
|
29
|
-
@primary_keys = @primary_keys.split(
|
29
|
+
@primary_keys = @primary_keys.split(/\s*,\s*/)
|
30
30
|
end
|
31
31
|
|
32
32
|
def start
|
@@ -41,8 +41,8 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
|
|
41
41
|
begin
|
42
42
|
poll
|
43
43
|
rescue StandardError => e
|
44
|
-
|
45
|
-
|
44
|
+
log.error "failed to execute query. error: #{e.message}"
|
45
|
+
log.error e.backtrace.join("\n")
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
@@ -52,15 +52,17 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
|
|
52
52
|
loop do
|
53
53
|
rows_count = 0
|
54
54
|
start_time = Time.now
|
55
|
-
rows, conn = query(@
|
55
|
+
rows, conn = query(@sql, conn)
|
56
56
|
rows.each do |row|
|
57
57
|
row_ids = Array.new
|
58
58
|
@primary_keys.each do |primary_key|
|
59
|
-
if row[primary_key].nil?
|
60
|
-
|
61
|
-
break
|
59
|
+
if !row[primary_key].nil?
|
60
|
+
row_ids << row[primary_key]
|
62
61
|
end
|
63
|
-
|
62
|
+
end
|
63
|
+
if row_ids.size != @primary_keys.size
|
64
|
+
log.error "primary_keys column value is something wrong. :tag=>#{@tag} :primary_keys=>#{@primary_keys}"
|
65
|
+
break
|
64
66
|
end
|
65
67
|
|
66
68
|
hash_value_id = row_ids.join('_')
|
@@ -77,18 +79,18 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
|
|
77
79
|
end
|
78
80
|
conn.close
|
79
81
|
elapsed_time = sprintf('%0.02f', Time.now - start_time)
|
80
|
-
|
82
|
+
log.info "success to execute replicator. :tag=>#{@tag} :rows_count=>#{rows_count} :elapsed_time=>#{elapsed_time} sec"
|
81
83
|
sleep @interval
|
82
84
|
end
|
83
85
|
|
84
86
|
end
|
85
87
|
|
86
|
-
def query(
|
88
|
+
def query(sql, conn = nil)
|
87
89
|
begin
|
88
90
|
conn = (conn.nil? || conn.finished?) ? get_connection : conn
|
89
|
-
return conn.query(
|
91
|
+
return conn.query(sql), conn
|
90
92
|
rescue Exception => e
|
91
|
-
|
93
|
+
log.warn "failed to execute query and will retry. error: #{e}"
|
92
94
|
sleep @interval
|
93
95
|
retry
|
94
96
|
end
|
@@ -97,7 +99,7 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
|
|
97
99
|
def format_tag(tag, param)
|
98
100
|
pattern = {'${event}' => param[:event].to_s, '${primary_keys}' => @primary_keys.join('_')}
|
99
101
|
tag.gsub(/(\${[a-z_]+})/) do
|
100
|
-
|
102
|
+
log.warn "placeholder value is not found. :tag=>#{tag} :placeholder=>#{$1}" unless pattern.include?($1)
|
101
103
|
pattern[$1]
|
102
104
|
end
|
103
105
|
end
|
@@ -116,7 +118,7 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
|
|
116
118
|
:dbname => @database
|
117
119
|
})
|
118
120
|
rescue Exception => e
|
119
|
-
|
121
|
+
log.warn "failed to get connection and will retry. error: #{e}"
|
120
122
|
sleep @interval
|
121
123
|
retry
|
122
124
|
end
|
@@ -5,13 +5,18 @@ require 'fluent/plugin/in_postgres_replicator'
|
|
5
5
|
class PostgresReplicatorTest < Test::Unit::TestCase
|
6
6
|
def setup
|
7
7
|
Fluent::Test.setup
|
8
|
+
@time = Fluent::Engine.now
|
8
9
|
end
|
9
10
|
|
10
11
|
CONFIG = %[
|
11
12
|
host 127.0.0.1
|
12
13
|
port 5432
|
14
|
+
username postgres
|
15
|
+
database pg_repli_test_db
|
16
|
+
sql SELECT id, total from pg_repli_test_table;
|
17
|
+
interval 1s
|
13
18
|
primary_keys id
|
14
|
-
tag pgreplicator.
|
19
|
+
tag pgreplicator.pg_repli_test_db.pg_repli_test_table.${event}.${primary_keys}
|
15
20
|
]
|
16
21
|
|
17
22
|
def create_driver(conf = CONFIG)
|
@@ -26,8 +31,26 @@ class PostgresReplicatorTest < Test::Unit::TestCase
|
|
26
31
|
d = create_driver
|
27
32
|
assert_equal '127.0.0.1', d.instance.host
|
28
33
|
assert_equal 5432, d.instance.port
|
29
|
-
assert_equal
|
30
|
-
assert_equal
|
34
|
+
assert_equal 'postgres', d.instance.username
|
35
|
+
assert_equal nil, d.instance.password
|
36
|
+
assert_equal 'pg_repli_test_db', d.instance.database
|
37
|
+
assert_equal 'SELECT id, total from pg_repli_test_table;', d.instance.sql
|
38
|
+
assert_equal 1, d.instance.interval
|
39
|
+
assert_equal ['id'], d.instance.primary_keys
|
40
|
+
assert_equal 'pgreplicator.pg_repli_test_db.pg_repli_test_table.${event}.${primary_keys}', d.instance.tag
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_emit
|
44
|
+
d = create_driver
|
45
|
+
|
46
|
+
d.run do
|
47
|
+
sleep 2
|
48
|
+
end
|
49
|
+
|
50
|
+
emits = d.emits
|
51
|
+
assert_equal true, emits.length > 0
|
52
|
+
assert_equal ['pgreplicator.pg_repli_test_db.pg_repli_test_table.insert.id', @time, {'id' => '1', 'total' => '10'}], emits[0]
|
53
|
+
assert_equal ['pgreplicator.pg_repli_test_db.pg_repli_test_table.insert.id', @time, {'id' => '2', 'total' => '20'}], emits[1]
|
31
54
|
end
|
32
55
|
|
33
56
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fluent-plugin-postgres-replicator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- innossh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fluentd
|