-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make PersistentAdapterRegistry use PersistentMapping and PersistentList instead of dict and tuple #51
Comments
There's a large test suite for much of this in I can think of one example where this is done, and that's ZODB: The RelStorage tests extend the ZODB tests. This would probably at least need a comment in Is there a better way? |
Scratch that. Because the |
…ith zope.interface 5.3+ Fixes #51
Hey @jamadden Seems like migrating old tuples breaks:
|
Is that |
Sorry, I'm way behind on my notifications. Yes, |
No, it's
|
Same test failure: ``` File "/Users/maurits/community/plone-coredev/6.0/src/plone.app.users/plone/app/users/tests/password.rst", line 27, in password.rst Failed example: browser.open('http://nohost/plone/' + view_name) Expected: Traceback (most recent call last): ... zExceptions.unauthorized.Unauthorized: ...You are not authorized to access this resource... Got: Traceback (most recent call last): File "/Users/maurits/.pyenv/versions/3.9.9/lib/python3.9/doctest.py", line 1334, in __run exec(compile(example.source, filename, "single", File "<doctest password.rst[8]>", line 1, in <module> browser.open('http://nohost/plone/' + view_name) File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 256, in open self._processRequest(url, make_request) File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 282, in _processRequest resp = make_request(reqargs) File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 253, in make_request return self.testapp.get(url, **args) File "/Users/maurits/shared-eggs/cp39/WebTest-3.0.0-py3.9.egg/webtest/app.py", line 324, in get return self.do_request(req, status=status, File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 93, in do_request response = super(TestbrowserApp, self).do_request(req, status, File "/Users/maurits/shared-eggs/cp39/WebTest-3.0.0-py3.9.egg/webtest/app.py", line 620, in do_request res = req.get_response(app, catch_exc_info=True) File "/Users/maurits/shared-eggs/cp39/WebOb-1.8.7-py3.9.egg/webob/request.py", line 1309, in send status, headers, app_iter, exc_info = self.call_application( File "/Users/maurits/shared-eggs/cp39/WebOb-1.8.7-py3.9.egg/webob/request.py", line 1278, in call_application app_iter = application(self.environ, start_response) File "/Users/maurits/shared-eggs/cp39/WebTest-3.0.0-py3.9.egg/webtest/lint.py", line 196, in lint_app iterator = application(environ, start_response_wrapper) File "/Users/maurits/shared-eggs/cp39/plone.testing-8.0.3-py3.9.egg/plone/testing/_z2_testbrowser.py", line 39, in wrapped_func return func(*args, **kw) File "/Users/maurits/shared-eggs/cp39/plone.testing-8.0.3-py3.9.egg/plone/testing/_z2_testbrowser.py", line 66, in __call__ wsgi_result = publish(environ, start_response) File "/Users/maurits/community/plone-coredev/6.0/src/Zope/src/ZPublisher/WSGIPublisher.py", line 391, in publish_module request.close() File "/Users/maurits/community/plone-coredev/6.0/src/Zope/src/ZPublisher/BaseRequest.py", line 215, in close notify(EndRequestEvent(None, self)) File "/Users/maurits/shared-eggs/cp39/zope.event-4.5.0-py3.9.egg/zope/event/__init__.py", line 32, in notify subscriber(event) File "/Users/maurits/shared-eggs/cp39/zope.component-5.0.1-py3.9.egg/zope/component/event.py", line 27, in dispatch component_subscribers(event, None) File "/Users/maurits/shared-eggs/cp39/zope.component-5.0.1-py3.9.egg/zope/component/_api.py", line 134, in subscribers return sitemanager.subscribers(objects, interface) File "/Users/maurits/shared-eggs/cp39/zope.interface-5.4.0-py3.9-macosx-10.15-x86_64.egg/zope/interface/registry.py", line 448, in subscribers return self.adapters.subscribers(objects, provided) File "/Users/maurits/shared-eggs/cp39/zope.interface-5.4.0-py3.9-macosx-10.15-x86_64.egg/zope/interface/adapter.py", line 895, in subscribers subscriptions = self.subscriptions([providedBy(o) for o in objects], provided) File "/Users/maurits/shared-eggs/cp39/zope.interface-5.4.0-py3.9-macosx-10.15-x86_64.egg/zope/interface/adapter.py", line 877, in _uncached_subscriptions if order >= len(byorder): File "/Users/maurits/.pyenv/versions/3.9.9/lib/python3.9/collections/__init__.py", line 1169, in __len__ return len(self.data) File "/Users/maurits/shared-eggs/cp39/ZODB-5.6.0-py3.9.egg/ZODB/Connection.py", line 785, in setstate raise ConnectionStateError(msg) ZODB.POSException.ConnectionStateError: Shouldn't load state for persistent.list.PersistentList 0x3953f62e87808eb2 when the connection is closed ``` Should be somehow caused by this zope.component PR: zopefoundation/zope.component#51 From what I can follow from the traceback and running around in the pdb, it goes wrong in this part of `ZPublisher/BaseRequest.py`: ``` def close(self): try: notify(EndRequestEvent(None, self)) finally: # subscribers might need the zodb, so `clear` must come afterwards # (since `self._held=None` might close the connection, see above) self.clear() ``` The notify fails, even when I comment out the two subscribers that I found (`Products.Five` and `plone.app.contentrules`). Probably some other code has already called `self.clear()` before this. A reason for this can be that several subrequests are made for the Diazo theme, and this seems to call `self.clear()` directly. Whatever the reason, the comment is true: subscribers might need the zodb. In fact, with zope.component 5, the list of subscribers itself, even when it is empty, is something that we get from the zodb. So it fails here. When I run `bin/instance fg` and visit the same page anonymously (`@@change-password`) it works fine: I am Unauthorized and get redirected, which is also what the test expects. So in practice all seems fine. So what somehow triggers the ZODB error, is that this is a test, and the first browser visit is to a page that raises an Unauthorized. The fix/workaround is to first visit the root of the site. Then somehow all is well. Note: last time I tried this, most tests in most packages passed just fine. `plone.app.users` was an exception, and there were about two other packages I think. I will follow up with more PRs, but this one should be fine, also for use with our current outdated zope.component 4.6.2.
Sample test failure: ``` File "/Users/maurits/community/plone-coredev/6.0/src/plone.app.users/plone/app/users/tests/password.rst", line 27, in password.rst Failed example: browser.open('http://nohost/plone/' + view_name) Expected: Traceback (most recent call last): ... zExceptions.unauthorized.Unauthorized: ...You are not authorized to access this resource... Got: Traceback (most recent call last): File "/Users/maurits/.pyenv/versions/3.9.9/lib/python3.9/doctest.py", line 1334, in __run exec(compile(example.source, filename, "single", File "<doctest password.rst[8]>", line 1, in <module> browser.open('http://nohost/plone/' + view_name) File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 256, in open self._processRequest(url, make_request) File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 282, in _processRequest resp = make_request(reqargs) File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 253, in make_request return self.testapp.get(url, **args) File "/Users/maurits/shared-eggs/cp39/WebTest-3.0.0-py3.9.egg/webtest/app.py", line 324, in get return self.do_request(req, status=status, File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 93, in do_request response = super(TestbrowserApp, self).do_request(req, status, File "/Users/maurits/shared-eggs/cp39/WebTest-3.0.0-py3.9.egg/webtest/app.py", line 620, in do_request res = req.get_response(app, catch_exc_info=True) File "/Users/maurits/shared-eggs/cp39/WebOb-1.8.7-py3.9.egg/webob/request.py", line 1309, in send status, headers, app_iter, exc_info = self.call_application( File "/Users/maurits/shared-eggs/cp39/WebOb-1.8.7-py3.9.egg/webob/request.py", line 1278, in call_application app_iter = application(self.environ, start_response) File "/Users/maurits/shared-eggs/cp39/WebTest-3.0.0-py3.9.egg/webtest/lint.py", line 196, in lint_app iterator = application(environ, start_response_wrapper) File "/Users/maurits/shared-eggs/cp39/plone.testing-8.0.3-py3.9.egg/plone/testing/_z2_testbrowser.py", line 39, in wrapped_func return func(*args, **kw) File "/Users/maurits/shared-eggs/cp39/plone.testing-8.0.3-py3.9.egg/plone/testing/_z2_testbrowser.py", line 66, in __call__ wsgi_result = publish(environ, start_response) File "/Users/maurits/community/plone-coredev/6.0/src/Zope/src/ZPublisher/WSGIPublisher.py", line 391, in publish_module request.close() File "/Users/maurits/community/plone-coredev/6.0/src/Zope/src/ZPublisher/BaseRequest.py", line 215, in close notify(EndRequestEvent(None, self)) File "/Users/maurits/shared-eggs/cp39/zope.event-4.5.0-py3.9.egg/zope/event/__init__.py", line 32, in notify subscriber(event) File "/Users/maurits/shared-eggs/cp39/zope.component-5.0.1-py3.9.egg/zope/component/event.py", line 27, in dispatch component_subscribers(event, None) File "/Users/maurits/shared-eggs/cp39/zope.component-5.0.1-py3.9.egg/zope/component/_api.py", line 134, in subscribers return sitemanager.subscribers(objects, interface) File "/Users/maurits/shared-eggs/cp39/zope.interface-5.4.0-py3.9-macosx-10.15-x86_64.egg/zope/interface/registry.py", line 448, in subscribers return self.adapters.subscribers(objects, provided) File "/Users/maurits/shared-eggs/cp39/zope.interface-5.4.0-py3.9-macosx-10.15-x86_64.egg/zope/interface/adapter.py", line 895, in subscribers subscriptions = self.subscriptions([providedBy(o) for o in objects], provided) File "/Users/maurits/shared-eggs/cp39/zope.interface-5.4.0-py3.9-macosx-10.15-x86_64.egg/zope/interface/adapter.py", line 877, in _uncached_subscriptions if order >= len(byorder): File "/Users/maurits/.pyenv/versions/3.9.9/lib/python3.9/collections/__init__.py", line 1169, in __len__ return len(self.data) File "/Users/maurits/shared-eggs/cp39/ZODB-5.6.0-py3.9.egg/ZODB/Connection.py", line 785, in setstate raise ConnectionStateError(msg) ZODB.POSException.ConnectionStateError: Shouldn't load state for persistent.list.PersistentList 0x3953f62e87808eb2 when the connection is closed ``` Should be somehow caused by this zope.component PR: zopefoundation/zope.component#51 From what I can follow from the traceback and running around in the pdb, it goes wrong in this part of `ZPublisher/BaseRequest.py`: ``` def close(self): try: notify(EndRequestEvent(None, self)) finally: # subscribers might need the zodb, so `clear` must come afterwards # (since `self._held=None` might close the connection, see above) self.clear() ``` The notify fails, even when I comment out the two subscribers that I found (`Products.Five` and `plone.app.contentrules`). Probably some other code has already called `self.clear()` before this. A reason for this can be that several subrequests are made for the Diazo theme, and this seems to call `self.clear()` directly. Whatever the reason, the comment is true: subscribers might need the zodb. In fact, with zope.component 5, the list of subscribers itself, even when it is empty, is something that we get from the zodb. So it fails here. When I run `bin/instance fg` and visit the same page anonymously (`@@change-password`) it works fine: I am Unauthorized and get redirected, which is also what the test expects. So in practice all seems fine. So what somehow triggers the ZODB error, is that this is a test, and the first browser visit is to a page that raises an Unauthorized. The fix/workaround is to first visit the root of the site. Then somehow all is well. Note: last time I tried this, most tests in most packages passed just fine. `plone.app.users` was an exception, and there were about two other packages I think. I will follow up with more PRs, but this one should be fine, also for use with our current outdated zope.component 4.6.2.
Branch: refs/heads/master Date: 2022-03-08T20:46:41+01:00 Author: Maurits van Rees (mauritsvanrees) <maurits@vanrees.org> Commit: plone/plone.app.users@e99f375 Fixed tests when run with zope.component 5+. Sample test failure: ``` File "/Users/maurits/community/plone-coredev/6.0/src/plone.app.users/plone/app/users/tests/password.rst", line 27, in password.rst Failed example: browser.open('http://nohost/plone/' + view_name) Expected: Traceback (most recent call last): ... zExceptions.unauthorized.Unauthorized: ...You are not authorized to access this resource... Got: Traceback (most recent call last): File "/Users/maurits/.pyenv/versions/3.9.9/lib/python3.9/doctest.py", line 1334, in __run exec(compile(example.source, filename, "single", File "<doctest password.rst[8]>", line 1, in <module> browser.open('http://nohost/plone/' + view_name) File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 256, in open self._processRequest(url, make_request) File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 282, in _processRequest resp = make_request(reqargs) File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 253, in make_request return self.testapp.get(url, **args) File "/Users/maurits/shared-eggs/cp39/WebTest-3.0.0-py3.9.egg/webtest/app.py", line 324, in get return self.do_request(req, status=status, File "/Users/maurits/shared-eggs/cp39/zope.testbrowser-5.5.1-py3.9.egg/zope/testbrowser/browser.py", line 93, in do_request response = super(TestbrowserApp, self).do_request(req, status, File "/Users/maurits/shared-eggs/cp39/WebTest-3.0.0-py3.9.egg/webtest/app.py", line 620, in do_request res = req.get_response(app, catch_exc_info=True) File "/Users/maurits/shared-eggs/cp39/WebOb-1.8.7-py3.9.egg/webob/request.py", line 1309, in send status, headers, app_iter, exc_info = self.call_application( File "/Users/maurits/shared-eggs/cp39/WebOb-1.8.7-py3.9.egg/webob/request.py", line 1278, in call_application app_iter = application(self.environ, start_response) File "/Users/maurits/shared-eggs/cp39/WebTest-3.0.0-py3.9.egg/webtest/lint.py", line 196, in lint_app iterator = application(environ, start_response_wrapper) File "/Users/maurits/shared-eggs/cp39/plone.testing-8.0.3-py3.9.egg/plone/testing/_z2_testbrowser.py", line 39, in wrapped_func return func(*args, **kw) File "/Users/maurits/shared-eggs/cp39/plone.testing-8.0.3-py3.9.egg/plone/testing/_z2_testbrowser.py", line 66, in __call__ wsgi_result = publish(environ, start_response) File "/Users/maurits/community/plone-coredev/6.0/src/Zope/src/ZPublisher/WSGIPublisher.py", line 391, in publish_module request.close() File "/Users/maurits/community/plone-coredev/6.0/src/Zope/src/ZPublisher/BaseRequest.py", line 215, in close notify(EndRequestEvent(None, self)) File "/Users/maurits/shared-eggs/cp39/zope.event-4.5.0-py3.9.egg/zope/event/__init__.py", line 32, in notify subscriber(event) File "/Users/maurits/shared-eggs/cp39/zope.component-5.0.1-py3.9.egg/zope/component/event.py", line 27, in dispatch component_subscribers(event, None) File "/Users/maurits/shared-eggs/cp39/zope.component-5.0.1-py3.9.egg/zope/component/_api.py", line 134, in subscribers return sitemanager.subscribers(objects, interface) File "/Users/maurits/shared-eggs/cp39/zope.interface-5.4.0-py3.9-macosx-10.15-x86_64.egg/zope/interface/registry.py", line 448, in subscribers return self.adapters.subscribers(objects, provided) File "/Users/maurits/shared-eggs/cp39/zope.interface-5.4.0-py3.9-macosx-10.15-x86_64.egg/zope/interface/adapter.py", line 895, in subscribers subscriptions = self.subscriptions([providedBy(o) for o in objects], provided) File "/Users/maurits/shared-eggs/cp39/zope.interface-5.4.0-py3.9-macosx-10.15-x86_64.egg/zope/interface/adapter.py", line 877, in _uncached_subscriptions if order >= len(byorder): File "/Users/maurits/.pyenv/versions/3.9.9/lib/python3.9/collections/__init__.py", line 1169, in __len__ return len(self.data) File "/Users/maurits/shared-eggs/cp39/ZODB-5.6.0-py3.9.egg/ZODB/Connection.py", line 785, in setstate raise ConnectionStateError(msg) ZODB.POSException.ConnectionStateError: Shouldn't load state for persistent.list.PersistentList 0x3953f62e87808eb2 when the connection is closed ``` Should be somehow caused by this zope.component PR: zopefoundation/zope.component#51 From what I can follow from the traceback and running around in the pdb, it goes wrong in this part of `ZPublisher/BaseRequest.py`: ``` def close(self): try: notify(EndRequestEvent(None, self)) finally: # subscribers might need the zodb, so `clear` must come afterwards # (since `self._held=None` might close the connection, see above) self.clear() ``` The notify fails, even when I comment out the two subscribers that I found (`Products.Five` and `plone.app.contentrules`). Probably some other code has already called `self.clear()` before this. A reason for this can be that several subrequests are made for the Diazo theme, and this seems to call `self.clear()` directly. Whatever the reason, the comment is true: subscribers might need the zodb. In fact, with zope.component 5, the list of subscribers itself, even when it is empty, is something that we get from the zodb. So it fails here. When I run `bin/instance fg` and visit the same page anonymously (`@@change-password`) it works fine: I am Unauthorized and get redirected, which is also what the test expects. So in practice all seems fine. So what somehow triggers the ZODB error, is that this is a test, and the first browser visit is to a page that raises an Unauthorized. The fix/workaround is to first visit the root of the site. Then somehow all is well. Note: last time I tried this, most tests in most packages passed just fine. `plone.app.users` was an exception, and there were about two other packages I think. I will follow up with more PRs, but this one should be fine, also for use with our current outdated zope.component 4.6.2. Files changed: A news/500.bugfix M plone/app/users/tests/password.rst M plone/app/users/tests/personal_preferences.rst M plone/app/users/tests/userdata.rst Repository: plone.app.users Branch: refs/heads/master Date: 2022-03-16T09:26:08+01:00 Author: Maurits van Rees (mauritsvanrees) <maurits@vanrees.org> Commit: plone/plone.app.users@eb12ba1 Merge pull request #107 from plone/maurits-zope-component-5 Fixed tests when run with zope.component 5+. Files changed: A news/500.bugfix M plone/app/users/tests/password.rst M plone/app/users/tests/personal_preferences.rst M plone/app/users/tests/userdata.rst
PersistentComponents
already usesPersistentMapping
andPersistentList
for its internal structures:zope.component/src/zope/component/persistentregistry.py
Lines 57 to 61 in e7fe69a
Beginning with zopefoundation/zope.interface#228 in what will be zope.interface 5.3, it's possible to control the data structures an adapter registry uses.
I think it would be good to make the
PersistentAdapterRegistry
use these same data structures. This way, when a registry is unpickled, you don't have to also create ghosts for all the components that have been registered/subscribed to it, and when a registry is pickled, you don't have to write a huge object tree, only the parts that have been changed. This makes it much for scalable. (BTrees are even more scalable, but may not support certain use cases.)Doing this for new objects is easy, it's just a matter of setting certain fields in the class (and implementing two trivial methods).
For existing objects, though, it will take a bit more care because the internal mapping objects are mutated in place. This means we have two cases to consider:
dict
holding a reference to a newPersistentMapping
which is mutated. This one should be easy, and nothing should need done: mutating aPersistentMapping
will automatically persist thePersistentMapping
.PersistentMapping
holding a reference to an olddict
which is mutated. That would be bad, because mutating the dict wouldn't know that the persistent parent needs to be saved. I'm not seeing how we could actually get into that state, though. By definition, a newPersistentMapping
will only have other newPersistentMapping
objects as values.There is a
rebuild()
method that can re-create the entire data structures based on the new types; for those wishing to do a complete migration, that would be an effective way to minimize what needs pickled in future changes.One more wrinkle: Right now, because the old data structures were non-persistent, making any change (registering, unregistering, etc) causes the
PersistentAdapterRegistry
to mark itself as changed:zope.component/src/zope/component/persistentregistry.py
Lines 26 to 29 in e7fe69a
In a new object, or one that has been
rebuild()
and hence consists of only persistent data structures, this isn't necessary. It would be a nice optimization to handle those cases.The text was updated successfully, but these errors were encountered: