Răsfoiți Sursa

Cache OpenStackVMType.extra_data to avoid repeated /os-extra_specs calls

`extra_data` calls Nova's flavor.get_keys() under the hood, which hits
/compute/v2.1/flavors/<id>/os-extra_specs. test_vm_type_properties
iterates every flavor and reads vm_type.family (→ extra_data) for each;
under pytest-xdist -n 5 that fans out to enough concurrent requests to
push nova-api into timeout territory.

Cache the result on the OpenStackVMType instance. This matches the
object-lifetime semantics already implied by the rest of the class —
vcpus, ram, disk, etc. all read from the snapshot in self._os_flavor
that was fetched at construction time, and OpenStackVMType has no
refresh() method. A caller wanting fresh extra_specs re-fetches the
VMType via provider.compute.vm_types.get(id), which yields a new
Python object with an empty cache.
Nuwan Goonasekera 9 ore în urmă
părinte
comite
dcf906c56a
1 a modificat fișierele cu 12 adăugiri și 5 ștergeri
  1. 12 5
      cloudbridge/providers/openstack/resources.py

+ 12 - 5
cloudbridge/providers/openstack/resources.py

@@ -215,6 +215,7 @@ class OpenStackVMType(BaseVMType):
     def __init__(self, provider, os_flavor):
         super(OpenStackVMType, self).__init__(provider)
         self._os_flavor = os_flavor
+        self._extra_data = None
 
     @property
     def id(self):
@@ -254,11 +255,17 @@ class OpenStackVMType(BaseVMType):
 
     @property
     def extra_data(self):
-        extras = self._os_flavor.get_keys()
-        extras['rxtx_factor'] = self._os_flavor.rxtx_factor
-        extras['swap'] = self._os_flavor.swap
-        extras['is_public'] = self._os_flavor.is_public
-        return extras
+        # get_keys() hits Nova's /flavors/<id>/os-extra_specs endpoint.
+        # Cache the result so repeat property accesses (and family, which
+        # delegates here) don't fan out to N concurrent API calls under
+        # pytest-xdist load.
+        if self._extra_data is None:
+            extras = self._os_flavor.get_keys()
+            extras['rxtx_factor'] = self._os_flavor.rxtx_factor
+            extras['swap'] = self._os_flavor.swap
+            extras['is_public'] = self._os_flavor.is_public
+            self._extra_data = extras
+        return self._extra_data
 
 
 class OpenStackInstance(BaseInstance):