mongodb: stop relying on global conf object in utils

Change-Id: Ib2dd1f73195ece19bfea612a1a46580f9418609f
This commit is contained in:
Julien Danjou 2015-07-27 11:34:38 +02:00
parent bed80141c1
commit 269047989e
3 changed files with 25 additions and 14 deletions

View File

@ -40,7 +40,10 @@ class Connection(pymongo_base.Connection):
# db2 driver from mongodb driver be replaced so that pymongo will not # db2 driver from mongodb driver be replaced so that pymongo will not
# produce an exception on the scheme. # produce an exception on the scheme.
url = url.replace('db2:', 'mongodb:', 1) url = url.replace('db2:', 'mongodb:', 1)
self.conn = self.CONNECTION_POOL.connect(url) self.conn = self.CONNECTION_POOL.connect(
url,
conf.database.max_retries,
conf.database.retry_interval)
# Require MongoDB 2.2 to use aggregate(), since we are using mongodb # Require MongoDB 2.2 to use aggregate(), since we are using mongodb
# as backend for test, the following code is necessary to make sure # as backend for test, the following code is necessary to make sure

View File

@ -46,7 +46,10 @@ class Connection(pymongo_base.Connection):
# connection since we instantiate a Pymongo client each time someone # connection since we instantiate a Pymongo client each time someone
# requires a new storage connection. # requires a new storage connection.
self.conn = self.CONNECTION_POOL.connect( self.conn = self.CONNECTION_POOL.connect(
url, conf.database.mongodb_replica_set) url,
conf.database.max_retries,
conf.database.retry_interval,
conf.database.mongodb_replica_set)
# Require MongoDB 2.4 to use $setOnInsert # Require MongoDB 2.4 to use $setOnInsert
if self.conn.server_info()['versionArray'] < [2, 4]: if self.conn.server_info()['versionArray'] < [2, 4]:

View File

@ -65,7 +65,7 @@ class ConnectionPool(object):
def __init__(self): def __init__(self):
self._pool = {} self._pool = {}
def connect(self, url, replica_set=None): def connect(self, url, max_retries, retry_interval, replica_set=None):
connection_options = pymongo.uri_parser.parse_uri(url) connection_options = pymongo.uri_parser.parse_uri(url)
del connection_options['database'] del connection_options['database']
del connection_options['username'] del connection_options['username']
@ -85,7 +85,9 @@ class ConnectionPool(object):
client = MongoProxy( client = MongoProxy(
pymongo.MongoClient( pymongo.MongoClient(
url, replicaSet=replica_set, url, replicaSet=replica_set,
) ),
max_retries,
retry_interval,
) )
except pymongo.errors.ConnectionFailure as e: except pymongo.errors.ConnectionFailure as e:
LOG.warn(_('Unable to connect to the database server: ' LOG.warn(_('Unable to connect to the database server: '
@ -218,7 +220,6 @@ class QueryTransformer(object):
return self._handle_simple_op(operator_node, nodes) return self._handle_simple_op(operator_node, nodes)
MONGO_METHODS = set([typ for typ in dir(pymongo.collection.Collection) MONGO_METHODS = set([typ for typ in dir(pymongo.collection.Collection)
if not typ.startswith('_')]) if not typ.startswith('_')])
MONGO_METHODS.update(set([typ for typ in dir(pymongo.MongoClient) MONGO_METHODS.update(set([typ for typ in dir(pymongo.MongoClient)
@ -237,8 +238,10 @@ def _safe_mongo_call(max_retries, retry_interval):
class MongoProxy(object): class MongoProxy(object):
def __init__(self, conn): def __init__(self, conn, max_retries, retry_interval):
self.conn = conn self.conn = conn
self.max_retries = max_retries
self.retry_interval = retry_interval
def __getitem__(self, item): def __getitem__(self, item):
"""Create and return proxy around the method in the connection. """Create and return proxy around the method in the connection.
@ -251,7 +254,9 @@ class MongoProxy(object):
# We need this modifying method to return a CursorProxy object so that # We need this modifying method to return a CursorProxy object so that
# we can handle the Cursor next function to catch the AutoReconnect # we can handle the Cursor next function to catch the AutoReconnect
# exception. # exception.
return CursorProxy(self.conn.find(*args, **kwargs)) return CursorProxy(self.conn.find(*args, **kwargs),
self.max_retries,
self.retry_interval)
def __getattr__(self, item): def __getattr__(self, item):
"""Wrap MongoDB connection. """Wrap MongoDB connection.
@ -264,22 +269,22 @@ class MongoProxy(object):
return getattr(self.conn, item) return getattr(self.conn, item)
if item in MONGO_METHODS: if item in MONGO_METHODS:
return _safe_mongo_call( return _safe_mongo_call(
cfg.CONF.database.max_retries, self.max_retries,
cfg.CONF.database.retry_interval, self.retry_interval,
)(getattr(self.conn, item)) )(getattr(self.conn, item))
return MongoProxy(getattr(self.conn, item)) return MongoProxy(getattr(self.conn, item),
self.max_retries,
self.retry_interval)
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
return self.conn(*args, **kwargs) return self.conn(*args, **kwargs)
class CursorProxy(pymongo.cursor.Cursor): class CursorProxy(pymongo.cursor.Cursor):
def __init__(self, cursor): def __init__(self, cursor, max_retries, retry_interval):
self.cursor = cursor self.cursor = cursor
self.next = _safe_mongo_call( self.next = _safe_mongo_call(
cfg.CONF.database.max_retries, max_retries, retry_interval)(self._next)
cfg.CONF.database.retry_interval,
)(self._next)
def __getitem__(self, item): def __getitem__(self, item):
return self.cursor[item] return self.cursor[item]