Merge "Break cycle between Resource and Attributes"

This commit is contained in:
Jenkins 2016-09-30 11:00:23 +00:00 committed by Gerrit Code Review
commit 3d4d2f7d3a
3 changed files with 22 additions and 5 deletions

View File

@ -209,7 +209,7 @@ class Resource(object):
"""
return attributes.Attributes(self.name,
self.attributes_schema,
self._resolve_all_attributes)
self._make_resolver(weakref.ref(self)))
def __init__(self, name, definition, stack):
@ -2279,3 +2279,17 @@ class Resource(object):
except Exception:
return False
return True
@staticmethod
def _make_resolver(ref):
"""Return an attribute resolution method.
This builds a resolver without a strong reference to this resource, to
break a possible cycle.
"""
def resolve(attr):
res = ref()
if res is None:
raise RuntimeError("Resource collected")
return res._resolve_all_attributes(attr)
return resolve

View File

@ -13,6 +13,7 @@
import json
import warnings
import weakref
from oslo_config import cfg
from oslo_log import log as logging
@ -87,7 +88,7 @@ class StackResource(resource.Resource):
# with all available outputs
self.attributes = attributes.Attributes(
self.name, self.attributes_schema,
self._resolve_all_attributes)
self._make_resolver(weakref.ref(self)))
def _needs_update(self, after, before, after_props, before_props,
prev_resource, check_init_complete=True):

View File

@ -11,6 +11,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import weakref
from oslo_serialization import jsonutils
from requests import exceptions
import six
@ -118,9 +120,9 @@ class TemplateResource(stack_resource.StackResource):
tmpl, self.stack.env.param_defaults)
self.attributes_schema.update(self.base_attributes_schema)
self.attributes = attributes.Attributes(self.name,
self.attributes_schema,
self._resolve_all_attributes)
self.attributes = attributes.Attributes(
self.name, self.attributes_schema,
self._make_resolver(weakref.ref(self)))
def child_params(self):
"""Override method of child_params for the resource.