Nir Soffer has uploaded a new change for review.
Change subject: cache: Replace utils.memoized with cache.memoized()
......................................................................
cache: Replace utils.memoized with cache.memoized()
Repalce users of utils.memoized with cache.memoized() and remove
utils.memoized. No behavior change is expected.
Change-Id: I12e2f2919cf92ff7d0758d70e2ed40523d66174f
Signed-off-by: Nir Soffer <nsoffer(a)redhat.com>
---
M lib/vdsm/netinfo.py
M lib/vdsm/utils.py
M tests/vmApiTests.py
M vdsm/caps.py
M vdsm/dmidecodeUtil.py
M vdsm/ppc64HardwareInfo.py
M vdsm/supervdsmServer
M vdsm/virt/sampling.py
8 files changed, 30 insertions(+), 51 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/10/34710/1
diff --git a/lib/vdsm/netinfo.py b/lib/vdsm/netinfo.py
index d2ef90b..8c56863 100644
--- a/lib/vdsm/netinfo.py
+++ b/lib/vdsm/netinfo.py
@@ -43,7 +43,7 @@
from .ipwrapper import routeShowGateways
from . import libvirtconnection
from .netconfpersistence import RunningConfig
-from .utils import memoized
+from .cache import memoized
from .netlink import link as nl_link
from .netlink import addr as nl_addr
from .netlink import route as nl_route
@@ -395,7 +395,7 @@
return paddr
-@memoized
+@memoized()
def _getAllDefaultBondingOptions():
"""
Return default options per mode, in a dictionary of dictionaries. All keys
@@ -405,7 +405,7 @@
return json.loads(defaults.read())
-@memoized
+@memoized()
def _getDefaultBondingOptions(mode=None):
"""
Return default options for the given mode. If it is None, return options
diff --git a/lib/vdsm/utils.py b/lib/vdsm/utils.py
index feb4079..131145c 100644
--- a/lib/vdsm/utils.py
+++ b/lib/vdsm/utils.py
@@ -51,6 +51,7 @@
import vdsm.infra.zombiereaper as zombiereaper
from cpopen import CPopen
+from . import cache
from . import constants
# Buffsize is 1K because I tested it on some use cases and 1K was fastest. If
@@ -828,32 +829,6 @@
fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
-class memoized(object):
- """
- Decorator that caches a function's return value each time it is called.
- If called later with the same arguments, the cached value is returned, and
- not re-evaluated. There is no support for uncachable arguments.
-
- Adaptation from
http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
- """
- def __init__(self, func):
- self.func = func
- self.cache = {}
- functools.update_wrapper(self, func)
-
- def __call__(self, *args):
- try:
- return self.cache[args]
- except KeyError:
- value = self.func(*args)
- self.cache[args] = value
- return value
-
- def __get__(self, obj, objtype):
- """Support instance methods."""
- return functools.partial(self.__call__, obj)
-
-
def validateMinimalKeySet(dictionary, reqParams):
if not all(key in dictionary for key in reqParams):
raise ValueError
@@ -993,7 +968,7 @@
sys.exit(-3)
-@memoized
+(a)cache.memoized()
def isOvirtNode():
return (os.path.exists('/etc/rhev-hypervisor-release') or
bool(glob.glob('/etc/ovirt-node-*-release')))
diff --git a/tests/vmApiTests.py b/tests/vmApiTests.py
index eb89fb1..4f940cf 100644
--- a/tests/vmApiTests.py
+++ b/tests/vmApiTests.py
@@ -25,14 +25,14 @@
from virt import vmexitreason
from vdsm import define
from testlib import VdsmTestCase as TestCaseBase
-from vdsm import utils
+from vdsm import cache
from rpc import vdsmapi
import vmfakelib as fake
class TestSchemaCompliancyBase(TestCaseBase):
- @utils.memoized
+ @cache.memoized()
def _getAPI(self):
testPath = os.path.realpath(__file__)
dirName = os.path.split(testPath)[0]
diff --git a/vdsm/caps.py b/vdsm/caps.py
index dba9b5b..6e261d8 100644
--- a/vdsm/caps.py
+++ b/vdsm/caps.py
@@ -40,6 +40,7 @@
import dsaversion
from vdsm import netinfo
import hooks
+from vdsm import cache
from vdsm import utils
import storage.hba
from network.configurators import qos
@@ -218,12 +219,12 @@
ENABLED = 1
-(a)utils.memoized
+(a)cache.memoized()
def _getCapsXMLStr():
return libvirtconnection.get().getCapabilities()
-(a)utils.memoized
+(a)cache.memoized()
def _getCpuTopology(capabilities):
if capabilities is None:
capabilities = _getCapsXMLStr()
@@ -277,7 +278,7 @@
return None
-(a)utils.memoized
+(a)cache.memoized()
def _getLiveSnapshotSupport(arch, capabilities=None):
if capabilities is None:
capabilities = _getCapsXMLStr()
@@ -294,7 +295,7 @@
return None
-(a)utils.memoized
+(a)cache.memoized()
def getLiveMergeSupport():
"""
Determine if libvirt provides the necessary features to enable live merge.
@@ -318,7 +319,7 @@
return True
-(a)utils.memoized
+(a)cache.memoized()
def getNumaTopology():
capabilities = _getCapsXMLStr()
caps = minidom.parseString(capabilities)
@@ -369,7 +370,7 @@
return memDict
-(a)utils.memoized
+(a)cache.memoized()
def getNumaNodeDistance():
nodeDistance = {}
retcode, out, err = utils.execCmd(['numactl', '--hardware'])
@@ -385,7 +386,7 @@
return nodeDistance
-(a)utils.memoized
+(a)cache.memoized()
def getAutoNumaBalancingInfo():
retcode, out, err = utils.execCmd(['sysctl', '-n', '-e',
'kernel.numa_balancing'])
@@ -399,7 +400,7 @@
return AutoNumaBalancingStatus.UNKNOWN
-(a)utils.memoized
+(a)cache.memoized()
def _getEmulatedMachines(arch, capabilities=None):
if capabilities is None:
capabilities = _getCapsXMLStr()
@@ -461,7 +462,7 @@
return allModels
-(a)utils.memoized
+(a)cache.memoized()
def _getCompatibleCpuModels():
c = libvirtconnection.get()
allModels = _getAllCpuModels()
@@ -532,7 +533,7 @@
return kdumpStatus
-(a)utils.memoized
+(a)cache.memoized()
def getos():
if os.path.exists('/etc/rhev-hypervisor-release'):
return OSName.RHEVH
@@ -550,7 +551,7 @@
return OSName.UNKNOWN
-(a)utils.memoized
+(a)cache.memoized()
def osversion():
version = release = ''
@@ -700,7 +701,7 @@
return info
-(a)utils.memoized
+(a)cache.memoized()
def _getVersionInfo():
if not hasattr(libvirt, 'VIR_MIGRATE_ABORT_ON_ERROR'):
return _dropVersion('3.4',
diff --git a/vdsm/dmidecodeUtil.py b/vdsm/dmidecodeUtil.py
index add2526..f0c72fa 100644
--- a/vdsm/dmidecodeUtil.py
+++ b/vdsm/dmidecodeUtil.py
@@ -18,7 +18,7 @@
# Refer to the README and COPYING files for full details of the license
#
-from vdsm import utils
+from vdsm import cache
# This function gets dict and returns new dict that includes only string
@@ -35,7 +35,7 @@
return ret
-(a)utils.memoized
+(a)cache.memoized()
def getAllDmidecodeInfo():
import dmidecode
@@ -45,7 +45,7 @@
return myLeafDict
-(a)utils.memoized
+(a)cache.memoized()
def getHardwareInfoStructure():
dmiInfo = getAllDmidecodeInfo()
sysStruct = {}
diff --git a/vdsm/ppc64HardwareInfo.py b/vdsm/ppc64HardwareInfo.py
index 23f7388..1a89639 100644
--- a/vdsm/ppc64HardwareInfo.py
+++ b/vdsm/ppc64HardwareInfo.py
@@ -16,6 +16,7 @@
# Refer to the README and COPYING files for full details of the license
#
+from vdsm import cache
from vdsm import utils
import os
@@ -31,7 +32,7 @@
return 'unavailable'
-(a)utils.memoized
+(a)cache.memoized()
def getHardwareInfoStructure():
infoStructure = {'systemSerialNumber': 'unavailable',
'systemFamily': 'unavailable',
@@ -59,7 +60,7 @@
return infoStructure
-(a)utils.memoized
+(a)cache.memoized()
def getCpuTopology(capabilities):
topology = {}
diff --git a/vdsm/supervdsmServer b/vdsm/supervdsmServer
index c6c139b..aba32fb 100755
--- a/vdsm/supervdsmServer
+++ b/vdsm/supervdsmServer
@@ -51,6 +51,7 @@
except ImportError:
_glusterEnabled = False
+from vdsm import cache
from vdsm import utils
from vdsm.tool import restore_nets
from parted_utils import getDevicePartedInfo as _getDevicePartedInfo
@@ -378,7 +379,7 @@
raise OSError(errno.EINVAL, "Could not reload-rules for device "
"%s" % guid)
- @utils.memoized
+ @cache.memoized()
def __udevVersion(self):
cmd = [EXT_UDEVADM, '--version']
rc, out, err = utils.execCmd(cmd)
diff --git a/vdsm/virt/sampling.py b/vdsm/virt/sampling.py
index 6bb4211..b1c3db6 100644
--- a/vdsm/virt/sampling.py
+++ b/vdsm/virt/sampling.py
@@ -30,6 +30,7 @@
import errno
import re
+from vdsm import cache
from vdsm import utils
from vdsm import netinfo
from vdsm.ipwrapper import getLinks
@@ -524,7 +525,7 @@
if not self._stopEvent.isSet():
self._log.error("Error while sampling stats", exc_info=True)
- @utils.memoized
+ @cache.memoized()
def _boot_time(self):
# Try to get boot time only once, if N/A just log the error and never
# include it in the response.
--
To view, visit
http://gerrit.ovirt.org/34710
To unsubscribe, visit
http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I12e2f2919cf92ff7d0758d70e2ed40523d66174f
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsoffer(a)redhat.com>