storage: only retry connection, not driver finding
The current code will retry over and over to reload the driver from stevedore if it fails to connect. That's not what we meant, we really just want to retry the connection part. Change-Id: I681029196f834999895a4d4186417c1da85a32ba
This commit is contained in:
parent
28dd79c383
commit
c22c89b104
@ -77,21 +77,21 @@ class StorageBadAggregate(Exception):
|
||||
|
||||
def get_connection_from_config(conf):
|
||||
retries = conf.database.max_retries
|
||||
url = conf.database.connection
|
||||
connection_scheme = urlparse.urlparse(url).scheme
|
||||
# SQLAlchemy connections specify may specify a 'dialect' or
|
||||
# 'dialect+driver'. Handle the case where driver is specified.
|
||||
engine_name = connection_scheme.split('+')[0]
|
||||
# NOTE: translation not applied bug #1446983
|
||||
LOG.debug('looking for %(name)r driver in %(namespace)r',
|
||||
{'name': engine_name, 'namespace': _NAMESPACE})
|
||||
mgr = driver.DriverManager(_NAMESPACE, engine_name)
|
||||
|
||||
# Convert retry_interval secs to msecs for retry decorator
|
||||
@retrying.retry(wait_fixed=conf.database.retry_interval * 1000,
|
||||
stop_max_attempt_number=retries if retries >= 0 else None)
|
||||
def _get_connection():
|
||||
"""Return an open connection to the database."""
|
||||
url = conf.database.connection
|
||||
connection_scheme = urlparse.urlparse(url).scheme
|
||||
# SQLAlchemy connections specify may specify a 'dialect' or
|
||||
# 'dialect+driver'. Handle the case where driver is specified.
|
||||
engine_name = connection_scheme.split('+')[0]
|
||||
# NOTE: translation not applied bug #1446983
|
||||
LOG.debug('looking for %(name)r driver in %(namespace)r',
|
||||
{'name': engine_name, 'namespace': _NAMESPACE})
|
||||
mgr = driver.DriverManager(_NAMESPACE, engine_name)
|
||||
return mgr.driver(conf, url)
|
||||
|
||||
return _get_connection()
|
||||
|
@ -1,5 +1,6 @@
|
||||
#
|
||||
# Copyright 2012 New Dream Network, LLC (DreamHost)
|
||||
# Copyright 2015 Red Hat, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
@ -56,12 +57,20 @@ class ConnectionRetryTest(base.BaseTestCase):
|
||||
|
||||
def test_retries(self):
|
||||
with mock.patch.object(retrying.time, 'sleep') as retry_sleep:
|
||||
try:
|
||||
self.CONF.set_override("connection", "no-such-engine://",
|
||||
group="database")
|
||||
storage.get_connection_from_config(self.CONF)
|
||||
except RuntimeError as err:
|
||||
self.assertIn('no-such-engine', six.text_type(err))
|
||||
with mock.patch.object(
|
||||
storage.impl_log.Connection, '__init__') as log_init:
|
||||
|
||||
class ConnectionError(Exception):
|
||||
pass
|
||||
|
||||
def x(a, b):
|
||||
raise ConnectionError
|
||||
|
||||
log_init.side_effect = x
|
||||
self.CONF.set_override("connection", "log://", "database")
|
||||
self.assertRaises(ConnectionError,
|
||||
storage.get_connection_from_config,
|
||||
self.CONF)
|
||||
self.assertEqual(9, retry_sleep.call_count)
|
||||
retry_sleep.assert_called_with(10.0)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user