Switching to the new EngineFacade
-
Upload
roman-podoliaka -
Category
Engineering
-
view
69 -
download
1
Transcript of Switching to the new EngineFacade
Switching to the new EngineFacadeby Roman Podoliaka
@rpodoliakairc: [email protected]
slides: http://podoliaka.org/talks/
The new EngineFacadespec: https://specs.openstack.org/openstack/oslospecs/specs/kilo/makeenginefacadeafacade.htmlimplemented by zzzeek (Mike Bayer) in Liberty (oslo.db >=1.12.0)a new clean API for using of oslo.db
threadsafe initializationdeclarative management of session / connection scopeeasier offloading of readonly transactions toasynchronous replicas
"old" EngineFacade: threadsafe initialization is left upto users
_ENGINE_FACADE = None_LOCK = threading.Lock()
def _create_facade_lazily(): global _LOCK, _ENGINE_FACADE if _ENGINE_FACADE is None: with _LOCK: if _ENGINE_FACADE is None: _ENGINE_FACADE = db_session.EngineFacade.from_config(CONF) return _ENGINE_FACADE
"new" EngineFacade: lazy threadsafe initialization
from oslo.db import enginefacade as sql
@sql.readerdef some_api_method(context): # work with context.session
@sql.writerdef some_other_api_method(context): # work with context.session
"new" EngineFacade: multiple instances ofEngineFacade
main_context_manager = sql.transaction_context()api_context_manager = sql.transaction_context()
"new" EngineFacade: project specific configuration
context_manager = enginefacade.transaction_context()
context_manager.configure(sqlite_fk=True)context_manager.append_on_engine_create(set_hook)
"old" EngineFacade: get_session() boilerplate
@require_context@handle_db_data_errordef snapshot_create(context, values): values['snapshot_metadata'] = \ _metadata_refs(values.get('metadata'), models.SnapshotMetadata) if not values.get('id'): values['id'] = str(uuid.uuid4())
session = get_session() with session.begin(): snapshot_ref = models.Snapshot() snapshot_ref.update(values) session.add(snapshot_ref)
return _snapshot_get(context, values['id'], session=session)
"old" EngineFacade: private DB API functions
@require_contextdef _snapshot_metadata_get(context, snapshot_id, session=None): rows = _snapshot_metadata_get_query( context,snapshot_id, session).all() result = {} for row in rows: result[row['key']] = row['value']
return result
"new" EngineFacade: session scope is manageddeclaratively
@main_context_manager.writerdef quota_destroy_all_by_project_and_user(context, project_id, user_id): model_query(context, models.ProjectUserQuota, read_deleted="no").\ filter_by(project_id=project_id).\ filter_by(user_id=user_id).\ soft_delete(synchronize_session=False)
What projects are already using thenew EngineFacade?
NovaNeutronIronicKeystone...
Need help?Slides: http://podoliaka.org/talks/
Bugs: https://bugs.launchpad.net/oslo.db
Questions:
IRC#openstackoslo at freenodezzzeek (Mike Bayer)rpodolyaka (Roman Podoliaka)
MLhttp://lists.openstack.org/cgibin/mailman/listinfo/openstackdevtag with [oslo][db]