ar-octopus 0.0.30 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +9 -6
- data/README.mkdn +28 -20
- data/Rakefile +1 -1
- data/ar-octopus.gemspec +2 -2
- data/lib/octopus.rb +4 -3
- data/lib/octopus/model.rb +0 -1
- data/lib/octopus/proxy.rb +8 -0
- data/sample_app/Gemfile +1 -1
- data/sample_app/Gemfile.lock +0 -8
- data/sample_app/db/schema.rb +7 -6
- data/spec/config/shards.yml +1 -0
- data/spec/octopus/proxy_spec.rb +5 -0
- metadata +4 -4
data/Gemfile
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
-
source :
|
1
|
+
source :rubygems
|
2
2
|
|
3
|
-
gem "rspec", "2.0.0.beta.19"
|
4
|
-
gem "mysql", ">= 2.8.1"
|
5
|
-
gem "pg", ">= 0.9.0"
|
6
|
-
gem "sqlite3-ruby", ">= 1.3.1"
|
7
3
|
gem 'activerecord', '>= 3.0.0.rc'
|
8
4
|
gem 'actionpack', '>= 3.0.0.rc'
|
9
|
-
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
gem "jeweler", ">= 1.4"
|
8
|
+
gem "rspec", "2.0.0.beta.19"
|
9
|
+
gem "mysql", ">= 2.8.1"
|
10
|
+
gem "pg", ">= 0.9.0"
|
11
|
+
gem "sqlite3-ruby", ">= 1.3.1"
|
12
|
+
end
|
data/README.mkdn
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# Octopus - Easy Database Sharding for ActiveRecord
|
2
2
|
|
3
|
-
Octopus is a better way to do Database Sharding in ActiveRecord. Sharding allows multiple databases in the same rails application. While there are several projects that implement Sharding (e.g. DbCharmer, DataFabric, MultiDb), each project has its own limitations. The main goal of octopus project is to provide a
|
3
|
+
Octopus is a better way to do Database Sharding in ActiveRecord. Sharding allows multiple databases in the same rails application. While there are several projects that implement Sharding (e.g. DbCharmer, DataFabric, MultiDb), each project has its own limitations. The main goal of octopus project is to provide a better way of doing Database Sharding.
|
4
4
|
|
5
5
|
## Feature list:
|
6
|
-
The
|
6
|
+
The api is designed to be simple as possible. Octopus focuses on the end user, giving the power of multiple databases but with reliable code and flexibility. Octopus is focused on Rails 3, but is compatible with Rails 2.x.
|
7
7
|
|
8
8
|
Octopus supports:
|
9
9
|
|
@@ -16,10 +16,10 @@ Octopus supports:
|
|
16
16
|
When using replication, all writes queries will be sent to master, and read queries to slaves. More info could be found at: <a href="http://wiki.github.com/tchandy/octopus/replication"> Wiki</a>
|
17
17
|
|
18
18
|
### Sharding
|
19
|
-
When using sharding, you need to specify
|
19
|
+
When using sharding, you need to specify which shard to send the query. Octopus supports selecting the shard inside a controller, or manually in each object. More could be found at <a href="http://wiki.github.com/tchandy/octopus/sharding"> Wiki</a>
|
20
20
|
|
21
21
|
### Replication + Sharding
|
22
|
-
Replication + Sharding isn't supported yet. This is
|
22
|
+
Replication + Sharding isn't supported yet. This is on our TODO list and will be done ASAP. If you need, feel free to fork and implement it.
|
23
23
|
|
24
24
|
## Install
|
25
25
|
|
@@ -45,17 +45,17 @@ First, you need to create a config file, shards.yml, inside your config/ directo
|
|
45
45
|
|
46
46
|
### Syntax
|
47
47
|
|
48
|
-
Octopus adds a method to each AR Class and object
|
48
|
+
Octopus adds a method to each AR Class and object: the using method is used to select the shard like this:
|
49
49
|
<pre>User.where(:name => "Thiago").limit(3).using(:slave_one) </pre>
|
50
50
|
|
51
|
-
|
51
|
+
Octopus also supports queries within a block. When you pass a block to the using method, all queries inside the block will be sent to the specified shard.
|
52
52
|
<pre>
|
53
53
|
Octopus.using(:slave_two) do
|
54
54
|
User.create(:name => "Mike")
|
55
55
|
end
|
56
56
|
</pre>
|
57
57
|
|
58
|
-
|
58
|
+
Each model instance knows which shard it came from so this will work automatically:
|
59
59
|
<pre>
|
60
60
|
# This will find the user in the shard1
|
61
61
|
@user = User.using(:shard1).find_by_name("Joao")
|
@@ -67,12 +67,12 @@ end
|
|
67
67
|
@user.name = "Mike"
|
68
68
|
|
69
69
|
# Save the user in the correct shard, shard1.
|
70
|
-
@user.save
|
70
|
+
@user.save
|
71
71
|
</pre>
|
72
72
|
|
73
73
|
### Migrations
|
74
74
|
|
75
|
-
|
75
|
+
In migrations, you also have access to the using method. The syntax is basically the same. This migration will run in the brazil and canada shards.
|
76
76
|
<pre>
|
77
77
|
class CreateUsersOnBothShards < ActiveRecord::Migration
|
78
78
|
using(:brazil, :canada)
|
@@ -82,11 +82,12 @@ end
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def self.down
|
85
|
-
User.delete_all
|
85
|
+
User.delete_all
|
86
86
|
end
|
87
87
|
end
|
88
88
|
</pre>
|
89
|
-
|
89
|
+
|
90
|
+
You also could send a migration to a group of shards. This migration will be sent to all shards that belongs to history_shards group, specified in shards.yml:
|
90
91
|
<pre>
|
91
92
|
class CreateUsersOnMultiplesGroups < ActiveRecord::Migration
|
92
93
|
using_group(:history_shards)
|
@@ -96,7 +97,7 @@ end
|
|
96
97
|
end
|
97
98
|
|
98
99
|
def self.down
|
99
|
-
User.delete_all
|
100
|
+
User.delete_all
|
100
101
|
end
|
101
102
|
end
|
102
103
|
</pre>
|
@@ -109,27 +110,34 @@ If you want to send a specified action, or all actions from a controller, to a s
|
|
109
110
|
class ApplicationController < ActionController::Base
|
110
111
|
around_filter :select_shard
|
111
112
|
|
112
|
-
def select_shard
|
113
|
-
Octopus.using(:brazil)
|
114
|
-
yield
|
115
|
-
end
|
113
|
+
def select_shard(&block)
|
114
|
+
Octopus.using(:brazil, &block)
|
116
115
|
end
|
117
116
|
end
|
118
117
|
</pre>
|
119
118
|
|
120
119
|
To see the complete list of features and syntax, please check out our <a href="http://wiki.github.com/tchandy/octopus/"> Wiki</a>
|
121
|
-
|
120
|
+
Want to see sample rails applications using octopus features? please check it out: <a href="http://github.com/tchandy/octopus_sharding_example">Sharding Example</a> and <a href="http://github.com/tchandy/octopus_replication_example">Replication Example</a>. Also, we have an example that shows how to use Octopus without Rails: <a href="http://github.com/tchandy/octopus_sinatra"> Octopus + Sinatra Example</a>.
|
121
|
+
|
122
|
+
|
123
|
+
## Important!
|
124
|
+
Sometimes, when a connection isn't used for much time, this will makes ActiveRecord raising an exception. if you have this kind of applications, please, add the following line to your configuration:
|
125
|
+
|
126
|
+
<pre>
|
127
|
+
verify_connection: true
|
128
|
+
</pre>
|
122
129
|
|
130
|
+
This will tell Octopus to verify the connection before sending the query.
|
123
131
|
|
124
132
|
## Contributing with Octopus
|
125
|
-
Contributors are welcome! To run the test suite, you need mysql, postgresql and sqlite3 installed.
|
133
|
+
Contributors are welcome! To run the test suite, you need mysql, postgresql and sqlite3 installed. This is what you need to setup your Octopus development environment:
|
126
134
|
|
127
135
|
<pre>
|
128
136
|
git clone http://github.com/tchandy/octopus.git
|
129
137
|
cd octopus
|
130
138
|
bundle install
|
131
139
|
rake db:prepare
|
132
|
-
rake
|
140
|
+
rake
|
133
141
|
</pre>
|
134
142
|
|
135
143
|
To run our integrations tests inside sample_app, you need to following commands:
|
@@ -140,7 +148,7 @@ To run our integrations tests inside sample_app, you need to following commands:
|
|
140
148
|
cucumber
|
141
149
|
</pre>
|
142
150
|
|
143
|
-
If you are having issues
|
151
|
+
If you are having issues running the octopus spec suite, verify your database users and passwords match those inside the config files and your permissions are correct.
|
144
152
|
|
145
153
|
## Thanks
|
146
154
|
|
data/Rakefile
CHANGED
@@ -37,7 +37,7 @@ begin
|
|
37
37
|
gem.add_development_dependency "jeweler", ">= 1.4"
|
38
38
|
gem.add_development_dependency "actionpack", ">= 2.3"
|
39
39
|
gem.add_dependency('activerecord', '>= 2.3')
|
40
|
-
gem.version = "0.0
|
40
|
+
gem.version = "0.1.0"
|
41
41
|
end
|
42
42
|
Jeweler::GemcutterTasks.new
|
43
43
|
rescue LoadError
|
data/ar-octopus.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ar-octopus}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Thiago Pradi", "Mike Perham"]
|
12
|
-
s.date = %q{2010-08-
|
12
|
+
s.date = %q{2010-08-20}
|
13
13
|
s.description = %q{This gem allows you to use sharded databases with ActiveRecord. this also provides a interface for replication and for running migrations with multiples shards.}
|
14
14
|
s.email = %q{tchandy@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/octopus.rb
CHANGED
@@ -50,9 +50,10 @@ module Octopus
|
|
50
50
|
|
51
51
|
def self.using(shard, &block)
|
52
52
|
ActiveRecord::Base.hijack_initializer()
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
conn = ActiveRecord::Base.connection
|
54
|
+
|
55
|
+
if conn.is_a?(Octopus::Proxy)
|
56
|
+
conn.run_queries_on_shard(shard, &block)
|
56
57
|
else
|
57
58
|
yield
|
58
59
|
end
|
data/lib/octopus/model.rb
CHANGED
data/lib/octopus/proxy.rb
CHANGED
@@ -11,6 +11,13 @@ class Octopus::Proxy
|
|
11
11
|
@groups = {}
|
12
12
|
@shards[:master] = ActiveRecord::Base.connection_pool()
|
13
13
|
@current_shard = :master
|
14
|
+
|
15
|
+
if !config.nil? && config.has_key?("verify_connection")
|
16
|
+
@verify_connection = config["verify_connection"]
|
17
|
+
else
|
18
|
+
@verify_connection = false
|
19
|
+
end
|
20
|
+
|
14
21
|
if !config.nil?
|
15
22
|
@entire_sharded = config['entire_sharded']
|
16
23
|
shards_config = config[Octopus.rails_env()]
|
@@ -71,6 +78,7 @@ class Octopus::Proxy
|
|
71
78
|
end
|
72
79
|
|
73
80
|
def select_connection()
|
81
|
+
@shards[shard_name].verify_active_connections! if @verify_connection
|
74
82
|
@shards[shard_name].connection()
|
75
83
|
end
|
76
84
|
|
data/sample_app/Gemfile
CHANGED
data/sample_app/Gemfile.lock
CHANGED
@@ -46,7 +46,6 @@ GEM
|
|
46
46
|
rack (>= 1.0.0)
|
47
47
|
rack-test (>= 0.5.4)
|
48
48
|
selenium-webdriver (>= 0.0.3)
|
49
|
-
columnize (0.3.1)
|
50
49
|
configuration (1.1.0)
|
51
50
|
cucumber (0.8.5)
|
52
51
|
builder (~> 2.1.2)
|
@@ -70,7 +69,6 @@ GEM
|
|
70
69
|
launchy (0.3.7)
|
71
70
|
configuration (>= 0.0.5)
|
72
71
|
rake (>= 0.8.1)
|
73
|
-
linecache (0.43)
|
74
72
|
mail (2.2.5)
|
75
73
|
activesupport (>= 2.3.6)
|
76
74
|
mime-types
|
@@ -108,11 +106,6 @@ GEM
|
|
108
106
|
rspec-rails (2.0.0.beta.19)
|
109
107
|
rspec (= 2.0.0.beta.19)
|
110
108
|
webrat (>= 0.7.2.beta.1)
|
111
|
-
ruby-debug (0.10.3)
|
112
|
-
columnize (>= 0.1)
|
113
|
-
ruby-debug-base (~> 0.10.3.0)
|
114
|
-
ruby-debug-base (0.10.3)
|
115
|
-
linecache (>= 0.3)
|
116
109
|
rubyzip (0.9.4)
|
117
110
|
selenium-webdriver (0.0.27)
|
118
111
|
ffi (>= 0.6.1)
|
@@ -144,6 +137,5 @@ DEPENDENCIES
|
|
144
137
|
launchy
|
145
138
|
rails (= 3.0.0.rc)
|
146
139
|
rspec-rails (>= 2.0.0.beta.16)
|
147
|
-
ruby-debug
|
148
140
|
spork
|
149
141
|
sqlite3-ruby
|
data/sample_app/db/schema.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
# This file is auto-generated from the current state of the database. Instead
|
2
|
-
# please use the migrations feature of Active Record to
|
3
|
-
# then regenerate this schema definition.
|
1
|
+
# This file is auto-generated from the current state of the database. Instead
|
2
|
+
# of editing this file, please use the migrations feature of Active Record to
|
3
|
+
# incrementally modify your database, and then regenerate this schema definition.
|
4
4
|
#
|
5
|
-
# Note that this schema.rb definition is the authoritative source for your
|
6
|
-
# to create the application database on another
|
7
|
-
#
|
5
|
+
# Note that this schema.rb definition is the authoritative source for your
|
6
|
+
# database schema. If you need to create the application database on another
|
7
|
+
# system, you should be using db:schema:load, not running all the migrations
|
8
|
+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
8
9
|
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
9
10
|
#
|
10
11
|
# It's strongly recommended to check this file into your version control system.
|
data/spec/config/shards.yml
CHANGED
data/spec/octopus/proxy_spec.rb
CHANGED
@@ -16,6 +16,10 @@ describe Octopus::Proxy do
|
|
16
16
|
it "should initialize replicated attribute as false" do
|
17
17
|
proxy.instance_variable_get(:@replicated).should be_false
|
18
18
|
end
|
19
|
+
|
20
|
+
it "should not verify connections for default" do
|
21
|
+
proxy.instance_variable_get(:@verify_connection).should be_false
|
22
|
+
end
|
19
23
|
|
20
24
|
describe "should raise error if you have duplicated shard names" do
|
21
25
|
before(:each) do
|
@@ -72,6 +76,7 @@ describe Octopus::Proxy do
|
|
72
76
|
Octopus.config()
|
73
77
|
|
74
78
|
proxy.instance_variable_get(:@replicated).should be_true
|
79
|
+
proxy.instance_variable_get(:@verify_connection).should be_true
|
75
80
|
Octopus.enviroments.should == ["staging", "production"]
|
76
81
|
end
|
77
82
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-octopus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.30
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Thiago Pradi
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-08-
|
19
|
+
date: 2010-08-20 00:00:00 -03:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|