mongodb: update TTL on expirer run
It's counter-intuitive to need to have to call dbsync when changing the value in the config file, as one expect it to be updated also when running the expirer. Expirer is the standard way. Change-Id: I04bee39ee116f58566e1aabbe72e8af09121776a
This commit is contained in:
parent
e38a3426bf
commit
5760b50204
|
@ -12,7 +12,6 @@
|
||||||
# under the License.
|
# under the License.
|
||||||
"""MongoDB storage backend"""
|
"""MongoDB storage backend"""
|
||||||
|
|
||||||
from oslo_config import cfg
|
|
||||||
from oslo_log import log
|
from oslo_log import log
|
||||||
import pymongo
|
import pymongo
|
||||||
|
|
||||||
|
@ -88,21 +87,19 @@ class Connection(pymongo_base.Connection):
|
||||||
('timestamp', pymongo.ASCENDING)],
|
('timestamp', pymongo.ASCENDING)],
|
||||||
name='event_type_idx'
|
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):
|
def clear(self):
|
||||||
self.conn.drop_database(self.db.name)
|
self.conn.drop_database(self.db.name)
|
||||||
# Connection will be reopened automatically if needed
|
# Connection will be reopened automatically if needed
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
|
|
||||||
@staticmethod
|
def clear_expired_event_data(self, ttl):
|
||||||
def clear_expired_event_data(ttl):
|
|
||||||
"""Clear expired data from the backend storage system.
|
"""Clear expired data from the backend storage system.
|
||||||
|
|
||||||
Clearing occurs according to the time-to-live.
|
Clearing occurs according to the time-to-live.
|
||||||
|
|
||||||
:param ttl: Number of seconds to keep records for.
|
:param ttl: Number of seconds to keep records for.
|
||||||
"""
|
"""
|
||||||
LOG.debug("Clearing expired event data is based on native "
|
self.update_ttl(ttl, 'event_ttl', 'timestamp', self.db.event)
|
||||||
"MongoDB time to live feature and going in background.")
|
LOG.info("Clearing expired event data is based on native "
|
||||||
|
"MongoDB time to live feature and going in background.")
|
||||||
|
|
|
@ -29,40 +29,26 @@ from panko.tests import db as tests_db
|
||||||
@tests_db.run_with('mongodb')
|
@tests_db.run_with('mongodb')
|
||||||
class IndexTest(tests_db.TestBase):
|
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):
|
def test_event_ttl_index_absent(self):
|
||||||
self._test_ttl_index_absent(self.event_conn, 'event',
|
# create a fake index and check it is deleted
|
||||||
'event_time_to_live')
|
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):
|
self.event_conn.clear_expired_event_data(456789)
|
||||||
coll = getattr(conn.db, coll_name)
|
|
||||||
self.CONF.set_override(ttl_opt, 456789, group='database')
|
|
||||||
conn.upgrade()
|
|
||||||
index_name = '%s_ttl' % coll_name
|
|
||||||
self.assertEqual(456789,
|
self.assertEqual(456789,
|
||||||
coll.index_information()
|
self.event_conn.db.event.index_information()
|
||||||
[index_name]['expireAfterSeconds'])
|
["event_ttl"]['expireAfterSeconds'])
|
||||||
|
|
||||||
self.CONF.set_override(ttl_opt, -1, group='database')
|
|
||||||
conn.upgrade()
|
|
||||||
self.assertNotIn(index_name, coll.index_information())
|
|
||||||
|
|
||||||
def test_event_ttl_index_present(self):
|
def test_event_ttl_index_present(self):
|
||||||
self._test_ttl_index_present(self.event_conn, 'event',
|
self.event_conn.clear_expired_event_data(456789)
|
||||||
'event_time_to_live')
|
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):
|
class CapabilitiesTest(test_base.BaseTestCase):
|
||||||
|
|
Loading…
Reference in New Issue