-
Notifications
You must be signed in to change notification settings - Fork 192
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
♻️ REFACTOR: Reorganise Backend class UML #5172
♻️ REFACTOR: Reorganise Backend class UML #5172
Conversation
The `BackendManager` is an implementation detail of the Django/SQLA backends and should not be accessed directly.
Codecov Report
@@ Coverage Diff @@
## develop #5172 +/- ##
===========================================
- Coverage 81.00% 81.00% -0.00%
===========================================
Files 535 535
Lines 37376 37323 -53
===========================================
- Hits 30274 30230 -44
+ Misses 7102 7093 -9
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
In general all good here, I think this is a positive tidy up of some of the backend classes. My main question is why rename the SQLA What do you think? |
Heya, thanks for taking a look. Yeh I think that makes sense (can't remember any reason specifically I didn't change the django one) |
This was folded in to #5330 🎉 |
This PR addresses parts of #5154, in conjunction with #5169 (also relevant are #5145 and #5171)
It is of note that this PR does not change anything for the "front-end" user, i.e. there should be no apparent change when using the AiiDA API or CLI.
The goal of this PR and #5154, is to improve the structure of the
Backend
; simplifying it and allowing for the possibility of new Backends (such as the archive).This is an illustration of the current UML of the SQLAlchemy backend for the
Node
entity (fromdevelop
branch).(the diagrams are available at https://www.figma.com/file/oluW9WVn1v1yDj6pWpTiQ7/aiida-backend-design?node-id=0%3A1)
There are some key issues here:
RepositoryBackend
is implemented on theProfile
, meaning that it can not be implemented on a per-backend basis.SqlaBackendManager
is an implementation detail of the Django/SQLA backends, yet is exposed and accessed via theManager
.ENGINE
/SCOPED_SESSION
global variables, meaning that only one backend can be used at a time, and access to the database often by-passes the backend; using directlyget_scoped_session
.In terms of the global variable use for database access, it is clear that this has risen from the original choice of using Django as the database API. This API has a key shortcoming, in that it also ties a Python process to a single connection configuration (in
aiida/backends/djsite/settings.py
, viadjago.setup
).With sqlalchemy though, this is not necessary and there is a clear separation of concerns between the ORM models, to provide the schema for each database table, and the engine/session, which provide the interface to the actual database.
AiiDA's current implementation, tries to "force" the sqlalchemy implementation to be like the Django one, and thus "inherits" its shortcomings.
This is now the new UML:
The key changes then, are ensuring all persistent data access flows through the
Backend
instance:RepositoryBackend
is only accessed via the backendBackendManager
is only accesses via the backendModeWrapper
is renamedStorableModel
; this class links aDbNode
(or other SQLA mapper) to the backend's DB connection (i.e. session).DbNode
, re-separating its concern to only be "describing" the database table, rather than dealing with the connection.Manager
is closed, this also callsclose()
on the backend, which shutdown the session and engine connection to the database.the
Backend
is also now instantiated with theProfile
, which specifies the configuration for accessing the persistent storage (e.g. the database connection config and repository URI)For the sqlalachemy backend this actually means that multiple backends can "exist" at the same time (in particular the main backend and the archive backend). For django, because of its design, you are still restricted to one backend.
(note though the
Manager
still restricts one "main" Backend to be loaded, so users will not notice this)To accommodate these changes (the backend being responsible for the session), a few changes had to be made to
AiidaTestCase
:User
&Computer
were both persisted as class variables, meaning they were always using the same backend, despite the manager being reset after each test.close_all_backends()
method added to allow for ensuring all backends have been closed after each test.Note, these changes were not required for the newer pytest fixtures (which eventually should deprecate
AiidaTestCase
), since they reset the DB at the same time as resetting the manager and don't store class variables.