Created
November 30, 2012 18:27
-
-
Save lcowell/4177578 to your computer and use it in GitHub Desktop.
apartment postgres threading issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
development: &defaults | |
adapter: postgresql | |
database: app_development | |
pool: 25 | |
encoding: unicode | |
user: root | |
password: | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'apartment' | |
gem 'sidekiq' | |
gem 'rails' | |
gem 'pg' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GEM | |
remote: https://rubygems.org/ | |
specs: | |
actionmailer (3.2.9) | |
actionpack (= 3.2.9) | |
mail (~> 2.4.4) | |
actionpack (3.2.9) | |
activemodel (= 3.2.9) | |
activesupport (= 3.2.9) | |
builder (~> 3.0.0) | |
erubis (~> 2.7.0) | |
journey (~> 1.0.4) | |
rack (~> 1.4.0) | |
rack-cache (~> 1.2) | |
rack-test (~> 0.6.1) | |
sprockets (~> 2.2.1) | |
activemodel (3.2.9) | |
activesupport (= 3.2.9) | |
builder (~> 3.0.0) | |
activerecord (3.2.9) | |
activemodel (= 3.2.9) | |
activesupport (= 3.2.9) | |
arel (~> 3.0.2) | |
tzinfo (~> 0.3.29) | |
activeresource (3.2.9) | |
activemodel (= 3.2.9) | |
activesupport (= 3.2.9) | |
activesupport (3.2.9) | |
i18n (~> 0.6) | |
multi_json (~> 1.0) | |
apartment (0.18.0) | |
activerecord (>= 3.1.2) | |
rack (>= 1.3.6) | |
arel (3.0.2) | |
builder (3.0.4) | |
celluloid (0.12.3) | |
facter (>= 1.6.12) | |
timers (>= 1.0.0) | |
connection_pool (0.9.2) | |
erubis (2.7.0) | |
facter (1.6.15) | |
hike (1.2.1) | |
i18n (0.6.1) | |
journey (1.0.4) | |
json (1.7.5) | |
mail (2.4.4) | |
i18n (>= 0.4.0) | |
mime-types (~> 1.16) | |
treetop (~> 1.4.8) | |
mime-types (1.19) | |
multi_json (1.3.7) | |
pg (0.14.0) | |
polyglot (0.3.3) | |
rack (1.4.1) | |
rack-cache (1.2) | |
rack (>= 0.4) | |
rack-ssl (1.3.2) | |
rack | |
rack-test (0.6.2) | |
rack (>= 1.0) | |
rails (3.2.9) | |
actionmailer (= 3.2.9) | |
actionpack (= 3.2.9) | |
activerecord (= 3.2.9) | |
activeresource (= 3.2.9) | |
activesupport (= 3.2.9) | |
bundler (~> 1.0) | |
railties (= 3.2.9) | |
railties (3.2.9) | |
actionpack (= 3.2.9) | |
activesupport (= 3.2.9) | |
rack-ssl (~> 1.3.2) | |
rake (>= 0.8.7) | |
rdoc (~> 3.4) | |
thor (>= 0.14.6, < 2.0) | |
rake (10.0.2) | |
rdoc (3.12) | |
json (~> 1.4) | |
redis (3.0.2) | |
redis-namespace (1.2.1) | |
redis (~> 3.0.0) | |
sidekiq (2.5.4) | |
celluloid (~> 0.12.0) | |
connection_pool (~> 0.9.2) | |
multi_json (~> 1) | |
redis (~> 3) | |
redis-namespace | |
sprockets (2.2.2) | |
hike (~> 1.2) | |
multi_json (~> 1.0) | |
rack (~> 1.0) | |
tilt (~> 1.1, != 1.3.0) | |
thor (0.16.0) | |
tilt (1.3.3) | |
timers (1.0.1) | |
treetop (1.4.12) | |
polyglot | |
polyglot (>= 0.3.1) | |
tzinfo (0.3.35) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
apartment | |
pg | |
rails | |
sidekiq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "rubygems" | |
require "bundler/setup" | |
require 'rails/all' | |
require 'apartment' | |
class Worker | |
WORKER_COUNT = 20 | |
SCHEMA_NAMES = (1..5).to_a.map {|n| "company_#{n}" } | |
def initialize | |
@connection = Apartment.connection | |
end | |
def spawn | |
puts "#{self.class} starting" | |
WORKER_COUNT.times.map do | |
Thread.new do | |
perform | |
end | |
end.each do |th| | |
th.join | |
end | |
puts "#{self.class} done" | |
end | |
def perform | |
ActiveRecord::Base.connection_pool.with_connection do |connection| | |
stash_name | |
connection.schema_search_path = Thread.current[:name] | |
# sleep rand(3) | |
check(connection) | |
end | |
end | |
def check(connection) | |
# result = connection.execute("SELECT COUNT(*) FROM reminders;") | |
# printf "result: %s, company: %s\n", result.values[0], Thread.current[:name] | |
printf "%s: %s :%s\n", | |
Thread.current[:name], | |
connection.schema_search_path, | |
Thread.current[:name] == connection.schema_search_path | |
end | |
def stash_name | |
name = SCHEMA_NAMES.sample | |
Thread.current[:name] = name | |
end | |
def connection_info(current_connection) | |
printf "main: %s, apartment: %s, ar: %s con: %s\n", @connection.object_id, Apartment.connection.object_id, ActiveRecord::Base.connection.object_id, current_connection.object_id | |
end | |
end | |
class PooledApartmentWorker < Worker | |
def perform | |
ActiveRecord::Base.connection_pool.with_connection do |connection| | |
stash_name | |
Apartment::Database.switch(Thread.current[:name]) | |
check(Apartment.connection) | |
end | |
end | |
end | |
class ApartmentWorker < Worker | |
# Broken | |
def perform | |
stash_name | |
Apartment::Database.switch(Thread.current[:name]) | |
check(@connection) | |
end | |
end | |
Apartment.configure do |config| | |
config.database_names = Worker::SCHEMA_NAMES | |
end | |
class MyApp < Rails::Application | |
config.active_support.deprecation = :log | |
config.threadsafe! | |
end | |
MyApp.initialize! | |
Apartment.connection | |
Worker.new.spawn | |
PooledApartmentWorker.new.spawn | |
# ApartmentWorker.new.spawn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment