proxysqlweightlifter 0.1.0 → 0.2.0

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
  SHA256:
3
- metadata.gz: 4083bbfbafb2c33b3237ab1344346b3a8c38c2f1855a618226c6bf037ff008d8
4
- data.tar.gz: 0d090098060d10351ca4f06b935ee6332848dd9412863be9072920000268a574
3
+ metadata.gz: d20c202dbb5e6fd963e4d5b58f52a4ac7766bfcd43009e79cbf8b10b0dc7272e
4
+ data.tar.gz: bb9faa6666c5babf550175365aae062fd11d9e5853596039cdd4a2fd7354a300
5
5
  SHA512:
6
- metadata.gz: 03d8850273413d02f80c9b364dc9bb40f87ce5f4d439c134fdf0ebb4e1d85bca979d27f74e7f6c91e718ae9f8f4ea7f82a6ec9a208a0b3c7ce7d4bc1c01fd9a3
7
- data.tar.gz: 260ec3a0299528edd9a8c121728464e6d3dddff2d0904bb29def9c4411bc85eb79f949c907c22fadc16bc51990d72cba26b9bebc28d27ed4be5acac99bbe314b
6
+ metadata.gz: f05d6b3a374739a7aa845f1c5b7ce778fb1dc8dd3e125b6a7e79d79133a83328c211002b844f5213a73127e9ac76e4a6f21c319348df4e694bd97836201a47e0
7
+ data.tar.gz: e762f7d8f221ce83d08215f391952dc494489ea1fad5136d78033b793bd012ab2cfd7dd965d88c84b5b9eb225d266b84a27d07a2b6e357aa51c881984e43f246
@@ -1,12 +1,12 @@
1
1
  require 'mysql2'
2
- require "proxysqlweightlifter/version"
3
- require "proxysqlweightlifter/lift"
2
+ require 'proxysqlweightlifter/version'
3
+ require 'proxysqlweightlifter/lift'
4
4
 
5
5
  module Proxysqlweightlifter
6
6
  class Error < StandardError; end
7
7
 
8
8
  def self.run(argv)
9
- host, port, user, pass, weight = argv
9
+ host, port, user, pass, weight, custom_weight = argv
10
10
  Lift.new(
11
11
  Mysql2::Client.new(
12
12
  host: host,
@@ -15,7 +15,8 @@ module Proxysqlweightlifter
15
15
  password: pass,
16
16
  database: 'main'
17
17
  ),
18
- weight || 9
18
+ weight || 9,
19
+ custom_weight
19
20
  ).run
20
21
  end
21
22
  end
@@ -2,11 +2,12 @@ module Proxysqlweightlifter
2
2
  # Lift is responsible for resolving current slaves from
3
3
  # mysql_replication_hostgroups table
4
4
  class Lift
5
- attr_reader :db, :weight
5
+ attr_reader :db, :default_weight
6
6
 
7
- def initialize(database, weight = 10)
7
+ def initialize(database, default_weight = 10, custom_weight = '')
8
8
  @db = database
9
- @weight = weight
9
+ @default_weight = default_weight
10
+ @custom_weight = custom_weight.to_s
10
11
  end
11
12
 
12
13
  def run
@@ -30,6 +31,16 @@ module Proxysqlweightlifter
30
31
  end
31
32
  end
32
33
 
34
+ def hostgroup_weights
35
+ @hostgroup_weights ||= @custom_weight.split(',').map do |cs|
36
+ hostgroup_id, group_id, weight = cs.split(':')
37
+ raise 'Invalid or unsupported format: hostgroup_id:9100:70' if hostgroup_id != 'hostgroup_id'
38
+ {
39
+ group_id.to_i => weight.to_i
40
+ }
41
+ end.reduce(&:merge) || {}
42
+ end
43
+
33
44
  def hostgroups
34
45
  db.query(%(
35
46
  SELECT writer_hostgroup,reader_hostgroup
@@ -69,9 +80,10 @@ module Proxysqlweightlifter
69
80
  end
70
81
 
71
82
  def update_weight(hostname:, port:, hostgroup_id:)
83
+ weight = hostgroup_weights[hostgroup_id] || default_weight || 1
72
84
  db.query(%(
73
85
  UPDATE mysql_servers
74
- SET weight = #{weight || 1}
86
+ SET weight = #{weight}
75
87
  WHERE hostname = "#{hostname}" AND port = "#{port}" AND hostgroup_id = "#{hostgroup_id}";
76
88
  ))
77
89
  end
@@ -1,3 +1,3 @@
1
1
  module Proxysqlweightlifter
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proxysqlweightlifter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernestas Poskus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-12 00:00:00.000000000 Z
11
+ date: 2019-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2