fluent-plugin-postgres-replicator 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ca7d5dc0cc16563d50a913c50da4d89f130ceb3
4
+ data.tar.gz: a12eaccf0624403b32359673935b6614ed367363
5
+ SHA512:
6
+ metadata.gz: c5d817d1e75134379f26443e0106b28a1afc2e03de7b94467cc8a21660169365a6295da941e6bad9cf25373e066acfd3ec7628ce09c13a4c1d1f3fe749fd846e
7
+ data.tar.gz: 13db91bfb686ed5eeec844d50a68eb55f72f7e82495223191f27b2d2f9ee2b92e65ac7bcaeaf3923f20b65f9416069a69eaf825c77677c8840e6ad869c0c6b4d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-postgres-replicator.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Fluent::Plugin::Postgres::Replicator
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fluent/plugin/postgres/replicator`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fluent-plugin-postgres-replicator'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fluent-plugin-postgres-replicator
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome.
36
+
37
+ ## License
38
+
39
+ - Copyright (c) 2015 innossh
40
+ - [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ Rake::TestTask.new(:test) do |test|
4
+ test.libs << 'lib' << 'test'
5
+ test.pattern = 'test/**/test_*.rb'
6
+ test.verbose = true
7
+ end
8
+
9
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fluent/plugin/postgres/replicator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "fluent-plugin-postgres-replicator"
7
+ gem.version = "0.0.1"
8
+ gem.authors = ["innossh"]
9
+ gem.email = ["innossh@users.noreply.github.com"]
10
+
11
+ gem.summary = %q{PostgreSQL replication input plugin for Fluent}
12
+ gem.homepage = "https://github.com/innossh/fluent-plugin-postgres-replicator"
13
+ gem.license = "Apache-2.0"
14
+
15
+ gem.files = `git ls-files -z`.split("\x0")
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "fluentd"
21
+ gem.add_runtime_dependency "pg"
22
+ gem.add_development_dependency "rake"
23
+ gem.add_development_dependency "test-unit"
24
+ end
@@ -0,0 +1,124 @@
1
+ class Fluent::PostgresReplicatorInput < Fluent::Input
2
+ Fluent::Plugin.register_input('postgres_replicator', self)
3
+
4
+ config_param :host, :string, :default => 'localhost'
5
+ config_param :port, :integer, :default => 5432
6
+ config_param :username, :string, :default => 'root'
7
+ config_param :password, :string, :default => nil, :secret => true
8
+ config_param :database, :string, :default => nil
9
+ config_param :query, :string, :default => nil
10
+ config_param :primary_keys, :string, :default => nil
11
+ config_param :interval, :string, :default => '10s'
12
+ config_param :tag, :string, :default => nil
13
+
14
+ def initialize
15
+ super
16
+ require 'pg'
17
+ require 'digest/sha1'
18
+ end
19
+
20
+ def configure(conf)
21
+ super
22
+ @interval = Fluent::Config.time_value(@interval)
23
+ if @primary_keys.nil?
24
+ raise Fluent::ConfigError, "primary_keys MUST be specified"
25
+ end
26
+ if @tag.nil?
27
+ raise Fluent::ConfigError, "tag MUST be specified"
28
+ end
29
+ @primary_keys = @primary_keys.split(',')
30
+ end
31
+
32
+ def start
33
+ @thread = Thread.new(&method(:run))
34
+ end
35
+
36
+ def shutdown
37
+ Thread.kill(@thread)
38
+ end
39
+
40
+ def run
41
+ begin
42
+ poll
43
+ rescue StandardError => e
44
+ $log.error "failed to execute query. error: #{e.message}"
45
+ $log.error e.backtrace.join("\n")
46
+ end
47
+ end
48
+
49
+ def poll
50
+ hash_values = Hash.new
51
+ conn = get_connection()
52
+ loop do
53
+ rows_count = 0
54
+ start_time = Time.now
55
+ rows, conn = query(@query, conn)
56
+ rows.each do |row|
57
+ row_ids = Array.new
58
+ @primary_keys.each do |primary_key|
59
+ if row[primary_key].nil?
60
+ $log.error "primary_key value is not found. :tag=>#{@tag} :primary_key=>#{primary_key}"
61
+ break
62
+ end
63
+ row_ids << row[primary_key]
64
+ end
65
+
66
+ hash_value_id = row_ids.join('_')
67
+ hash_value = Digest::SHA1.hexdigest(row.flatten.join)
68
+ if !hash_values.include?(hash_value_id)
69
+ tag = format_tag(@tag, {:event => :insert})
70
+ emit_record(tag, row)
71
+ elsif hash_values[hash_value_id] != hash_value
72
+ tag = format_tag(@tag, {:event => :update})
73
+ emit_record(tag, row)
74
+ end
75
+ hash_values[hash_value_id] = hash_value
76
+ rows_count += 1
77
+ end
78
+ conn.close
79
+ elapsed_time = sprintf('%0.02f', Time.now - start_time)
80
+ $log.info "success to execute replicator. :tag=>#{@tag} :rows_count=>#{rows_count} :elapsed_time=>#{elapsed_time} sec"
81
+ sleep @interval
82
+ end
83
+
84
+ end
85
+
86
+ def query(query, conn = nil)
87
+ begin
88
+ conn = (conn.nil? || conn.finished?) ? get_connection : conn
89
+ return conn.query(query), conn
90
+ rescue Exception => e
91
+ $log.warn "failed to execute query and will retry. error: #{e}"
92
+ sleep @interval
93
+ retry
94
+ end
95
+ end
96
+
97
+ def format_tag(tag, param)
98
+ pattern = {'${event}' => param[:event].to_s, '${primary_keys}' => @primary_keys.join('_')}
99
+ tag.gsub(/(\${[a-z_]+})/) do
100
+ $log.warn "placeholder value is not found. :tag=>#{tag} :placeholder=>#{$1}" unless pattern.include?($1)
101
+ pattern[$1]
102
+ end
103
+ end
104
+
105
+ def emit_record(tag, record)
106
+ router.emit(tag, Fluent::Engine.now, record)
107
+ end
108
+
109
+ def get_connection
110
+ begin
111
+ return PG::Connection.new({
112
+ :host => @host,
113
+ :port => @port,
114
+ :user => @username,
115
+ :password => @password,
116
+ :dbname => @database
117
+ })
118
+ rescue Exception => e
119
+ $log.warn "failed to get connection and will retry. error: #{e}"
120
+ sleep @interval
121
+ retry
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,33 @@
1
+ require 'test/unit'
2
+ require 'fluent/test'
3
+ require 'fluent/plugin/in_postgres_replicator'
4
+
5
+ class PostgresReplicatorTest < Test::Unit::TestCase
6
+ def setup
7
+ Fluent::Test.setup
8
+ end
9
+
10
+ CONFIG = %[
11
+ host 127.0.0.1
12
+ port 5432
13
+ primary_keys id
14
+ tag pgreplicator.db.table.${event}.${primary_keys}
15
+ ]
16
+
17
+ def create_driver(conf = CONFIG)
18
+ Fluent::Test::InputTestDriver.new(Fluent::PostgresReplicatorInput).configure(conf)
19
+ end
20
+
21
+ def test_configure
22
+ assert_raise(Fluent::ConfigError) {
23
+ create_driver('')
24
+ }
25
+
26
+ d = create_driver
27
+ assert_equal '127.0.0.1', d.instance.host
28
+ assert_equal 5432, d.instance.port
29
+ assert_equal [ 'id' ], d.instance.primary_keys
30
+ assert_equal 'pgreplicator.db.table.${event}.${primary_keys}', d.instance.tag
31
+ end
32
+
33
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-postgres-replicator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - innossh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fluentd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - innossh@users.noreply.github.com
72
+ executables:
73
+ - console
74
+ - setup
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - fluent-plugin-postgres-replicator.gemspec
86
+ - lib/fluent/plugin/in_postgres_replicator.rb
87
+ - test/plugin/test_in_postgres_replicator.rb
88
+ homepage: https://github.com/innossh/fluent-plugin-postgres-replicator
89
+ licenses:
90
+ - Apache-2.0
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.5.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: PostgreSQL replication input plugin for Fluent
112
+ test_files:
113
+ - test/plugin/test_in_postgres_replicator.rb