mysql_replica_finder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95f6ba6c974950ac0e1c522540246ea3ff176853
4
+ data.tar.gz: 0161dc8b905417f7bcede666240b552a8a7b4e9a
5
+ SHA512:
6
+ metadata.gz: 32e24fdf38e91e1ba5edd67ab8f4dfc4f52a138d0d72f9da893dfb99e8d17b818341c3f36492f49dda9f38af39cb887e973c924ecba6d229e27980ed1eb56e5a
7
+ data.tar.gz: 1ca07b576e2b3d860e9c002144a78393b4d39b8038d3bd47798c63396f5d613dc499eeafc4a55dad273c2409224b9ba9d8abc761d53f93dece2c660cffa64620
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mysql_replica_finder.gemspec
4
+ gemspec
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mysql_replica_finder (0.0.1)
5
+ mysql2
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ mysql2 (0.3.16)
11
+ rake (10.3.2)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.5)
18
+ mysql_replica_finder!
19
+ rake
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Sam Lambert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # MysqlReplicaFinder
2
+
3
+ A gem that finds all replicas on a given mysql2 connection. MysqlReplicaFinder can also recurse through a replication topology and find all replicas in the chain.
4
+
5
+ ## Usage
6
+
7
+ Pass in a connection and whether you would like to recurse:
8
+
9
+ `finder = MysqlReplicaFinder::Finder.new(connection, false)`
10
+
11
+ Then call:
12
+
13
+ `finder.replicas`
14
+
15
+ #### Recurse = true
16
+ If recurse is true then MysqlReplicaFinder will make a new connection to each slave using the same credentials as the passed connection. It will use each connection to navigate down the replication chain.
17
+
18
+ ## Example
19
+
20
+ Run:
21
+
22
+ `ruby example/example.rb <master_host> <user> <password>`
23
+
24
+ to return a list of replicas.
25
+
26
+ ## Installation
27
+
28
+ Add this line to your application's Gemfile:
29
+
30
+ gem 'mysql_replica_finder'
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install mysql_replica_finder
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it ( http://github.com/samlambert/mysql_replica_finder/fork )
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,12 @@
1
+
2
+ require 'mysql2'
3
+ require_relative '../lib/mysql_replica_finder.rb'
4
+
5
+ def client(host, username, password)
6
+ @client ||= Mysql2::Client.new(:host => host, :username => username, :password => password)
7
+ end
8
+
9
+ connection = client(ARGV[0], ARGV[1], ARGV[2])
10
+
11
+ thing = MysqlReplicaFinder::Finder.new(connection, false)
12
+ puts thing.replicas
@@ -0,0 +1,55 @@
1
+ require 'mysql2'
2
+
3
+ module MysqlReplicaFinder
4
+
5
+ class Finder
6
+
7
+ def initialize(connection, recurse=false)
8
+ @replicas = []
9
+ @primary_connection = connection
10
+ @recurse = recurse
11
+ end
12
+
13
+ def fetch
14
+ unless @recurse
15
+ @replicas << replica_hosts_from_connection(@primary_connection)
16
+ else
17
+ replica_hosts_from_connection_rec(@primary_connection)
18
+ end
19
+ end
20
+
21
+ def replicas
22
+ fetch
23
+ @replicas
24
+ end
25
+
26
+ # Returns an array or replicas that are connected
27
+ def replica_hosts_from_connection(connection)
28
+ replicas = []
29
+ rows = select_all(connection, "SHOW PROCESSLIST")
30
+ rows.each do |row|
31
+ if row["Command"] == "Binlog Dump"
32
+ replicas.push(row['Host'].split(":")[0])
33
+ end
34
+ end
35
+ replicas
36
+ end
37
+
38
+ def select_all(connection, query)
39
+ connection.query(query)
40
+ end
41
+
42
+ def transform_replica_to_connection(host)
43
+ Mysql2::Client.new(@primary_connection.query_options.merge({ :host => host}))
44
+ end
45
+
46
+ def replica_hosts_from_connection_rec(connection)
47
+ replica_hosts_from_connection(connection).map do |replica|
48
+ @replicas << replica
49
+ replica_hosts_from_connection_rec(transform_replica_to_connection(replica))
50
+ end
51
+ end
52
+
53
+ end
54
+ end
55
+
@@ -0,0 +1,3 @@
1
+ module MysqlReplicaFinder
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mysql_replica_finder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mysql_replica_finder"
8
+ spec.version = MysqlReplicaFinder::VERSION
9
+ spec.authors = ["Sam Lambert"]
10
+ spec.email = ["sam.lambert@github.com"]
11
+ spec.summary = "Get replicas from mysql2 connection"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "mysql2"
20
+ spec.add_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "rake"
22
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql_replica_finder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sam Lambert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mysql2
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
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
+ description:
56
+ email:
57
+ - sam.lambert@github.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - example/example.rb
69
+ - lib/mysql_replica_finder.rb
70
+ - lib/mysql_replica_finder/version.rb
71
+ - mysql_replica_finder.gemspec
72
+ homepage:
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.3
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Get replicas from mysql2 connection
96
+ test_files: []