github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

mperham / data_fabric forked from fiveruns/data_fabric

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 54
    • 8
  • Source
  • Commits
  • Network (8)
  • Issues (2)
  • Downloads (7)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (2)
    • activerecord-2.2
    • master ✓
  • Tags (7)
    • v1.2.5
    • v1.2.4
    • v1.2.3
    • v1.2.2
    • v1.2.1
    • v1.2.0
    • v1.1.0
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Sharding support for ActiveRecord 2.x — Read more

  cancel

http://github.com/mperham/data_fabric

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Better fix for STI issues. 
geni (author)
Wed Dec 16 18:09:42 -0800 2009
mperham (committer)
Tue Jan 05 08:45:54 -0800 2010
commit  70dc8bff39be0e6d7021bbc72414c8858f8d802c
tree    1acdd7d1661ce34ce880cc9a77d8eeba062208fb
parent  b37c12c46b6b2cddad0e33e1284458e58d03c5d6
data_fabric /
name age
history
message
file CHANGELOG Sat Mar 28 14:05:00 -0700 2009 Move to Jeweler [mperham]
file Manifest Mon Mar 09 09:45:20 -0700 2009 Prepare for 1.2.4 release [mperham]
file README.rdoc Sat Mar 28 13:43:56 -0700 2009 Version bump to 0.0.0 [mperham]
file Rakefile Wed Nov 18 13:47:17 -0800 2009 Changed rcov dependency to gemcutter. [geni]
file TESTING.rdoc Sat Nov 22 12:46:15 -0800 2008 Support testing in sqlite by default, rather th... [mperham]
file VERSION.yml Tue Jan 05 08:45:54 -0800 2010 Better fix for STI issues. [geni]
file data_fabric.gemspec Sat Mar 28 14:05:00 -0700 2009 Move to Jeweler [mperham]
directory example/ Mon Dec 01 18:33:58 -0800 2008 A cleaned up 1.2.1 release [mperham]
directory example22/ Thu Feb 05 17:42:17 -0800 2009 Prepare for 1.2.3 release [mperham]
file init.rb Sat Mar 28 13:43:56 -0700 2009 Version bump to 0.0.0 [mperham]
directory lib/ Tue Jan 05 08:45:54 -0800 2010 Better fix for STI issues. [geni]
directory rails/ Sat Mar 28 13:43:56 -0700 2009 Version bump to 0.0.0 [mperham]
directory test/ Thu Feb 05 18:09:35 -0800 2009 Ruby 1.9.1 test failure [mperham]
README.rdoc

data_fabric

DataFabric provides flexible database connection switching for ActiveRecord.

We needed two features to scale our mysql database: application-level sharding and master/slave replication. Sharding is the process of splitting a dataset across many independent databases. This often happens based on geographical region (e.g. craigslist) or category (e.g. ebay). Replication provides a near-real-time copy of a database which can be used for fault tolerance and to reduce load on the master node. Combined, you get a scalable database solution which does not require huge hardware to scale to huge volumes. Or: DPAYEIOB - don’t put all your eggs in one basket. :-)

Installation

  gem install data_fabric

then in config/environment.rb, add a config.gem entry:

  Rails::Initializer.run do |config|
    ...
    config.gem 'data_fabric'

How does it work?

You describe the topology for your database infrastructure in your model(s). Different models can use different topologies.

  class MyHugeVolumeOfDataModel < ActiveRecord::Base
    data_fabric :replicated => true, :shard_by => :city
  end

There are four supported modes of operation, depending on the options given to the data_fabric method. The plugin will look for connections in your config/database.yml with the following convention:

No connection topology: #{environment} - this is the default, as with ActiveRecord, e.g. "production"

  data_fabric :replicated => true

#{environment}_#{role} - no sharding, just replication, where role is "master" or "slave", e.g. "production_master"

  data_fabric :shard_by => :city

#{group}_#{shard}_#{environment} - sharding, no replication, e.g. "city_austin_production"

  data_fabric :replicated => true, :shard_by => :city

#{group}_#{shard}_#{environment}_#{role} - sharding with replication, e.g. "city_austin_production_master"

When marked as replicated, all write and transactional operations for the model go to the master, whereas read operations go to the slave.

Since sharding is an application-level concern, your application must set the shard to use based on the current request or environment. The current shard is set on a thread local variable. For example, you can set the shard in an ActionController around_filter based on the user as follows:

  class ApplicationController < ActionController::Base
    around_filter :select_shard

    private
    def select_shard(&block)
      DataFabric.activate_shard(:city => @current_user.city, &block)
    end
  end

Warnings

  • Sharded models should never be placed in the session store or you will get "Shard not set" errors when the session is persisted.
  • DataFabric does not support running with ActiveRecord’s allow_concurrency = true in AR 2.0 and 2.1. allow_concurrency is gone in AR 2.2.
  • DataFabric does not work in development mode with cache_classes = false. You will get an error in ActiveRecord’s clear_reloadable_connections! method.

Testing and Bug Reports

If you think you’ve found a problem with data_fabric, please use the example application to reproduce the bug and send me the diff. The example application is a stock Rails 2.1 application which uses data_fabric in the expected fashion.

Thanks to…

Rick Olsen of Rails Core:for the Masochism plugin, which showed me how to bend AR’s connection handling to my will
Bradley Taylor of RailsMachine:for the advice to shard while at acts_as_conference
FiveRuns:for paying me to develop this code and allowing its release

Author

Mike Perham <mperham@gmail.com> mikeperham.com

LICENSE:

(The FiveRuns License)

Copyright © 2008 FiveRuns Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server