Skip to content

Commit

Permalink
Fixes bug 723235
Browse files Browse the repository at this point in the history
The XML templates have been converted into properties, thus we can
compare the mtime of the XML templates (libvirt and cpuinfo) each
time they are needed, checking if they have been modified and
reloading them. Added a function to read cached files.

Change-Id: I6cf0229c6435300e73f9d9a6b10b0bf9bf144a55
  • Loading branch information
alvarolopez committed Dec 15, 2011
1 parent 3679187 commit f68abf9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
29 changes: 29 additions & 0 deletions nova/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import __builtin__
import mox
import datetime
import os
import tempfile
Expand Down Expand Up @@ -330,6 +332,33 @@ def test_generate_glance_url(self):
actual_url = "http://%s:%d" % (FLAGS.glance_host, FLAGS.glance_port)
self.assertEqual(generated_url, actual_url)

def test_read_cached_file(self):
self.mox.StubOutWithMock(os.path, "getmtime")
os.path.getmtime(mox.IgnoreArg()).AndReturn(1)
self.mox.ReplayAll()

cache_data = {"data": 1123, "mtime": 1}
data = utils.read_cached_file("/this/is/a/fake", cache_data)
self.assertEqual(cache_data["data"], data)

def test_read_modified_cached_file(self):
self.mox.StubOutWithMock(os.path, "getmtime")
self.mox.StubOutWithMock(__builtin__, 'open')

os.path.getmtime(mox.IgnoreArg()).AndReturn(2)

fake_contents = "lorem ipsum"
fake_file = self.mox.CreateMockAnything()
fake_file.read().AndReturn(fake_contents)
__builtin__.open(mox.IgnoreArg()).AndReturn(fake_file)

self.mox.ReplayAll()
cache_data = {"data": 1123, "mtime": 1}
data = utils.read_cached_file("/this/is/a/fake", cache_data)
self.mox.VerifyAll()
self.mox.UnsetStubs()
self.assertEqual(data, fake_contents)


class IsUUIDLikeTestCase(test.TestCase):
def assertUUIDLike(self, val, expected):
Expand Down
14 changes: 14 additions & 0 deletions nova/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,3 +1110,17 @@ def sanitize_hostname(hostname):
hostname = hostname.strip('.-')

return hostname


def read_cached_file(filename, cache_info):
"""Return the contents of a file. If the file hasn't changed since the
last invocation, a cached version will be returned.
"""
mtime = os.path.getmtime(filename)
if cache_info and mtime == cache_info.get('mtime', None):
return cache_info['data']

data = open(filename).read()
cache_info['data'] = data
cache_info['mtime'] = mtime
return data
18 changes: 16 additions & 2 deletions nova/virt/libvirt/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ class LibvirtConnection(driver.ComputeDriver):
def __init__(self, read_only):
super(LibvirtConnection, self).__init__()

self.libvirt_xml = open(FLAGS.libvirt_xml_template).read()
self.cpuinfo_xml = open(FLAGS.cpuinfo_xml_template).read()
self._host_state = None
self._wrapped_conn = None
self.read_only = read_only
Expand Down Expand Up @@ -220,6 +218,22 @@ def init_host(self, host):
# NOTE(nsokolov): moved instance restarting to ComputeManager
pass

@property
def libvirt_xml(self):
if not hasattr(self, '_libvirt_xml_cache_info'):
self._libvirt_xml_cache_info = {}

return utils.read_cached_file(FLAGS.libvirt_xml_template,
self._libvirt_xml_cache_info)

@property
def cpuinfo_xml(self):
if not hasattr(self, '_cpuinfo_xml_cache_info'):
self._cpuinfo_xml_cache_info = {}

return utils.read_cached_file(FLAGS.cpuinfo_xml_template,
self._cpuinfo_xml_cache_info)

def _get_connection(self):
if not self._wrapped_conn or not self._test_connection():
LOG.debug(_('Connecting to libvirt: %s'), self.uri)
Expand Down

0 comments on commit f68abf9

Please sign in to comment.