Class: OpenShift::MongoDataStore
- Defined in:
- lib/openshift/mongo_data_store.rb
Constant Summary collapse
- MAX_CON_RETRIES =
60
- CON_RETRY_WAIT_TM =
in secs
0.5
Instance Attribute Summary collapse
-
#collections ⇒ Object
readonly
Returns the value of attribute collections.
-
#db(connection_opts = nil) ⇒ Object
readonly
Returns the value of attribute db.
-
#host_port ⇒ Object
readonly
Returns the value of attribute host_port.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#replica_set ⇒ Object
readonly
Returns the value of attribute replica_set.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
- .instance ⇒ Object
-
.rescue_con_failure(max_retries = MAX_CON_RETRIES, retry_wait_tm = CON_RETRY_WAIT_TM) ⇒ Object
Ensure retry upon connection failure.
Instance Method Summary collapse
- #activate_district_node(uuid, server_identity) ⇒ Object
- #add_district_node(uuid, server_identity) ⇒ Object
- #add_district_uids(uuid, uids) ⇒ Object
- #create(obj_type, user_id, id, obj_attrs) ⇒ Object
- #deactivate_district_node(uuid, server_identity) ⇒ Object
- #delete(obj_type, user_id, id = nil) ⇒ Object
- #delete_district(uuid) ⇒ Object
- #delete_usage_record_by_gear_uuid(user_id, gear_uuid, usage_type) ⇒ Object
- #delete_usage_records_by_uuids(user_id, uuids) ⇒ Object
- #find(obj_type, user_id, id) ⇒ Object
- #find_all(obj_type, user_id = nil, opts = nil, &block) ⇒ Object
- #find_all_districts ⇒ Object
- #find_all_logins(opts) ⇒ Object
- #find_and_modify(collection, *args) ⇒ Object
- #find_available_district(node_profile = nil) ⇒ Object
- #find_by_gear_uuid(gear_uuid) ⇒ Object
- #find_by_uuid(obj_type_of_uuid, uuid) ⇒ Object
- #find_district(uuid, connection_opts = nil) ⇒ Object
- #find_district_by_name(name, connection_opts = nil) ⇒ Object
- #find_district_with_node(server_identity) ⇒ Object
- #find_one(collection, *args) ⇒ Object
- #find_subaccounts_by_parent_login(parent_id) ⇒ Object
- #inc_district_externally_reserved_uids_size(uuid) ⇒ Object
-
#initialize(access_info = nil) ⇒ MongoDataStore
constructor
A new instance of MongoDataStore.
- #insert(collection, *args) ⇒ Object
- #remove(collection, *args) ⇒ Object
- #remove_district_node(uuid, server_identity) ⇒ Object
- #remove_district_uids(uuid, uids) ⇒ Object
- #reserve_district_given_uid(uuid, uid) ⇒ Object
- #reserve_district_uid(uuid) ⇒ Object
- #save(obj_type, user_id, id, obj_attrs) ⇒ Object
- #save_district(uuid, district_attrs) ⇒ Object
- #unreserve_district_uid(uuid, uid) ⇒ Object
- #update(collection, *args) ⇒ Object
- #user_collection(connection_opts = nil) ⇒ Object
Methods inherited from DataStore
Constructor Details
#initialize(access_info = nil) ⇒ MongoDataStore
Returns a new instance of MongoDataStore.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/openshift/mongo_data_store.rb', line 12 def initialize(access_info = nil) if access_info != nil # no-op elsif defined? Rails access_info = Rails.application.config.datastore else raise Exception.new("Mongo DataStore service is not initialized") end @replica_set = access_info[:replica_set] @host_port = access_info[:host_port] @user = access_info[:user] @password = access_info[:password] @db = access_info[:db] @collections = access_info[:collections] end |
Instance Attribute Details
#collections ⇒ Object (readonly)
Returns the value of attribute collections.
10 11 12 |
# File 'lib/openshift/mongo_data_store.rb', line 10 def collections @collections end |
#db(connection_opts = nil) ⇒ Object (readonly)
Returns the value of attribute db.
10 11 12 |
# File 'lib/openshift/mongo_data_store.rb', line 10 def db @db end |
#host_port ⇒ Object (readonly)
Returns the value of attribute host_port.
10 11 12 |
# File 'lib/openshift/mongo_data_store.rb', line 10 def host_port @host_port end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
10 11 12 |
# File 'lib/openshift/mongo_data_store.rb', line 10 def password @password end |
#replica_set ⇒ Object (readonly)
Returns the value of attribute replica_set.
10 11 12 |
# File 'lib/openshift/mongo_data_store.rb', line 10 def replica_set @replica_set end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
10 11 12 |
# File 'lib/openshift/mongo_data_store.rb', line 10 def user @user end |
Class Method Details
.instance ⇒ Object
28 29 30 |
# File 'lib/openshift/mongo_data_store.rb', line 28 def self.instance OpenShift::MongoDataStore.new end |
.rescue_con_failure(max_retries = MAX_CON_RETRIES, retry_wait_tm = CON_RETRY_WAIT_TM) ⇒ Object
Ensure retry upon connection failure
224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/openshift/mongo_data_store.rb', line 224 def self.rescue_con_failure(max_retries=MAX_CON_RETRIES, retry_wait_tm=CON_RETRY_WAIT_TM) retries = 0 begin yield rescue Mongo::ConnectionFailure => ex retries += 1 raise ex if retries > max_retries sleep(retry_wait_tm) retry end end |
Instance Method Details
#activate_district_node(uuid, server_identity) ⇒ Object
318 319 320 321 |
# File 'lib/openshift/mongo_data_store.rb', line 318 def activate_district_node(uuid, server_identity) Rails.logger.debug "activate_district_node(#{uuid},#{server_identity})\n\n" update( district_collection, {"_id" => uuid, "server_identities" => {"$elemMatch" => {"name" => server_identity, "active" => false}}}, {"$set" => { "server_identities.$.active" => true}, "$inc" => { "active_server_identities_size" => 1 }} ) end |
#add_district_node(uuid, server_identity) ⇒ Object
300 301 302 303 |
# File 'lib/openshift/mongo_data_store.rb', line 300 def add_district_node(uuid, server_identity) Rails.logger.debug "add_district_node(#{uuid},#{server_identity})\n\n" update( district_collection, {"_id" => uuid, "server_identities.name" => { "$ne" => server_identity }}, {"$push" => { "server_identities" => {"name" => server_identity, "active" => true}}, "$inc" => { "active_server_identities_size" => 1 }} ) end |
#add_district_uids(uuid, uids) ⇒ Object
323 324 325 326 |
# File 'lib/openshift/mongo_data_store.rb', line 323 def add_district_uids(uuid, uids) Rails.logger.debug "add_district_capacity(#{uuid},#{uids})\n\n" update( district_collection, {"_id" => uuid}, {"$pushAll" => { "available_uids" => uids }, "$inc" => { "available_capacity" => uids.length, "max_uid" => uids.length, "max_capacity" => uids.length }} ) end |
#create(obj_type, user_id, id, obj_attrs) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/openshift/mongo_data_store.rb', line 134 def create(obj_type, user_id, id, obj_attrs) Rails.logger.debug "MongoDataStore.create(#{obj_type}, #{user_id}, #{id}, #hidden)\n\n" case obj_type when "CloudUser" add_user(user_id, obj_attrs) when "Application" add_app(user_id, id, obj_attrs) when "Domain" add_domain(user_id, id, obj_attrs) when "ApplicationTemplate" save_application_template(id, obj_attrs) end end |
#deactivate_district_node(uuid, server_identity) ⇒ Object
313 314 315 316 |
# File 'lib/openshift/mongo_data_store.rb', line 313 def deactivate_district_node(uuid, server_identity) Rails.logger.debug "deactivate_district_node(#{uuid},#{server_identity})\n\n" update( district_collection, {"_id" => uuid, "server_identities" => {"$elemMatch" => {"name" => server_identity, "active" => true}}}, {"$set" => { "server_identities.$.active" => false}, "$inc" => { "active_server_identities_size" => -1 }} ) end |
#delete(obj_type, user_id, id = nil) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/openshift/mongo_data_store.rb', line 148 def delete(obj_type, user_id, id=nil) Rails.logger.debug "MongoDataStore.delete(#{obj_type}, #{user_id}, #{id})\n\n" case obj_type when "CloudUser" delete_user(user_id) when "Application" delete_app(user_id, id) when "Domain" delete_domain(user_id, id) when "ApplicationTemplate" delete_application_template(id) when "UsageRecord" delete_usage_record(user_id, id) end end |
#delete_district(uuid) ⇒ Object
272 273 274 275 |
# File 'lib/openshift/mongo_data_store.rb', line 272 def delete_district(uuid) Rails.logger.debug "delete_district(#{uuid})\n\n" remove( district_collection, { "_id" => uuid, "active_server_identities_size" => 0 } ) end |
#delete_usage_record_by_gear_uuid(user_id, gear_uuid, usage_type) ⇒ Object
164 165 166 167 168 |
# File 'lib/openshift/mongo_data_store.rb', line 164 def delete_usage_record_by_gear_uuid(user_id, gear_uuid, usage_type) Rails.logger.debug "MongoDataStore.delete_usage_record_by_gear_uuid(#{user_id}, #{gear_uuid}, #{usage_type})\n\n" update( user_collection, { "_id" => user_id }, { "$pull" => { "usage_records" => {"gear_uuid" => gear_uuid, "usage_type" => usage_type}}} ) end |
#delete_usage_records_by_uuids(user_id, uuids) ⇒ Object
170 171 172 173 174 |
# File 'lib/openshift/mongo_data_store.rb', line 170 def delete_usage_records_by_uuids(user_id, uuids) Rails.logger.debug "MongoDataStore.delete_usage_record_by_gear_uuid(#{user_id}, #{uuids})\n\n" update( user_collection, { "_id" => user_id }, { "$pull" => { "usage_records" => {"uuid" => {"$in" => uuids}} }} ) end |
#find(obj_type, user_id, id) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/openshift/mongo_data_store.rb', line 32 def find(obj_type, user_id, id) Rails.logger.debug "MongoDataStore.find(#{obj_type}, #{user_id}, #{id})\n\n" case obj_type when "CloudUser" get_user(user_id) when "Application" get_app(user_id, id) when "Domain" get_domain(user_id, id) when "ApplicationTemplate" find_application_template(id) end end |
#find_all(obj_type, user_id = nil, opts = nil, &block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/openshift/mongo_data_store.rb', line 46 def find_all(obj_type, user_id=nil, opts=nil, &block) Rails.logger.debug "MongoDataStore.find_all(#{obj_type}, #{user_id}, #{opts})\n\n" case obj_type when "CloudUser" get_users(opts, &block) when "Application" get_apps(user_id, &block) when "Domain" get_domains(user_id, &block) when "ApplicationTemplate" if opts.nil? || opts.empty? find_all_application_templates(&block) else find_application_template_by_tag(opts[:tag], &block) end end end |
#find_all_districts ⇒ Object
248 249 250 251 252 253 254 |
# File 'lib/openshift/mongo_data_store.rb', line 248 def find_all_districts() Rails.logger.debug "find_all_districts()\n\n" MongoDataStore.rescue_con_failure do mcursor = district_collection.find() cursor_to_district_hash(mcursor) end end |
#find_all_logins(opts) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/openshift/mongo_data_store.rb', line 64 def find_all_logins(opts) Rails.logger.debug "MongoDataStore.find_all_logins()\n\n" query = {} if opts if opts[:with_gears] query["apps.group_instances.gears.0"] = {"$exists" => true} end if opts[:with_usage] query["usage_records.0"] = {"$exists" => true} end if opts[:with_plan] query["$or"] = [{"pending_plan_id" => {"$ne" => nil}}, {"plan_id" => {"$ne" => nil}}] end end mcursor = user_collection.find(query, {:fields => []}) ret = [] mcursor.each do |hash| ret.push(hash['_id']) end ret end |
#find_and_modify(collection, *args) ⇒ Object
199 200 201 202 203 |
# File 'lib/openshift/mongo_data_store.rb', line 199 def find_and_modify(collection, *args) MongoDataStore.rescue_con_failure do collection.find_and_modify(*args) end end |
#find_available_district(node_profile = nil) ⇒ Object
338 339 340 341 342 343 344 345 346 347 |
# File 'lib/openshift/mongo_data_store.rb', line 338 def find_available_district(node_profile=nil) node_profile = node_profile ? node_profile : "small" MongoDataStore.rescue_con_failure do hash = district_collection.find( { "available_capacity" => { "$gt" => 0 }, "active_server_identities_size" => { "$gt" => 0 }, "node_profile" => node_profile}).sort(["available_capacity", "descending"]).limit(1).next hash_to_district_ret(hash) end end |
#find_by_gear_uuid(gear_uuid) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/openshift/mongo_data_store.rb', line 86 def find_by_gear_uuid(gear_uuid) Rails.logger.debug "MongoDataStore.find_by_gear_uuid(#{gear_uuid})\n\n" hash = find_one( user_collection, { "apps.group_instances.gears.uuid" => gear_uuid } ) return nil unless hash user_hash_to_ret(hash) end |
#find_by_uuid(obj_type_of_uuid, uuid) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/openshift/mongo_data_store.rb', line 93 def find_by_uuid(obj_type_of_uuid, uuid) Rails.logger.debug "MongoDataStore.find_by_uuid(#{obj_type_of_uuid}, #{uuid})\n\n" case obj_type_of_uuid when "CloudUser" get_user_by_uuid(uuid) when "Application" get_user_by_app_uuid(uuid) when "Domain" get_user_by_domain_uuid(uuid) when "ApplicationTemplate" find_application_template(uuid) end end |
#find_district(uuid, connection_opts = nil) ⇒ Object
236 237 238 239 240 |
# File 'lib/openshift/mongo_data_store.rb', line 236 def find_district(uuid, connection_opts=nil) Rails.logger.debug "MongoDataStore.find_district(#{uuid})\n\n" hash = find_one( district_collection(connection_opts), "_id" => uuid ) hash_to_district_ret(hash) end |
#find_district_by_name(name, connection_opts = nil) ⇒ Object
242 243 244 245 246 |
# File 'lib/openshift/mongo_data_store.rb', line 242 def find_district_by_name(name, connection_opts=nil) Rails.logger.debug "MongoDataStore.find_district_by_name(#{name})\n\n" hash = find_one( district_collection(connection_opts), "name" => name ) hash_to_district_ret(hash) end |
#find_district_with_node(server_identity) ⇒ Object
256 257 258 259 260 |
# File 'lib/openshift/mongo_data_store.rb', line 256 def find_district_with_node(server_identity) Rails.logger.debug "find_district_with_node(#{server_identity})\n\n" hash = find_one( district_collection, {"server_identities.name" => server_identity } ) hash_to_district_ret(hash) end |
#find_one(collection, *args) ⇒ Object
193 194 195 196 197 |
# File 'lib/openshift/mongo_data_store.rb', line 193 def find_one(collection, *args) MongoDataStore.rescue_con_failure do collection.find_one(*args) end end |
#find_subaccounts_by_parent_login(parent_id) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/openshift/mongo_data_store.rb', line 107 def find_subaccounts_by_parent_login(parent_id) Rails.logger.debug "MongoDataStore.find_subaccounts_by_parent_login(#{parent_id})\n\n" cur = MongoDataStore.rescue_con_failure { user_collection.find({ "parent_user_login" => parent_id }) } return [] unless cur hash_list = [] cur.each do |hash| hash.delete("_id") hash_list << hash end hash_list end |
#inc_district_externally_reserved_uids_size(uuid) ⇒ Object
333 334 335 336 |
# File 'lib/openshift/mongo_data_store.rb', line 333 def inc_district_externally_reserved_uids_size(uuid) Rails.logger.debug "inc_district_externally_reserved_uids_size(#{uuid})\n\n" update( district_collection, {"_id" => uuid}, {"$inc" => { "externally_reserved_uids_size" => 1 }} ) end |
#insert(collection, *args) ⇒ Object
205 206 207 208 209 |
# File 'lib/openshift/mongo_data_store.rb', line 205 def insert(collection, *args) MongoDataStore.rescue_con_failure do collection.insert(*args) end end |
#remove(collection, *args) ⇒ Object
217 218 219 220 221 |
# File 'lib/openshift/mongo_data_store.rb', line 217 def remove(collection, *args) MongoDataStore.rescue_con_failure do collection.remove(*args) end end |
#remove_district_node(uuid, server_identity) ⇒ Object
305 306 307 308 309 310 311 |
# File 'lib/openshift/mongo_data_store.rb', line 305 def remove_district_node(uuid, server_identity) Rails.logger.debug "remove_district_node(#{uuid},#{server_identity})\n\n" hash = find_and_modify( district_collection, { :query => { "_id" => uuid, "server_identities" => {"$elemMatch" => {"name" => server_identity, "active" => false}}}, :update => { "$pull" => { "server_identities" => {"name" => server_identity }}} }) return hash != nil end |
#remove_district_uids(uuid, uids) ⇒ Object
328 329 330 331 |
# File 'lib/openshift/mongo_data_store.rb', line 328 def remove_district_uids(uuid, uids) Rails.logger.debug "remove_district_capacity(#{uuid},#{uids})\n\n" update( district_collection, {"_id" => uuid, "available_uids" => uids[0]}, {"$pullAll" => { "available_uids" => uids }, "$inc" => { "available_capacity" => -uids.length, "max_uid" => -uids.length, "max_capacity" => -uids.length }} ) end |
#reserve_district_given_uid(uuid, uid) ⇒ Object
277 278 279 280 281 282 283 284 |
# File 'lib/openshift/mongo_data_store.rb', line 277 def reserve_district_given_uid(uuid, uid) Rails.logger.debug "reserve_district_given_uid(#{uuid}, #{uid})\n\n" hash = find_and_modify( district_collection, { :query => {"_id" => uuid, "available_capacity" => {"$gt" => 0}}, :update => {"$pull" => { "available_uids" => uid }, "$inc" => { "available_capacity" => -1 }}, :new => false }) return hash ? (hash["available_uids"].include? uid) : false end |
#reserve_district_uid(uuid) ⇒ Object
286 287 288 289 290 291 292 293 |
# File 'lib/openshift/mongo_data_store.rb', line 286 def reserve_district_uid(uuid) Rails.logger.debug "reserve_district_uid(#{uuid})\n\n" hash = find_and_modify( district_collection, { :query => {"_id" => uuid, "available_capacity" => {"$gt" => 0}}, :update => {"$pop" => { "available_uids" => -1}, "$inc" => { "available_capacity" => -1 }}, :new => false }) return hash ? hash["available_uids"][0] : nil end |
#save(obj_type, user_id, id, obj_attrs) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/openshift/mongo_data_store.rb', line 120 def save(obj_type, user_id, id, obj_attrs) Rails.logger.debug "MongoDataStore.save(#{obj_type}, #{user_id}, #{id}, #hidden)\n\n" case obj_type when "CloudUser" put_user(user_id, obj_attrs) when "Application" put_app(user_id, id, obj_attrs) when "Domain" put_domain(user_id, id, obj_attrs) when "UsageRecord" put_usage_record(user_id, id, obj_attrs) end end |
#save_district(uuid, district_attrs) ⇒ Object
262 263 264 265 266 267 268 269 270 |
# File 'lib/openshift/mongo_data_store.rb', line 262 def save_district(uuid, district_attrs) Rails.logger.debug "save_district(#{uuid}, #{district_attrs.pretty_inspect})\n\n" district_attrs["_id"] = uuid orig_server_identities = district_attrs["server_identities"] district_attrs_to_internal(district_attrs) update( district_collection, { "_id" => uuid }, district_attrs, { :upsert => true } ) district_attrs.delete("_id") district_attrs["server_identities"] = orig_server_identities end |
#unreserve_district_uid(uuid, uid) ⇒ Object
295 296 297 298 |
# File 'lib/openshift/mongo_data_store.rb', line 295 def unreserve_district_uid(uuid, uid) Rails.logger.debug "unreserve_district_uid(#{uuid})\n\n" update( district_collection, {"_id" => uuid, "available_uids" => {"$ne" => uid}}, {"$push" => { "available_uids" => uid}, "$inc" => { "available_capacity" => 1 }} ) end |
#update(collection, *args) ⇒ Object
211 212 213 214 215 |
# File 'lib/openshift/mongo_data_store.rb', line 211 def update(collection, *args) MongoDataStore.rescue_con_failure do collection.update(*args) end end |
#user_collection(connection_opts = nil) ⇒ Object
189 190 191 |
# File 'lib/openshift/mongo_data_store.rb', line 189 def user_collection(connection_opts=nil) db(connection_opts).collection(@collections[:user]) end |