Hi John,

We've been thinking a bit on SingleConnectionMixin ('vendor/ruote/lib/
openwfe/extras/singlecon.rb') and ActiveRecord's pool management. It
seems that AR itself does not manage the connection pool at all and
it's up to the developer to checkout/checkin connections.

After some googling, we found a monkey patch from coderrr (http://
coderrr.wordpress.com/2009/01/16/monkey-patching-activerecord-to-
automatically-release-connections/) that makes AR to automatically
manage the pool.

Besides, I took the liberty of asking coderrr himself about the patch
itself, and about it being included into AR => 2.3. Here is the
thread:

---- BEGIN ----
Just checked and my with_connection patch isn't in 2.3.2.  It's in
trunk so it'll probably be in 3.  But my
monkeypatch which automatically releases connections will never go
into rails, because it's a monkey patch.  I don't know whether or not
someone will actually change all the calls in AR to do something like
my patch does. ...
Thanks for using it though

- steve

On Wed, Jul 1, 2009 at 11:38 PM, Gonzalo <[email protected]> wrote:
hi coderrr,

i've just read your neat post "Monkey patching ActiveRecord to
automatically release connections", dated back in January.

Right now, I'm refactoring an AR 2.1 app to AR 2.3.2 - It's not a
Rails app - just plain old Rack :-)

The first thing I want to take advantage of is connection pooling. The
documentation states that there are three ways of using it:
http://docs.rails.info/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html

And your MonkeyPatch uses
ActiveRecord::Base.connection_pool.with_connection(&block) to wrap
AR's finders and methods.

My question is, if your patch has already been merged into AR 2.3.2.
so that there'd be no need to use it in my code.

Best regards,
//gonzalo

---- END ----


With all this info, we only had to modify the patch slightly (http://
github.com/coderrr/cleanup_connection/blob/master/
cleanup_connection_patch.rb) and add the method
"save_without_transactions!" to the "methods_to_wrap" hash. It should
look like this:

42 ...
43   ActiveRecord::Base =>
[:save_without_transactions!, :quoted_id, :valid?],
44 ...

The last step is to require this patch from "lib/ar_con.rb" and
comment out the SingleConnectionMixin module.

Moreover, we also found that mysql gem performs poorly compared to the
newer gem mysqlplus (gem install mysqlplus). This nice post talks
about it
(http://www.igvita.com/2008/10/27/scaling-activerecord-with-
mysqlplus/)

In order to use mysqlplus within ruote-rest we added the following
lines to 'lib/ar_con.rb':

 27 require 'mysqlplus'
 28 class Mysql; alias :query :async_query; end

Obviously, JRuby ruotists wouldn't need this "mysqlplus" gem, as
connections are managed via jdbc-connectors.

Finally, we took all this for a test-drive, using this simple, even
naïve, code:

---- BEGIN ----
  1 wfid= "insert your wfid"
  2 threads = []
  3 100.times do |n|
  4
  5  threads <<  Thread.new {
  6     `curl -i --url http://192.168.168.128:4567/workitems/#{wfid}/0_0_0
-u charly:secret --data body="<workitem><flow_expression_id></
flow_expression_id><attributes><hash><entry><string>key0</strin
g><string>THREAD_0</string></entry><entry><string>params</
string><hash><entry><string>ref</string><string>alpha</string></
entry></hash></entry><entry><string>key1</
string><string>VALUE_THREAD_0</string></entry    ></hash></
attributes></workitem>" --request PUT -H "Content-Type: application/
xml" >/dev/null`
  7   }
  8 end
  9
 10 # block and wait for all threads to finish
 11 threads.each { |t| t.join }

---- END ----

60 simultaneous updates on the same workitem:

  - ruote-rest (out-of-the-box): 5,19 s.
  - ruote-rest (out-of-the-box + cleanup_connection_patch + mysqlplus
gem): 3,23 s.

100 simultaneous updates:

  -ruote-rest (out-of-the-box): crashes with conn pool exhaustion
errors:
   ActiveRecord::ConnectionTimeoutError: could not obtain a database
connection within 3 seconds.  The max pool size is currently 30;
consider increasing it.
  - ruote-rest (out-of-the-box + cleanup_connection_patch + mysqlplus
gem): 6,2 s.


Hope you find this interesting, and that this hints allow ruote-rest
to be even more useful :-)

- Gonzalo

--~--~---------~--~----~------------~-------~--~----~
you received this message because you are subscribed to the "ruote users" group.
to post : send email to [email protected]
to unsubscribe : send email to [email protected]
more options : http://groups.google.com/group/openwferu-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to