mixed_gauge 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b0f38717b364867534a54530bbd6a5279086a46e
4
+ data.tar.gz: ea86038064d74083b87d1d631bfc3b7769b71bfc
5
+ SHA512:
6
+ metadata.gz: e499016ee06b4683e9b71b48b315a758d296812fe87cffb62d02ffa9590687b7d4280ed6ea853fbf10de630588f148a7993717ecc7ec56ecb2c3c9977956e57d
7
+ data.tar.gz: 8e516f5b4f727b24bfeaebd3aa707ee60826d5b33af930508cec651e10bf95fc6241a872a752d582d8370abe8b03f17a3e2e35b1409fc37efa311a8f266576c6
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mixed_gauge.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Taiki Ono
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # MixedGauge
2
+ An ActiveRecord extension for database sharding.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'mixed_gauge'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install mixed_gauge
19
+
20
+ ## Usage
21
+
22
+ Add additional database connection config to `database.yml`.
23
+
24
+ ```yaml
25
+ # database.yml
26
+ production_user_001:
27
+ adapter: mysql2
28
+ username: user_writable
29
+ host: db-user-001
30
+ production_user_002:
31
+ adapter: mysql2
32
+ username: user_writable
33
+ host: db-user-002
34
+ production_user_003:
35
+ adapter: mysql2
36
+ username: user_writable
37
+ host: db-user-003
38
+ production_user_004:
39
+ adapter: mysql2
40
+ username: user_writable
41
+ host: db-user-004
42
+ ```
43
+
44
+ Configure slots (virtual node for cluster) then assign slots to real node.
45
+
46
+ ```ruby
47
+ MixedGauge.configure do |config|
48
+ config.define_cluster(:user) do |cluster|
49
+ # When slots per node * max nodes per cluster = (2 ** 10) * (2 ** 10)
50
+ cluster.define_slots(1..1048576)
51
+ cluster.register(1..262144, :production_user_001)
52
+ cluster.register(262145..524288, :production_user_002)
53
+ cluster.register(524289..786432, :production_user_003)
54
+ cluster.register(786433..1048576, :production_user_004)
55
+ end
56
+ end
57
+ ```
58
+
59
+ Include `MixedGauge::Model` to your model class, specify cluster name for the
60
+ model, specify distkey which determine nodes to store.
61
+
62
+ ```ruby
63
+ class User < ActiveRecord::Base
64
+ include MixedGauge::Model
65
+ use_cluster :user
66
+ distkey :email
67
+ end
68
+ ```
69
+
70
+ Use `.get` to retrive single model class which is connected to proper
71
+ database node. Use `.put!` to create new record to proper database node.
72
+ `.all_shards` enables you to all model class which is connected to all
73
+ database nodes in the cluster.
74
+
75
+ ```ruby
76
+ User.put!(email: 'alice@example.com', name: 'alice')
77
+
78
+ alice = User.get('alice@example.com')
79
+ alice.age = 1
80
+ alice.save!
81
+
82
+ User.all_shards.flat_map {|m| m.where(name: 'alice') }
83
+ ```
84
+
85
+ ## Development
86
+
87
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
88
+
89
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
90
+
91
+ ## Contributing
92
+
93
+ 1. Fork it ( https://github.com/[my-github-username]/mixed_gauge/fork )
94
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
95
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
96
+ 4. Push to the branch (`git push origin my-new-feature`)
97
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mixed_gauge"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,21 @@
1
+ require 'active_record'
2
+
3
+ require 'mixed_gauge/version'
4
+ require 'mixed_gauge/cluster_config'
5
+ require 'mixed_gauge/config'
6
+ require 'mixed_gauge/routing'
7
+ require 'mixed_gauge/sub_model_repository'
8
+ require 'mixed_gauge/model'
9
+
10
+ module MixedGauge
11
+ class << self
12
+ # @return [MixedGauge::Config]
13
+ def config
14
+ @config ||= Config.new
15
+ end
16
+
17
+ def configure(&block)
18
+ config.instance_eval(&block)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,47 @@
1
+ module MixedGauge
2
+ # Mapping of slot -> connection_name.
3
+ class ClusterConfig
4
+ attr_reader :name
5
+
6
+ # @param [Symbol] name
7
+ def initialize(name)
8
+ @name = name
9
+ @connection_registry = {}
10
+ end
11
+
12
+ # @param [Range] slots
13
+ # @return [nil]
14
+ def define_slots(slots)
15
+ @slots = slots
16
+ nil
17
+ end
18
+
19
+ # @param [Range] slots
20
+ # @param [Symbol] connection connection name
21
+ # @return [nil]
22
+ def register(slots, connection)
23
+ @connection_registry[slots] = connection
24
+ nil
25
+ end
26
+
27
+ def validate_config!
28
+ # TODO
29
+ end
30
+
31
+ # @return [Integer]
32
+ def slot_count
33
+ @slots.count
34
+ end
35
+
36
+ # @param [Integer] slot
37
+ # @return [Symbol] registered connection name
38
+ def fetch(slot)
39
+ @connection_registry.find {|slot_range, name| slot_range.cover?(slot) }[1]
40
+ end
41
+
42
+ # @return [Array<Symbol>] An array of connection name
43
+ def connections
44
+ @connection_registry.values
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,26 @@
1
+ module MixedGauge
2
+ class Config
3
+ def initialize
4
+ @cluster_configs = {}
5
+ end
6
+
7
+ # @param [Symbol] cluster_name
8
+ # @return [nil]
9
+ # @example
10
+ # config.define_cluster(:user) do |c|
11
+ # c.define_slots(1..1024)
12
+ # end
13
+ def define_cluster(cluster_name, &block)
14
+ cluster_config = ClusterConfig.new(cluster_name)
15
+ cluster_config.instance_eval(&block)
16
+ @cluster_configs[cluster_name] = cluster_config
17
+ nil
18
+ end
19
+
20
+ # @param [Symbol] cluster_name
21
+ # @return [MixedGauge::ClusterConfig]
22
+ def fetch_cluster_config(cluster_name)
23
+ @cluster_configs.fetch(cluster_name)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,8 @@
1
+ module MixedGauge
2
+ class Error < ::StandardError
3
+ end
4
+
5
+ # Raised when try to put new record without distkey attribute.
6
+ class MissingDistkeyAttribute < Error
7
+ end
8
+ end
@@ -0,0 +1,69 @@
1
+ require 'active_support/concern'
2
+
3
+ module MixedGauge
4
+ # @example
5
+ # class User < ActiveRecord::Base
6
+ # include MixedGauge::Model
7
+ # use_cluster :user
8
+ # distkey :email
9
+ # end
10
+ #
11
+ # User.put!(email: 'alice@example.com', name: 'alice')
12
+ #
13
+ # alice = User.get('alice@example.com')
14
+ # alice.age = 1
15
+ # alice.save!
16
+ #
17
+ # User.all_shards.flat_map {|m| m.where(name: 'alice') }
18
+ module Model
19
+ extend ActiveSupport::Concern
20
+
21
+ included do
22
+ class_attribute :cluster_routing, instance_writer: false
23
+ class_attribute :sub_model_repository, instance_writer: false
24
+ end
25
+
26
+ module ClassMethods
27
+ # @param [Symbol] A cluster name which is set by MixedGauge.configure
28
+ def use_cluster(name)
29
+ config = MixedGauge.config.fetch_cluster_config(name)
30
+ self.cluster_routing = MixedGauge::Routing.new(config)
31
+ self.sub_model_repository = MixedGauge::SubModelRepository.new(config, self)
32
+ self.abstract_class = true
33
+ end
34
+
35
+ # @param [Symbol] column
36
+ def distkey(column)
37
+ self.distkey = column.to_sym
38
+ end
39
+
40
+ # @param [Object] key
41
+ # @return [ActiveRecord::Base] A auto-generated sub model of included model
42
+ def get(key)
43
+ shard_for(key.to_s).find_by(distkey => key)
44
+ end
45
+
46
+ # @param [Hash] attributes
47
+ # @return [ActiveRecord::Base] A sub class instance of included model
48
+ def put!(attributes)
49
+ if key = attributes[distkey] || attributes[distkey.to_s]
50
+ shard_for(key).create!(attributes)
51
+ else
52
+ raise MixedGauge::MissingDistkeyAttribute
53
+ end
54
+ end
55
+
56
+ # @param [Object] key A value of distkey
57
+ # @return [Class] A sub model for this distkey value
58
+ def shard_for(key)
59
+ connection_name = cluster_routing.route(key.to_s)
60
+ sub_model_repository.fetch(connection_name)
61
+ end
62
+
63
+ # @return [Array<Class>] An array of sub models
64
+ def all_shards
65
+ sub_model_repository.all
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,23 @@
1
+ require 'digest/md5'
2
+
3
+ module MixedGauge
4
+ class Routing
5
+ # @param [ClusterConfig] cluster_config
6
+ def initialize(cluster_config)
7
+ @cluster_config = cluster_config
8
+ end
9
+
10
+ # @param [String] dist_key
11
+ # @return [String] connection name
12
+ def route(key)
13
+ slot = hash_f(key) % @cluster_config.slot_count
14
+ @cluster_config.fetch(slot)
15
+ end
16
+
17
+ # @param [String] key
18
+ # @return [Integer]
19
+ def hash_f(key)
20
+ Digest::MD5.hexdigest(key).to_i(16)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,53 @@
1
+ module MixedGauge
2
+ class SubModelRepository
3
+ attr_reader :base_class
4
+
5
+ # @param [ClusterConfig] cluster_config
6
+ # @param [Class] base_class A AR Model
7
+ def initialize(cluster_config, base_class)
8
+ @base_class = base_class
9
+
10
+ sub_models = cluster_config.connections.map do |connection_name|
11
+ [connection_name, generate_sub_model(connection_name)]
12
+ end
13
+ @sub_models = Hash[sub_models]
14
+ end
15
+
16
+ # @param [Symbol] connection_name
17
+ # @return [Class] A sub model of given base class
18
+ def fetch(connection_name)
19
+ @sub_models.fetch(connection_name)
20
+ end
21
+
22
+ # @return [Array<Class>]
23
+ def all
24
+ @sub_models.values
25
+ end
26
+
27
+ private
28
+
29
+ # @param [Symbol] connection_name
30
+ # @return [Class] A generated sub class of given AR model
31
+ def generate_sub_model(connection_name)
32
+ base_class_name = @base_class.name
33
+ class_name = generate_class_name(connection_name)
34
+
35
+ sub_model = Class.new(base_class) do
36
+ self.table_name = base_class.table_name
37
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
38
+ def self.name
39
+ "#{base_class_name}::#{class_name}"
40
+ end
41
+ RUBY
42
+ end
43
+ sub_model.class_eval { establish_connection(connection_name) }
44
+ sub_model
45
+ end
46
+
47
+ # @param [Symbol] name
48
+ # @return [String]
49
+ def generate_class_name(name)
50
+ "GeneratedModel#{name.to_s.gsub('-', '_').classify}"
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module MixedGauge
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mixed_gauge/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mixed_gauge'
8
+ spec.version = MixedGauge::VERSION
9
+ spec.authors = ['Taiki Ono']
10
+ spec.email = ['taiks.4559@gmail.com']
11
+
12
+ spec.summary = %q{An ActiveRecord extension for database sharding.}
13
+ spec.description = %q{Offers simple key-value action, node management with hash slots, allow to use some AR::B actions.}
14
+ spec.homepage = 'https://github.com/taiki45/mixed_gauge'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'activesupport', '>= 4.0.0'
23
+ spec.add_dependency 'activerecord', '>= 4.0.0'
24
+ spec.add_development_dependency 'bundler', '~> 1.9'
25
+ spec.add_development_dependency 'pry'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3'
28
+ spec.add_development_dependency 'sqlite3'
29
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mixed_gauge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Taiki Ono
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Offers simple key-value action, node management with hash slots, allow
112
+ to use some AR::B actions.
113
+ email:
114
+ - taiks.4559@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/mixed_gauge.rb
129
+ - lib/mixed_gauge/cluster_config.rb
130
+ - lib/mixed_gauge/config.rb
131
+ - lib/mixed_gauge/errors.rb
132
+ - lib/mixed_gauge/model.rb
133
+ - lib/mixed_gauge/routing.rb
134
+ - lib/mixed_gauge/sub_model_repository.rb
135
+ - lib/mixed_gauge/version.rb
136
+ - mixed_gauge.gemspec
137
+ homepage: https://github.com/taiki45/mixed_gauge
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.2.2
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: An ActiveRecord extension for database sharding.
161
+ test_files: []
162
+ has_rdoc: