sequel-schema-sharding 0.0.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sequel/schema-sharding.rb +2 -1
- data/lib/sequel/schema-sharding/connection_manager.rb +26 -5
- data/lib/sequel/schema-sharding/database_manager.rb +1 -1
- data/lib/sequel/schema-sharding/logger_proxy.rb +14 -0
- data/lib/sequel/schema-sharding/model.rb +1 -1
- data/lib/sequel/schema-sharding/version.rb +1 -1
- data/spec/fixtures/test_db_config.yml +4 -0
- data/spec/schema-sharding/configuration_spec.rb +1 -0
- data/spec/schema-sharding/connection_manager_spec.rb +21 -3
- data/spec/schema-sharding/model_spec.rb +10 -0
- data/spec/spec_helper.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9259594ee38417f3162a7b250722bb2f6d784b4b
|
4
|
+
data.tar.gz: 7ab7c107c400e6ec46d3cd612bda9097c82e8243
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1c5b3345a5ce05accb17231d8981a61b077cfe30f653992dbcfefb71c52689b31c1d9248ac831a2d857fb30fa326bbf383535b2ae5615600e7cc0e734316e2b
|
7
|
+
data.tar.gz: 47367a85fe138b51f66a8a60368ef2faf960d4c539117cc972abfc32770562cbab770a29b07cf697892d61d9d3eb07f48c3c422eca68975fa90a021bb6e61b9b
|
@@ -7,6 +7,7 @@ require 'sequel/schema-sharding/finder'
|
|
7
7
|
require 'sequel/schema-sharding/sequel_ext'
|
8
8
|
require 'sequel/schema-sharding/model'
|
9
9
|
require 'logger'
|
10
|
+
require 'sequel/schema-sharding/logger_proxy'
|
10
11
|
|
11
12
|
module Sequel
|
12
13
|
module SchemaSharding
|
@@ -19,7 +20,7 @@ module Sequel
|
|
19
20
|
end
|
20
21
|
|
21
22
|
def self.logger
|
22
|
-
@logger ||= Logger.new(
|
23
|
+
@logger ||= Logger.new(StringIO.new)
|
23
24
|
end
|
24
25
|
|
25
26
|
def self.logger=(logger)
|
@@ -11,11 +11,11 @@ module Sequel
|
|
11
11
|
|
12
12
|
def [](name)
|
13
13
|
config = db_config_for(name)
|
14
|
-
@connections[name.to_s] ||= Sequel.postgres(
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
@connections[name.to_s] ||= Sequel.postgres(master_config_for(config).merge!(replica_hash_for(config)))
|
15
|
+
end
|
16
|
+
|
17
|
+
def master(name)
|
18
|
+
@connections["master_#{name}"] ||= Sequel.postgres(master_config_for(db_config_for(name)))
|
19
19
|
end
|
20
20
|
|
21
21
|
def disconnect
|
@@ -37,6 +37,27 @@ module Sequel
|
|
37
37
|
|
38
38
|
private
|
39
39
|
|
40
|
+
def master_config_for(config)
|
41
|
+
{
|
42
|
+
:user => config['username'],
|
43
|
+
:password => config['password'],
|
44
|
+
:host => config['host'],
|
45
|
+
:database => config['database'],
|
46
|
+
:port => config['port'],
|
47
|
+
:single_threaded => true,
|
48
|
+
:loggers => [Sequel::SchemaSharding::LoggerProxy.new]
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
def replica_hash_for(config)
|
53
|
+
return {} if config['replicas'].nil?
|
54
|
+
{
|
55
|
+
:servers => {
|
56
|
+
:read_only => ->(db) { {:host => config['replicas'].sample} }
|
57
|
+
}
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
40
61
|
def db_config_for(name)
|
41
62
|
config.physical_shard_configs[name]
|
42
63
|
end
|
@@ -59,7 +59,7 @@ module Sequel
|
|
59
59
|
config.logical_shard_configs(table_name).each_pair do |shard_number, physical_shard|
|
60
60
|
schema_name = connection_manager.schema_for(table_name, env, shard_number)
|
61
61
|
Sequel::SchemaSharding.logger.info "Creating schema #{schema_name} on #{physical_shard}.."
|
62
|
-
connection = connection_manager
|
62
|
+
connection = connection_manager.master(physical_shard)
|
63
63
|
|
64
64
|
begin
|
65
65
|
connection.run("CREATE SCHEMA #{schema_name}")
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module SchemaSharding
|
5
|
+
class LoggerProxy < ::Logger
|
6
|
+
def initialize
|
7
|
+
end
|
8
|
+
|
9
|
+
def add(severity, message = nil, progname = nil, &block)
|
10
|
+
Sequel::SchemaSharding.logger.add(severity, message, progname, &block)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -19,6 +19,7 @@ module Sequel
|
|
19
19
|
klass = Sequel::Model(Sequel::SchemaSharding.connection_manager.default_dataset_for(source))
|
20
20
|
|
21
21
|
klass.include(SchemaSharding::ShardedModel)
|
22
|
+
klass.send(:simple_table=, false)
|
22
23
|
|
23
24
|
klass
|
24
25
|
end
|
@@ -85,7 +86,6 @@ module Sequel
|
|
85
86
|
def _insert_dataset
|
86
87
|
this_server
|
87
88
|
end
|
88
|
-
|
89
89
|
end
|
90
90
|
end
|
91
91
|
end
|
@@ -17,6 +17,8 @@ test:
|
|
17
17
|
shard2:
|
18
18
|
host: 127.0.0.1
|
19
19
|
database: sequel_test_shard2
|
20
|
+
replicas:
|
21
|
+
- 127.0.0.1
|
20
22
|
common:
|
21
23
|
username: postgres
|
22
24
|
password: boomboomkaboom
|
@@ -39,6 +41,8 @@ boom:
|
|
39
41
|
shard2:
|
40
42
|
host: 127.0.0.1
|
41
43
|
database: sequel_boom_shard2
|
44
|
+
replicas:
|
45
|
+
- 127.0.0.1
|
42
46
|
common:
|
43
47
|
username: postgres
|
44
48
|
password: boomboomkaboom
|
@@ -34,6 +34,7 @@ describe Sequel::SchemaSharding::Configuration do
|
|
34
34
|
expect(value['username']).to eq('postgres')
|
35
35
|
expect(value['password']).to eq('boomboomkaboom')
|
36
36
|
expect(value['port']).to eq(5432)
|
37
|
+
expect(value['replicas']).to eq(['127.0.0.1']) if key == 'shard2'
|
37
38
|
|
38
39
|
i += 1
|
39
40
|
end
|
@@ -2,22 +2,40 @@ require 'spec_helper'
|
|
2
2
|
require 'sequel/schema-sharding/connection_manager'
|
3
3
|
|
4
4
|
describe Sequel::SchemaSharding::ConnectionManager do
|
5
|
-
let(:config) { Sequel::SchemaSharding::Configuration.new('
|
5
|
+
let(:config) { Sequel::SchemaSharding::Configuration.new('test', 'spec/fixtures/test_db_config.yml') }
|
6
6
|
|
7
7
|
subject { Sequel::SchemaSharding::ConnectionManager.new }
|
8
8
|
|
9
9
|
before { subject.stubs(:config).returns(config) }
|
10
|
+
after do
|
11
|
+
subject.disconnect
|
12
|
+
end
|
10
13
|
|
11
14
|
describe '#[]' do
|
12
15
|
it 'returns a valid connection instance for the specified physical shard' do
|
13
16
|
expect(subject['shard1']).to be_a(Sequel::Postgres::Database)
|
17
|
+
subject['shard1'].execute("SELECT 1")
|
14
18
|
expect(subject['shard2']).to be_a(Sequel::Postgres::Database)
|
15
19
|
end
|
20
|
+
|
21
|
+
context 'read/write splitting' do
|
22
|
+
it 'has a replica for shard2' do
|
23
|
+
expect(subject['shard2'].servers).to include(:read_only)
|
24
|
+
expect(subject['shard1'].servers).to_not include(:read_only)
|
25
|
+
end
|
26
|
+
|
27
|
+
#it 'executes a select against a replica' do
|
28
|
+
# shard = subject['shard2']
|
29
|
+
# Sequel::ShardedThreadedConnectionPool.any_instance.expects(:hold).with(:read_only).at_least_once
|
30
|
+
# Sequel::ShardedThreadedConnectionPool.any_instance.expects(:hold).with(:default).at_least_once
|
31
|
+
# shard[:"sequel_explosions_boof_pickles_3__artists"].first
|
32
|
+
#end
|
33
|
+
end
|
16
34
|
end
|
17
35
|
|
18
36
|
describe "#schema_for" do
|
19
37
|
it "returns the schema name based on env and shard number" do
|
20
|
-
subject.schema_for('boof', 'pickles', 3).should eq '
|
38
|
+
subject.schema_for('boof', 'pickles', 3).should eq 'sequel_logical_boof_pickles_3'
|
21
39
|
end
|
22
40
|
end
|
23
41
|
|
@@ -27,7 +45,7 @@ describe Sequel::SchemaSharding::ConnectionManager do
|
|
27
45
|
# This should be deconstructed to allow for injection of a mock config for testing.
|
28
46
|
dataset = subject.default_dataset_for("artists")
|
29
47
|
expect(dataset).to be_a(Sequel::Dataset)
|
30
|
-
expect(dataset.first_source_table).to eql(:'
|
48
|
+
expect(dataset.first_source_table).to eql(:'sequel_logical_artists_test_1__artists')
|
31
49
|
end
|
32
50
|
end
|
33
51
|
end
|
@@ -16,6 +16,12 @@ describe Sequel::SchemaSharding, 'Model' do
|
|
16
16
|
'artists'
|
17
17
|
end
|
18
18
|
end
|
19
|
+
|
20
|
+
klass.class_eval do
|
21
|
+
def this
|
22
|
+
@this ||= model.by_id(artist_id).limit(1)
|
23
|
+
end
|
24
|
+
end
|
19
25
|
klass
|
20
26
|
end
|
21
27
|
|
@@ -26,6 +32,9 @@ describe Sequel::SchemaSharding, 'Model' do
|
|
26
32
|
read_back_artist = model.by_id(14).first
|
27
33
|
expect(read_back_artist).to be_a(model)
|
28
34
|
expect(read_back_artist.name).to eql('Paul')
|
35
|
+
read_back_artist.destroy
|
36
|
+
read_back_artist = model.by_id(14).first
|
37
|
+
expect(read_back_artist).to be_nil
|
29
38
|
end
|
30
39
|
end
|
31
40
|
|
@@ -34,6 +43,7 @@ describe Sequel::SchemaSharding, 'Model' do
|
|
34
43
|
artist = model.create(artist_id: 234, name: 'Paul')
|
35
44
|
expect(artist).to be_a(model)
|
36
45
|
expect(artist.name).to eql('Paul')
|
46
|
+
artist.destroy
|
37
47
|
end
|
38
48
|
end
|
39
49
|
|
data/spec/spec_helper.rb
CHANGED
@@ -27,7 +27,7 @@ RSpec.configure do |config|
|
|
27
27
|
Sequel::SchemaSharding.migration_path = "spec/fixtures/db/migrate"
|
28
28
|
end
|
29
29
|
|
30
|
-
config.around :each do |ex|
|
30
|
+
config.around :each, type: :transactional do |ex|
|
31
31
|
#Sequel::SchemaSharding.config = Sequel::SchemaSharding::Configuration.new('boom', 'spec/fixtures/test_db_config.yml')
|
32
32
|
|
33
33
|
# Start transactions in each connection to the physical shards
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel-schema-sharding
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Henry
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-09-
|
13
|
+
date: 2013-09-27 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: sequel
|
@@ -94,6 +94,7 @@ files:
|
|
94
94
|
- lib/sequel/schema-sharding/connection_manager.rb
|
95
95
|
- lib/sequel/schema-sharding/database_manager.rb
|
96
96
|
- lib/sequel/schema-sharding/finder.rb
|
97
|
+
- lib/sequel/schema-sharding/logger_proxy.rb
|
97
98
|
- lib/sequel/schema-sharding/model.rb
|
98
99
|
- lib/sequel/schema-sharding/ring.rb
|
99
100
|
- lib/sequel/schema-sharding/sequel_ext.rb
|
@@ -131,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
132
|
version: '0'
|
132
133
|
requirements: []
|
133
134
|
rubyforge_project:
|
134
|
-
rubygems_version: 2.0.
|
135
|
+
rubygems_version: 2.0.7
|
135
136
|
signing_key:
|
136
137
|
specification_version: 4
|
137
138
|
summary: Create horizontally sharded Sequel models with Postgres
|