From 269047989e934c988aee7f71e58c33a7de0e8e76 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Mon, 27 Jul 2015 11:34:38 +0200 Subject: [PATCH] mongodb: stop relying on global conf object in utils Change-Id: Ib2dd1f73195ece19bfea612a1a46580f9418609f --- aodh/storage/impl_db2.py | 5 ++++- aodh/storage/impl_mongodb.py | 5 ++++- aodh/storage/mongo/utils.py | 29 +++++++++++++++++------------ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/aodh/storage/impl_db2.py b/aodh/storage/impl_db2.py index d22e32bb..307eabd0 100644 --- a/aodh/storage/impl_db2.py +++ b/aodh/storage/impl_db2.py @@ -40,7 +40,10 @@ class Connection(pymongo_base.Connection): # db2 driver from mongodb driver be replaced so that pymongo will not # produce an exception on the scheme. 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 # as backend for test, the following code is necessary to make sure diff --git a/aodh/storage/impl_mongodb.py b/aodh/storage/impl_mongodb.py index 60942d37..71201dbe 100644 --- a/aodh/storage/impl_mongodb.py +++ b/aodh/storage/impl_mongodb.py @@ -46,7 +46,10 @@ class Connection(pymongo_base.Connection): # connection since we instantiate a Pymongo client each time someone # requires a new storage connection. 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 if self.conn.server_info()['versionArray'] < [2, 4]: diff --git a/aodh/storage/mongo/utils.py b/aodh/storage/mongo/utils.py index 643d7864..0d34423b 100644 --- a/aodh/storage/mongo/utils.py +++ b/aodh/storage/mongo/utils.py @@ -65,7 +65,7 @@ class ConnectionPool(object): def __init__(self): 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) del connection_options['database'] del connection_options['username'] @@ -85,7 +85,9 @@ class ConnectionPool(object): client = MongoProxy( pymongo.MongoClient( url, replicaSet=replica_set, - ) + ), + max_retries, + retry_interval, ) except pymongo.errors.ConnectionFailure as e: LOG.warn(_('Unable to connect to the database server: ' @@ -218,7 +220,6 @@ class QueryTransformer(object): return self._handle_simple_op(operator_node, nodes) - MONGO_METHODS = set([typ for typ in dir(pymongo.collection.Collection) if not typ.startswith('_')]) 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): - def __init__(self, conn): + def __init__(self, conn, max_retries, retry_interval): self.conn = conn + self.max_retries = max_retries + self.retry_interval = retry_interval def __getitem__(self, item): """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 can handle the Cursor next function to catch the AutoReconnect # 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): """Wrap MongoDB connection. @@ -264,22 +269,22 @@ class MongoProxy(object): return getattr(self.conn, item) if item in MONGO_METHODS: return _safe_mongo_call( - cfg.CONF.database.max_retries, - cfg.CONF.database.retry_interval, + self.max_retries, + self.retry_interval, )(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): return self.conn(*args, **kwargs) class CursorProxy(pymongo.cursor.Cursor): - def __init__(self, cursor): + def __init__(self, cursor, max_retries, retry_interval): self.cursor = cursor self.next = _safe_mongo_call( - cfg.CONF.database.max_retries, - cfg.CONF.database.retry_interval, - )(self._next) + max_retries, retry_interval)(self._next) def __getitem__(self, item): return self.cursor[item]