ebisu_connection 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/README.md +8 -4
- data/ebisu_connection.gemspec +1 -1
- data/lib/ebisu_connection/conf_file.rb +3 -11
- data/lib/ebisu_connection/connection_manager.rb +8 -17
- data/lib/ebisu_connection/slave.rb +14 -13
- data/lib/ebisu_connection/slave_group.rb +7 -4
- data/lib/ebisu_connection/version.rb +1 -1
- data/lib/ebisu_connection.rb +14 -9
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/ebisu_connection_spec.rb +4 -0
- data/spec/unit/slave_group_spec.rb +3 -8
- data/spec/unit/slave_spec.rb +7 -15
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98f897fee0659bddbed6e4539387a46f2c43a72a
|
4
|
+
data.tar.gz: 8dcd5b499e30cd9bd32681e08ffa00cd7d338bb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 502269d5808c9dc2bb0d13e494d01fc15b726b9923f6dc8a2c736b732a8d587b82e57ef389c4c0fd8d1c8dca8af114a87a114924e0a8c73518ad007d7a03c915
|
7
|
+
data.tar.gz: 543a8f54458d6f5bba4f523a689ef59958d649fe7942474f547a5858454272d4981b529a8f593199ad31213e29a25e8fdea37f59190461712801a1a90b37f99f
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -133,9 +133,13 @@ Read query will be access to slave server.
|
|
133
133
|
Article.where(:id => 1)
|
134
134
|
Article.count
|
135
135
|
|
136
|
-
If you want to access to master
|
136
|
+
If you want to access to the master server, use read_master.
|
137
137
|
|
138
|
-
|
138
|
+
```ruby
|
139
|
+
Article.where(:id => 1).read_master
|
140
|
+
```
|
141
|
+
|
142
|
+
It is possible to use readonly(false) instead of read_master, but it will be depricated at future version.
|
139
143
|
|
140
144
|
In transaction, Always will be access to master server.
|
141
145
|
|
@@ -159,8 +163,8 @@ To run the test suite, you need mysql installed.
|
|
159
163
|
How to setup your test environment.
|
160
164
|
|
161
165
|
```bash
|
162
|
-
bundle install --path bundle
|
163
|
-
GEM_HOME
|
166
|
+
bundle install --path .bundle
|
167
|
+
GEM_HOME=.bundle/ruby/(your ruby version) gem install bundler --pre
|
164
168
|
bundle exec appraisal install
|
165
169
|
```
|
166
170
|
|
data/ebisu_connection.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency 'fresh_connection', '~> 0.
|
21
|
+
spec.add_dependency 'fresh_connection', '~> 0.4.0'
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.7"
|
24
24
|
spec.add_development_dependency "rake", '~> 10.0'
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
1
3
|
module EbisuConnection
|
2
4
|
class ConfFile
|
3
5
|
class << self
|
@@ -11,7 +13,6 @@ module EbisuConnection
|
|
11
13
|
|
12
14
|
def conf_clear!
|
13
15
|
@slaves_conf = nil
|
14
|
-
@spec = nil
|
15
16
|
end
|
16
17
|
|
17
18
|
def slaves_conf(slave_group)
|
@@ -23,11 +24,6 @@ module EbisuConnection
|
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
26
|
-
def spec(slave_group)
|
27
|
-
@spec ||= get_spec
|
28
|
-
@spec.merge(@spec[slave_group] || {})
|
29
|
-
end
|
30
|
-
|
31
27
|
def slaves_file
|
32
28
|
return @slaves_file if @slaves_file
|
33
29
|
raise "nothing slaves_file. You have to set a file path using EbisuConnection.slaves_file= method" unless defined?(Rails)
|
@@ -57,11 +53,7 @@ module EbisuConnection
|
|
57
53
|
def get_slaves_conf
|
58
54
|
@file_mtime = File.mtime(slaves_file)
|
59
55
|
conf = YAML.load_file(slaves_file)
|
60
|
-
conf[
|
61
|
-
end
|
62
|
-
|
63
|
-
def get_spec
|
64
|
-
ActiveRecord::Base.configurations[FreshConnection.env]
|
56
|
+
conf[EbisuConnection.env.to_s] || {}
|
65
57
|
end
|
66
58
|
end
|
67
59
|
end
|
@@ -1,10 +1,9 @@
|
|
1
|
-
require 'yaml'
|
2
1
|
require 'fresh_connection/abstract_connection_manager'
|
2
|
+
require 'ebisu_connection/conf_file'
|
3
|
+
require 'ebisu_connection/slave_group'
|
3
4
|
|
4
5
|
module EbisuConnection
|
5
6
|
class ConnectionManager < FreshConnection::AbstractConnectionManager
|
6
|
-
delegate :if_modify, :conf_clear!, :to => ConfFile
|
7
|
-
|
8
7
|
def initialize(slave_group = "slave")
|
9
8
|
super
|
10
9
|
@slaves = {}
|
@@ -17,14 +16,14 @@ module EbisuConnection
|
|
17
16
|
def put_aside!
|
18
17
|
return if check_own_connection
|
19
18
|
|
20
|
-
if_modify do
|
19
|
+
ConfFile.if_modify do
|
21
20
|
reserve_release_all_connection
|
22
21
|
check_own_connection
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
26
25
|
def recovery(failure_connection, exception)
|
27
|
-
if
|
26
|
+
if slave_down_message?(exception.message)
|
28
27
|
slaves.remove_connection(failure_connection)
|
29
28
|
true
|
30
29
|
else
|
@@ -32,10 +31,6 @@ module EbisuConnection
|
|
32
31
|
end
|
33
32
|
end
|
34
33
|
|
35
|
-
def recoverable?
|
36
|
-
true
|
37
|
-
end
|
38
|
-
|
39
34
|
def clear_all_connection!
|
40
35
|
synchronize do
|
41
36
|
@slaves.values.each do |s|
|
@@ -43,7 +38,7 @@ module EbisuConnection
|
|
43
38
|
end
|
44
39
|
|
45
40
|
@slaves = {}
|
46
|
-
conf_clear!
|
41
|
+
ConfFile.conf_clear!
|
47
42
|
end
|
48
43
|
end
|
49
44
|
|
@@ -68,7 +63,7 @@ module EbisuConnection
|
|
68
63
|
@slaves.values.each do |s|
|
69
64
|
s.reserve_release_connection!
|
70
65
|
end
|
71
|
-
conf_clear!
|
66
|
+
ConfFile.conf_clear!
|
72
67
|
end
|
73
68
|
end
|
74
69
|
|
@@ -79,15 +74,11 @@ module EbisuConnection
|
|
79
74
|
end
|
80
75
|
|
81
76
|
def get_slaves
|
82
|
-
SlaveGroup.new(slaves_conf,
|
77
|
+
SlaveGroup.new(slaves_conf, slave_group)
|
83
78
|
end
|
84
79
|
|
85
80
|
def slaves_conf
|
86
|
-
ConfFile.slaves_conf(
|
87
|
-
end
|
88
|
-
|
89
|
-
def spec
|
90
|
-
ConfFile.spec(@slave_group)
|
81
|
+
ConfFile.slaves_conf(slave_group)
|
91
82
|
end
|
92
83
|
end
|
93
84
|
end
|
@@ -1,33 +1,27 @@
|
|
1
|
-
require 'active_support/core_ext/hash/keys'
|
2
|
-
require 'active_support/core_ext/object/blank'
|
3
|
-
|
4
1
|
module EbisuConnection
|
5
2
|
class Slave
|
6
3
|
attr_reader :hostname, :weight
|
7
4
|
|
8
|
-
def initialize(conf,
|
5
|
+
def initialize(conf, slave_group)
|
9
6
|
case conf
|
10
7
|
when String
|
11
8
|
host, weight = conf.split(/\s*,\s*/)
|
12
9
|
@hostname, port = host.split(/\s*:\s*/)
|
13
10
|
when Hash
|
14
|
-
conf
|
15
|
-
|
16
|
-
|
17
|
-
port = conf["port"]
|
11
|
+
@hostname = conf["host"] || conf[:host]
|
12
|
+
weight = conf["weight"] || conf[:weight]
|
13
|
+
port = conf["port"] || conf[:port]
|
18
14
|
else
|
19
15
|
raise ArgumentError, "slaves config is invalid"
|
20
16
|
end
|
21
17
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
@spec = spec.merge(modify_spec)
|
18
|
+
spec = modify_spec(port)
|
19
|
+
@connection_factory = FreshConnection::ConnectionFactory.new(slave_group, spec)
|
26
20
|
@weight = (weight || 1).to_i
|
27
21
|
end
|
28
22
|
|
29
23
|
def connection
|
30
|
-
@connection ||=
|
24
|
+
@connection ||= @connection_factory.new_connection
|
31
25
|
end
|
32
26
|
|
33
27
|
def disconnect!
|
@@ -37,5 +31,12 @@ module EbisuConnection
|
|
37
31
|
end
|
38
32
|
rescue
|
39
33
|
end
|
34
|
+
|
35
|
+
def modify_spec(port)
|
36
|
+
modify_spec = {"host" => @hostname}
|
37
|
+
return modify_spec unless port
|
38
|
+
modify_spec["port"] = port.to_i if port.is_a?(Integer) || !port.empty?
|
39
|
+
modify_spec
|
40
|
+
end
|
40
41
|
end
|
41
42
|
end
|
@@ -1,17 +1,20 @@
|
|
1
|
+
require 'ebisu_connection/greatest_common_divisor'
|
2
|
+
require 'ebisu_connection/slave'
|
3
|
+
|
1
4
|
module EbisuConnection
|
2
5
|
class SlaveGroup
|
3
6
|
class AllSlavesHasGoneError < StandardError; end
|
4
7
|
|
5
|
-
def initialize(slaves_conf,
|
8
|
+
def initialize(slaves_conf, slave_group)
|
6
9
|
@slaves = slaves_conf.map do |conf|
|
7
|
-
Slave.new(conf,
|
10
|
+
Slave.new(conf, slave_group)
|
8
11
|
end
|
9
12
|
|
10
13
|
recalc_roulette
|
11
14
|
end
|
12
15
|
|
13
16
|
def sample
|
14
|
-
raise AllSlavesHasGoneError if @slaves.
|
17
|
+
raise AllSlavesHasGoneError if @slaves.empty?
|
15
18
|
@slaves[@roulette.sample]
|
16
19
|
end
|
17
20
|
|
@@ -19,7 +22,7 @@ module EbisuConnection
|
|
19
22
|
return unless s = @slaves.detect{|s| s.connection == connection}
|
20
23
|
s.disconnect! rescue nil
|
21
24
|
@slaves.delete(s)
|
22
|
-
raise AllSlavesHasGoneError if @slaves.
|
25
|
+
raise AllSlavesHasGoneError if @slaves.empty?
|
23
26
|
recalc_roulette
|
24
27
|
nil
|
25
28
|
end
|
data/lib/ebisu_connection.rb
CHANGED
@@ -1,17 +1,22 @@
|
|
1
1
|
require "fresh_connection"
|
2
|
-
require
|
2
|
+
require 'ebisu_connection/conf_file'
|
3
|
+
require 'ebisu_connection/connection_manager'
|
3
4
|
|
4
5
|
module EbisuConnection
|
5
|
-
|
6
|
+
class << self
|
7
|
+
attr_writer :env
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
autoload :Slave
|
11
|
-
autoload :GreatestCommonDivisor
|
9
|
+
def slaves_file=(file)
|
10
|
+
ConfFile.slaves_file = file
|
11
|
+
end
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
def check_interval=(interval)
|
14
|
+
ConfFile.check_interval = interval
|
15
|
+
end
|
16
|
+
|
17
|
+
def env
|
18
|
+
@env ||= defined?(Rails) && Rails.env || ENV["RAILS_ENV"] || ENV["RACK_ENV"]
|
19
|
+
end
|
15
20
|
end
|
16
21
|
end
|
17
22
|
|
data/spec/spec_helper.rb
CHANGED
@@ -2,6 +2,6 @@ ENV["RAILS_ENV"]="test"
|
|
2
2
|
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
3
3
|
require 'ebisu_connection'
|
4
4
|
|
5
|
-
|
5
|
+
EbisuConnection.env = "test"
|
6
6
|
EbisuConnection.slaves_file = File.join(File.dirname(__FILE__), "slaves.yaml")
|
7
7
|
require File.join(File.dirname(__FILE__), "prepare.rb")
|
@@ -8,6 +8,10 @@ describe EbisuConnection do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
context "access to master" do
|
11
|
+
it "specify read_master" do
|
12
|
+
expect(User.read_master.first.name).to be_include("master")
|
13
|
+
end
|
14
|
+
|
11
15
|
it "specify readonly(false)" do
|
12
16
|
expect(User.readonly(false).first.name).to be_include("master")
|
13
17
|
end
|
@@ -3,30 +3,25 @@ require 'spec_helper'
|
|
3
3
|
describe EbisuConnection::SlaveGroup do
|
4
4
|
before(:all) do
|
5
5
|
@sg = EbisuConnection::SlaveGroup
|
6
|
-
@spec = {
|
7
|
-
"adapter" => "mysql2",
|
8
|
-
"database" => "ebisu_connection_test",
|
9
|
-
"username" => "root"
|
10
|
-
}
|
11
6
|
end
|
12
7
|
|
13
8
|
context "#sample" do
|
14
9
|
it "raise exception if slaves empty" do
|
15
|
-
inst = @sg.new([],
|
10
|
+
inst = @sg.new([], "slave")
|
16
11
|
expect{
|
17
12
|
inst.sample
|
18
13
|
}.to raise_error(EbisuConnection::SlaveGroup::AllSlavesHasGoneError)
|
19
14
|
end
|
20
15
|
|
21
16
|
it "return slve instance object" do
|
22
|
-
inst = @sg.new(["h"],
|
17
|
+
inst = @sg.new(["h"], "slave")
|
23
18
|
expect(inst.sample).to be_a(EbisuConnection::Slave)
|
24
19
|
end
|
25
20
|
end
|
26
21
|
|
27
22
|
context "#remove_connection" do
|
28
23
|
it "raise exception AllSlavesHasGoneError when slaves size is one" do
|
29
|
-
inst = @sg.new(["localhost"],
|
24
|
+
inst = @sg.new(["localhost"], "slave")
|
30
25
|
c = inst.sample.connection
|
31
26
|
expect {
|
32
27
|
inst.remove_connection(c)
|
data/spec/unit/slave_spec.rb
CHANGED
@@ -1,29 +1,21 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe EbisuConnection::Slave do
|
4
|
-
before(:all) do
|
5
|
-
@spec = {
|
6
|
-
"adapter" => "mysql2",
|
7
|
-
"database" => "ebisu_connection_test",
|
8
|
-
"username" => "root"
|
9
|
-
}
|
10
|
-
end
|
11
|
-
|
12
4
|
context "initialize(conf is String)" do
|
13
5
|
it "hostname only" do
|
14
|
-
s = EbisuConnection::Slave.new("host_1",
|
6
|
+
s = EbisuConnection::Slave.new("host_1", "slave")
|
15
7
|
expect(s.hostname).to eq("host_1")
|
16
8
|
expect(s.weight).to eq(1)
|
17
9
|
end
|
18
10
|
|
19
11
|
it "hostname and weight" do
|
20
|
-
s = EbisuConnection::Slave.new("host_1, 10",
|
12
|
+
s = EbisuConnection::Slave.new("host_1, 10", "slave")
|
21
13
|
expect(s.hostname).to eq("host_1")
|
22
14
|
expect(s.weight).to eq(10)
|
23
15
|
end
|
24
16
|
|
25
17
|
it "hostname, weight and port" do
|
26
|
-
s = EbisuConnection::Slave.new("host_1:1975, 10",
|
18
|
+
s = EbisuConnection::Slave.new("host_1:1975, 10", "slave")
|
27
19
|
expect(s.hostname).to eq("host_1")
|
28
20
|
expect(s.weight).to eq(10)
|
29
21
|
end
|
@@ -31,19 +23,19 @@ describe EbisuConnection::Slave do
|
|
31
23
|
|
32
24
|
context "initialize(conf is Hash)" do
|
33
25
|
it "hostname only" do
|
34
|
-
s = EbisuConnection::Slave.new({:host => "host_1"},
|
26
|
+
s = EbisuConnection::Slave.new({:host => "host_1"}, "slave")
|
35
27
|
expect(s.hostname).to eq("host_1")
|
36
28
|
expect(s.weight).to eq(1)
|
37
29
|
end
|
38
30
|
|
39
31
|
it "hostname and weight" do
|
40
|
-
s = EbisuConnection::Slave.new({:host => "host_1", :weight => 10},
|
32
|
+
s = EbisuConnection::Slave.new({:host => "host_1", :weight => 10}, "slave")
|
41
33
|
expect(s.hostname).to eq("host_1")
|
42
34
|
expect(s.weight).to eq(10)
|
43
35
|
end
|
44
36
|
|
45
37
|
it "hostname, weight and port" do
|
46
|
-
s = EbisuConnection::Slave.new({:host => "host_1", :weight => 10, :port => 1975},
|
38
|
+
s = EbisuConnection::Slave.new({:host => "host_1", :weight => 10, :port => 1975}, "slave")
|
47
39
|
expect(s.hostname).to eq("host_1")
|
48
40
|
expect(s.weight).to eq(10)
|
49
41
|
end
|
@@ -51,7 +43,7 @@ describe EbisuConnection::Slave do
|
|
51
43
|
|
52
44
|
context "#connection" do
|
53
45
|
it "return Mysql2Adapter object" do
|
54
|
-
s = EbisuConnection::Slave.new("localhost",
|
46
|
+
s = EbisuConnection::Slave.new("localhost", "slave")
|
55
47
|
expect(s.connection).to be_a(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
|
56
48
|
end
|
57
49
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ebisu_connection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tsukasaoishi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fresh_connection
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.4.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 0.4.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|