From 47ce3824650d2566ff308fc385e246dce025db17 Mon Sep 17 00:00:00 2001 From: Mike Perham Date: Sat, 27 Aug 2011 17:09:03 -0700 Subject: [PATCH] Remove EM/fiber support from this gem. Support has moved to the em-synchrony gem. --- CHANGELOG.md | 4 + .../connection_adapters/em_mysql2_adapter.rb | 64 --------- lib/active_record/fiber_patches.rb | 132 ------------------ lib/mysql2/em_fiber.rb | 31 ---- spec/em/em_fiber_spec.rb | 22 --- 5 files changed, 4 insertions(+), 249 deletions(-) delete mode 100644 lib/active_record/connection_adapters/em_mysql2_adapter.rb delete mode 100644 lib/active_record/fiber_patches.rb delete mode 100644 lib/mysql2/em_fiber.rb delete mode 100644 spec/em/em_fiber_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ec6012d4..357f00761 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## HEAD +* remove fiber support from mysql2, the code has moved to the + em-synchrony gem. + ## 0.3.7 (August 16th, 2011) * ensure symbolized column names support encodings in 1.9 diff --git a/lib/active_record/connection_adapters/em_mysql2_adapter.rb b/lib/active_record/connection_adapters/em_mysql2_adapter.rb deleted file mode 100644 index 5deb575fb..000000000 --- a/lib/active_record/connection_adapters/em_mysql2_adapter.rb +++ /dev/null @@ -1,64 +0,0 @@ -# encoding: utf-8 - -# AR adapter for using a fibered mysql2 connection with EM -# This adapter should be used within Thin or Unicorn with the rack-fiber_pool middleware. -# Just update your database.yml's adapter to be 'em_mysql2' - -module ActiveRecord - class Base - def self.em_mysql2_connection(config) - client = ::Mysql2::Fibered::Client.new(config.symbolize_keys) - options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0] - ConnectionAdapters::Mysql2Adapter.new(client, logger, options, config) - end - end -end - -require 'fiber' -require 'eventmachine' -require 'mysql2' -require 'active_record/connection_adapters/mysql2_adapter' -require 'active_record/fiber_patches' - -module Mysql2 - module Fibered - class Client < ::Mysql2::Client - module Watcher - def initialize(client, deferable) - @client = client - @deferable = deferable - end - - def notify_readable - begin - detach - results = @client.async_result - @deferable.succeed(results) - rescue Exception => e - @deferable.fail(e) - end - end - end - - def query(sql, opts={}) - if ::EM.reactor_running? - super(sql, opts.merge(:async => true)) - deferrable = ::EM::DefaultDeferrable.new - ::EM.watch(self.socket, Watcher, self, deferrable).notify_readable = true - fiber = Fiber.current - deferrable.callback do |result| - fiber.resume(result) - end - deferrable.errback do |err| - fiber.resume(err) - end - Fiber.yield.tap do |result| - raise result if result.is_a?(Exception) - end - else - super(sql, opts) - end - end - end - end -end \ No newline at end of file diff --git a/lib/active_record/fiber_patches.rb b/lib/active_record/fiber_patches.rb deleted file mode 100644 index d2c22233d..000000000 --- a/lib/active_record/fiber_patches.rb +++ /dev/null @@ -1,132 +0,0 @@ -# Necessary monkeypatching to make AR fiber-friendly. - -module ActiveRecord - module ConnectionAdapters - - def self.fiber_pools - @fiber_pools ||= [] - end - def self.register_fiber_pool(fp) - fiber_pools << fp - end - - class FiberedMonitor - class Queue - def initialize - @queue = [] - end - - def wait(timeout) - t = timeout || 5 - fiber = Fiber.current - x = EM::Timer.new(t) do - @queue.delete(fiber) - fiber.resume(false) - end - @queue << fiber - Fiber.yield.tap do - x.cancel - end - end - - def signal - fiber = @queue.pop - fiber.resume(true) if fiber - end - end - - def synchronize - yield - end - - def new_cond - Queue.new - end - end - - # ActiveRecord's connection pool is based on threads. Since we are working - # with EM and a single thread, multiple fiber design, we need to provide - # our own connection pool that keys off of Fiber.current so that different - # fibers running in the same thread don't try to use the same connection. - class ConnectionPool - def initialize(spec) - @spec = spec - - # The cache of reserved connections mapped to threads - @reserved_connections = {} - - # The mutex used to synchronize pool access - @connection_mutex = FiberedMonitor.new - @queue = @connection_mutex.new_cond - - # default 5 second timeout unless on ruby 1.9 - @timeout = spec.config[:wait_timeout] || 5 - - # default max pool size to 5 - @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5 - - @connections = [] - @checked_out = [] - @automatic_reconnect = true - @tables = {} - - @columns = Hash.new do |h, table_name| - h[table_name] = with_connection do |conn| - - # Fetch a list of columns - conn.columns(table_name, "#{table_name} Columns").tap do |columns| - - # set primary key information - columns.each do |column| - column.primary = column.name == primary_keys[table_name] - end - end - end - end - - @columns_hash = Hash.new do |h, table_name| - h[table_name] = Hash[columns[table_name].map { |col| - [col.name, col] - }] - end - - @primary_keys = Hash.new do |h, table_name| - h[table_name] = with_connection do |conn| - table_exists?(table_name) ? conn.primary_key(table_name) : 'id' - end - end - end - - def clear_stale_cached_connections! - cache = @reserved_connections - keys = Set.new(cache.keys) - - ActiveRecord::ConnectionAdapters.fiber_pools.each do |pool| - pool.busy_fibers.each_pair do |object_id, fiber| - keys.delete(object_id) - end - end - - keys.each do |key| - next unless cache.has_key?(key) - checkin cache[key] - cache.delete(key) - end - end - - private - - def current_connection_id #:nodoc: - Fiber.current.object_id - end - - def checkout_and_verify(c) - @checked_out << c - c.run_callbacks :checkout - c.verify! - c - end - end - - end -end diff --git a/lib/mysql2/em_fiber.rb b/lib/mysql2/em_fiber.rb deleted file mode 100644 index 36cfddad6..000000000 --- a/lib/mysql2/em_fiber.rb +++ /dev/null @@ -1,31 +0,0 @@ -# encoding: utf-8 - -require 'mysql2/em' -require 'fiber' - -module Mysql2 - module EM - module Fiber - class Client < ::Mysql2::EM::Client - def query(sql, opts={}) - if ::EM.reactor_running? - deferable = super(sql, opts) - - fiber = ::Fiber.current - deferable.callback do |result| - fiber.resume(result) - end - deferable.errback do |err| - fiber.resume(err) - end - ::Fiber.yield.tap do |result| - raise result if result.is_a?(::Exception) - end - else - super(sql, opts) - end - end - end - end - end -end diff --git a/spec/em/em_fiber_spec.rb b/spec/em/em_fiber_spec.rb deleted file mode 100644 index 91b294ea0..000000000 --- a/spec/em/em_fiber_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -# encoding: UTF-8 -if defined? EventMachine && defined? Fiber - require 'spec_helper' - require 'mysql2/em_fiber' - - describe Mysql2::EM::Fiber::Client do - it 'should support queries' do - results = [] - EM.run do - Fiber.new { - client1 = Mysql2::EM::Fiber::Client.new - results = client1.query "SELECT sleep(0.1) as first_query" - EM.stop_event_loop - }.resume - end - - results.first.keys.should include("first_query") - end - end -else - puts "Either EventMachine or Fibers not available. Skipping tests that use them." -end \ No newline at end of file