galerab 0.0.2 → 0.0.3
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.
- data/bin/galerab +32 -33
- data/files/galerab.yml +7 -17
- data/lib/galerab/configuration.rb +11 -0
- data/lib/galerab/proxy.rb +1 -11
- data/spec/lib/backend_spec.rb +2 -34
- data/spec/lib/configuration_spec.rb +33 -18
- data/spec/spec_helper.rb +1 -1
- metadata +1 -1
data/bin/galerab
CHANGED
@@ -16,44 +16,42 @@ end
|
|
16
16
|
|
17
17
|
|
18
18
|
puts "[#{Process.pid}] galerab starting with config file at #{ARGV[1]}"
|
19
|
+
configuration = Configuration.new(path)
|
20
|
+
conf = configuration.conf
|
21
|
+
backend = Backend.new(conf)
|
19
22
|
|
20
|
-
|
23
|
+
balancer = fork { BalancingProxy::Server.run(backend) }
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
"
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
puts "#{next_backend} is ready"
|
43
|
-
backend.not_ready.delete(next_backend) if backend.not_ready
|
44
|
-
end
|
25
|
+
checker = fork do
|
26
|
+
while true
|
27
|
+
# Here we check if the backend is ready
|
28
|
+
next_backend = backend.get_next
|
29
|
+
puts "\nChecking if (wsrep_ready == ON) #{next_backend}\n"
|
30
|
+
|
31
|
+
if next_backend
|
32
|
+
begin
|
33
|
+
connection = Sequel.connect(
|
34
|
+
"mysql://#{conf["user"]}:#{conf["password"]}@#{next_backend}/#{conf["database"]}"
|
35
|
+
).fetch("show status like 'wsrep_ready'") do |row|
|
36
|
+
|
37
|
+
if row[:Value] == "OFF"
|
38
|
+
puts "#{next_backend} is not ready"
|
39
|
+
configuration.remove_backend(next_backend)
|
40
|
+
Process.kill(:SIGHUP, balancer)
|
41
|
+
elsif row[:Value] == "ON"
|
42
|
+
puts "#{next_backend} is ready"
|
43
|
+
configuration.add_backend(next_backend)
|
44
|
+
Process.kill(:SIGHUP, balancer)
|
45
45
|
end
|
46
|
-
rescue
|
47
|
-
puts "#{next_backend} is not ready"
|
48
|
-
backend.not_ready.push(next_backend) unless backend.not_ready.include?(next_backend)
|
49
46
|
end
|
50
|
-
|
51
|
-
|
47
|
+
rescue Exception => msg
|
48
|
+
puts "#{next_backend} is not ready #{msg}"
|
49
|
+
configuration.remove_backend(next_backend)
|
50
|
+
Process.kill(:SIGHUP, balancer)
|
52
51
|
end
|
53
52
|
end
|
54
53
|
|
55
|
-
|
56
|
-
BalancingProxy::Server.run(backend)
|
54
|
+
sleep conf["check_every"]
|
57
55
|
end
|
58
56
|
end
|
59
57
|
|
@@ -63,5 +61,6 @@ trap (:SIGHUP) do
|
|
63
61
|
$stdout.puts backend.conf["balancer_ports"].inspect
|
64
62
|
end
|
65
63
|
|
66
|
-
|
67
|
-
|
64
|
+
Process.waitpid2(checker)
|
65
|
+
Process.waitpid2(balancer)
|
66
|
+
#connection = Sequel.connect("mysql://root:root@192.168.2.11/mysql").fetch("show status like 'wsrep_ready'") {puts row[:Value]}
|
data/files/galerab.yml
CHANGED
@@ -1,17 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
backend_port: 3306
|
9
|
-
|
10
|
-
2:
|
11
|
-
backends: ["192.168.1.11"]
|
12
|
-
check_every: 10
|
13
|
-
user: root
|
14
|
-
password: root
|
15
|
-
database: mysql
|
16
|
-
balancer_port: 3308
|
17
|
-
backend_port: 3306
|
1
|
+
backends: ["192.168.2.11"]
|
2
|
+
check_every: 3
|
3
|
+
user: root
|
4
|
+
password: root
|
5
|
+
database: mysql
|
6
|
+
balancer_port: 3308
|
7
|
+
backend_port: 3306
|
@@ -3,5 +3,16 @@ class Configuration
|
|
3
3
|
|
4
4
|
def initialize(path)
|
5
5
|
@conf = YAML.load_file(path)
|
6
|
+
@path = path
|
7
|
+
end
|
8
|
+
|
9
|
+
def remove_backend(address)
|
10
|
+
@conf['backends'].delete(address)
|
11
|
+
File.open(@path, 'w+') {|f| f.write(@conf.to_yaml) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_backend(address)
|
15
|
+
@conf['backends'] << address unless @conf['backends'].include?(address)
|
16
|
+
File.open(@path, 'w+') {|f| f.write(@conf.to_yaml) }
|
6
17
|
end
|
7
18
|
end
|
data/lib/galerab/proxy.rb
CHANGED
@@ -2,15 +2,5 @@ class Proxy
|
|
2
2
|
def self.stop
|
3
3
|
puts "Terminating ProxyServer"
|
4
4
|
EventMachine.stop
|
5
|
-
end
|
6
|
-
|
7
|
-
# this is not used
|
8
|
-
def self.kill_childs(servers, signal)
|
9
|
-
servers.each do |server|
|
10
|
-
puts "Killing process #{server[:pid]} with #{signal} signal."
|
11
|
-
Process.kill(signal, server[:pid])
|
12
|
-
end
|
13
|
-
|
14
|
-
Process.kill(signal, Process.pid)
|
15
|
-
end
|
5
|
+
end
|
16
6
|
end
|
data/spec/lib/backend_spec.rb
CHANGED
@@ -1,41 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Configuration do
|
4
|
-
before(:each) do
|
5
|
-
@conf = Configuration.new(ENV['GALERAB_CONFIG_PATH']).conf
|
6
|
-
end
|
7
|
-
|
8
|
-
context "when loading the file" do
|
9
|
-
it "should load the YAML file" do
|
10
|
-
@conf.should be_true
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should find two balancers in the config file" do
|
14
|
-
@conf.size.should be == 2
|
15
|
-
end
|
16
|
-
|
17
|
-
it "gets the backends for balancer 1" do
|
18
|
-
@conf[1]['backends'].size.should be == 3
|
19
|
-
@conf[1]['backends'][0].should be == "192.168.10.101"
|
20
|
-
@conf[1]['backends'][1].should be == "192.168.10.102"
|
21
|
-
@conf[1]['backends'][2].should be == "192.168.10.103"
|
22
|
-
end
|
23
|
-
|
24
|
-
it "loads the other parameters" do
|
25
|
-
@conf[1]['check_every'].size.should be == 8
|
26
|
-
@conf[1]['user'].should be == "your_user"
|
27
|
-
@conf[1]['password'].should be == "your_password"
|
28
|
-
@conf[1]['database'].should be == "your_db"
|
29
|
-
@conf[1]['balancer_port'].should be == 3307
|
30
|
-
@conf[1]['backend_port'].should be == 3306
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
3
|
describe Backend do
|
36
4
|
before(:each) do
|
37
|
-
@conf = Configuration.new(ENV['GALERAB_CONFIG_PATH']).conf
|
38
|
-
@backend = Backend.new(@conf
|
5
|
+
@conf = Configuration.new(ENV['GALERAB_CONFIG_PATH'] + '/galerab.yml').conf
|
6
|
+
@backend = Backend.new(@conf)
|
39
7
|
end
|
40
8
|
|
41
9
|
context "round robin load balancing" do
|
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
1
|
describe Configuration do
|
4
2
|
before(:each) do
|
5
|
-
|
3
|
+
%x[cp #{ENV['GALERAB_CONFIG_PATH'] + '/galerab.yml.backup'} #{ENV['GALERAB_CONFIG_PATH'] + '/galerab.yml'}]
|
4
|
+
@configuration = Configuration.new(ENV['GALERAB_CONFIG_PATH'] + '/galerab.yml')
|
5
|
+
@conf = @configuration.conf
|
6
6
|
end
|
7
7
|
|
8
8
|
context "when loading the file" do
|
@@ -10,24 +10,39 @@ describe Configuration do
|
|
10
10
|
@conf.should be_true
|
11
11
|
end
|
12
12
|
|
13
|
-
it "should find two balancers in the config file" do
|
14
|
-
@conf.size.should be == 2
|
15
|
-
end
|
16
|
-
|
17
13
|
it "gets the backends for balancer 1" do
|
18
|
-
@conf[
|
19
|
-
@conf[
|
20
|
-
@conf[
|
21
|
-
@conf[
|
14
|
+
@conf['backends'].size.should be == 3
|
15
|
+
@conf['backends'][0].should be == "192.168.10.101"
|
16
|
+
@conf['backends'][1].should be == "192.168.10.102"
|
17
|
+
@conf['backends'][2].should be == "192.168.10.103"
|
22
18
|
end
|
23
19
|
|
24
20
|
it "loads the other parameters" do
|
25
|
-
@conf[
|
26
|
-
@conf[
|
27
|
-
@conf[
|
28
|
-
@conf[
|
29
|
-
@conf[
|
30
|
-
@conf[
|
21
|
+
@conf['check_every'].size.should be == 8
|
22
|
+
@conf['user'].should be == "your_user"
|
23
|
+
@conf['password'].should be == "your_password"
|
24
|
+
@conf['database'].should be == "your_db"
|
25
|
+
@conf['balancer_port'].should be == 3307
|
26
|
+
@conf['backend_port'].should be == 3306
|
31
27
|
end
|
32
28
|
end
|
33
|
-
|
29
|
+
|
30
|
+
it "should remove a backend from the config file" do
|
31
|
+
@configuration.remove_backend("192.168.10.103")
|
32
|
+
@conf = @configuration.conf
|
33
|
+
@conf['backends'].should_not include("192.168.10.103")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should add a backend to the config file" do
|
37
|
+
@configuration.remove_backend("192.168.10.103") # make sure there is not that address
|
38
|
+
@configuration.add_backend("192.168.10.103")
|
39
|
+
@conf = @configuration.conf
|
40
|
+
@conf['backends'].should include("192.168.10.103")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not add another backend if it is already in the list" do
|
44
|
+
@conf['backends'].size.should be == 3
|
45
|
+
@configuration.add_backend("192.168.10.103")
|
46
|
+
@conf['backends'].size.should be == 3
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
CHANGED