thiagopradi / octopus

Database Sharding for ActiveRecord
2.53k stars 505 forks source link

Will not work with spring #303

Open travisp opened 9 years ago

travisp commented 9 years ago

When running commands using spring, I'm unable to obtain a connection. Without spring everything works great. I imagine this has to do with how spring makes and holds database connections. There was a mention of this as an issue within a report about spork, but I couldn't find any open issues mentioning the problem with spring https://github.com/tchandy/octopus/issues/120

ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
/Users/travis/.rvm/gems/ruby-2.2.0@eventsearch/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `checkout_new_connection'
/Users/travis/.rvm/gems/ruby-2.2.0@eventsearch/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in `acquire_connection'
/Users/travis/.rvm/gems/ruby-2.2.0@eventsearch/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `block in checkout'
/Users/travis/.rvm/gems/ruby-2.2.0@eventsearch/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:350:in `checkout'
/Users/travis/.rvm/gems/ruby-2.2.0@eventsearch/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/Users/travis/.rvm/gems/ruby-2.2.0@eventsearch/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
/Users/travis/.rvm/gems/ruby-2.2.0@eventsearch/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:205:in `safe_connection'
/Users/travis/.rvm/gems/ruby-2.2.0@eventsearch/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:212:in `select_connection'
velles commented 9 years ago

Hello, I had simuler issue where , commands and rake 'db:migrate' would not load the master db from the master shard. That was happening because they pre-load the db connection before running the command.

Solution for me was (using Rails app), adding this to 'config/initializers/octopus.rb'

if Octopus.enabled? 
  Octopus.config[Rails.env.to_s]['master'] = ActiveRecord::Base.connection.config
  ActiveRecord::Base.connection.initialize_shards(Octopus.config)
end

Not sure where you would do that in spring though.

jipiboily commented 9 years ago

Any luck with this?

I'm hitting a similar (I'm assuming) problem here:

ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"clients"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
from /Users/jipiboily/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
jipiboily commented 9 years ago

It looks, for me, that using what @velles suggested works. Just add those lines in the config/spring.rb.

This is what it looks like for me at this point:

Spring.after_fork do
  if defined?(ActiveRecord::Base)
    if Octopus.enabled?
      Octopus.config[Rails.env.to_s]['master'] = ActiveRecord::Base.connection.config
      ActiveRecord::Base.connection.initialize_shards(Octopus.config)
    end

    ActiveRecord::Base.establish_connection
    QC.default_conn_adapter = QC::ConnAdapter.new(ActiveRecord::Base.connection.raw_connection)
  end
end