Class: ActiveRecord::ConnectionAdapters::MariaDbClusterPoolAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb

Defined Under Namespace

Classes: AvailableConnection, DatabaseConnectionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, logger, connections, pool_weights) ⇒ MariaDbClusterPoolAdapter

Returns a new instance of MariaDbClusterPoolAdapter.



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 117

def initialize(connection, logger, connections, pool_weights)
  # @available_connections = connections.dup.freeze
  @connections = connections.dup.freeze
  @available_connections = []
  @master_connection = connection

  super(connection, logger)

  pool_weights.each_pair do |conn, weight|
    @available_connections[weight] = AvailableConnection.new(conn)
  end
end

Instance Attribute Details

#available_connectionsObject (readonly)

Get the available weighted connections. When a connection is dead and cannot be reconnected, it will be temporarily removed from the read pool so we don’t keep trying to reconnect to a database that isn’t listening.



73
74
75
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 73

def available_connections
  @available_connections
end

#connectionsObject (readonly)

The total sum of connections



71
72
73
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 71

def connections
  @connections
end

#master_connectionObject (readonly)

The current connection in use



72
73
74
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 72

def master_connection
  @master_connection
end

Class Method Details

.adapter_class(master_connection) ⇒ Object

Create an anonymous class that extends this one and proxies methods to the pool connections.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 77

def adapter_class(master_connection)
  # Define methods to proxy to the appropriate pool
  master_methods = []
  master_connection_classes = [AbstractAdapter, Quoting, DatabaseStatements, SchemaStatements]
  master_connection_classes << DatabaseLimits if const_defined?(:DatabaseLimits)
  master_connection_class = master_connection.class
  while ![Object, AbstractAdapter].include?(master_connection_class) do
    master_connection_classes << master_connection_class
    master_connection_class = master_connection_class.superclass
  end
  master_connection_classes.each do |connection_class|
    master_methods.concat(connection_class.public_instance_methods(false))
    master_methods.concat(connection_class.protected_instance_methods(false))
  end
  master_methods.uniq!
  master_methods -= public_instance_methods(false) + protected_instance_methods(false) + private_instance_methods(false)
  master_methods = master_methods.collect{|m| m.to_sym}

  klass = Class.new(self)
  master_methods.each do |method_name|
    klass.class_eval <<-EOS, __FILE__, __LINE__ + 1
      def #{method_name}(*args, &block)
        return proxy_connection_method(master_connection, :#{method_name}, *args, &block)
      end
    EOS
  end

  return klass
end

.visitor_for(pool) ⇒ Object

Set the arel visitor on the connections.



108
109
110
111
112
113
114
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 108

def visitor_for(pool)
  # This is ugly, but then again, so is the code in ActiveRecord for setting the arel
  # visitor. There is a note in the code indicating the method signatures should be updated.
  config = pool.spec.config.with_indifferent_access
  adapter = config[:master][:adapter] || config[:pool_adapter]
  MariaDbClusterPool.adapter_class_for(adapter).visitor_for(pool)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


151
152
153
154
155
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 151

def active?
  active = true
  do_to_connections {|conn| active &= conn.active?}
  return active
end

#adapter_nameObject

:nodoc:



130
131
132
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 130

def adapter_name #:nodoc:
  'MariaDB_Cluster_Pool'
end

#all_connectionsObject

Returns an array of the master connection and the read pool connections



135
136
137
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 135

def all_connections
  @connections
end

#disconnect!Object



161
162
163
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 161

def disconnect!
  do_to_connections {|conn| conn.disconnect!}
end

#next_usable_connectionObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 282

def next_usable_connection
  available = available_connections
  available.each do |a|
    if a != nil
      unless a.failed?
        if a.connection.active?
          @logger.info("New master connection is now : #{a.connection.inspect}") if @logger
          @master_connection = a.connection
          break
        end
      end
    end
  end
end

#reconnect!Object



157
158
159
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 157

def reconnect!
  do_to_connections {|conn| conn.reconnect!}
end

#requires_reloading?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 139

def requires_reloading?
  false
end

#reset!Object



165
166
167
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 165

def reset!
  do_to_connections {|conn| conn.reset!}
end

#reset_available_connectionsObject



260
261
262
263
264
265
266
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 260

def reset_available_connections
  @available_connections.each do |a|
    if a != nil
      a.reconnect! rescue nil
    end
  end
end

#reset_runtimeObject



173
174
175
176
177
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 173

def reset_runtime
  total = 0.0
  do_to_connections {|conn| total += conn.reset_runtime}
  total
end

#suppress_connection(conn, expire) ⇒ Object

Temporarily remove a connection from the read pool.



269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 269

def suppress_connection(conn, expire)
  available = available_connections
  available.each do |a|
    if a != nil
      if a.connection == conn
        a.failed_connection = true
        a.expires = expire.seconds.from_now
        @logger.info("Supressing database connection from the pool : #{a.connection.inspect}") if @logger
      end
    end
  end
end

#verify!(*ignored) ⇒ Object



169
170
171
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 169

def verify!(*ignored)
  do_to_connections {|conn| conn.verify!(*ignored)}
end

#visitorObject



147
148
149
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 147

def visitor
  connection.visitor
end

#visitor=(visitor) ⇒ Object



143
144
145
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 143

def visitor=(visitor)
  all_connections.each{|conn| conn.visitor = visitor}
end