From 957cadc2f333c652d51534899b656d0f7c8464ff Mon Sep 17 00:00:00 2001 From: Alejandro Cabrera Date: Thu, 30 Jan 2014 14:29:42 -0500 Subject: [PATCH] feat(sql/driver): expose ControlDriver, more config This patch adds two features to our current sqlalchemy driver: - option to configure connection URI for driver - Skeleton of ControlDriver written With the ControlDriver, the expected methods were stubbed out. Small fix: all controller methods not yet implemented now raise NotImplementedError. Change-Id: I1cd4a4d75cbbee7f0ff574c5be4d11660359ab7e Partially-Implements: blueprint: sql-storage-driver --- etc/marconi.conf-sample | 3 ++ marconi/queues/storage/sqlalchemy/driver.py | 53 +++++++++++++++++---- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/etc/marconi.conf-sample b/etc/marconi.conf-sample index e2650f1e6..1e83085c4 100644 --- a/etc/marconi.conf-sample +++ b/etc/marconi.conf-sample @@ -99,6 +99,9 @@ storage = mongodb uri = mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test&ssl=true&w=majority database = marconi +[drivers:storage:sqlalchemy] +;uri = sqlite:///:memory: + # Number of databases across which to partition message data, # in order to reduce writer lock %. DO NOT change this setting # after initial deployment. It MUST remain static. Also, diff --git a/marconi/queues/storage/sqlalchemy/driver.py b/marconi/queues/storage/sqlalchemy/driver.py index 66707bdd0..84e7bf1b1 100644 --- a/marconi/queues/storage/sqlalchemy/driver.py +++ b/marconi/queues/storage/sqlalchemy/driver.py @@ -24,8 +24,8 @@ from marconi.queues.storage.sqlalchemy import tables _SQLALCHEMY_OPTIONS = [ - cfg.StrOpt('database', default=':memory:', - help='Sqlalchemy database to use.') + cfg.StrOpt('uri', default='sqlite:///:memory:', + help='An sqlalchemy URL') ] _SQLALCHEMY_GROUP = 'drivers:storage:sqlalchemy' @@ -36,17 +36,19 @@ class DataDriver(storage.DataDriverBase): def __init__(self, conf, cache): super(DataDriver, self).__init__(conf, cache) - self.conf.register_opts(_SQLALCHEMY_OPTIONS, group=_SQLALCHEMY_GROUP) + self.conf.register_opts(_SQLALCHEMY_OPTIONS, + group=_SQLALCHEMY_GROUP) self.sqlalchemy_conf = self.conf[_SQLALCHEMY_GROUP] - self.__path = self.sqlalchemy_conf.database - @decorators.lazy_property(write=False) def engine(self, *args, **kwargs): - engine = sa.create_engine(*args, **kwargs) + engine = sa.create_engine(self.sqlalchemy_conf.uri, **kwargs) tables.metadata.create_all(engine, checkfirst=True) return engine + # TODO(cpp-cabrera): expose connect/close as a context manager + # that acquires the connection to the DB for the desired scope and + # closes it once the operations are completed @decorators.lazy_property(write=False) def connection(self): return self.engine.connect() @@ -56,12 +58,45 @@ class DataDriver(storage.DataDriverBase): @decorators.lazy_property(write=False) def queue_controller(self): - return None + raise NotImplementedError() @decorators.lazy_property(write=False) def message_controller(self): - return None + raise NotImplementedError() @decorators.lazy_property(write=False) def claim_controller(self): - return None + raise NotImplementedError() + + +class ControlDriver(storage.ControlDriverBase): + + def __init__(self, conf, cache): + super(ControlDriver, self).__init__(conf, cache) + self.conf.register_opts(_SQLALCHEMY_OPTIONS, + group=_SQLALCHEMY_GROUP) + self.sqlalchemy_conf = self.conf[_SQLALCHEMY_GROUP] + + @decorators.lazy_property(write=False) + def engine(self, *args, **kwargs): + engine = sa.create_engine(self.sqlalchemy_conf.uri, **kwargs) + tables.metadata.create_all(engine, checkfirst=True) + return engine + + # TODO(cpp-cabrera): expose connect/close as a context manager + # that acquires the connection to the DB for the desired scope and + # closes it once the operations are completed + @decorators.lazy_property(write=False) + def connection(self): + return self.engine.connect() + + def close_connection(self): + self.connection.close() + + @property + def shards_controller(self): + raise NotImplementedError() + + @property + def catalogue_controller(self): + raise NotImplementedError()