diff --git a/panko/event/storage/impl_mongodb.py b/panko/event/storage/impl_mongodb.py index c2ca7fe0..320516d7 100644 --- a/panko/event/storage/impl_mongodb.py +++ b/panko/event/storage/impl_mongodb.py @@ -12,7 +12,6 @@ # under the License. """MongoDB storage backend""" -from oslo_config import cfg from oslo_log import log import pymongo @@ -88,21 +87,19 @@ class Connection(pymongo_base.Connection): ('timestamp', pymongo.ASCENDING)], name='event_type_idx' ) - ttl = cfg.CONF.database.event_time_to_live - self.update_ttl(ttl, 'event_ttl', 'timestamp', self.db.event) def clear(self): self.conn.drop_database(self.db.name) # Connection will be reopened automatically if needed self.conn.close() - @staticmethod - def clear_expired_event_data(ttl): + def clear_expired_event_data(self, ttl): """Clear expired data from the backend storage system. Clearing occurs according to the time-to-live. :param ttl: Number of seconds to keep records for. """ - LOG.debug("Clearing expired event data is based on native " - "MongoDB time to live feature and going in background.") + self.update_ttl(ttl, 'event_ttl', 'timestamp', self.db.event) + LOG.info("Clearing expired event data is based on native " + "MongoDB time to live feature and going in background.") diff --git a/panko/tests/functional/storage/test_impl_mongodb.py b/panko/tests/functional/storage/test_impl_mongodb.py index 3c1d5f33..efbc0211 100644 --- a/panko/tests/functional/storage/test_impl_mongodb.py +++ b/panko/tests/functional/storage/test_impl_mongodb.py @@ -29,40 +29,26 @@ from panko.tests import db as tests_db @tests_db.run_with('mongodb') class IndexTest(tests_db.TestBase): - def _test_ttl_index_absent(self, conn, coll_name, ttl_opt): - # create a fake index and check it is deleted - coll = getattr(conn.db, coll_name) - index_name = '%s_ttl' % coll_name - self.CONF.set_override(ttl_opt, -1, group='database') - conn.upgrade() - self.assertNotIn(index_name, coll.index_information()) - - self.CONF.set_override(ttl_opt, 456789, group='database') - conn.upgrade() - self.assertEqual(456789, - coll.index_information() - [index_name]['expireAfterSeconds']) - def test_event_ttl_index_absent(self): - self._test_ttl_index_absent(self.event_conn, 'event', - 'event_time_to_live') + # create a fake index and check it is deleted + self.event_conn.clear_expired_event_data(-1) + self.assertNotIn("event_ttl", + self.event_conn.db.event.index_information()) - def _test_ttl_index_present(self, conn, coll_name, ttl_opt): - coll = getattr(conn.db, coll_name) - self.CONF.set_override(ttl_opt, 456789, group='database') - conn.upgrade() - index_name = '%s_ttl' % coll_name + self.event_conn.clear_expired_event_data(456789) self.assertEqual(456789, - coll.index_information() - [index_name]['expireAfterSeconds']) - - self.CONF.set_override(ttl_opt, -1, group='database') - conn.upgrade() - self.assertNotIn(index_name, coll.index_information()) + self.event_conn.db.event.index_information() + ["event_ttl"]['expireAfterSeconds']) def test_event_ttl_index_present(self): - self._test_ttl_index_present(self.event_conn, 'event', - 'event_time_to_live') + self.event_conn.clear_expired_event_data(456789) + self.assertEqual(456789, + self.event_conn.db.event.index_information() + ["event_ttl"]['expireAfterSeconds']) + + self.event_conn.clear_expired_event_data(-1) + self.assertNotIn("event_ttl", + self.event_conn.db.event.index_information()) class CapabilitiesTest(test_base.BaseTestCase):