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.



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 114

def initialize(connection, logger, connections, pool_weights)
  super(connection, logger)

  @available_connections = []
  @master_connection = connection
  @connections = connections.dup.freeze

  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.



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

def available_connections
  @available_connections
end

#connectionsObject (readonly)

The total sum of connections



68
69
70
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 68

def connections
  @connections
end

#master_connectionObject (readonly)

The current connection in use



69
70
71
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 69

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.



74
75
76
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
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 74

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 "      def \#{method_name}(*args, &block)\n        return proxy_connection_method(master_connection, :\#{method_name}, *args, &block)\n      end\n    EOS\n  end\n\n  return klass\nend\n", __FILE__, __LINE__ + 1

.visitor_for(pool) ⇒ Object

Set the arel visitor on the connections.



105
106
107
108
109
110
111
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 105

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)


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

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

#adapter_nameObject

:nodoc:



126
127
128
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 126

def adapter_name #:nodoc:
  'MariaDB_Cluster_Pool'
end

#all_connectionsObject

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



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

def all_connections
  @connections
end

#disconnect!Object



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

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

#next_usable_connectionObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 255

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



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

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

#requires_reloading?Boolean

Returns:

  • (Boolean)


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

def requires_reloading?
  false
end

#reset!Object



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

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

#reset_available_connectionsObject



233
234
235
236
237
238
239
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 233

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

#reset_runtimeObject



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

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.



242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/active_record/connection_adapters/maria_db_cluster_pool_adapter.rb', line 242

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



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

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

#visitorObject



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

def visitor
  connection.visitor
end

#visitor=(visitor) ⇒ Object



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

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