right_support 2.0.4 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,7 +19,6 @@
19
19
  # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
20
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
-
23
22
  begin
24
23
  require 'cassandra/0.8'
25
24
 
@@ -125,29 +124,78 @@ module RightSupport::DB
125
124
  class << self
126
125
 
127
126
  @@logger = nil
128
- @@conn = nil
129
-
127
+
128
+ attr_reader :default_keyspace
130
129
  attr_accessor :column_family
131
- attr_writer :keyspace
130
+ attr_accessor :custom_operation_exception
131
+
132
+ @@current_keyspace = nil
133
+
134
+ @@connections = {}
132
135
 
133
136
  def config
134
137
  @@config
135
138
  end
136
-
139
+
137
140
  def config=(value)
138
141
  @@config = value
139
142
  end
140
-
143
+
141
144
  def logger=(l)
142
145
  @@logger = l
143
146
  end
144
-
147
+
145
148
  def logger
146
- @@logger
149
+ @@logger
147
150
  end
148
151
 
152
+ # Return current keyspaces name as Array of String
153
+ #
154
+ # === Return
155
+ # (Array):: keyspaces names
156
+
157
+ def keyspaces
158
+ @@connections.keys
159
+ end
160
+
161
+ # Returns current active keyspace.
162
+ #
163
+ # === Return
164
+ # keyspace(String):: current_keyspace or default_keyspace
165
+
149
166
  def keyspace
150
- @keyspace + "_" + (ENV['RACK_ENV'] || 'development')
167
+ @@current_keyspace || @@default_keyspace
168
+ end
169
+
170
+ # Sets the default keyspace
171
+ #
172
+ # === Parameters
173
+ # keyspace(String):: Set the default keyspace
174
+
175
+ def keyspace=(kyspc)
176
+ @@default_keyspace = (kyspc + "_" + (ENV['RACK_ENV'] || 'development'))
177
+ end
178
+
179
+ # Execute given block in kyspc context
180
+ #
181
+ # === Parameters
182
+ # kyspc(String):: Keyspace context
183
+ # block(Proc):: Code that will be called in keyspace context
184
+
185
+ def with_keyspace(kyspc, &block)
186
+ @@current_keyspace = (kyspc + "_" + (ENV['RACK_ENV'] || 'development'))
187
+ begin
188
+ block.call
189
+ rescue Exception => e
190
+ if !self.custom_operation_exception.nil? && self.custom_operation_exception.kind_of?(Proc)\
191
+ && e.kind_of?(Thrift::Exception)
192
+ custom_operation_exception.call
193
+ else
194
+ raise e
195
+ end
196
+ ensure
197
+ @@current_keyspace = nil
198
+ end
151
199
  end
152
200
 
153
201
  # Client connected to Cassandra server
@@ -156,8 +204,8 @@ module RightSupport::DB
156
204
  #
157
205
  # === Return
158
206
  # (Cassandra):: Client connected to server
159
- def conn
160
- return @@conn if @@conn
207
+ def conn()
208
+ @@connections ||= {}
161
209
 
162
210
  # TODO remove hidden dependency on ENV['RACK_ENV'] (maybe require config= to accept a sub hash?)
163
211
  config = @@config[ENV["RACK_ENV"]]
@@ -167,9 +215,9 @@ module RightSupport::DB
167
215
  thrift_client_options.merge!({:protocol => Thrift::BinaryProtocolAccelerated})\
168
216
  if defined? Thrift::BinaryProtocolAccelerated
169
217
 
170
- @@conn = Cassandra.new(keyspace, config["server"], thrift_client_options)
171
- @@conn.disable_node_auto_discovery!
172
- @@conn
218
+ @@connections[self.keyspace] ||= Cassandra.new(self.keyspace, config["server"], thrift_client_options)
219
+ @@connections[self.keyspace].disable_node_auto_discovery!
220
+ @@connections[self.keyspace]
173
221
  end
174
222
 
175
223
  # Get row(s) for specified key(s)
@@ -185,7 +233,7 @@ module RightSupport::DB
185
233
  def all(k, opt = {})
186
234
  real_get(k, opt)
187
235
  end
188
-
236
+
189
237
  # Get row for specified primary key and convert into object of given class
190
238
  # Unless :count is specified, a maximum of 100 columns are retrieved
191
239
  #
@@ -240,6 +288,37 @@ module RightSupport::DB
240
288
  end
241
289
  end
242
290
 
291
+ # Get all rows for specified secondary key
292
+ #
293
+ # === Parameters
294
+ # index(String):: Name of secondary index
295
+ # key(String):: Index value that each selected row is required to match
296
+ # columns(Array|nil):: Names of columns to be retrieved, defaults to all
297
+ # opt(Hash):: Request options with only :consistency used
298
+ #
299
+ # === Return
300
+ # (OrderedHash):: Rows retrieved with each key, value is columns
301
+ def get_all_indexed_slices(index, key, columns = nil, opt = {})
302
+ rows = Cassandra::OrderedHash.new
303
+ start = ""
304
+ count = opt.delete(:count) || DEFAULT_COUNT
305
+ expr = do_op(:create_idx_expr, index, key, "EQ")
306
+ opt = opt[:consistency] ? {:consistency => opt[:consistency]} : {}
307
+ while true
308
+ clause = do_op(:create_idx_clause, [expr], start, count)
309
+ chunk = self.conn.get_indexed_slices(column_family, clause, columns, opt)
310
+ rows.merge!(chunk)
311
+ if chunk.size == count
312
+ # Assume there are more chunks, use last key as start of next get
313
+ start = chunk.keys.last
314
+ else
315
+ # This must be the last chunk
316
+ break
317
+ end
318
+ end
319
+ rows
320
+ end
321
+
243
322
  # Get all rows for specified secondary key
244
323
  #
245
324
  # === Parameters
@@ -382,16 +461,19 @@ module RightSupport::DB
382
461
  config = @@config[ENV["RACK_ENV"]]
383
462
  raise MissingConfiguration, "CassandraModel config is missing a '#{ENV['RACK_ENV']}' section" unless config
384
463
 
464
+ return false if keyspace.nil?
465
+
385
466
  thrift_client_options = {:timeout => RightSupport::DB::CassandraModel::DEFAULT_TIMEOUT}
386
467
  thrift_client_options.merge!({:protocol => Thrift::BinaryProtocolAccelerated})\
387
468
  if defined? Thrift::BinaryProtocolAccelerated
388
469
 
389
- @@conn = Cassandra.new(keyspace, config["server"], thrift_client_options)
390
- @@conn.disable_node_auto_discovery!
470
+ connection = Cassandra.new(keyspace, config["server"], thrift_client_options)
471
+ connection.disable_node_auto_discovery!
472
+ @@connections[keyspace] = connection
391
473
  true
392
474
  end
393
475
 
394
- # Cassandra ring for given keyspace
476
+ # Cassandra ring
395
477
  #
396
478
  # === Return
397
479
  # (Array):: Members of ring
@@ -422,7 +504,7 @@ module RightSupport::DB
422
504
  self.class.insert(key, attributes)
423
505
  true
424
506
  end
425
-
507
+
426
508
  # Load object from Cassandra without modifying this object
427
509
  #
428
510
  # === Return
@@ -146,6 +146,10 @@ module RightSupport::Net
146
146
  # * :user and :password - for basic auth, will be replaced by a user/password available in the url
147
147
  # * :raw_response - return a low-level RawResponse instead of a Response
148
148
  # * :verify_ssl - enable ssl verification, possible values are constants from OpenSSL::SSL
149
+ # * OpenSSL::SSL::VERIFY_NONE (default)
150
+ # * OpenSSL::SSL::VERIFY_CLIENT_ONCE
151
+ # * OpenSSL::SSL::VERIFY_PEER
152
+ # * OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
149
153
  # * :timeout and :open_timeout - specify overall request timeout + socket connect timeout
150
154
  # * :ssl_client_cert, :ssl_client_key, :ssl_ca_file
151
155
  #
@@ -103,10 +103,11 @@ module RightSupport::Rack
103
103
  query_info,
104
104
  remote_addr,
105
105
  sess,
106
+ 'Shard_id:' + (env['HTTP_X_SHARD'] || 'not set').to_s + ';',
106
107
  env["rack.request_uuid"] || ''
107
108
  ]
108
109
 
109
- logger.info %Q{Processing %s "%s%s" (for %s) %s Request ID: %s} % params
110
+ logger.info %Q{Processing %s "%s%s" (for %s) %s %s Request ID: %s} % params
110
111
  end
111
112
 
112
113
  # Log end of request
@@ -127,7 +128,7 @@ module RightSupport::Rack
127
128
  else
128
129
  '-'
129
130
  end
130
-
131
+
131
132
  params = [
132
133
  duration,
133
134
  status,
@@ -7,8 +7,8 @@ spec = Gem::Specification.new do |s|
7
7
  s.required_ruby_version = Gem::Requirement.new(">= 1.8.7")
8
8
 
9
9
  s.name = 'right_support'
10
- s.version = '2.0.4'
11
- s.date = '2012-05-01'
10
+ s.version = '2.1.0'
11
+ s.date = '2012-06-16'
12
12
 
13
13
  s.authors = ['Tony Spataro', 'Sergey Sergyenko', 'Ryan Williamson', 'Lee Kirchhoff', 'Sergey Enin']
14
14
  s.email = 'support@rightscale.com'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: right_support
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
5
- prerelease: false
4
+ hash: 11
5
+ prerelease:
6
6
  segments:
7
7
  - 2
8
+ - 1
8
9
  - 0
9
- - 4
10
- version: 2.0.4
10
+ version: 2.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tony Spataro
@@ -19,8 +19,7 @@ autorequire:
19
19
  bindir: bin
20
20
  cert_chain: []
21
21
 
22
- date: 2012-05-01 00:00:00 -07:00
23
- default_executable:
22
+ date: 2012-06-16 00:00:00 Z
24
23
  dependencies: []
25
24
 
26
25
  description: A toolkit of useful, reusable foundation code created by RightScale.
@@ -80,7 +79,6 @@ files:
80
79
  - lib/right_support/validation/openssl.rb
81
80
  - lib/right_support/validation/ssh.rb
82
81
  - right_support.gemspec
83
- has_rdoc: true
84
82
  homepage: https://github.com/rightscale/right_support
85
83
  licenses: []
86
84
 
@@ -112,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
110
  requirements: []
113
111
 
114
112
  rubyforge_project:
115
- rubygems_version: 1.3.7
113
+ rubygems_version: 1.8.24
116
114
  signing_key:
117
115
  specification_version: 3
118
116
  summary: Reusable foundation code.