Merge "Reuse Redfish sessions follow up"

This commit is contained in:
Zuul
2018-11-20 12:45:15 +00:00
committed by Gerrit Code Review
3 changed files with 47 additions and 24 deletions

View File

@@ -27,7 +27,16 @@ opts = [
min=1, min=1,
default=4, default=4,
help=_('Number of seconds to wait between attempts to ' help=_('Number of seconds to wait between attempts to '
'connect to Redfish')) 'connect to Redfish')),
cfg.IntOpt('connection_cache_size',
min=0,
default=1000,
help=_('Maximum Redfish client connection cache size. '
'Redfish driver would strive to reuse authenticated '
'BMC connections (obtained through Redfish Session '
'Service). This option caps the maximum number of '
'connections to maintain. The value of `0` disables '
'client connection caching completely.'))
] ]

View File

@@ -152,9 +152,7 @@ def parse_driver_info(node):
class SessionCache(object): class SessionCache(object):
"""Cache of HTTP sessions credentials""" """Cache of HTTP sessions credentials"""
MAX_SESSIONS = 1000 _sessions = collections.OrderedDict()
sessions = collections.OrderedDict()
def __init__(self, driver_info): def __init__(self, driver_info):
self._driver_info = driver_info self._driver_info = driver_info
@@ -165,7 +163,7 @@ class SessionCache(object):
def __enter__(self): def __enter__(self):
try: try:
return self.sessions[self._session_key] return self.__class__._sessions[self._session_key]
except KeyError: except KeyError:
conn = sushy.Sushy( conn = sushy.Sushy(
@@ -175,25 +173,29 @@ class SessionCache(object):
verify=self._driver_info['verify_ca'] verify=self._driver_info['verify_ca']
) )
self._expire_oldest_session() if CONF.redfish.connection_cache_size:
self.__class__._sessions[self._session_key] = conn
self.sessions[self._session_key] = conn if (len(self.__class__._sessions) >
CONF.redfish.connection_cache_size):
self._expire_oldest_session()
return conn return conn
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):
# NOTE(etingof): perhaps this session token is no good # NOTE(etingof): perhaps this session token is no good
if isinstance(exc_val, sushy.exceptions.ConnectionError): if isinstance(exc_val, sushy.exceptions.ConnectionError):
self.sessions.pop(self._session_key, None) self.__class__._sessions.pop(self._session_key, None)
def _expire_oldest_session(self): @classmethod
def _expire_oldest_session(cls):
"""Expire oldest session""" """Expire oldest session"""
if len(self.sessions) >= self.MAX_SESSIONS: session_keys = list(cls._sessions)
session_keys = list(self.sessions) session_key = next(iter(session_keys))
session_key = session_keys[0] # NOTE(etingof): GC should cause sushy to HTTP DELETE session
# NOTE(etingof): GC should cause sushy to HTTP DELETE session # at BMC. Trouble is that contemporary sushy (1.6.0) does
# at BMC. Trouble is that as of this commit sushy does not do that. # does not do that.
self.sessions.pop(session_key, None) cls._sessions.pop(session_key, None)
def get_system(node): def get_system(node):

View File

@@ -18,6 +18,7 @@ import copy
import os import os
import mock import mock
from oslo_config import cfg
from oslo_utils import importutils from oslo_utils import importutils
import requests import requests
@@ -141,7 +142,7 @@ class RedfishUtilsTestCase(db_base.DbTestCase):
@mock.patch.object(sushy, 'Sushy', autospec=True) @mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.' @mock.patch('ironic.drivers.modules.redfish.utils.'
'SessionCache.sessions', {}) 'SessionCache._sessions', {})
def test_get_system(self, mock_sushy): def test_get_system(self, mock_sushy):
fake_conn = mock_sushy.return_value fake_conn = mock_sushy.return_value
fake_system = fake_conn.get_system.return_value fake_system = fake_conn.get_system.return_value
@@ -152,7 +153,7 @@ class RedfishUtilsTestCase(db_base.DbTestCase):
@mock.patch.object(sushy, 'Sushy', autospec=True) @mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.' @mock.patch('ironic.drivers.modules.redfish.utils.'
'SessionCache.sessions', {}) 'SessionCache._sessions', {})
def test_get_system_resource_not_found(self, mock_sushy): def test_get_system_resource_not_found(self, mock_sushy):
fake_conn = mock_sushy.return_value fake_conn = mock_sushy.return_value
fake_conn.get_system.side_effect = ( fake_conn.get_system.side_effect = (
@@ -168,7 +169,7 @@ class RedfishUtilsTestCase(db_base.DbTestCase):
@mock.patch('time.sleep', autospec=True) @mock.patch('time.sleep', autospec=True)
@mock.patch.object(sushy, 'Sushy', autospec=True) @mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.' @mock.patch('ironic.drivers.modules.redfish.utils.'
'SessionCache.sessions', {}) 'SessionCache._sessions', {})
def test_get_system_resource_connection_error_retry(self, mock_sushy, def test_get_system_resource_connection_error_retry(self, mock_sushy,
mock_sleep): mock_sleep):
# Redfish specific configurations # Redfish specific configurations
@@ -191,7 +192,7 @@ class RedfishUtilsTestCase(db_base.DbTestCase):
@mock.patch.object(sushy, 'Sushy', autospec=True) @mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.' @mock.patch('ironic.drivers.modules.redfish.utils.'
'SessionCache.sessions', {}) 'SessionCache._sessions', {})
def test_auth_auto(self, mock_sushy): def test_auth_auto(self, mock_sushy):
redfish_utils.get_system(self.node) redfish_utils.get_system(self.node)
mock_sushy.assert_called_with( mock_sushy.assert_called_with(
@@ -202,7 +203,7 @@ class RedfishUtilsTestCase(db_base.DbTestCase):
@mock.patch.object(sushy, 'Sushy', autospec=True) @mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.' @mock.patch('ironic.drivers.modules.redfish.utils.'
'SessionCache.sessions', {}) 'SessionCache._sessions', {})
def test_ensure_session_reuse(self, mock_sushy): def test_ensure_session_reuse(self, mock_sushy):
redfish_utils.get_system(self.node) redfish_utils.get_system(self.node)
redfish_utils.get_system(self.node) redfish_utils.get_system(self.node)
@@ -225,14 +226,25 @@ class RedfishUtilsTestCase(db_base.DbTestCase):
self.assertEqual(2, mock_sushy.call_count) self.assertEqual(2, mock_sushy.call_count)
@mock.patch.object(sushy, 'Sushy', autospec=True) @mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.' @mock.patch('ironic.drivers.modules.redfish.utils.SessionCache._sessions',
'SessionCache.MAX_SESSIONS', 10)
@mock.patch('ironic.drivers.modules.redfish.utils.SessionCache.sessions',
collections.OrderedDict()) collections.OrderedDict())
def test_expire_old_sessions(self, mock_sushy): def test_expire_old_sessions(self, mock_sushy):
cfg.CONF.set_override('connection_cache_size', 10, 'redfish')
for num in range(20): for num in range(20):
self.node.driver_info['redfish_username'] = 'foo-%d' % num self.node.driver_info['redfish_username'] = 'foo-%d' % num
redfish_utils.get_system(self.node) redfish_utils.get_system(self.node)
self.assertEqual(mock_sushy.call_count, 20) self.assertEqual(mock_sushy.call_count, 20)
self.assertEqual(len(redfish_utils.SessionCache.sessions), 10) self.assertEqual(len(redfish_utils.SessionCache._sessions), 10)
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.SessionCache._sessions',
collections.OrderedDict())
def test_disabled_sessions_cache(self, mock_sushy):
cfg.CONF.set_override('connection_cache_size', 0, 'redfish')
for num in range(2):
self.node.driver_info['redfish_username'] = 'foo-%d' % num
redfish_utils.get_system(self.node)
self.assertEqual(mock_sushy.call_count, 2)
self.assertEqual(len(redfish_utils.SessionCache._sessions), 0)