Fix duplicate registration for resource_extend

Description field is added to resources by inheriting
StandardAttrDescriptionMixin, and StandardAttrDescriptionMixin use the
@has_resource_extenders decorator to register resource_extend functions.
Any plugin which inherits StandardAttrDescriptionMixin will register
_extend_standard_attr_description duplicately, leading to performance
issues caused by duplicate executions. This fix ensures that resource
extend functions are executed only once.

Closes-bug: #2127835
Change-Id: Id3e36dc5475ccff69b1257ff9816201ed04257fe
Signed-off-by: Xu Qi<xuqi_yewu@cmss.chinamobile.com>
This commit is contained in:
XuQi
2025-10-10 10:14:30 +00:00
parent 7e78006e5a
commit 7b3be70b35
3 changed files with 26 additions and 3 deletions

View File

@@ -58,9 +58,12 @@ def register_funcs(resource, funcs):
return foo_res
"""
funcs = [helpers.make_weak_ref(f) if callable(f) else f
for f in funcs]
_resource_extend_functions.setdefault(resource, []).extend(funcs)
funcs = [helpers.make_weak_ref(f) if callable(f) else f for f in funcs]
existing_funcs = _resource_extend_functions.setdefault(resource, [])
for func in funcs:
if func not in existing_funcs:
existing_funcs.append(func)
def get_funcs(resource):

View File

@@ -49,6 +49,19 @@ class TestResourceExtend(base.BaseTestCase):
for r in resources:
self.assertIsNotNone(resource_extend.get_funcs(r))
def test_register_funcs_no_duplicates(self):
resource = 'A'
def _cb(resp, db_obj):
pass
resource_extend.register_funcs(resource, (_cb,))
resource_extend.register_funcs(resource, (_cb,))
funcs = resource_extend.get_funcs(resource)
self.assertEqual(funcs.count(funcs[0]), 1)
self.assertEqual(len(funcs), 1)
def test_apply_funcs(self):
resources = ['A', 'B', 'C']
callbacks = []

View File

@@ -0,0 +1,7 @@
---
other:
- |
Avoid the duplicated method registration in class methods decorated with
``resource_extend.has_resource_extenders``. Before registering a new
extended method, it is first checked if this method is already registered.
See bug: `2127835 <https://bugs.launchpad.net/neutron/+bug/2127835>`_.