Skip to content

Commit

Permalink
Remove MongoDB TTL support for MongoDB < 2.2
Browse files Browse the repository at this point in the history
We already depends on MongoDB >= 2.2

Change-Id: I3d91fb08eb51ba6cfd5256abbeca43ed98321412
  • Loading branch information
jd committed Aug 8, 2013
1 parent 92bed7c commit 1e8b2b9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 68 deletions.
18 changes: 0 additions & 18 deletions ceilometer/storage/impl_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import calendar
import copy
import datetime
import operator
import uuid
import weakref
Expand All @@ -34,7 +33,6 @@
from oslo.config import cfg

from ceilometer.openstack.common import log
from ceilometer.openstack.common import timeutils
from ceilometer import storage
from ceilometer.storage import base
from ceilometer.storage import models
Expand Down Expand Up @@ -287,11 +285,6 @@ def upgrade(self):
self.db.meter.ensure_index([('timestamp', pymongo.DESCENDING)],
name='timestamp_idx')

# Since mongodb 2.2 support db-ttl natively
if self._is_natively_ttl_supported():
self._ensure_meter_ttl_index()

def _ensure_meter_ttl_index(self):
indexes = self.db.meter.index_information()

ttl = cfg.CONF.database.time_to_live
Expand All @@ -316,10 +309,6 @@ def _ensure_meter_ttl_index(self):
name='meter_ttl'
)

def _is_natively_ttl_supported(self):
# Assume is not supported if we can get the version
return self.conn.server_info().get('versionArray', []) >= [2, 2]

def clear(self):
self.conn.drop_database(self.db)
# Connection will be reopened automatically if needed
Expand Down Expand Up @@ -377,13 +366,6 @@ def clear_expired_metering_data(self, ttl):
:param ttl: Number of seconds to keep records for.
"""
# Before mongodb 2.2 we need to clear expired data manually
if not self._is_natively_ttl_supported():
end = timeutils.utcnow() - datetime.timedelta(seconds=ttl)
f = storage.SampleFilter(end=end)
q = make_query_from_filter(f, require_meter=False)
self.db.meter.remove(q)

results = self.db.meter.group(
key={},
condition={},
Expand Down
57 changes: 7 additions & 50 deletions tests/storage/test_impl_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,55 +102,27 @@ def test_meter_ttl_index_absent(self):
# create a fake index and check it is deleted
self.conn.db.meter.ensure_index('foo', name='meter_ttl')
cfg.CONF.set_override('time_to_live', -1, group='database')

self.conn._ensure_meter_ttl_index()
self.conn.upgrade()
self.assertTrue(self.conn.db.meter.ensure_index('foo',
name='meter_ttl'))
cfg.CONF.set_override('time_to_live', 456789, group='database')
self.conn._ensure_meter_ttl_index()
self.conn.upgrade()
self.assertFalse(self.conn.db.meter.ensure_index('foo',
name='meter_ttl'))

def test_meter_ttl_index_present(self):
cfg.CONF.set_override('time_to_live', 456789, group='database')
self.conn._ensure_meter_ttl_index()
self.conn.upgrade()
self.assertFalse(self.conn.db.meter.ensure_index('foo',
name='meter_ttl'))
self.assertEqual(self.conn.db.meter.index_information()[
'meter_ttl']['expireAfterSeconds'], 456789)

cfg.CONF.set_override('time_to_live', -1, group='database')
self.conn._ensure_meter_ttl_index()
self.conn.upgrade()
self.assertTrue(self.conn.db.meter.ensure_index('foo',
name='meter_ttl'))

def test_ttl_index_is_supported(self):
self.mox.StubOutWithMock(self.conn.conn, "server_info")
self.conn.conn.server_info().AndReturn({'versionArray': [2, 4, 5, 0]})

self.mox.ReplayAll()
self.assertTrue(self.conn._is_natively_ttl_supported())
self.mox.UnsetStubs()
self.mox.VerifyAll()

def test_ttl_index_is_not_supported(self):
self.mox.StubOutWithMock(self.conn.conn, "server_info")
self.conn.conn.server_info().AndReturn({'versionArray': [2, 0, 1, 0]})

self.mox.ReplayAll()
self.assertFalse(self.conn._is_natively_ttl_supported())
self.mox.UnsetStubs()
self.mox.VerifyAll()

def test_ttl_index_is_unkown(self):
self.mox.StubOutWithMock(self.conn.conn, "server_info")
self.conn.conn.server_info().AndReturn({})

self.mox.ReplayAll()
self.assertFalse(self.conn._is_natively_ttl_supported())
self.mox.UnsetStubs()
self.mox.VerifyAll()


class UserTest(base.UserTest, MongoDBEngineTestBase):
pass
Expand All @@ -173,25 +145,10 @@ class MeterTestPagination(base.MeterTestPagination, MongoDBEngineTestBase):


class RawSampleTest(base.RawSampleTest, MongoDBEngineTestBase):
# NOTE(jd) Override this test in MongoDB because our code doesn't clear
# the collections, this is handled by MongoDB TTL feature.
def test_clear_metering_data(self):
# NOTE(sileht): ensure this tests is played for any version of mongo
self.mox.StubOutWithMock(self.conn, "_is_natively_ttl_supported")
self.conn._is_natively_ttl_supported().AndReturn(False)

self.mox.ReplayAll()
super(RawSampleTest, self).test_clear_metering_data()
self.mox.UnsetStubs()
self.mox.VerifyAll()

def test_clear_metering_data_no_data_to_remove(self):
# NOTE(sileht): ensure this tests is played for any version of mongo
self.mox.StubOutWithMock(self.conn, "_is_natively_ttl_supported")
self.conn._is_natively_ttl_supported().AndReturn(False)

self.mox.ReplayAll()
super(RawSampleTest, self).test_clear_metering_data_no_data_to_remove()
self.mox.UnsetStubs()
self.mox.VerifyAll()
pass


class StatisticsTest(base.StatisticsTest, MongoDBEngineTestBase):
Expand Down

0 comments on commit 1e8b2b9

Please sign in to comment.