sessionm-cassandra_object 2.2.23 → 2.2.24

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,8 @@ module CassandraObject
4
4
  extend ActiveSupport::Autoload
5
5
 
6
6
  autoload :Base
7
+ autoload :AsyncConnection
8
+ autoload :Connection
7
9
  autoload :Attributes
8
10
  autoload :Dirty
9
11
  autoload :Consistency
@@ -23,8 +25,6 @@ module CassandraObject
23
25
  autoload :Type
24
26
  autoload :Schema
25
27
 
26
- autoload :ConnectionAdapters, 'cassandra_object/connection/connection_pool'
27
-
28
28
  module Tasks
29
29
  extend ActiveSupport::Autoload
30
30
  autoload :Keyspace
@@ -49,4 +49,3 @@ module CassandraObject
49
49
  end
50
50
 
51
51
  require 'cassandra_object/railtie'
52
-
@@ -0,0 +1,48 @@
1
+ module CassandraObject
2
+ module AsyncConnection
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :connection_spec
7
+
8
+ class_eval do
9
+ @@fiber_connections = {}
10
+ def self.connection()
11
+ @@fiber_connections[Fiber.current.object_id] ||=
12
+ begin
13
+ spec = connection_spec.dup
14
+
15
+ if EM.reactor_running?
16
+ require 'thrift_client/event_machine'
17
+ spec[:thrift].merge!(:transport => Thrift::EventMachineTransport,
18
+ :transport_wrapper => nil)
19
+ end
20
+
21
+ Cassandra.new(spec[:keyspace], spec[:servers], spec[:thrift])
22
+ end
23
+ end
24
+ def self.connection?() !!connection end
25
+
26
+ def connection
27
+ defined?(@connection) ? @connection : singleton_class.connection
28
+ end
29
+
30
+ def connection?
31
+ !!connection
32
+ end
33
+ end
34
+ end
35
+
36
+ module ClassMethods
37
+ DEFAULT_OPTIONS = {
38
+ servers: "127.0.0.1:9160",
39
+ thrift: {}
40
+ }
41
+ def establish_connection(spec)
42
+ spec.reverse_merge!(DEFAULT_OPTIONS)
43
+ spec[:thrift].symbolize_keys!
44
+ self.connection_spec = spec
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,6 +1,5 @@
1
1
  require 'set'
2
2
 
3
- require 'cassandra_object/connection/connection_specification'
4
3
  require 'cassandra_object/log_subscriber'
5
4
  require 'cassandra_object/types'
6
5
  require 'cassandra_object/errors'
@@ -32,9 +31,8 @@ module CassandraObject
32
31
  extend ActiveSupport::DescendantsTracker
33
32
 
34
33
  #TODO: make the connection type configurable
35
- #include AsyncConnection
34
+ include AsyncConnection
36
35
  #include Connection
37
- #include ConnectionPool
38
36
  include Consistency
39
37
  include Identity
40
38
  include Attributes
@@ -7,7 +7,4 @@ module CassandraObject
7
7
 
8
8
  class RecordNotFound < CasssandraObjectError
9
9
  end
10
-
11
- class ConnectionNotEstablished < CasssandraObjectError
12
- end
13
- end
10
+ end
@@ -3,7 +3,9 @@ module CassandraObject
3
3
  extend ActiveSupport::Concern
4
4
  module ClassMethods
5
5
  def find(key)
6
- if parse_key(key) && attributes = ActiveSupport::Notifications.instrument("get.cassandra_object", column_family: column_family, key: key) { connection.get(column_family, key) }
6
+ if parse_key(key) &&
7
+ (attributes = ActiveSupport::Notifications.instrument("get.cassandra_object", column_family: column_family, key: key) { connection.get(column_family, key) }) &&
8
+ !attributes.empty?
7
9
  instantiate(key, attributes)
8
10
  else
9
11
  raise CassandraObject::RecordNotFound
@@ -1,8 +1,5 @@
1
1
  module CassandraObject
2
2
  class Railtie < Rails::Railtie
3
- config.app_middleware.insert_after "::ActionDispatch::Callbacks",
4
- "CassandraObject::ConnectionAdapters::ConnectionManagement"
5
-
6
3
  rake_tasks do
7
4
  load 'cassandra_object/tasks/ks.rake'
8
5
  end
@@ -11,4 +8,4 @@ module CassandraObject
11
8
  require 'cassandra_object/generators/migration_generator'
12
9
  end
13
10
  end
14
- end
11
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'sessionm-cassandra_object'
5
- s.version = '2.2.23'
5
+ s.version = '2.2.24'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 2
7
7
  - 2
8
- - 23
9
- version: 2.2.23
8
+ - 24
9
+ version: 2.2.24
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Koziarski
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-08-18 00:00:00 -04:00
19
+ date: 2011-09-02 00:00:00 -04:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -80,14 +80,13 @@ files:
80
80
  - lib/cassandra_object/associations.rb
81
81
  - lib/cassandra_object/associations/one_to_many.rb
82
82
  - lib/cassandra_object/associations/one_to_one.rb
83
+ - lib/cassandra_object/async_connection.rb
83
84
  - lib/cassandra_object/attributes.rb
84
85
  - lib/cassandra_object/base.rb
85
86
  - lib/cassandra_object/batches.rb
86
87
  - lib/cassandra_object/callbacks.rb
87
88
  - lib/cassandra_object/collection.rb
88
- - lib/cassandra_object/connection/connection.rb
89
- - lib/cassandra_object/connection/connection_pool.rb
90
- - lib/cassandra_object/connection/connection_specification.rb
89
+ - lib/cassandra_object/connection.rb
91
90
  - lib/cassandra_object/consistency.rb
92
91
  - lib/cassandra_object/cursor.rb
93
92
  - lib/cassandra_object/dirty.rb
@@ -1,301 +0,0 @@
1
- require 'thread'
2
- require 'monitor'
3
- require 'set'
4
- require 'active_support/core_ext/module/synchronization'
5
- require 'cassandra_object/errors'
6
-
7
- module CassandraObject
8
- # Raised when a connection could not be obtained within the connection
9
- # acquisition timeout period.
10
- class ConnectionTimeoutError < ConnectionNotEstablished
11
- end
12
-
13
- module ConnectionAdapters
14
- class ConnectionPool
15
- attr_reader :spec, :connections
16
-
17
- # Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification
18
- # object which describes database connection information (e.g. adapter,
19
- # host name, username, password, etc), as well as the maximum size for
20
- # this ConnectionPool.
21
- #
22
- # The default ConnectionPool maximum size is 5.
23
- def initialize(spec)
24
- @spec = spec
25
-
26
- # The cache of reserved connections mapped to threads
27
- @reserved_connections = {}
28
-
29
- # The mutex used to synchronize pool access
30
- @connection_mutex = Monitor.new
31
- @queue = @connection_mutex.new_cond
32
-
33
- @thrift = spec[:thrift]
34
-
35
- # default max pool size to 5
36
- @size = (spec[:pool] && spec[:pool].to_i) || 5
37
-
38
- @connections = []
39
- @checked_out = []
40
- end
41
-
42
- # Retrieve the connection associated with the current thread, or call
43
- # #checkout to obtain one if necessary.
44
- #
45
- # #connection can be called any number of times; the connection is
46
- # held in a hash keyed by the thread id.
47
- def connection
48
- @reserved_connections[current_connection_id] ||= checkout
49
- end
50
-
51
- # Signal that the thread is finished with the current connection.
52
- # #release_connection releases the connection-thread association
53
- # and returns the connection to the pool.
54
- def release_connection(with_id = current_connection_id)
55
- conn = @reserved_connections.delete(with_id)
56
- checkin conn if conn
57
- end
58
-
59
- # If a connection already exists yield it to the block. If no connection
60
- # exists checkout a connection, yield it to the block, and checkin the
61
- # connection when finished.
62
- def with_connection
63
- connection_id = current_connection_id
64
- fresh_connection = true unless @reserved_connections[connection_id]
65
- yield connection
66
- ensure
67
- release_connection(connection_id) if fresh_connection
68
- end
69
-
70
- # Returns true if a connection has already been opened.
71
- def connected?
72
- !@connections.empty?
73
- end
74
-
75
- # Disconnects all connections in the pool, and clears the pool.
76
- def disconnect!
77
- @reserved_connections.each do |name,conn|
78
- checkin conn
79
- end
80
- @reserved_connections = {}
81
- @connections.each do |conn|
82
- conn.disconnect!
83
- end
84
- @connections = []
85
- end
86
-
87
- # Clears the cache which maps classes
88
- def clear_reloadable_connections!
89
- @reserved_connections.each do |name, conn|
90
- checkin conn
91
- end
92
- @reserved_connections = {}
93
- @connections.each do |conn|
94
- conn.disconnect! if conn.requires_reloading?
95
- end
96
- @connections.delete_if do |conn|
97
- conn.requires_reloading?
98
- end
99
- end
100
-
101
- # Verify active connections and remove and disconnect connections
102
- # associated with stale threads.
103
- def verify_active_connections! #:nodoc:
104
- clear_stale_cached_connections!
105
- @connections.each do |connection|
106
- connection.verify!
107
- end
108
- end
109
-
110
- # Return any checked-out connections back to the pool by threads that
111
- # are no longer alive.
112
- def clear_stale_cached_connections!
113
- keys = @reserved_connections.keys - Thread.list.find_all { |t|
114
- t.alive?
115
- }.map { |thread| thread.object_id }
116
- keys.each do |key|
117
- checkin @reserved_connections[key]
118
- @reserved_connections.delete(key)
119
- end
120
- end
121
-
122
- # Check-out a database connection from the pool, indicating that you want
123
- # to use it. You should call #checkin when you no longer need this.
124
- #
125
- # This is done by either returning an existing connection, or by creating
126
- # a new connection. If the maximum number of connections for this pool has
127
- # already been reached, but the pool is empty (i.e. they're all being used),
128
- # then this method will wait until a thread has checked in a connection.
129
- # The wait time is bounded however: if no connection can be checked out
130
- # within the timeout specified for this pool, then a ConnectionTimeoutError
131
- # exception will be raised.
132
- #
133
- # Returns: an AbstractAdapter object.
134
- #
135
- # Raises:
136
- # - ConnectionTimeoutError: no connection can be obtained from the pool
137
- # within the timeout period.
138
- def checkout
139
- # Checkout an available connection
140
- @connection_mutex.synchronize do
141
- loop do
142
- conn = if @checked_out.size < @connections.size
143
- checkout_existing_connection
144
- elsif @connections.size < @size
145
- checkout_new_connection
146
- end
147
- return conn if conn
148
-
149
- @queue.wait(@timeout)
150
-
151
- if(@checked_out.size < @connections.size)
152
- next
153
- else
154
- clear_stale_cached_connections!
155
- if @size == @checked_out.size
156
- raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it."
157
- end
158
- end
159
-
160
- end
161
- end
162
- end
163
-
164
- # Check-in a database connection back into the pool, indicating that you
165
- # no longer need this connection.
166
- #
167
- # +conn+: an AbstractAdapter object, which was obtained by earlier by
168
- # calling +checkout+ on this pool.
169
- def checkin(conn)
170
- @connection_mutex.synchronize do
171
- # conn.send(:_run_checkin_callbacks) do
172
- @checked_out.delete conn
173
- @queue.signal
174
- # end
175
- end
176
- end
177
-
178
- synchronize :clear_reloadable_connections!, :verify_active_connections!,
179
- :connected?, :disconnect!, :with => :@connection_mutex
180
-
181
- private
182
- def new_connection
183
- Cassandra.new(spec[:keyspace], spec[:servers], spec[:thrift])
184
-
185
- #this_spec = spec.dup
186
- #require 'thrift_client/event_machine'
187
- #this_spec[:thrift].merge!(:transport => Thrift::EventMachineTransport,
188
- # :transport_wrapper => nil)
189
- #Cassandra.new(this_spec[:keyspace], this_spec[:servers], this_spec[:thrift])
190
- end
191
-
192
- def current_connection_id #:nodoc:
193
- Thread.current.object_id
194
- end
195
-
196
- def checkout_new_connection
197
- c = new_connection
198
- @connections << c
199
- checkout_and_verify(c)
200
- end
201
-
202
- def checkout_existing_connection
203
- c = (@connections - @checked_out).first
204
- checkout_and_verify(c)
205
- end
206
-
207
- def checkout_and_verify(c)
208
- # c.run_callbacks :checkout do
209
- # c.verify!
210
- @checked_out << c
211
- # end
212
- c
213
- end
214
- end
215
-
216
- class ConnectionHandler
217
- attr_reader :connection_pools
218
-
219
- def initialize(pools = {})
220
- @connection_pools = pools
221
- end
222
-
223
- def establish_connection(name, spec)
224
- @connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec)
225
- end
226
-
227
- # Returns any connections in use by the current thread back to the pool,
228
- # and also returns connections to the pool cached by threads that are no
229
- # longer alive.
230
- def clear_active_connections!
231
- @connection_pools.each_value {|pool| pool.release_connection }
232
- end
233
-
234
- # Clears the cache which maps classes
235
- def clear_reloadable_connections!
236
- @connection_pools.each_value {|pool| pool.clear_reloadable_connections! }
237
- end
238
-
239
- def clear_all_connections!
240
- @connection_pools.each_value {|pool| pool.disconnect! }
241
- end
242
-
243
- # Verify active connections.
244
- def verify_active_connections! #:nodoc:
245
- @connection_pools.each_value {|pool| pool.verify_active_connections! }
246
- end
247
-
248
- # Locate the connection of the nearest super class. This can be an
249
- # active or defined connection: if it is the latter, it will be
250
- # opened and set as the active connection for the class it was defined
251
- # for (not necessarily the current class).
252
- def retrieve_connection(klass) #:nodoc:
253
- pool = retrieve_connection_pool(klass)
254
- (pool && pool.connection) or raise ConnectionNotEstablished
255
- end
256
-
257
- # Returns true if a connection that's accessible to this class has
258
- # already been opened.
259
- def connected?(klass)
260
- conn = retrieve_connection_pool(klass)
261
- conn && conn.connected?
262
- end
263
-
264
- # Remove the connection for this class. This will close the active
265
- # connection and the defined connection (if they exist). The result
266
- # can be used as an argument for establish_connection, for easily
267
- # re-establishing the connection.
268
- def remove_connection(klass)
269
- pool = @connection_pools[klass.name]
270
- return nil unless pool
271
-
272
- @connection_pools.delete_if { |key, value| value == pool }
273
- pool.disconnect!
274
- pool.spec.config
275
- end
276
-
277
- def retrieve_connection_pool(klass)
278
- pool = @connection_pools[klass.name]
279
- return pool if pool
280
- return nil if CassandraObject::Base == klass
281
- retrieve_connection_pool klass.superclass
282
- end
283
- end
284
-
285
- class ConnectionManagement
286
- def initialize(app)
287
- @app = app
288
- end
289
-
290
- def call(env)
291
- @app.call(env)
292
- ensure
293
- # Don't return connection (and perform implicit rollback) if
294
- # this request is a part of integration test
295
- unless env.key?("rack.test")
296
- CassandraObject::Base.clear_active_connections!
297
- end
298
- end
299
- end
300
- end
301
- end
@@ -1,56 +0,0 @@
1
- module CassandraObject
2
- class Base
3
- class ConnectionSpecification #:nodoc:
4
- attr_reader :config, :adapter_method
5
- def initialize (config, adapter_method)
6
- @config, @adapter_method = config, adapter_method
7
- end
8
- end
9
-
10
- ##
11
- # :singleton-method:
12
- # The connection handler
13
- class_attribute :connection_handler, :instance_writer => false
14
- self.connection_handler = ConnectionAdapters::ConnectionHandler.new
15
-
16
- # Returns the connection currently associated with the class. This can
17
- # also be used to "borrow" the connection to do database work that isn't
18
- # easily done without going straight to SQL.
19
- def connection
20
- self.class.connection
21
- end
22
-
23
- def self.establish_connection(spec = nil)
24
- self.connection_handler.establish_connection(self.name, spec)
25
- end
26
-
27
- class << self
28
- # Returns the connection currently associated with the class. This can
29
- # also be used to "borrow" the connection to do database work unrelated
30
- # to any of the specific Active Records.
31
- def connection
32
- retrieve_connection
33
- end
34
-
35
- def connection_pool
36
- connection_handler.retrieve_connection_pool(self)
37
- end
38
-
39
- def retrieve_connection
40
- connection_handler.retrieve_connection(self)
41
- end
42
-
43
- # Returns true if Active Record is connected.
44
- def connected?
45
- connection_handler.connected?(self)
46
- end
47
-
48
- def remove_connection(klass = self)
49
- connection_handler.remove_connection(klass)
50
- end
51
-
52
- delegate :clear_active_connections!, :clear_reloadable_connections!,
53
- :clear_all_connections!,:verify_active_connections!, :to => :connection_handler
54
- end
55
- end
56
- end