proxysqlweightlifter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4083bbfbafb2c33b3237ab1344346b3a8c38c2f1855a618226c6bf037ff008d8
4
+ data.tar.gz: 0d090098060d10351ca4f06b935ee6332848dd9412863be9072920000268a574
5
+ SHA512:
6
+ metadata.gz: 03d8850273413d02f80c9b364dc9bb40f87ce5f4d439c134fdf0ebb4e1d85bca979d27f74e7f6c91e718ae9f8f4ea7f82a6ec9a208a0b3c7ce7d4bc1c01fd9a3
7
+ data.tar.gz: 260ec3a0299528edd9a8c121728464e6d3dddff2d0904bb29def9c4411bc85eb79f949c907c22fadc16bc51990d72cba26b9bebc28d27ed4be5acac99bbe314b
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'proxysqlweightlifter'
4
+ Proxysqlweightlifter.run(ARGV)
@@ -0,0 +1,21 @@
1
+ require 'mysql2'
2
+ require "proxysqlweightlifter/version"
3
+ require "proxysqlweightlifter/lift"
4
+
5
+ module Proxysqlweightlifter
6
+ class Error < StandardError; end
7
+
8
+ def self.run(argv)
9
+ host, port, user, pass, weight = argv
10
+ Lift.new(
11
+ Mysql2::Client.new(
12
+ host: host,
13
+ port: port,
14
+ username: user,
15
+ password: pass,
16
+ database: 'main'
17
+ ),
18
+ weight || 9
19
+ ).run
20
+ end
21
+ end
@@ -0,0 +1,86 @@
1
+ module Proxysqlweightlifter
2
+ # Lift is responsible for resolving current slaves from
3
+ # mysql_replication_hostgroups table
4
+ class Lift
5
+ attr_reader :db, :weight
6
+
7
+ def initialize(database, weight = 10)
8
+ @db = database
9
+ @weight = weight
10
+ end
11
+
12
+ def run
13
+ # We reset all weights to 1 because during runs of
14
+ # lift master might have switched
15
+ reset_weight
16
+
17
+ hostgroups.each do |hostgroup|
18
+ find_slaves(hostgroup).map do |slave|
19
+ slave.delete(:weight)
20
+ update_weight(slave)
21
+ end
22
+
23
+ debug find_slaves(hostgroup)
24
+ end
25
+
26
+ begin
27
+ db.query('LOAD MYSQL SERVERS TO RUNTIME;')
28
+ rescue => e
29
+ puts "LOAD MYSQL SERVERS TO RUNTIME error #{e}"
30
+ end
31
+ end
32
+
33
+ def hostgroups
34
+ db.query(%(
35
+ SELECT writer_hostgroup,reader_hostgroup
36
+ FROM mysql_replication_hostgroups;
37
+ )).map { |slave| Hash[slave.map{|(k,v)| [k.to_sym,v]}] }
38
+ end
39
+
40
+ def find_master(writer_hostgroup)
41
+ master = db.query(%(
42
+ SELECT hostname
43
+ FROM mysql_servers
44
+ WHERE hostgroup_id IN (#{writer_hostgroup});
45
+ ))
46
+ if master.size > 1
47
+ raise "More than 1 `writer` #{master} found in #{writer_hostgroup}"
48
+ end
49
+ master.map { |m| m['hostname'] }
50
+ end
51
+
52
+ def find_slaves(writer_hostgroup:, reader_hostgroup:)
53
+ master = find_master(writer_hostgroup)
54
+ master_ip = master.first
55
+
56
+ slaves = db.query(%(
57
+ SELECT hostgroup_id, hostname, port, weight
58
+ FROM mysql_servers
59
+ WHERE hostgroup_id IN (#{writer_hostgroup}, #{reader_hostgroup}) AND hostname != "#{master_ip}";
60
+ ))
61
+ if slaves.size.zero?
62
+ debug "No readers #{slaves} found in hostgroup `writer` #{writer_hostgroup} `reader` #{reader_hostgroup}"
63
+ end
64
+ slaves.map { |slave| Hash[slave.map{|(k,v)| [k.to_sym,v]}] }
65
+ end
66
+
67
+ def reset_weight(weight = 1)
68
+ db.query("UPDATE mysql_servers SET weight = #{weight};")
69
+ end
70
+
71
+ def update_weight(hostname:, port:, hostgroup_id:)
72
+ db.query(%(
73
+ UPDATE mysql_servers
74
+ SET weight = #{weight || 1}
75
+ WHERE hostname = "#{hostname}" AND port = "#{port}" AND hostgroup_id = "#{hostgroup_id}";
76
+ ))
77
+ end
78
+
79
+ private
80
+
81
+ def debug(s)
82
+ return
83
+ puts s
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module Proxysqlweightlifter
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proxysqlweightlifter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ernestas Poskus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-12 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.5.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.2
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.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: ProxySQL mysql_servers table automatic weight lifter for reader hosts
70
+ email:
71
+ - ernestas@vinted.com
72
+ executables:
73
+ - proxysqlweightlifter
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - bin/proxysqlweightlifter
78
+ - lib/proxysqlweightlifter.rb
79
+ - lib/proxysqlweightlifter/lift.rb
80
+ - lib/proxysqlweightlifter/version.rb
81
+ homepage: https://github.com/vinted/proxysqlweightlifter
82
+ licenses:
83
+ - MIT
84
+ metadata:
85
+ homepage_uri: https://github.com/vinted/proxysqlweightlifter
86
+ source_code_uri: https://github.com/vinted/proxysqlweightlifter
87
+ changelog_uri: https://github.com/vinted/proxysqlweightlifter/
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.7.6
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: ProxySQL mysql_servers table automatic weight lifter for reader hosts
108
+ test_files: []